honojs / hono

Web framework built on Web Standards
https://hono.dev
MIT License
19.87k stars 566 forks source link

Types: LambdaEvent type is missing authorizer context for lambda auth #3281

Open DaveLo opened 2 months ago

DaveLo commented 2 months ago

What version of Hono are you using?

4.5.3

What runtime/platform is your app running on?

aws lambda

What steps can reproduce the bug?

Create a HTTP Api proxy v2 endpoint in aws-lambda and use a lambda authorizer in front of it, but practically since this is a typings issue you can set up your bindings like:

type Bindings = {
  Bindings: { event: LambdaEvent, lambdaContext:LambdaContext };
  Variables: { userId: string };
};

const app = new Hono<Bindings>();

Then try and set a value from the lambda authorizer context


app.use(async (ctx, next) => {
  ctx.set(
    "userId",
    ctx.env.event.requestConstext.authorizer.lambda.userId
  );
});

This throws up TS warnings because the current setup of LambdaEvent only has a key for iam auth.

What is the expected behavior?

I would expect the types to work correctly.

What do you see instead?

A typescript error.

My current workaround is to do this:

type Event = LambdaEvent & {
  requestContext: {
    authorizer: {
        lambda: { userId: string };
    }
  }
};

type Bindings = {
  Bindings: { event: Event, lambdaContext:LambdaContext };
  Variables: { userId: string };
};

Additional information

I referenced @types/aws-lambda to figure out typings here.

yusukebe commented 2 months ago

Hi @DaveLo Thank you for raising the issue.

@watany-dev Can you take a look at it?