@p-j/eapi-util-applymiddlewares
TypeScript icon, indicating that this package has built-in type declarations

1.1.0 • Public • Published

@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-applymiddlewares

Usage

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".

Package Sidebar

Install

npm i @p-j/eapi-util-applymiddlewares

Weekly Downloads

1

Version

1.1.0

License

MIT

Unpacked Size

10.5 kB

Total Files

13

Last publish

Collaborators

  • jparker