Skip to content

Commit 0cf57c4

Browse files
authored
Fix validateWebhook backwards incompatibility with older Node.js (#298)
* Use backward compatible syntax for accessing object keys on requestData.headers * Stringifying requestData.body when it is of type object. This is backward compatibility for Next.js 12.1.x / Node.js v18
1 parent ac75077 commit 0cf57c4

File tree

1 file changed

+11
-3
lines changed

1 file changed

+11
-3
lines changed

lib/util.js

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -35,9 +35,15 @@ async function validateWebhook(requestData, secret) {
3535
const signingSecret = secret || requestData.secret;
3636

3737
if (requestData && requestData.headers && requestData.body) {
38-
id = requestData.headers.get("webhook-id");
39-
timestamp = requestData.headers.get("webhook-timestamp");
40-
signature = requestData.headers.get("webhook-signature");
38+
id =
39+
requestData.headers["webhook-id"] ||
40+
requestData.headers.get?.("webhook-id");
41+
timestamp =
42+
requestData.headers["webhook-timestamp"] ||
43+
requestData.headers.get?.("webhook-timestamp");
44+
signature =
45+
requestData.headers["webhook-signature"] ||
46+
requestData.headers.get?.("webhook-signature");
4147
body = requestData.body;
4248
}
4349

@@ -49,6 +55,8 @@ async function validateWebhook(requestData, secret) {
4955
}
5056
} else if (isTypedArray(body)) {
5157
body = await new Blob([body]).text();
58+
} else if (typeof body === "object") {
59+
body = JSON.stringify(body);
5260
} else if (typeof body !== "string") {
5361
throw new Error("Invalid body type");
5462
}

0 commit comments

Comments
 (0)