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

1.2.0 • Public • Published

@p-j/eapi-middleware-cors

A middleware to configure CORS behavior on a per route or request basis

Installation

  • From the NPM registry
npm install @p-j/eapi-middleware-cors
# or
yarn add @p-j/eapi-middleware-cors

API

  • withCors is a Middleware Factory; it takes in the following options:

    export interface WithCorsOptions {
      isOriginAllowed?: Function
      accessControlAllowHeaders?: string[]
      accessControlAllowMethods?: string[]
      accessControlMaxAge?: number
      accessControlAllowCredentials?: boolean
      accessControlExposeHeaders?: string[]
    }

    As noted above, none of the options are required.

  • cors is a utility function that adds CORS headers to a Response; it takes in the following options:

    export interface CorsOptions extends WithCorsOptions {
      response: Response
      accessControlAllowOrigin?: string
    }

    Note: it extends the WithCorsOptions

    This function is used by withCors where the accessControlAllowOrigin is set to the Origin header of the request.

Additional resources:

Note:

If the requestHandler given to withCors or the response given to cors already contains Access-Control-* Headers, they will be overriden with whatever config is given.

Package Sidebar

Install

npm i @p-j/eapi-middleware-cors

Weekly Downloads

3

Version

1.2.0

License

MIT

Unpacked Size

43.1 kB

Total Files

13

Last publish

Collaborators

  • jparker