serverless-express icon indicating copy to clipboard operation
serverless-express copied to clipboard

Use nullish coalescing to determine path

Open sanderkoenders opened this issue 2 years ago • 1 comments

Issue #, if available: We are currently struggling with a situation where /test proxies to a lambda. This causes event.pathParameters.proxy to be undefined. The request is then translated to /test rather than /.

Description of changes: To solve this we want to overwrite event.pathParameters.proxy with an empty string but the absence of nullish coalescing causes this to still use the event.path rather than event.pathParameters.proxy

Checklist

  • [x] Tests have been added and are passing
  • [x] Documentation has been updated

By submitting this pull request, I confirm that my contribution is made under the terms of the Apache 2.0 license.

sanderkoenders avatar Oct 31 '23 11:10 sanderkoenders

Hey @sanderkoenders could you share the test case for when ?? would result in a different output than || ? In this scenario, both yield the same result:

let event = { path: '/test', pathParameters: { proxy: undefined } }
let path
path = (event.pathParameters && event.pathParameters.proxy && `/${event.pathParameters.proxy}`) || event.path
console.log(path)
path = (event.pathParameters && event.pathParameters.proxy && `/${event.pathParameters.proxy}`) ?? event.path
console.log(path)

brettstack avatar Dec 15 '23 05:12 brettstack