ResponseSystemMiddleware

libflitter/routing/ResponseSystemMiddleware~ ResponseSystemMiddleware

System Middleware is an abstract Flitter construct. System middleware is applied to every request as it is processed by Flitter. It's different to traditional app-space middleware in that it is inherently global and is applied to each transaction without discrimination. The point of this type of middleware is to modify objects like Express requests and responses to add Flitter-specific helper methods. ResponseSystemMiddleware specifically adds helpers to the Express response.


Constructor

new ResponseSystemMiddleware(app, response, request)

Instantiate the middleware. Bootstrap the response with methods.

Parameters:
Name Type Description
app module:libflitter/app/FlitterApp~FlitterApp

the Flitter app

response express/response

the Express response

request express/request

the Express request

Extends

Members

(static) services

Defines the services required by this unit.

(private) _di_allow_defer :boolean

If true, the injector will defer the class if the class requests any services that the container is missing. These services are filled in later and added to the prototype and any instances. True by default.

Type:
  • boolean

(private) _di_deferred_instances :Array.<module:flitter-di/src/Injectable~Injectable>

Collection of instances of this class that need to have the deferred service instances injected into them when the deferred services are finally provided.

(private) _di_deferred_services :Array.<string>

List of services that were deferred and not provided at the time of injection.

Type:
  • Array.<string>

Methods

(async) api(data) → {Promise.<*>}

Sends an JSON-formatted response in a standard API format containing the HTTP status code, some message, and a data key with the passed in data.

Parameters:
Name Type Description
data
Returns:
Type
Promise.<*>

(async) error(status, params) → {Promise.<*>}

Sends an HTTP error and renders the error view with the corresponding status code. If the status code cannot be resolved to an integer, the request status will default to 400. Wraps the {module:libflitter/views/ViewEngineUnit~ViewEngineUnit#error} method.

Parameters:
Name Type Description
status string | int

HTTP or error status code

params Object

collection of arguments to be passed to the view. The "code" key will be overwritten with the status.

Returns:
Type
Promise.<*>

message(msg) → {express|response}

Sets the response message, if used.

Parameters:
Name Type Default Description
msg OK
Returns:
Type
express | response

(async) page(view_name, args, resource_list) → {Promise.<*>}

Renders a view for the user. Wraps the {module:libflitter/views/ViewEngineUnit~ViewEngineUnit#view} method. Additionally, adds the _app variable to the view, which contains page-specific information like the title. See {module:libflitter/routing/ResponseSystemMiddleware~ResponseSystemMiddleware~PageAppData} for more info.

Parameters:
Name Type Description
view_name string

the Flitter canonical name of the view to be rendered

args Object

variables to be passed to the view

resource_list Array.<module:libflitter/views/ViewEngineUnit~ViewEngineUnit~ResourceSpec>

an array of resource specifications to be passed to the view

Returns:
  • returns the output of the wrapped function
Type
Promise.<*>

status(code) → {express|response}

Sets the response status code.

Parameters:
Name Type Default Description
code 200
Returns:
Type
express | response

(async) view(view_name, args, resource_list) → {Promise.<*>}

Renders a view for the user. Wraps the {module:libflitter/views/ViewEngineUnit~ViewEngineUnit#view} method.

Parameters:
Name Type Description
view_name string

the Flitter canonical name of the view to be rendered

args Object

variables to be passed to the view

resource_list Array.<module:libflitter/views/ViewEngineUnit~ViewEngineUnit~ResourceSpec>

an array of resource specifications to be passed to the view

Returns:
  • returns the output of the wrapped function
Type
Promise.<*>