dbartholomae / lambda-middleware

A collection of middleware for AWS lambda functions.
https://dbartholomae.github.io/lambda-middleware/
MIT License
151 stars 18 forks source link

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

Closed drmikecrowe closed 1 year ago

drmikecrowe commented 2 years ago

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?

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.

dbartholomae commented 2 years ago

Thanks! What's the problem you run into?

drmikecrowe commented 2 years ago

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
dbartholomae commented 2 years ago

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! :)

github-actions[bot] commented 1 year ago

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.