Custom Middlewares

A middleware is an object that should contain at least 1 of 3 possible keys:

  1. before: a function that is executed in the before phase
  2. after: a function that is executed in the after phase
  3. onError: 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 current event (request), the response (in the after phase), and error (in case of an error).