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

getApiDefinitions() : SchemasObject

registerApiDefinitions(defs: SchemasObject) : void

Arguments
Name Type Description
defs SchemasObject

registerController(spec: ControllerSpec, controllerCtor: ControllerClass, controllerFactory: ControllerFactory) : void

Arguments
Name Type Description
spec ControllerSpec
controllerCtor ControllerClass
controllerFactory ControllerFactory

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

Type alias: BindElement(key: string): Binding

Arguments
Name Type Description
key string

Type alias: 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

Type alias: GetFromContext(key: string): Promise<BoundValue>

Arguments
Name Type Description
key string

Type alias: 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

Type alias: 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.

Type alias: OperationArgs = any[]

Type alias: OperationRetval = any

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

Arguments
Name Type Description
request ParsedRequest
route ResolvedRoute

Type alias: PathParameterValues(key: string): any

Arguments
Name Type Description
key string

Type alias: 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.

Type alias: 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.

Type alias: HttpError = HttpErrors.HttpError

Function: parseOperationArgs

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

Class: bindelementprovider = new BindElementProvider()

constructor(context: Context) : BindElementProvider

Arguments
Name Type Description
context Context

action(key: string) : Binding

Arguments
Name Type Description
key string

value() : BindElement

Class: findrouteprovider = new FindRouteProvider()

constructor(context: Context, handler: HttpHandler) : FindRouteProvider

Arguments
Name Type Description
context Context
handler HttpHandler

action(request: ParsedRequest) : ResolvedRoute

Arguments
Name Type Description
request ParsedRequest

value() : FindRoute

Class: getfromcontextprovider = new GetFromContextProvider()

constructor(context: Context) : GetFromContextProvider

Arguments
Name Type Description
context Context

action(key: string) : Promise

Arguments
Name Type Description
key string

value() : GetFromContext

Class: invokemethodprovider = new InvokeMethodProvider()

constructor(context: Context) : InvokeMethodProvider

Arguments
Name Type Description
context Context

action(route: RouteEntry, args: OperationArgs) : Promise

Arguments
Name Type Description
route RouteEntry
args OperationArgs

value() : InvokeMethod

Class: logerrorprovider = new LogErrorProvider()

action(err: Error, statusCode: number, req: ServerRequest) : void

Arguments
Name Type Description
err Error
statusCode number
req ServerRequest

value() : LogError

Class: parseparamsprovider = new ParseParamsProvider()

Provides the function for parsing args in requests at runtime.

value() : parseOperationArgs

Class: rejectprovider = new RejectProvider()

constructor(logError: LogError) : RejectProvider

Arguments
Name Type Description
logError LogError

action(response: ServerResponse, request: ServerRequest, error: Error) : void

Arguments
Name Type Description
response ServerResponse
request ServerRequest
error Error

value() : Reject

Class: sendprovider = new SendProvider()

Provides the function that populates the response object with the results of the operation.

value() : writeResultToResponse

Class: restapplication = new RestApplication()

An implementation of the Application class that automatically provides an instance of a REST server. This application class is intended to be a single-server implementation. Any attempt to bind additional servers will throw an error.

constructor(config: ApplicationConfig) : RestApplication

Arguments
Name Type Description
config ApplicationConfig

An implementation of the Application class that automatically provides an instance of a REST server. This application class is intended to be a single-server implementation. Any attempt to bind additional servers will throw an error.

api(spec: OpenApiSpec) : Binding

Set the OpenAPI specification that defines the REST API schema for this application. All routes, parameter definitions and return types will be defined in this way.

Note that this will override any routes defined via decorators at the controller level (this function takes precedent).

Arguments
Name Type Description
spec OpenApiSpec

The OpenAPI specification, as an object.

handler(handlerFn: SequenceFunction) : void

Arguments
Name Type Description
handlerFn SequenceFunction

An implementation of the Application class that automatically provides an instance of a REST server. This application class is intended to be a single-server implementation. Any attempt to bind additional servers will throw an error.

route(verb: string, path: string, spec: OperationObject, controllerCtor: ControllerClass, controllerFactory: ControllerFactory, 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)

controllerCtor ControllerClass

Controller constructor

controllerFactory ControllerFactory

A factory function to create controller instance

methodName string

The name of the controller method

route(route: RouteEntry) : Binding

Register a new route.

function greet(name: string) {
 return `hello ${name}`;
}
const route = new Route('get', '/', operationSpec, greet);
app.route(route);

Arguments
Name Type Description
route RouteEntry

The route to add.

sequence(sequence: Constructor) : Binding

Arguments
Name Type Description
sequence Constructor

An implementation of the Application class that automatically provides an instance of a REST server. This application class is intended to be a single-server implementation. Any attempt to bind additional servers will throw an error.

server(server: Constructor, name: undefined) : Binding

Arguments
Name Type Description
server Constructor

An implementation of the Application class that automatically provides an instance of a REST server. This application class is intended to be a single-server implementation. Any attempt to bind additional servers will throw an error.

name

An implementation of the Application class that automatically provides an instance of a REST server. This application class is intended to be a single-server implementation. Any attempt to bind additional servers will throw an error.

Class: restcomponent = new RestComponent()

constructor(app: Application, config: RestComponentConfig) : RestComponent

Arguments
Name Type Description
app Application
config RestComponentConfig

Interface: RestComponentConfig

Class: restserver = new RestServer()

A REST API server for use with Loopback. Add this server to your application by importing the RestComponent.

const app = new MyApplication();
app.component(RestComponent);

To add additional instances of RestServer to your application, use the .server function:

app.server(RestServer, 'nameOfYourServer');

By default, one instance of RestServer will be created when the RestComponent is bootstrapped. This instance can be retrieved with app.getServer(RestServer), or by calling app.get('servers.RestServer') Note that retrieving other instances of RestServer must be done using the server's name:

const server = await app.getServer('foo')
// OR
const server = await app.get('servers.foo');

Properties
Name Type Description
requestHandler HttpRequestListener

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

constructor(app: Application, options: RestServerConfig) : RestServer

Arguments
Name Type Description
app Application

The application instance (injected via CoreBindings.APPLICATION_INSTANCE).

options RestServerConfig

The configuration options (injected via RestBindings.CONFIG).

api(spec: OpenApiSpec) : Binding

Set the OpenAPI specification that defines the REST API schema for this server. All routes, parameter definitions and return types will be defined in this way.

Note that this will override any routes defined via decorators at the controller level (this function takes precedent).

Arguments
Name Type Description
spec OpenApiSpec

The OpenAPI specification, as an object.

controller(controllerCtor: ControllerClass) : Binding

Register a controller class with this server.

Returns:

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

class MyController {
}
app.controller(MyController).lock();

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, controllerCtor: ControllerClass, controllerFactory: ControllerFactory, 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)

controllerCtor ControllerClass

Controller constructor

controllerFactory ControllerFactory

A factory function to create controller instance

methodName string

The name of the controller method

route(route: RouteEntry) : Binding

Register a new route.

function greet(name: string) {
 return `hello ${name}`;
}
const route = new Route('get', '/', operationSpec, greet);
app.route(route);

Arguments
Name Type Description
route RouteEntry

The route to add.

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 this REST API's HTTP/HTTPS server.

stop() : Promise

Stop this REST API's HTTP/HTTPS server.

Interface: HttpServerLike

Properties
Name Type Description
requestHandler HttpRequestListener

Interface: OpenApiSpecOptions

The object format used for building the template bases of our OpenAPI spec files.

Properties
Name Type Description
format

The object format used for building the template bases of our OpenAPI spec files.

version

The object format used for building the template bases of our OpenAPI spec files.

Interface: RestServerConfig

Valid configuration for the RestServer constructor.

Properties
Name Type Description
apiExplorerUrl

Valid configuration for the RestServer constructor.

cors cors.CorsOptions

Valid configuration for the RestServer constructor.

host

Valid configuration for the RestServer constructor.

port

Valid configuration for the RestServer constructor.

sequence Constructor

Valid configuration for the RestServer constructor.

Type alias: HttpRequestListener(req: ServerRequest, res: ServerResponse): void

Arguments
Name Type Description
req ServerRequest
res ServerResponse

Object literal: OPENAPI_SPEC_MAPPING

Class: baseroute = new BaseRoute()

Base implementation of RouteEntry

Properties
Name Type Description
path string

http request path pattern

spec OperationObject

OpenAPI operation spec

verb string

Base implementation of RouteEntry

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

Construct a new route

Arguments
Name Type Description
verb string

http verb

path string

http request path pattern

spec OperationObject

OpenAPI operation spec

describe() : string

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

Arguments
Name Type Description
requestContext Context

Base implementation of RouteEntry

args OperationArgs

Base implementation of RouteEntry

match(request: ParsedRequest) : ResolvedRoute | undefined

Arguments
Name Type Description
request ParsedRequest

Base implementation of RouteEntry

updateBindings(requestContext: Context) : void

Arguments
Name Type Description
requestContext Context

Base implementation of RouteEntry

Class: controllerroute = new ControllerRoute()

A route backed by a controller

constructor(verb: string, path: string, spec: OperationObject, controllerCtor: ControllerClass, controllerFactory: ControllerFactory, methodName: undefined) : ControllerRoute

Construct a controller based route

Arguments
Name Type Description
verb string

http verb

path string

http request path

spec OperationObject

OpenAPI operation spec

controllerCtor ControllerClass

Controller class

controllerFactory ControllerFactory

A factory function to create a controller instance

methodName

Controller method name, default to x-operation-name

describe() : string

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

Arguments
Name Type Description
requestContext Context

A route backed by a controller

args OperationArgs

A route backed by a controller

updateBindings(requestContext: Context) : void

Arguments
Name Type Description
requestContext Context

A route backed by a controller

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()

Routing table

describeApiPaths() : PathsObject

find(request: ParsedRequest) : ResolvedRoute

Map a request to a route

Arguments
Name Type Description
request ParsedRequest

registerController(spec: ControllerSpec, controllerCtor: ControllerClass, controllerFactory: ControllerFactory) : void

Register a controller as the route

Arguments
Name Type Description
spec ControllerSpec

controllerCtor ControllerClass

controllerFactory ControllerFactory

registerRoute(route: RouteEntry) : void

Register a route

Arguments
Name Type Description
route RouteEntry

A route entry

joinPath(basePath: string, path: string) : string

Arguments
Name Type Description
basePath string

Routing table

path string

Routing table

Interface: ResolvedRoute

A route with path parameters resolved

Properties
Name Type Description
pathParams PathParameterValues

A route with path parameters resolved

Interface: RouteEntry

An entry in the routing table

Properties
Name Type Description
path string

http path

spec OperationObject

OpenAPI operation spec

verb string

http verb

describe() : string

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

A handler to invoke the resolved controller method

Arguments
Name Type Description
requestContext Context

args OperationArgs

match(request: ParsedRequest) : ResolvedRoute | undefined

Map an http request to a route

Arguments
Name Type Description
request ParsedRequest

updateBindings(requestContext: Context) : void

Update bindings for the request context

Arguments
Name Type Description
requestContext Context

Type alias: ControllerClass = Constructor

Type alias: ControllerFactory(ctx: Context): ValueOrPromise

A factory function to create controller instances synchronously or asynchronously

Arguments
Name Type Description
ctx Context

Function: createControllerFactoryForBinding

createControllerFactoryForBinding(key: string) : ControllerFactory

Create a controller factory function for a given binding key

Arguments
Name Type Description
key string

Binding key

Function: createControllerFactoryForClass

createControllerFactoryForClass(controllerCtor: ControllerClass) : ControllerFactory

Create a controller factory function for a given class

Arguments
Name Type Description
controllerCtor ControllerClass

Controller class

Function: createControllerFactoryForInstance

createControllerFactoryForInstance(controllerInst: T) : ControllerFactory

Create a controller factory function for a given instance

Arguments
Name Type Description
controllerInst T

Function: createResolvedRoute

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

Arguments
Name Type Description
route RouteEntry
pathParams PathParameterValues

Function: parseRequestUrl

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 context for the sequence (injected via RestBindings.Http.CONTEXT).

reject Reject

The action to take if the invoke returns a rejected promise result (injected via SequenceActions.REJECT).

send Send

The action to merge the invoke result with the response (injected via SequenceActions.SEND)

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

The context for the sequence (injected via RestBindings.Http.CONTEXT).

findRoute FindRoute

Finds the appropriate controller method, spec and args for invocation (injected via SequenceActions.FIND_ROUTE).

parseParams ParseParams

The parameter parsing function (injected via SequenceActions.PARSE_PARAMS).

invoke InvokeMethod

Invokes the method specified by the route (injected via SequenceActions.INVOKE_METHOD).

send Send

The action to merge the invoke result with the response (injected via SequenceActions.SEND)

reject Reject

The action to take if the invoke returns a rejected promise result (injected via SequenceActions.REJECT).

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

Type alias: 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

Function: writeErrorToResponse

writeErrorToResponse(response: Response, error: Error) : void

Write an error into the HTTP response

Arguments
Name Type Description
response Response

HTTP response

error Error

Error

Function: writeResultToResponse

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