Skip to content

Commit 0fd1478

Browse files
committed
fix: parse body as form data no matter what in trmnl/generate
1 parent c8adc40 commit 0fd1478

File tree

2 files changed

+12
-6
lines changed

2 files changed

+12
-6
lines changed

src/server/trmnl/generate.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ import { verifyAccessToken } from '@/server/trmnl/token.ts';
1111
import { fetchAssignmentData } from '@/server/utilities/fetchAssignmentData.ts';
1212
import {
1313
createRequestLogger,
14-
performSafeContextBodyParse,
14+
performSafeContextFormBodyParse,
1515
} from '@/server/utilities/honoUtilities.ts';
1616
import { stringifyError, tryCatch } from '@/shared/utilities/tryCatch.ts';
1717

@@ -50,7 +50,7 @@ export const generate = async (c: Context) => {
5050
}
5151

5252
// Parse the body as application/x-www-form-urlencoded
53-
const [formSuccess, form] = await performSafeContextBodyParse(c);
53+
const [formSuccess, form] = await performSafeContextFormBodyParse(c);
5454
if (!formSuccess) {
5555
logger.info('Invalid form body.');
5656
return c.text('Invalid form body.', 400);

src/server/utilities/honoUtilities.ts

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,12 +14,18 @@ export const performSafeContextJsonParse = async (
1414
}
1515
};
1616

17-
export const performSafeContextBodyParse = async (
17+
export const performSafeContextFormBodyParse = async (
1818
c: Context,
19-
): Promise<[true, unknown] | [false, undefined]> => {
19+
): Promise<[true, Record<string, string>] | [false, undefined]> => {
2020
try {
21-
const body = await c.req.parseBody();
22-
return [true, body];
21+
const body = await c.req.text();
22+
// Pase x-www-form-urlencoded body
23+
const params = new URLSearchParams(body);
24+
const bodyObject: Map<string, string> = new Map();
25+
params.forEach((value, key) => {
26+
bodyObject.set(key, value);
27+
});
28+
return [true, Object.fromEntries(bodyObject)];
2329
} catch (_error) {
2430
return [false, undefined];
2531
}

0 commit comments

Comments
 (0)