Class: application = new Application()

Properties
Name Type Description
handleHttp Function

Handle incoming HTTP(S) request by invoking the corresponding Controller method via the configured Sequence.

options ApplicationOptions

constructor(options: ApplicationOptions) : Application

Arguments
Name Type Description
options ApplicationOptions

api(spec: OpenApiSpec) : Binding

Arguments
Name Type Description
spec OpenApiSpec

component(component: Constructor) : void

Add a component to this application.

Arguments
Name Type Description
component Constructor

The component to add.


export class ProductComponent {
  controllers = [ProductController];
  repositories = [ProductRepo, UserRepo];
  providers = {
    [AUTHENTICATION_STRATEGY]: AuthStrategy,
    [AUTHORIZATION_ROLE]: Role,
  };
};

app.component(ProductComponent);

controller(controllerCtor: ControllerClass) : Binding

Register a controller class with this application.

Returns:

The newly created binding, you can use the reference to further modify the binding, e.g. lock the value to prevent further modifications.

```ts

Arguments
Name Type Description
controllerCtor ControllerClass

The controller class (constructor function).

getApiSpec() : OpenApiSpec

Get the OpenAPI specification describing the REST API provided by this application.

This method merges operations (HTTP endpoints) from the following sources:

  • app.api(spec)
  • app.controller(MyController)
  • app.route(route)
  • app.route('get', '/greet', operationSpec, MyController, 'greet')

handler(handlerFn: SequenceFunction) : void

Configure a custom sequence function for handling incoming requests.

app.handler((sequence, request, response) => {
  sequence.send(response, 'hello world'));
});

Arguments
Name Type Description
handlerFn SequenceFunction

The handler to invoke for each incoming request.

route(verb: string, path: string, spec: OperationObject, controller: ControllerClass, methodName: string) : Binding

Register a new Controller-based route.

class MyController {
  greet(name: string) {
    return `hello ${name}`;
  }
}
app.route('get', '/greet', operationSpec, MyController, 'greet');

Arguments
Name Type Description
verb string

HTTP verb of the endpoint

path string

URL path of the endpoint

spec OperationObject

The OpenAPI spec describing the endpoint (operation)

controller ControllerClass

Controller constructor

methodName string

The name of the controller method

sequence(value: Constructor) : void

Configure a custom sequence class for handling incoming requests.

class MySequence implements SequenceHandler {
  constructor(
    @inject('send) public send: Send)) {
  }

  public async handle(request: ParsedRequest, response: ServerResponse) {
    send(response, 'hello world');
  }
}

Arguments
Name Type Description
value Constructor

The sequence to invoke for each incoming request.

start() : Promise

Start the application (e.g. HTTP/HTTPS servers).

Interface: ApplicationOptions

Properties
Name Type Description
components Array
http HttpConfig
sequence Constructor

Interface: HttpConfig

Properties
Name Type Description
port number

Interface: Component

Properties
Name Type Description
controllers
providers ProviderMap

Interface: ProviderMap

mountComponent(app: Application, component: Component) : void

Arguments
Name Type Description
app Application
component Component

Class: httphandler = new HttpHandler()

Properties
Name Type Description
handleRequest Function

constructor(_rootContext: Context) : HttpHandler

Arguments
Name Type Description
_rootContext Context

describeApiPaths() : PathsObject

findRoute(request: ParsedRequest) : ResolvedRoute

Arguments
Name Type Description
request ParsedRequest

registerController(name: ControllerClass, spec: ControllerSpec) : void

Arguments
Name Type Description
name ControllerClass
spec ControllerSpec

registerRoute(route: RouteEntry) : void

Arguments
Name Type Description
route RouteEntry

Interface: ParsedRequest

Properties
Name Type Description
method string
path string
pathname string
query Function
url string

BindElement(key: string): Binding

Arguments
Name Type Description
key string

FindRoute(request: ParsedRequest): ResolvedRoute

Find a route matching the incoming request. Throw an error when no route was found.

Arguments
Name Type Description
request ParsedRequest

GetFromContext(key: string): Promise<BoundValue>

Arguments
Name Type Description
key string

InvokeMethod(route: RouteEntry, args: OperationArgs): Promise<OperationRetval>

Invokes a method defined in the Application Controller

Returns:

OperationRetval Result from method invocation

Arguments
Name Type Description
route RouteEntry

args OperationArgs

Operation arguments for the method

LogError(err: Error, statusCode: number, request: ServerRequest): void

Log information about a failed request.

Returns:

Arguments
Name Type Description
err Error

The error reported by request handling code.

statusCode number

Status code of the HTTP response

request ServerRequest

The request that failed.

OperationArgs = any[]

OperationRetval = any

ParseParams(request: ParsedRequest, route: ResolvedRoute): Promise<OperationArgs>

Arguments
Name Type Description
request ParsedRequest
route ResolvedRoute

PathParameterValues(key: string): any

Arguments
Name Type Description
key string

Reject(response: ServerResponse, request: ServerRequest, err: Error): void

Reject the request with an error.

Returns:

Arguments
Name Type Description
response ServerResponse

The response the response to send to.

request ServerRequest

The request that triggered the error.

err Error

The error.

Send(response: ServerResponse, result: OperationRetval): void

Send the operation response back to the client.

Returns:

Arguments
Name Type Description
response ServerResponse

The response the response to send to.

result OperationRetval

The operation result to send.

HttpError = HttpErrors.HttpError

jsonBodyFn(req: ServerRequest, cb: (err: Error, body: __type) => void): void

Arguments
Name Type Description
req ServerRequest
cb Function

parseOperationArgs(request: ParsedRequest, route: ResolvedRoute) : Promise

Parses the request to derive arguments to be passed in for the Application controller method

Arguments
Name Type Description
request ParsedRequest

Incoming HTTP request

route ResolvedRoute

promisify(func: (callback: (err: any, result: T) => void) => void) :

Arguments
Name Type Description
func Function

Interface: ControllerSpec

Properties
Name Type Description
basePath string

The base path on which the Controller API is served. If it is not included, the API is served directly under the host. The value MUST start with a leading slash (/).

paths PathsObject

The available paths and operations for the API.

Interface: RestEndpoint

Properties
Name Type Description
path string
verb string

api(spec: ControllerSpec) : (Anonymous function)

Decorate the given Controller constructor with metadata describing the HTTP/REST API the Controller implements/provides.

Arguments
Name Type Description
spec ControllerSpec

OpenAPI specification describing the endpoints handled by this controller

del(path: string, spec: OperationObject) : (Anonymous function)

Expose a Controller method as a REST API operation mapped to DELETE request method.

Arguments
Name Type Description
path string

The URL path of this operation, e.g. /product/{id}

spec OperationObject

The OpenAPI specification describing parameters and responses of this operation.

get(path: string, spec: OperationObject) : (Anonymous function)

Expose a Controller method as a REST API operation mapped to GET request method.

Arguments
Name Type Description
path string

The URL path of this operation, e.g. /product/{id}

spec OperationObject

The OpenAPI specification describing parameters and responses of this operation.

getControllerSpec(constructor: Function) : ControllerSpec

Arguments
Name Type Description
constructor Function

operation(verb: string, path: string, spec: OperationObject) : (Anonymous function)

Expose a Controller method as a REST API operation.

Arguments
Name Type Description
verb string

HTTP verb, e.g. GET or POST.

path string

The URL path of this operation, e.g. /product/{id}

spec OperationObject

The OpenAPI specification describing parameters and responses of this operation.

patch(path: string, spec: OperationObject) : (Anonymous function)

Expose a Controller method as a REST API operation mapped to PATCH request method.

Arguments
Name Type Description
path string

The URL path of this operation, e.g. /product/{id}

spec OperationObject

The OpenAPI specification describing parameters and responses of this operation.

post(path: string, spec: OperationObject) : (Anonymous function)

Expose a Controller method as a REST API operation mapped to POST request method.

Arguments
Name Type Description
path string

The URL path of this operation, e.g. /product/{id}

spec OperationObject

The OpenAPI specification describing parameters and responses of this operation.

put(path: string, spec: OperationObject) : (Anonymous function)

Expose a Controller method as a REST API operation mapped to PUT request method.

Arguments
Name Type Description
path string

The URL path of this operation, e.g. /product/{id}

spec OperationObject

The OpenAPI specification describing parameters and responses of this operation.

Class: baseroute = new BaseRoute()

Properties
Name Type Description
path string
spec OperationObject
verb string

constructor(verb: string, path: string, spec: OperationObject) : BaseRoute

Arguments
Name Type Description
verb string
path string
spec OperationObject

describe() : string

invokeHandler(requestContext: Context, args: OperationArgs) : Promise

Arguments
Name Type Description
requestContext Context
args OperationArgs

match(request: ParsedRequest) : ResolvedRoute | undefined

Arguments
Name Type Description
request ParsedRequest

updateBindings(requestContext: Context) : void

Arguments
Name Type Description
requestContext Context

Class: controllerroute = new ControllerRoute()

constructor(verb: string, path: string, spec: OperationObject, _controllerCtor: ControllerClass, methodName: string) : ControllerRoute

Arguments
Name Type Description
verb string
path string
spec OperationObject
_controllerCtor ControllerClass
methodName string

describe() : string

invokeHandler(requestContext: Context, args: OperationArgs) : Promise

Arguments
Name Type Description
requestContext Context
args OperationArgs

updateBindings(requestContext: Context) : void

Arguments
Name Type Description
requestContext Context

Class: route = new Route()

Properties
Name Type Description
spec OperationObject

constructor(verb: string, path: string, spec: OperationObject, _handler: Function) : Route

Arguments
Name Type Description
verb string
path string
spec OperationObject
_handler Function

describe() : string

invokeHandler(requestContext: Context, args: OperationArgs) : Promise

Arguments
Name Type Description
requestContext Context
args OperationArgs

updateBindings(requestContext: Context) : void

Arguments
Name Type Description
requestContext Context

Class: routingtable = new RoutingTable()

describeApiPaths() : PathsObject

find(request: ParsedRequest) : ResolvedRoute

Arguments
Name Type Description
request ParsedRequest

registerController(controller: ControllerClass, spec: ControllerSpec) : void

Arguments
Name Type Description
controller ControllerClass
spec ControllerSpec

registerRoute(route: RouteEntry) : void

Arguments
Name Type Description
route RouteEntry

Interface: ResolvedRoute

Properties
Name Type Description
pathParams PathParameterValues

Interface: RouteEntry

Properties
Name Type Description
path string
spec OperationObject
verb string

describe() : string

invokeHandler(requestContext: Context, args: OperationArgs) : Promise

Arguments
Name Type Description
requestContext Context
args OperationArgs

match(request: ParsedRequest) : ResolvedRoute | undefined

Arguments
Name Type Description
request ParsedRequest

updateBindings(requestContext: Context) : void

Arguments
Name Type Description
requestContext Context

ControllerClass = Constructor

ControllerInstance(opName: string): Function

Arguments
Name Type Description
opName string

createResolvedRoute(route: RouteEntry, pathParams: PathParameterValues) : ResolvedRoute

Arguments
Name Type Description
route RouteEntry
pathParams PathParameterValues

parseRequestUrl(request: ServerRequest) : ParsedRequest

Parse the URL of the incoming request and set additional properties on this request object:

  • path
  • query

Arguments
Name Type Description
request ServerRequest

Class: defaultsequence = new DefaultSequence()

The default implementation of SequenceHandler.

This class implements default Sequence for the LoopBack framework. Default sequence is used if user hasn't defined their own Sequence for their application.

Sequence constructor() and run() methods are invoked from [[http-handler]] when the API request comes in. User defines APIs in their Application Controller class.

User can bind their own Sequence to app as shown below

app.bind(CoreBindings.SEQUENCE).toClass(MySequence);

Properties
Name Type Description
ctx Context

The default implementation of SequenceHandler.

This class implements default Sequence for the LoopBack framework. Default sequence is used if user hasn't defined their own Sequence for their application.

Sequence constructor() and run() methods are invoked from [[http-handler]] when the API request comes in. User defines APIs in their Application Controller class.

User can bind their own Sequence to app as shown below

app.bind(CoreBindings.SEQUENCE).toClass(MySequence);

reject Reject

The default implementation of SequenceHandler.

This class implements default Sequence for the LoopBack framework. Default sequence is used if user hasn't defined their own Sequence for their application.

Sequence constructor() and run() methods are invoked from [[http-handler]] when the API request comes in. User defines APIs in their Application Controller class.

User can bind their own Sequence to app as shown below

app.bind(CoreBindings.SEQUENCE).toClass(MySequence);

send Send

The default implementation of SequenceHandler.

This class implements default Sequence for the LoopBack framework. Default sequence is used if user hasn't defined their own Sequence for their application.

Sequence constructor() and run() methods are invoked from [[http-handler]] when the API request comes in. User defines APIs in their Application Controller class.

User can bind their own Sequence to app as shown below

app.bind(CoreBindings.SEQUENCE).toClass(MySequence);

constructor(ctx: Context, findRoute: FindRoute, parseParams: ParseParams, invoke: InvokeMethod, send: Send, reject: Reject) : DefaultSequence

Constructor: Injects findRoute, invokeMethod & logError methods as promises.

Arguments
Name Type Description
ctx Context

Constructor: Injects findRoute, invokeMethod & logError methods as promises.

findRoute FindRoute

Finds the appropriate controller method, spec and args for invocation

parseParams ParseParams

Constructor: Injects findRoute, invokeMethod & logError methods as promises.

invoke InvokeMethod

Invokes the method

send Send

Constructor: Injects findRoute, invokeMethod & logError methods as promises.

reject Reject

Constructor: Injects findRoute, invokeMethod & logError methods as promises.

handle(req: ParsedRequest, res: ServerResponse) : Promise

Runs the default sequence. Given a request and response, running the sequence will produce a response or an error.

Default sequence executes these steps

  • Finds the appropriate controller method, swagger spec and args for invocation
  • Parses HTTP request to get API argument list
  • Invokes the API which is defined in the Application Controller
  • Writes the result from API into the HTTP response
  • Error is caught and logged using 'logError' if any of the above steps in the sequence fails with an error.

Arguments
Name Type Description
req ParsedRequest

Parsed incoming HTTP request

res ServerResponse

HTTP server response with result from Application controller method invocation

Interface: SequenceHandler

A sequence handler is a class implementing sequence of actions required to handle an incoming request.

handle(request: ParsedRequest, response: ServerResponse) : Promise

Handle the request by running the configured sequence of actions.

Arguments
Name Type Description
request ParsedRequest

The incoming HTTP request

response ServerResponse

The HTTP server response where to write the result

SequenceFunction(sequence: DefaultSequence, request: ParsedRequest, response: ServerResponse):

A sequence function is a function implementing a custom sequence of actions to handle an incoming request.

Arguments
Name Type Description
sequence DefaultSequence
request ParsedRequest
response ServerResponse

writeResultToResponse(response: Response, result: OperationRetval) : void

Writes the result from Application controller method into the HTTP response

Arguments
Name Type Description
response Response

HTTP Response

result OperationRetval

Result from the API to write into HTTP Response