Skip to content

Latest commit

 

History

History

eapi-middleware-cors

Folders and files

NameName
Last commit message
Last commit date

parent directory

..
 
 
 
 
 
 
 
 
 
 
 
 

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