Skip to content

Commit 37880ff

Browse files
committed
feat(server): add ctx.request.rawBody when parsing JSON or url encoded forms
This is required in some cases where an SDK provides their own webhook data validation and parsing.
1 parent 45bf26f commit 37880ff

1 file changed

Lines changed: 13 additions & 4 deletions

File tree

  • packages/server/src/middleware

‎packages/server/src/middleware/body.js‎

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -78,27 +78,34 @@ export function createBodyParser(opts = {}) {
7878

7979
return async function (ctx) {
8080
let bodyResult;
81+
let rawBodyResult;
8182
// only parse the body on specifically chosen methods
8283
// @ts-ignore
8384
if (opts.parsedMethods.includes(ctx.method.toUpperCase())) {
8485
try {
8586
if (opts.json && ctx.is(jsonTypes)) {
86-
bodyResult = await coBody.json(ctx, {
87+
const result = await coBody.json(ctx, {
8788
encoding: opts.encoding,
8889
limit: opts.jsonLimit,
8990
strict: true,
90-
returnRawBody: false,
91+
returnRawBody: true,
9192
});
93+
94+
bodyResult = result.parsed;
95+
rawBodyResult = result.raw;
9296
} else if (
9397
opts.urlencoded &&
9498
ctx.is("application/x-www-form-urlencoded")
9599
) {
96-
bodyResult = await coBody.form(ctx, {
100+
const result = await coBody.form(ctx, {
97101
encoding: opts.encoding,
98102
limit: opts.urlencodedLimit,
99103
queryString: opts.queryString,
100-
returnRawBody: false,
104+
returnRawBody: true,
101105
});
106+
107+
bodyResult = result.parsed;
108+
rawBodyResult = result.raw;
102109
} else if (opts.text && ctx.is("text/*")) {
103110
bodyResult = await coBody.text(ctx, {
104111
encoding: opts.encoding,
@@ -158,5 +165,7 @@ export function createBodyParser(opts = {}) {
158165

159166
// @ts-expect-error
160167
ctx.request.body = bodyResult;
168+
// @ts-expect-error
169+
ctx.request.rawBody = rawBodyResult;
161170
};
162171
}

0 commit comments

Comments
 (0)