Skip to content
This repository was archived by the owner on Nov 19, 2024. It is now read-only.

Commit e2c48be

Browse files
authored
fix: Lambda adapter supports application/x-www-form-urlencoded (#330)
1 parent dc26bcd commit e2c48be

File tree

2 files changed

+80
-2
lines changed

2 files changed

+80
-2
lines changed

src/adapters/aws-lambda.ts

+20
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,26 @@ const createMockNodeHTTPRequest = (path: string, event: APIGatewayEvent): NodeHT
6060
});
6161
}
6262
}
63+
if (contentType === 'application/x-www-form-urlencoded') {
64+
try {
65+
if (event.body) {
66+
const searchParamsString = event.isBase64Encoded
67+
? Buffer.from(event.body, 'base64').toString('utf-8')
68+
: event.body;
69+
const searchParams = new URLSearchParams(searchParamsString);
70+
body = {} as Record<string, unknown>;
71+
for (const [key, value] of searchParams.entries()) {
72+
body[key] = value;
73+
}
74+
}
75+
} catch (cause) {
76+
throw new TRPCError({
77+
message: 'Failed to parse request body',
78+
code: 'PARSE_ERROR',
79+
cause,
80+
});
81+
}
82+
}
6383

6484
return createRequest({
6585
url,

test/adapters/aws-lambda.test.ts

+60-2
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ describe('v1', () => {
8484
});
8585
});
8686

87-
test('with body input', async () => {
87+
test('with JSON body input', async () => {
8888
const {
8989
statusCode,
9090
headers,
@@ -115,6 +115,35 @@ describe('v1', () => {
115115
});
116116
});
117117

118+
test('with url encoded body input', async () => {
119+
const {
120+
statusCode,
121+
headers,
122+
body: rawBody,
123+
} = await handler(
124+
mockAPIGatewayProxyEventV1({
125+
body: 'name=Aphex',
126+
headers: {
127+
'content-type': 'application/x-www-form-urlencoded',
128+
},
129+
method: 'POST',
130+
path: 'hello',
131+
queryStringParameters: {},
132+
resource: '/hello',
133+
}),
134+
ctx,
135+
);
136+
const body = JSON.parse(rawBody);
137+
138+
expect(statusCode).toBe(200);
139+
expect(headers).toEqual({
140+
'content-type': 'application/json',
141+
});
142+
expect(body).toEqual({
143+
greeting: 'Hello Aphex',
144+
});
145+
});
146+
118147
test('with context', async () => {
119148
const {
120149
statusCode,
@@ -275,7 +304,7 @@ describe('v2', () => {
275304
});
276305
});
277306

278-
test('with body input', async () => {
307+
test('with JSON body input', async () => {
279308
const {
280309
statusCode,
281310
headers,
@@ -306,6 +335,35 @@ describe('v2', () => {
306335
});
307336
});
308337

338+
test('with url encoded body input', async () => {
339+
const {
340+
statusCode,
341+
headers,
342+
body: rawBody,
343+
} = await handler(
344+
mockAPIGatewayProxyEventV2({
345+
body: 'name=Aphex',
346+
headers: {
347+
'content-type': 'application/x-www-form-urlencoded',
348+
},
349+
method: 'POST',
350+
path: 'hello',
351+
queryStringParameters: {},
352+
routeKey: '$default',
353+
}),
354+
ctx,
355+
);
356+
const body = JSON.parse(rawBody);
357+
358+
expect(statusCode).toBe(200);
359+
expect(headers).toEqual({
360+
'content-type': 'application/json',
361+
});
362+
expect(body).toEqual({
363+
greeting: 'Hello Aphex',
364+
});
365+
});
366+
309367
test('with context', async () => {
310368
const {
311369
statusCode,

0 commit comments

Comments
 (0)