1.2.0 • Published 3 years ago

@p-j/eapi-middleware-cors v1.2.0

Weekly downloads
2
License
MIT
Repository
github
Last release
3 years ago

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