Skip to content
Myr

fix(aws-lambda): add jwt and lambda authorizer types for API Gateway v2 (#5142)

Before adoption: this commit came in with the history the project adopted, so no landing records it.

`ApiGatewayRequestContextV2["authorizer"]` only declared the `iam` variant, so reading the context of a Lambda (REQUEST) authorizer or a JWT authorizer was a TypeScript error even though API Gateway populates those keys: ctx.env.event.requestContext.authorizer.lambda.userId // Property 'lambda' does not exist on type 'Authorizer'. The payload format 2.0 event documented by AWS carries `authorizer.jwt` with `claims` and `scopes`, and a Lambda authorizer's `context` object is delivered under `authorizer.lambda`. Add both as optional properties, mirroring `@types/aws-lambda`. Types only — no runtime change, and `iam` is untouched, so this is backwards compatible. Closes #3281

authored by Jason G · committed by GitHub · parent bf8608c · Browse files · 2 files+69 added−1 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

2 files+69 added−1 removed

  • tests:+60 added−1 removed
  • other:+9 added−0 removed

2 files+69 added−1 removed

modified:src/adapter/aws-lambda/handler.test.ts+60 added-1 removed
1import { setCookie } from '../../helper/cookie'2import { Hono } from '../../hono'3import { bodyLimit } from '../../middleware/body-limit'4import type { LambdaEvent, LatticeProxyEventV2 } from './handler'4import type { APIGatewayProxyEventV2, LambdaEvent, LatticeProxyEventV2 } from './handler'5import {6 getProcessor,7 handle,8 isContentEncodingBinary,9 defaultIsContentTypeBinary,10} from './handler'11import type { ApiGatewayRequestContextV2 } from './types'1213// Base event objects to reduce duplication14const baseV1Event: LambdaEvent = {15 version: '1.0',
556 unmodified lines
572 const result = await handler(event)573 expect(result.statusCode).toBe(413)574 })575})576577describe('V2 request context authorizer', () => {578 const baseV2RequestContext = baseV2Event.requestContext as ApiGatewayRequestContextV2579580 it('Should expose the context of a Lambda (REQUEST) authorizer', async () => {581 const app = new Hono<{ Bindings: { event: APIGatewayProxyEventV2 } }>()582 app.get('/my/path', (c) => c.json(c.env.event.requestContext.authorizer.lambda))583 const handler = handle(app)584585 const event: LambdaEvent = {586 ...baseV2Event,587 requestContext: {588 ...baseV2RequestContext,589 http: { ...baseV2RequestContext.http, method: 'GET' },590 authorizer: { lambda: { userId: 'user-123', isAdmin: true } },591 },592 }593594 const result = await handler(event)595 expect(result.statusCode).toBe(200)596 expect(JSON.parse(result.body)).toEqual({ userId: 'user-123', isAdmin: true })597 })598599 it('Should expose the claims and scopes of a JWT authorizer', async () => {600 const app = new Hono<{ Bindings: { event: APIGatewayProxyEventV2 } }>()601 app.get('/my/path', (c) => c.json(c.env.event.requestContext.authorizer.jwt))602 const handler = handle(app)603604 const event: LambdaEvent = {605 ...baseV2Event,606 requestContext: {607 ...baseV2RequestContext,608 http: { ...baseV2RequestContext.http, method: 'GET' },609 authorizer: {610 jwt: { claims: { sub: 'user-123', email_verified: true }, scopes: ['read'] },611 },612 },613 }614615 const result = await handler(event)616 expect(result.statusCode).toBe(200)617 expect(JSON.parse(result.body)).toEqual({618 claims: { sub: 'user-123', email_verified: true },619 scopes: ['read'],620 })621 })622623 it('Should type each authorizer variant as optional', () => {624 const authorizer: ApiGatewayRequestContextV2['authorizer'] = {}625 expectTypeOf(authorizer.lambda).toEqualTypeOf<Record<string, unknown> | null | undefined>()626 expectTypeOf(authorizer.jwt).toEqualTypeOf<627 | { claims: Record<string, string | number | boolean | string[]>; scopes: string[] | null }628 | undefined629 >()630 // A JWT authorizer reports no scopes as `null`.631 expectTypeOf<null>().toMatchTypeOf<NonNullable<typeof authorizer.jwt>['scopes']>()632 })633})
modified:src/adapter/aws-lambda/types.ts+9 added