Skip to content

Commit

Permalink
feat: aws APIGatewayProxyEventV2 support
Browse files Browse the repository at this point in the history
Co-authored-by: danielroe <danielroe@users.noreply.github.com>
  • Loading branch information
pi0 and danielroe committed Feb 10, 2022
1 parent 5d3ca2f commit d135f01
Showing 1 changed file with 22 additions and 5 deletions.
27 changes: 22 additions & 5 deletions src/runtime/entries/lambda.ts
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]))
}

0 comments on commit d135f01

Please sign in to comment.