Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 4 additions & 5 deletions src/presets/aws-lambda/runtime/_utils.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import type { APIGatewayProxyEvent, APIGatewayProxyEventV2 } from "aws-lambda";
import type { APIGatewayProxyEvent, APIGatewayProxyEventV2, Context } from "aws-lambda";
import type { ServerRequest } from "srvx";
import { stringifyQuery } from "ufo";

// Incoming (AWS => Web)

export function awsRequest(
event: APIGatewayProxyEvent | APIGatewayProxyEventV2,
context: unknown
context: Context
): ServerRequest {
const method = awsEventMethod(event);
const url = awsEventURL(event);
Expand All @@ -17,10 +17,9 @@ export function awsRequest(

// srvx compatibility
req.runtime ??= { name: "aws-lambda" };
// @ts-expect-error (add to srvx types)
req.runtime.aws ??= { event, context } as any;
req.runtime.awsLambda ??= { event, context };

return new Request(url, { method, headers, body });
return req;
}

function awsEventMethod(event: APIGatewayProxyEvent | APIGatewayProxyEventV2): string {
Expand Down
61 changes: 61 additions & 0 deletions test/unit/aws-lambda.utils.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
import type { APIGatewayProxyEventV2 } from "aws-lambda";
import { describe, expect, it } from "vitest";
import { awsRequest } from "../../src/presets/aws-lambda/runtime/_utils.ts";

const mockContext = {
functionName: "test",
functionVersion: "$LATEST",
invokedFunctionArn: "arn:aws:lambda:us-east-1:123456789012:function:test",
memoryLimitInMB: "128",
awsRequestId: "test-request-id",
logGroupName: "/aws/lambda/test",
logStreamName: "test-stream",
callbackWaitsForEmptyEventLoop: false,
getRemainingTimeInMillis: () => 1000,
done: () => {},
fail: () => {},
succeed: () => {},
};

const mockEvent: APIGatewayProxyEventV2 = {
rawPath: "/test",
headers: { host: "example.com" },
rawQueryString: "",
body: undefined,
isBase64Encoded: false,
version: "2.0",
routeKey: "GET /test",
requestContext: {
accountId: "123456789012",
apiId: "api-id",
domainName: "example.com",
domainPrefix: "example",
requestId: "request-id",
routeKey: "GET /test",
stage: "$default",
time: "01/Jan/2024:00:00:00 +0000",
timeEpoch: 1704067200,
http: {
method: "GET",
path: "/test",
protocol: "HTTP/1.1",
sourceIp: "1.2.3.4",
userAgent: "test-agent",
},
},
};

describe("awsRequest", () => {
it("returns the same Request object with runtime metadata", () => {
const req = awsRequest(mockEvent, mockContext);
expect(req.runtime).toBeDefined();
expect(req.runtime?.name).toBe("aws-lambda");
});

it("sets runtime.awsLambda with event and context", () => {
const req = awsRequest(mockEvent, mockContext);
expect(req.runtime?.awsLambda).toBeDefined();
expect(req.runtime?.awsLambda?.event).toBe(mockEvent);
expect(req.runtime?.awsLambda?.context).toBe(mockContext);
});
});