Route

Extends

fileSystemLocation
readonly

tries to determine the route definition location

Type:
String
uid
readonly

unique identifier of the route

Type:
String
new Route(options)
Parameters:
  • options
    • Type: Object
      • name optional
        • Type: String
      • type
        • Type: String
        • see RequestType enum for available option values

      • url
        • Type: String
      • summary
        • Type: String
        • document the end point

      • desc
        • Type: String
        • document the end point

      • sdkMethodName
        • Type: String
        • sdk method name for API clients

Throws:
getAbsoluteUrl(pathParams, queryParams)String

returns absolute route endpoint.
In order to work properly service config option apps.<app-name>.baseUrl should be set

Parameters:
  • pathParams optional
    • Type: Object
  • queryParams optional
    • Type: Object
Returns: String
getName()String

returns route's name. If no name has been assigned, the name is dynamically created from route's url path

Returns: String
getUrl(pathParams, queryParams)String

returns hostname relative route endpoint

Parameters:
  • pathParams optional
    • Type: Object
  • queryParams optional
    • Type: Object
Returns: String
validate(valDef, dataProp)Route

pushes specifically configured validation middleware to the route's call stack

Parameters:
  • valDef
    • Type: string or Object
    • string => registered validator's name. Object => schema definition

  • dataProp
    • Type: string
    • query|body|params|headers

Returns: Route
  • self
Example:
route.validate({
    properties: {
        username: {type: 'string'}
    }
}, 'query');

//or

route.validate('ajv-registered-validation-schema-uid', 'body');
acceptedContentTypes()[String, ...]

returns collection of supported request content mime types

Returns: [String, ...]
acceptsContentType(type, options, parser)RouteInterface

define which content-type headers the route supports.
this is also gonna register and manage a single validation middleware for content-type header internally

Parameters:
  • type
    • Type: String
    • Content-Type header value

  • options optional
    • Type: Object
      • limit optional
        • Type: String
        • data size limit

  • parser optional
    • Type: function
    • custom data parser function - must return a Promise

Returns: RouteInterface
addStep(name, fn)RouteInterface

allows to hook up any middleware function to the request promise chain (call stack)

Parameters:
  • name optional
    • Type: String
  • fn
    • Type: function
Returns: RouteInterface
  • self
buildResponse(cb)Response

the Response object can be returned from within route middleware which will cause promise call chain interruption of current request and prioritized response

Parameters:
  • cb
    • Type: function
    • callback function which sets response on the express res object. The function's context is always set to the res object

Returns: Response
Example:
route.step(function() {
    return route.buildResponse(function() {
        this.json({response: 'data'});
    }):
}).step(function() {
    //will never be called
});
catch(filter, callback)RouteInterface

catch promise stack handler invoked when an Error occurs while executing one of the route middlwares

Parameters:
  • filter optional
    • Type: function
    • must be a constructor with .prototype property that is instanceof Error

  • callback
    • Type: function
Returns: RouteInterface
  • self
Example:
route.main(function() {
  throw new TypeError('test');
}).catch(TypeError, function(err, req, res) {
  //err handler logic
});
getAllSteps()[Object, ...]

returns route's internal middleware call stack

Returns: [Object, ...]
main(fn)RouteInterface
Parameters:
  • fn
    • Type: function
Returns: RouteInterface
  • self
Example:
route.main(() => {})

//is same as:

route.step('main', () => {})
rejectsContentType(type)RouteInterface

define a content-type which should be always rejected by this route. Content types black listed by this method can be later white listed by the RouteInterface#acceptsContentType method

Parameters:
  • type
    • Type: String
    • Content-Type header value

Returns: RouteInterface
respondsWith(descriptor)RouteInterface

allows to describe route's response data format in form of Ajv validation schema object or Error instance object/constructor which implements toSwagger method.
if a string is provided it's expected to be validation schema unique indentifier registered with the Ajv instance.
With res.filter({some: 'data'}).json() conjuction, data can be filtered with defined response schema.

Parameters:
  • descriptor
    • Type: Object or String or function
Returns: RouteInterface
  • self
Example:
route.respondsWith({
    type: 'object',
    additionalProperties: false,
    properties: {
        prop1: {type: 'string'}
    }
});

route.main(function(req, res) {
    res.filter({prop1: 'included', prop2: 'filtered out'}).json();
});
step(name, fn)RouteInterface

alias for RouteInterface#addStep

Parameters:
  • name optional
    • Type: String
  • fn
    • Type: function
Returns: RouteInterface
  • self
comments powered by Disqus