Custom Middlewares
A middleware is an object that should contain at least 1 of 3 possible keys:
before
: a function that is executed in the before phaseafter
: a function that is executed in the after phaseonError
: a function that is executed in case of errors
before
, after
and onError
functions need to have the following signature:
const defaults = {
// ...
}
const nameMiddleware = (opts = {}) => {
const options = { ...defaults, ...opts }
const nameMiddlewareBefore = async (request) => {
// ...
}
const nameMiddlewareAfter = async (request) => {
// ...
}
const nameMiddlewareOnError = async (request) => {
// ...
}
return {
before: nameMiddlewareBefore,
after: nameMiddlewareAfter,
onError: nameMiddlewareOnError
}
}
export default nameMiddleware
Where:
request
: is a reference to the current context and allows access to (and modification of) the currentevent
(request), theresponse
(in the after phase), anderror
(in case of an error).