46 lines
1.5 KiB
JavaScript
46 lines
1.5 KiB
JavaScript
import "#nitro-internal-pollyfills";
|
|
import { useNitroApp } from "nitropack/runtime";
|
|
import {
|
|
normalizeCookieHeader,
|
|
normalizeLambdaIncomingHeaders,
|
|
normalizeLambdaOutgoingBody,
|
|
normalizeLambdaOutgoingHeaders
|
|
} from "nitropack/runtime/internal";
|
|
import { withQuery } from "ufo";
|
|
const nitroApp = useNitroApp();
|
|
export async function handler(event, context) {
|
|
const query = {
|
|
...event.queryStringParameters,
|
|
...event.multiValueQueryStringParameters
|
|
};
|
|
const url = withQuery(
|
|
event.path || event.rawPath,
|
|
query
|
|
);
|
|
const method = event.httpMethod || event.requestContext?.http?.method || "get";
|
|
if ("cookies" in event && event.cookies) {
|
|
event.headers.cookie = event.cookies.join(";");
|
|
}
|
|
const r = await nitroApp.localCall({
|
|
event,
|
|
url,
|
|
context,
|
|
headers: normalizeLambdaIncomingHeaders(event.headers),
|
|
method,
|
|
query,
|
|
body: event.isBase64Encoded ? Buffer.from(event.body || "", "base64").toString("utf8") : event.body
|
|
});
|
|
const isApiGwV2 = "cookies" in event || "rawPath" in event;
|
|
const awsBody = await normalizeLambdaOutgoingBody(r.body, r.headers);
|
|
const cookies = normalizeCookieHeader(r.headers["set-cookie"]);
|
|
return {
|
|
...cookies.length > 0 && {
|
|
...isApiGwV2 ? { cookies } : { multiValueHeaders: { "set-cookie": cookies } }
|
|
},
|
|
statusCode: r.status,
|
|
headers: normalizeLambdaOutgoingHeaders(r.headers, true),
|
|
body: awsBody.body,
|
|
isBase64Encoded: awsBody.type === "binary"
|
|
};
|
|
}
|