1.1.0 • Published 5 years ago
@p-j/eapi-util-applymiddlewares v1.1.0
@p-j/eapi-util-applymiddlewares
Apply a list of middlwares to a given request handler, returning an enhanced request handler
Installation
- From the NPM registry
 
npm install @p-j/eapi-util-applymiddlewares
# or
yarn add @p-j/eapi-util-applymiddlewaresUsage
import { applyMiddlewares } from '@p-j/eapi-util-applymiddlewares'
import { withCache } from '@p-j/eapi-middleware-cache'
import { withErrorHandler } from '@p-j/eapi-middleware-errorhandler'
const requestHandler: RequestHandler = (context) => new Response('Hello World')
const finaleHandler = applyMiddlewares(
  requestHandler, // first argument is the original request handler
  withErrorHandler({ enableDebug: true }), // following arguments are as many middleware as you'd like
  withCache({
    cacheControl: `public, max-age=${TTL_30MINUTES}`,
    cdnTtl: TTL_30MINUTES,
  }),
)Note on the order of application for the middlewares:
applyMiddlewares(handler, a, b, c)
// is the same as
a(b(c(handler)))So for instance, if you want to catch all exceptions within the middleware stack, you want to put the Error middleware first in the list of middlewares, as shown above.
Generally speaking, you want to start with "generic middleware" first and end with the "specific ones".