lambda-middleware icon indicating copy to clipboard operation
lambda-middleware copied to clipboard

Can you please show how to add cors() to a compose chain?

Open drmikecrowe opened this issue 3 years ago • 3 comments

Is your feature request related to a problem? Please describe. I'm trying to add the cors middleware to a compose chain, and can't seem to find the right combination. Here's my current attempt:

const corsConfig: CorsMiddlewareOptions = {
  allowedHeaders: ['*'],
  cacheControl: 'max-age: 300',
  allowCredentials: true,
  exposedHeaders: ['X-Custom-Header'],
  maxAge: 300,
  allowedMethods: ['GET', 'HEAD', 'PUT', 'PATCH', 'POST', 'DELETE'],
  optionsSuccessStatus: 204,
  allowedOrigins: ['https://mysite.com'],
  preflightContinue: false,
}

export const standardHttp = compose<APIGatewayEvent, APIGatewayProxyResult>(
  createJWTForLocal,
  typeormInitializer,
  doNotWait(),
  jsonSerializer(),
  jsonDeserializer(),
  timingLogMiddleware(logDuration),
  errorHandler,
  cors(corsConfig)
)

** Would you be willing to help with a PR? **

  • [X] Yes, absolutely
  • [ ] Yes, with some guidance
  • [ ] Unfortunately no time :'-(

Describe the solution you'd like

Ideally, I'd like to add cors to this chain to add to all lambda functions

Describe alternatives you've considered

None

Additional context

Add any other context or screenshots about the feature request here.

drmikecrowe avatar Aug 24 '22 11:08 drmikecrowe

Thanks! What's the problem you run into?

dbartholomae avatar Aug 24 '22 12:08 dbartholomae

I'm seeing stuff like this:

    TypeError: Cannot read properties of undefined (reading 'toLowerCase')
      20 |   async (event: APIGatewayEvent, context: Context): Promise<APIGatewayProxyResult> => {
      21 |     logger('Running handler')
    > 22 |     const response = await handler(event, context)
         |                            ^
      23 |     logger('Response received')
      24 |     if (response === undefined) {
      25 |       logger('Undefined response, returning statusCode 204')
      at node_modules/@lambda-middleware/cors/src/cors.ts:38:26

drmikecrowe avatar Aug 25 '22 01:08 drmikecrowe

The line with the error is the line that calls your handler, so the error might be related to the handler. Could you please share a small repo with the error with the full context including a handler? Thanks! :)

dbartholomae avatar Sep 03 '22 18:09 dbartholomae

This issue is stale because it has been open 60 days with no activity. Remove stale label or comment or this will be closed in 7 days.

github-actions[bot] avatar Nov 03 '22 01:11 github-actions[bot]