Created
April 3, 2024 15:17
-
-
Save josefaidt/965241c25e7feb3cd97a757809b0f446 to your computer and use it in GitHub Desktop.
Parse a URL encoded form event in AWS Lambda
This file contains hidden or 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
import type { APIGatewayProxyEventV2 } from "aws-lambda" | |
import busboy from "busboy" | |
// https://github.com/francismeynard/lambda-multipart-parser/blob/master/index.js | |
async function parseUrlEncodedFormEvent<T>( | |
event: APIGatewayProxyEventV2, | |
): Promise<T> { | |
return new Promise((resolve, reject) => { | |
const fields: Record<string, unknown> = {} | |
const bb = busboy({ headers: event.headers }) | |
bb.on("field", (name, value, info) => { | |
fields[name] = value | |
}) | |
bb.on("finish", () => { | |
resolve(fields as T) | |
}) | |
bb.on("error", reject) | |
const encoding = event.isBase64Encoded ? "base64" : "binary" | |
bb.write(event.body, encoding) | |
bb.end() | |
}) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment