@whook/http-router
v16.0.0
Published
The Whook base HTTP router
Downloads
754
Maintainers
Readme
@whook/http-router
The Whook base HTTP router
The Whook's httpRouter
service
is responsible for wiring routes definitions to their actual
implementation while filtering inputs and ensuring good outputs.
This is the default implementation of the Framework but it can be replaced or customized by setting your own configurations to replace the default ones (see the API section).
API
Functions
initHTTPRouter(services) ⇒ Promise
Initialize an HTTP router
Kind: global function
Returns: Promise - A promise of a function to handle HTTP requests.
| Param | Type | Default | Description |
| --- | --- | --- | --- |
| services | Object | | The services the server depends on |
| services.ENV | Object | | The injected ENV value |
| [services.DEBUG_NODE_ENVS] | Array | | The environnement that activate debugging (prints stack trace in HTTP errors responses) |
| [services.BUFFER_LIMIT] | String | | The maximum bufferisation before parsing the request body |
| [services.BASE_PATH] | String | | API base path |
| services.HANDLERS | Object | | The handlers for the operations decribe by the OpenAPI API definition |
| services.API | Object | | The OpenAPI definition of the API |
| [services.PARSERS] | Object | | The synchronous body parsers (for operations that defines a request body schema) |
| [services.STRINGIFYERS] | Object | | The synchronous body stringifyers (for operations that defines a response body schema) |
| [services.ENCODERS] | Object | | A map of encoder stream constructors |
| [services.DECODERS] | Object | | A map of decoder stream constructors |
| [services.QUERY_PARSER] | Object | | A query parser with the strict-qs
signature |
| [services.log] | function | noop | A logging function |
| services.httpTransaction | function | | A function to create a new HTTP transaction |
initHTTPRouter~httpRouter(req, res) ⇒ Promise
Handle an HTTP incoming message
Kind: inner method of initHTTPRouter
Returns: Promise - A promise resolving when the operation
completes
| Param | Type | Description | | --- | --- | --- | | req | HTTPRequest | A raw NodeJS HTTP incoming message | | res | HTTPResponse | A raw NodeJS HTTP response |
flattenOpenAPI(API) ⇒ Object
Flatten the inputed OpenAPI file object
Kind: global function
Returns: Object - The flattened OpenAPI definition
| Param | Type | Description | | --- | --- | --- | | API | Object | An Object containing a parser OpenAPI JSON |
getOpenAPIOperations(API) ⇒ Array
Return a OpenAPI operation in a more convenient way to iterate onto its operations
Kind: global function
Returns: Array - An array of all the OpenAPI operations
| Param | Type | Description | | --- | --- | --- | | API | Object | The flattened OpenAPI defition |
Example
getOpenAPIOperations(API)
.map((operation) => {
const { path, method, operationId, parameters } = operation;
// Do something with that operation
});
dereferenceOpenAPIOperations(API, operations) ⇒ Object
Dereference API operations and transform OpenAPISchemas into JSONSchemas
Kind: global function
Returns: Object - The dereferenced OpenAPI operations
| Param | Type | Description | | --- | --- | --- | | API | Object | An OpenAPI object | | operations | Object | The OpenAPI operation objects |
initErrorHandler(services) ⇒ Promise
Initialize an error handler for the HTTP router
Kind: global function
Returns: Promise - A promise of a function to handle errors
| Param | Type | Description | | --- | --- | --- | | services | Object | The services the server depends on | | services.ENV | Object | The app ENV | | [services.DEBUG_NODE_ENVS] | Array | The environnement that activate debugging (prints stack trace in HTTP errors responses) | | [services.STRINGIFYERS] | Object | The synchronous body stringifyers | | [services.ERRORS_DESCRIPTORS] | Object | An hash of the various error descriptors | | [services.DEFAULT_ERROR_CODE] | Object | A string giving the default error code |
initErrorHandler~errorHandler(transactionId, responseSpec, err) ⇒ Promise
Handle an HTTP transaction error and map it to a serializable response
Kind: inner method of initErrorHandler
Returns: Promise - A promise resolving when the operation
completes
| Param | Type | Description | | --- | --- | --- | | transactionId | String | A raw NodeJS HTTP incoming message | | responseSpec | Object | The response specification | | err | YHTTPError | The encountered error |