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

jsonSerializer does not pass through correct event type #45

Closed parkan closed 3 years ago

parkan commented 3 years ago

Describe the bug

Hello it's me again 🙂

jsonSerializer (and I think some of the other middlewares as well) don't respect the pattern of generically passing the event from the previous middleware described in the README so the type information about the modified event is lost, replaced with base APIGatewayEvent

To Reproduce

compose(
  errorHandler(),
  jwtAuth(...),
  jsonSerializer()
)(handler) // the handler gets APIGatewayEvent without AuthorizedEvent

Expected behavior

Type information is preserved in the chain

Additional context

This should be a simple fix, replace

export const jsonSerializer = () => (
  handler: PromiseHandler<APIGatewayEvent, JSONObject | undefined>
) => 
...

with

export const jsonSerializer = () => (
  <E extends APIGatewayProxyEvent>(handler: PromiseHandler<APIGatewayEvent, JSONObject | undefined>): PromiseHandler<E, APIGatewayProxyResult> =>
...

happy to PR this!

issue-label-bot[bot] commented 3 years ago

Issue-Label Bot is automatically applying the label bug to this issue, with a confidence of 0.95. Please mark this comment with :thumbsup: or :thumbsdown: to give our bot feedback!

Links: app homepage, dashboard and code for this bot.

parkan commented 3 years ago

seems that this exact configuration is used in https://github.com/dbartholomae/lambda-middleware/blob/main/internal/e2e-tests/examples/helloWorld.ts#L52 but the handler doesn't actually use anything other than body on the event, I can add a test for .auth/explicit event type expectation

dbartholomae commented 3 years ago

Unfortunately there is a limit to how TypeScript is implemented that currently makes it impossible to have a full 100% passthrough of events. This was discussed in detail in #36. Please feel free to play around with it a bit, maybe there is a way to improve it I didn't find :)

parkan commented 3 years ago

gotcha, gonna close this for now and mess around with it a bit