Permalink
Branch: master
Find file Copy path
Fetching contributors…
Cannot retrieve contributors at this time
1529 lines (1012 sloc) 36.5 KB

Table of contents

index.ts

Functions

encryptPassword

Hash a password using the PBKDF2 algorithm.

Configured to use PBKDF2 + HMAC + SHA256. The result is a 64 byte binary string (or hex if the legacy option is true).

function encryptPassword(plainTextPassword: string, options: { legacy?: boolean | undefined; } = {}): Promise<string>;

Parameters

Name Type Default value Description
plainTextPassword string The password to encrypt.
options { legacy?: boolean | undefined; } {}

Return type

Promise


logIn

function logIn(ctx: Context<any>, user: object, idKey: string = 'id'): void;

Parameters

Name Type Default value
ctx Context
user object
idKey string 'id'

Return type

void


logOut

function logOut(ctx: Context<any>): void;

Parameters

Name Type
ctx Context

Return type

void


verifyPassword

function verifyPassword(plainTextPassword: string, encryptedPassword: string, options: { legacy?: boolean | undefined; } = {}): Promise<boolean>;

Parameters

Name Type Default value
plainTextPassword string
encryptedPassword string
options { legacy?: boolean | undefined; } {}

Return type

Promise


LoginOptional

function LoginOptional(options: { user: (id: number | string) => Promise<any>; }): HookDecorator;

Parameters

Name Type
options { user: (id: number | string) => Promise; }

Return type

HookDecorator


LoginRequired

function LoginRequired(options: { redirect?: string | undefined; user: (id: number | string) => Promise<any>; }): HookDecorator;

Parameters

Name Type
options { redirect?: string | undefined; user: (id: number | string) => Promise; }

Return type

HookDecorator


isObjectDoesNotExist

function isObjectDoesNotExist(err: object): err is ObjectDoesNotExist;

Parameters

Name Type
err object

Return type

err is ObjectDoesNotExist


isPermissionDenied

function isPermissionDenied(err: object): err is PermissionDenied;

Parameters

Name Type
err object

Return type

err is PermissionDenied


isValidationError

function isValidationError(err: object): err is ValidationError;

Parameters

Name Type
err object

Return type

err is ValidationError


Log

Logs a message with optional information on the HTTP request.

function Log(message: string, options: LogOptions = {}): HookDecorator;

Parameters

Name Type Default value Description
message string The message to print.
options LogOptions {} Options to specify which information on the HTTP request should be printed.

Return type

HookDecorator


ValidateBody

Hook to validate the body of the request.

function ValidateBody(schema: object): HookDecorator;

Parameters

Name Type Description
schema object Schema used to validate the body request.

Return type

HookDecorator


ValidateHeaders

Hook to validate the headers of the request.

function ValidateHeaders(schema: object): HookDecorator;

Parameters

Name Type Description
schema object Schema used to validate the headers request.

Return type

HookDecorator


ValidateParams

Hook to validate the params of the request.

function ValidateParams(schema: object): HookDecorator;

Parameters

Name Type Description
schema object Schema used to validate the params request.

Return type

HookDecorator


ValidateQuery

Hook to validate the query of the request.

function ValidateQuery(schema: object): HookDecorator;

Parameters

Name Type Description
schema object Schema used to validate the query request.

Return type

HookDecorator


controller

function controller(path: string, controllerClass: Class): Class;

Parameters

Name Type
path string
controllerClass Class

Return type

Class


escapeProp

function escapeProp(object: object, propName: string): void;

Parameters

Name Type
object object
propName string

Return type

void


escape

function escape(str: string): string;

Parameters

Name Type
str string

Return type

string


getAjvInstance

Returns the Ajv instance used internally by FoalTS.

It has this default configuration:

  • coerceTypes: true (Change data type of data to match type keyword.)
  • removeAdditional: true (Remove additional properties when additionalProperties keyword is false.)
  • useDefaults: true (Replace missing properties and items with the values from corresponding default keyword)

This configuration can be overrided using the file config/default.json or through environment variables: SETTINGS_AJV_COERCE_TYPES, SETTINGS_AJV_REMOVE_ADDITIONAL, SETTINGS_AJV_USE_DEFAULTS.

function getAjvInstance(): Ajv;

Return type

Ajv


isInFile

function isInFile(path: string): (content: string) => Promise<boolean>;

Parameters

Name Type
path string

Return type

(content: string) => Promise


render

function render(templatePath: string, locals: object, dirname: string): HttpResponseOK;

Parameters

Name Type
templatePath string
locals object
dirname string

Return type

HttpResponseOK


validate

function validate(schema: object, data: any): void;

Parameters

Name Type
schema object
data any

Return type

void


Head

function Head(path?: string | undefined): (target: any, propertyKey: string) => void;

Parameters

Name Type
path string | undefined

Return type

(target: any, propertyKey: string) => void


Options

function Options(path?: string | undefined): (target: any, propertyKey: string) => void;

Parameters

Name Type
path string | undefined

Return type

(target: any, propertyKey: string) => void


Get

function Get(path?: string | undefined): (target: any, propertyKey: string) => void;

Parameters

Name Type
path string | undefined

Return type

(target: any, propertyKey: string) => void


Post

function Post(path?: string | undefined): (target: any, propertyKey: string) => void;

Parameters

Name Type
path string | undefined

Return type

(target: any, propertyKey: string) => void


Put

function Put(path?: string | undefined): (target: any, propertyKey: string) => void;

Parameters

Name Type
path string | undefined

Return type

(target: any, propertyKey: string) => void


Patch

function Patch(path?: string | undefined): (target: any, propertyKey: string) => void;

Parameters

Name Type
path string | undefined

Return type

(target: any, propertyKey: string) => void


Delete

function Delete(path?: string | undefined): (target: any, propertyKey: string) => void;

Parameters

Name Type
path string | undefined

Return type

(target: any, propertyKey: string) => void


isHttpResponse

function isHttpResponse(obj: any): obj is HttpResponse;

Parameters

Name Type
obj any

Return type

obj is HttpResponse


isHttpResponseSuccess

function isHttpResponseSuccess(obj: any): obj is HttpResponseSuccess;

Parameters

Name Type
obj any

Return type

obj is HttpResponseSuccess


isHttpResponseOK

function isHttpResponseOK(obj: any): obj is HttpResponseOK;

Parameters

Name Type
obj any

Return type

obj is HttpResponseOK


isHttpResponseCreated

function isHttpResponseCreated(obj: any): obj is HttpResponseCreated;

Parameters

Name Type
obj any

Return type

obj is HttpResponseCreated


isHttpResponseNoContent

function isHttpResponseNoContent(obj: any): obj is HttpResponseNoContent;

Parameters

Name Type
obj any

Return type

obj is HttpResponseNoContent


isHttpResponseRedirection

function isHttpResponseRedirection(obj: any): obj is HttpResponseRedirection;

Parameters

Name Type
obj any

Return type

obj is HttpResponseRedirection


isHttpResponseRedirect

function isHttpResponseRedirect(obj: any): obj is HttpResponseRedirect;

Parameters

Name Type
obj any

Return type

obj is HttpResponseRedirect


isHttpResponseClientError

function isHttpResponseClientError(obj: any): obj is HttpResponseClientError;

Parameters

Name Type
obj any

Return type

obj is HttpResponseClientError


isHttpResponseBadRequest

function isHttpResponseBadRequest(obj: any): obj is HttpResponseBadRequest;

Parameters

Name Type
obj any

Return type

obj is HttpResponseBadRequest


isHttpResponseUnauthorized

function isHttpResponseUnauthorized(obj: any): obj is HttpResponseUnauthorized;

Parameters

Name Type
obj any

Return type

obj is HttpResponseUnauthorized


isHttpResponseForbidden

function isHttpResponseForbidden(obj: any): obj is HttpResponseForbidden;

Parameters

Name Type
obj any

Return type

obj is HttpResponseForbidden


isHttpResponseNotFound

function isHttpResponseNotFound(obj: any): obj is HttpResponseNotFound;

Parameters

Name Type
obj any

Return type

obj is HttpResponseNotFound


isHttpResponseMethodNotAllowed

function isHttpResponseMethodNotAllowed(obj: any): obj is HttpResponseMethodNotAllowed;

Parameters

Name Type
obj any

Return type

obj is HttpResponseMethodNotAllowed


isHttpResponseConflict

function isHttpResponseConflict(obj: any): obj is HttpResponseConflict;

Parameters

Name Type
obj any

Return type

obj is HttpResponseConflict


isHttpResponseServerError

function isHttpResponseServerError(obj: any): obj is HttpResponseServerError;

Parameters

Name Type
obj any

Return type

obj is HttpResponseServerError


isHttpResponseInternalServerError

function isHttpResponseInternalServerError(obj: any): obj is HttpResponseInternalServerError;

Parameters

Name Type
obj any

Return type

obj is HttpResponseInternalServerError


isHttpResponseNotImplemented

function isHttpResponseNotImplemented(obj: any): obj is HttpResponseNotImplemented;

Parameters

Name Type
obj any

Return type

obj is HttpResponseNotImplemented


Hook

function Hook(hookFunction: HookFunction): HookDecorator;

Parameters

Name Type
hookFunction HookFunction

Return type

HookDecorator


getHookFunction

function getHookFunction(hook: HookDecorator): HookFunction;

Parameters

Name Type
hook HookDecorator

Return type

HookFunction


makeControllerRoutes

function makeControllerRoutes(parentPath: string, parentHooks: HookFunction[], controllerClass: Class, services: ServiceManager): Route[];

Parameters

Name Type
parentPath string
parentHooks HookFunction[]
controllerClass Class
services ServiceManager

Return type

Route[]


getPath

function getPath(target: Class, propertyKey?: string | undefined): string | undefined;

Parameters

Name Type
target Class
propertyKey string | undefined

Return type

string | undefined


getHttpMethod

function getHttpMethod(target: Class, propertyKey?: string | undefined): string | undefined;

Parameters

Name Type
target Class
propertyKey string | undefined

Return type

string | undefined


createController

function createController<T>(controllerClass: Class<T>, dependencies?: object | ServiceManager): T;

Type parameters

Name
T

Parameters

Name Type
controllerClass Class
dependencies object | ServiceManager

Return type

T


createService

Create a new service with its dependencies.

function createService<Service>(serviceClass: Class<Service>, dependencies?: object | ServiceManager): Service;

Type parameters

Name
Service

Parameters

Name Type Description
serviceClass Class The service class.
dependencies object | ServiceManager Either a ServiceManager or an object which key/values are the service properties/instances.

Return type

Service


dependency

Decorator used to inject a service inside a controller or another service.

function dependency(target: any, propertyKey: string): void;

Parameters

Name Type
target any
propertyKey string

Return type

void


createApp

Main function to create a node.js (express) application from the root controller.

function createApp(rootControllerClass: Class, options: CreateAppOptions = {}, expressInstance?: any): any;

Parameters

Name Type Default value Description
rootControllerClass Class The root controller, usually called AppController
options CreateAppOptions {} Optional options to specify the session store (default is MemoryStore)
expressInstance any Optional express instance to be used as base.

Return type

any

Interfaces

LogOptions

interface LogOptions {
    body?: boolean | undefined;
    params?: boolean | undefined;
    headers?: string[] | boolean;
    query?: boolean | undefined;
    logFn?: ((message?: any, ...optionalParams: any[]) => void) | undefined;
}

Properties

Name Type Optional
body boolean | undefined true
params boolean | undefined true
headers string[] | boolean true
query boolean | undefined true
logFn ((message?: any, ...optionalParams: any[]) => void) | undefined true

CookieOptions

interface CookieOptions {
    domain?: string | undefined;
    expires?: Date | undefined;
    httpOnly?: boolean | undefined;
    maxAge?: number | undefined;
    path?: string | undefined;
    secure?: boolean | undefined;
    sameSite?: "strict" | "lax";
}

Properties

Name Type Optional
domain string | undefined true
expires Date | undefined true
httpOnly boolean | undefined true
maxAge number | undefined true
path string | undefined true
secure boolean | undefined true
sameSite "strict" | "lax" true

Route

interface Route {
    httpMethod: HttpMethod;
    path: string;
    hooks: HookFunction[];
    controller: any;
    propertyKey: string;
}

Properties

Name Type Optional
httpMethod HttpMethod false
path string false
hooks HookFunction[] false
controller any false
propertyKey string false

CreateAppOptions

interface CreateAppOptions {
    store(session: any)?: any;
}

Method

store(session: any)?: any;

Parameters

Name Type
session any

Return type

any

Types

Class

type Class<T = any> = new (args: any[]) => T;

Type parameters

Name Default
T any

Type

new (args: any[]) => T


HttpMethod

type HttpMethod = "POST" | "GET" | "PUT" | "PATCH" | "DELETE" | "HEAD" | "OPTIONS";

Type

"POST" | "GET" | "PUT" | "PATCH" | "DELETE" | "HEAD" | "OPTIONS"


HookPostFunction

type HookPostFunction = (ctx: Context<any>, services: ServiceManager, response: HttpResponse) => void | Promise<void>;

Type

(ctx: Context, services: ServiceManager, response: HttpResponse) => void | Promise


HookFunction

type HookFunction = (ctx: Context<any>, services: ServiceManager) => void | HttpResponse | HookPostFunction | Promise<void | HttpResponse | HookPostFunction>;

Type

(ctx: Context, services: ServiceManager) => void | HttpResponse | HookPostFunction | Promise<void | HttpResponse | HookPostFunction>


HookDecorator

type HookDecorator = (target: any, propertyKey?: string | undefined) => any;

Type

(target: any, propertyKey?: string | undefined) => any

Classes

ObjectDoesNotExist


PermissionDenied


ValidationError


Context


HttpResponse


HttpResponseSuccess


HttpResponseOK


HttpResponseCreated


HttpResponseNoContent


HttpResponseRedirection


HttpResponseRedirect


HttpResponseClientError


HttpResponseBadRequest


HttpResponseUnauthorized


HttpResponseForbidden


HttpResponseNotFound


HttpResponseMethodNotAllowed


HttpResponseConflict


HttpResponseServerError


HttpResponseInternalServerError


HttpResponseNotImplemented


Config


ServiceManager

Identity Mapper that instantiates and returns service singletons.