-
Notifications
You must be signed in to change notification settings - Fork 501
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: aws APIGatewayProxyEventV2 support
Co-authored-by: danielroe <danielroe@users.noreply.github.com>
- Loading branch information
Showing
1 changed file
with
22 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,21 +1,38 @@ | ||
import type { APIGatewayProxyEvent, APIGatewayProxyEventHeaders, APIGatewayProxyEventV2, APIGatewayProxyResult, APIGatewayProxyResultV2, Context } from 'aws-lambda' | ||
import '#polyfill' | ||
import { withQuery } from 'ufo' | ||
import type { HeadersObject } from 'unenv/runtime/_internal/types' | ||
import { localCall } from '..' | ||
|
||
export async function handler (event, context) { | ||
export const handler = async function handler (event: APIGatewayProxyEvent | APIGatewayProxyEventV2, context: Context): Promise<APIGatewayProxyResult | APIGatewayProxyResultV2> { | ||
const url = withQuery((event as APIGatewayProxyEvent).path || (event as APIGatewayProxyEventV2).rawPath, event.queryStringParameters) | ||
const method = (event as APIGatewayProxyEvent).httpMethod || (event as APIGatewayProxyEventV2).requestContext?.http?.method || 'get' | ||
|
||
if ('cookies' in event) { | ||
event.headers.cookie = event.cookies.join(',') | ||
} | ||
|
||
const r = await localCall({ | ||
event, | ||
url: withQuery(event.path, event.queryStringParameters), | ||
url, | ||
context, | ||
headers: event.headers, | ||
method: event.httpMethod, | ||
headers: normalizeIncomingHeaders(event.headers), | ||
method, | ||
query: event.queryStringParameters, | ||
body: event.body // TODO: handle event.isBase64Encoded | ||
}) | ||
|
||
return { | ||
statusCode: r.status, | ||
headers: r.headers, | ||
headers: normalizeOutgoingHeaders(r.headers), | ||
body: r.body.toString() | ||
} | ||
} | ||
|
||
function normalizeIncomingHeaders (headers: APIGatewayProxyEventHeaders) { | ||
return Object.fromEntries(Object.entries(headers).map(([key, value]) => [key.toLowerCase(), value])) | ||
} | ||
|
||
function normalizeOutgoingHeaders (headers: HeadersObject) { | ||
return Object.fromEntries(Object.entries(headers).map(([k, v]) => [k, Array.isArray(v) ? v.join(',') : v])) | ||
} |