Have ideas to improve npm?Join in the discussion! »

    @middy/http-cors
    TypeScript icon, indicating that this package has built-in type declarations

    2.0.1 • Public • Published

    Middy CORS middleware

    Middy logo

    CORS middleware for the middy framework, the stylish Node.js middleware engine for AWS Lambda

    This middleware sets HTTP CORS headers (Access-Control-Allow-Origin, Access-Control-Allow-Headers, Access-Control-Allow-Credentials), necessary for making cross-origin requests, to the response object.

    Sets headers in after and onError phases.

    Install

    To install this middleware you can use NPM:

    npm install --save @middy/http-cors

    Options

    • credentials (bool) (optional): if true, sets the Access-Control-Allow-Origin as request header Origin, if present (default false)
    • headers (string) (optional): value to put in Access-Control-Allow-Headers (default: false)
    • methods (string) (optional): value to put in Access-Control-Allow-Mehtods (default: false)
    • getOrigin (function(incomingOrigin:string, options)) (optional): take full control of the generating the returned origin. Defaults to using the origin or origins option.
    • origin (string) (optional): origin to put in the header (default: "*")
    • origins (array) (optional): An array of allowed origins. The incoming origin is matched against the list and is returned if present.
    • exposeHeaders (string) (optional): value to put in Access-Control-Expose-Headers (default: false)
    • maxAge (string) (optional): value to put in Access-Control-Max-Age header (default: null)
    • requestHeaders (string) (optional): value to put in Access-Control-Request-Headers (default: false)
    • requestMethods (string) (optional): value to put in Access-Control-Request-Mehtods (default: false)
    • cacheControl (string) (optional): value to put in Cache-Control header on pre-flight (OPTIONS) requests (default: null)
    import middy from '@middy/core'
    import httpErrorHandler from '@middy/http-error-handler'
    import cors from '@middy/http-cors'
    
    const handler = middy((event, context) => {
      throw new createError.UnprocessableEntity()
    })
    handler.use(httpErrorHandler())
           .use(cors())
               
    // when Lambda runs the handler...
    handler({}, {}, (_, response) => {
      t.is(response.headers['Access-Control-Allow-Origin'],'*')
      t.deepEqual(response,{
          statusCode: 422,
          body: 'Unprocessable Entity'
        })
    })

    Sample usage

    import middy from '@middy/core'
    import cors from '@middy/http-cors'
    
    const handler = middy((event, context) => {
      return {}
    })
    
    handler.use(cors())
    
    // when Lambda runs the handler...
    handler({}, {}, (_, response) => {
      t.is(response.headers['Access-Control-Allow-Origin'],'*')
    })

    Middy documentation and examples

    For more documentation and examples, refers to the main Middy monorepo on GitHub or Middy official website.

    Contributing

    Everyone is very welcome to contribute to this repository. Feel free to raise issues or to submit Pull Requests.

    License

    Licensed under MIT License. Copyright (c) 2017-2021 Luciano Mammino, will Farrell, and the Middy team.

    FOSSA Status

    Install

    npm i @middy/http-cors

    DownloadsWeekly Downloads

    24,011

    Version

    2.0.1

    License

    MIT

    Unpacked Size

    11.3 kB

    Total Files

    5

    Last publish

    Collaborators

    • avatar
    • avatar
    • avatar