Skip to content

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

Merged
yusukebe merged 1 commit into
honojs:mainfrom
gianghungtien:fix/aws-lambda-authorizer-types
Jul 21, 2026
Merged

fix(aws-lambda): add jwt and lambda authorizer types for API Gateway v2#5142
yusukebe merged 1 commit into
honojs:mainfrom
gianghungtien:fix/aws-lambda-authorizer-types

Conversation

@gianghungtien

@gianghungtien gianghungtien commented Jul 20, 2026

Copy link
Copy Markdown
Contributor

Problem

ApiGatewayRequestContextV2["authorizer"] declares only the iam variant:

interface Authorizer {
  iam?: {
    accessKey: string
    accountId: string
    // ...
  }
}

API Gateway populates two other keys on that object, so reading either one is a
TypeScript error even though the value is present at runtime:

app.use(async (ctx, next) => {
  ctx.set('userId', ctx.env.event.requestContext.authorizer.lambda.userId)
  //                                                          ^^^^^^
  // error TS2339: Property 'lambda' does not exist on type 'Authorizer'.
})

This affects anyone using an HTTP API (payload format 2.0) with a Lambda
(REQUEST) authorizer or a JWT authorizer. ApiGatewayRequestContextV2 is
exported from hono/aws-lambda, so the gap is user-facing. The reported
workaround is to redeclare the type by hand before using the bindings.

Why these two keys

The shapes mirror @types/aws-lambda
(APIGatewayEventRequestContextJWTAuthorizer and
APIGatewayEventRequestContextLambdaAuthorizer), which the issue cites as the
reference. lambda is typed Record<string, unknown> | null rather than
generic to keep the change contained; null is what API Gateway sends when the
authorizer returns no context. scopes is string[] | null because a JWT with
no scope claim yields null.

Changes

interface Authorizer {
  iam?: { /* unchanged */ }
  jwt?: {
    claims: Record<string, string | number | boolean | string[]>
    scopes: string[] | null
  }
  /**
   * The `context` object returned by a Lambda (REQUEST) authorizer.
   * It is `null` when the authorizer returns no context.
   */
  lambda?: Record<string, unknown> | null
}

Both properties are optional and iam is untouched, so this is purely additive
and backwards compatible. Types only — no runtime change.

Tests

Added a V2 request context authorizer block to
src/adapter/aws-lambda/handler.test.ts covering both authorizer variants
end-to-end through handle(), plus an expectTypeOf check that each variant is
optional and that scopes accepts null.

Without the type change these fail tsc -p tsconfig.spec.json with exactly the
error from the issue:

error TS2339: Property 'lambda' does not exist on type 'Authorizer'.
error TS2339: Property 'jwt' does not exist on type 'Authorizer'.

bun run test passes: 145 files, 4619 tests. eslint and prettier --check
are clean on the changed files.

Closes #3281


The author should do the following, if applicable

  • Add tests
  • Run tests
  • bun run format:fix && bun run lint:fix to format the code
  • Add TSDoc/JSDoc to document the code

`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 honojs#3281
@codecov

codecov Bot commented Jul 21, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 79.04%. Comparing base (cadff88) to head (ac350dd).

Additional details and impacted files
@@           Coverage Diff           @@
##             main    #5142   +/-   ##
=======================================
  Coverage   79.04%   79.04%           
=======================================
  Files         154      154           
  Lines       10779    10779           
  Branches     2256     2256           
=======================================
  Hits         8520     8520           
  Misses       2259     2259           

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

@yusukebe
yusukebe merged commit e36f57d into honojs:main Jul 21, 2026
20 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Types: LambdaEvent type is missing authorizer context for lambda auth

2 participants