Skip to content
Myr

feat(adaptor): Accessable Lambda "Context" (#1523)

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

* adding lambda-context * format * type any * ignore denoify * export type

authored by watany · committed by GitHub · parent dddd497 · Browse files · 4 files+89 added−2 removed
This commit

State

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

Diff stats

4 files+89 added−2 removed

  • tests:+34 added−0 removed
  • other:+55 added−2 removed

4 files+89 added−2 removed

modified:runtime_tests/lambda/index.test.ts+34 added
1 unmodified line
2 ApiGatewayRequestContext,3 LambdaFunctionUrlRequestContext,4} from '../../src/adapter/aws-lambda/custom-context'5import { handle } from '../../src/adapter/aws-lambda/handler'6import type { LambdaContext } from '../../src/adapter/aws-lambda/types'7import { Hono } from '../../src/hono'8import { basicAuth } from '../../src/middleware/basic-auth'910type Bindings = {11 lambdaContext: LambdaContext12 requestContext: ApiGatewayRequestContext | LambdaFunctionUrlRequestContext13}1415describe('AWS Lambda Adapter for Hono', () => {
23 unmodified lines
39 const password = 'hono-password-a'40 app.use('/auth/*', basicAuth({ username, password }))41 app.get('/auth/abc', (c) => c.text('Good Night Lambda!'))4243 app.get('/lambda-context', (c) => {44 const fnctx = c.env.lambdaContext45 return c.json(fnctx)46 })4748 app.get('/custom-context/apigw', (c) => {49 const lambdaContext = c.env.requestContext50 return c.json(lambdaContext)51 })
236 unmodified lines
288 const response = await handler(event)289 expect(response.statusCode).toBe(200)290 expect(JSON.parse(response.body).customProperty).toEqual('customValue')291 })292293 it('Should handle a GET request and context', async () => {294 const event = {295 httpMethod: 'GET',296 headers: { 'content-type': 'application/json' },297 path: '/lambda-context',298 body: null,299 isBase64Encoded: false,300 requestContext: testApiGatewayRequestContext,301 }302 const context: LambdaContext = {303 callbackWaitsForEmptyEventLoop: false,304 functionName: 'myLambdaFunction',305 functionVersion: '1.0.0',306 invokedFunctionArn: 'arn:aws:lambda:us-west-2:123456789012:function:myLambdaFunction',307 memoryLimitInMB: '128',308 awsRequestId: 'c6af9ac6-a7b0-11e6-80f5-76304dec7eb7',309 logGroupName: '/aws/lambda/myLambdaFunction',310 logStreamName: '2016/11/14/[$LATEST]f2d4b21cfb33490da2e8f8ef79a483s4',311 getRemainingTimeInMillis: () => {312 return 60000 // 60 seconds313 },314 }315 const response = await handler(event, context)316 expect(response.statusCode).toBe(200)317 expect(JSON.parse(response.body).callbackWaitsForEmptyEventLoop).toEqual(false)318 })319})
modified:src/adapter/aws-lambda/handler.ts+7 added-2 removed
modified:src/adapter/aws-lambda/index.ts+1 added
added:src/adapter/aws-lambda/types.ts+47 added
1// @denoify-ignore2export interface CognitoIdentity {3 cognitoIdentityId: string4 cognitoIdentityPoolId: string5}67export interface ClientContext {8 client: ClientContextClient9 // eslint-disable-next-line @typescript-eslint/no-explicit-any10 Custom?: any11 env: ClientContextEnv12}1314export interface ClientContextClient {15 installationId: string16 appTitle: string17 appVersionName: string18 appVersionCode: string19 appPackageName: string20}2122export interface ClientContextEnv {23 platformVersion: string24 platform: string25 make: string26 model: string27 locale: string28}2930/**31 * {@link Handler} context parameter.32 * See {@link https://docs.aws.amazon.com/lambda/latest/dg/nodejs-prog-model-context.html AWS documentation}.33 */34export interface LambdaContext {35 callbackWaitsForEmptyEventLoop: boolean36 functionName: string37 functionVersion: string38 invokedFunctionArn: string39 memoryLimitInMB: string40 awsRequestId: string41 logGroupName: string42 logStreamName: string43 identity?: CognitoIdentity | undefined44 clientContext?: ClientContext | undefined4546 getRemainingTimeInMillis(): number47}