Skip to content

Commit 3660ed1

Browse files
committed
fix: support request bodies in function urls
1 parent 368a3e6 commit 3660ed1

File tree

3 files changed

+16
-2
lines changed

3 files changed

+16
-2
lines changed

src/__test__/examples.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
import 'source-map-support/register.js';
21
import { ALBEvent, APIGatewayProxyEvent, CloudFrontRequestEvent } from 'aws-lambda';
32
import { UrlEvent } from '../http/request.url';
43

src/__test__/url.test.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,5 +46,17 @@ o.spec('FunctionUrl', () => {
4646
const req = new LambdaUrlRequest(UrlExample, fakeContext, fakeLog);
4747
o(req.path).equals('/v1/🦄/🌈/🦄.json');
4848
o(req.query.get('🦄')).equals('abc123');
49+
o(req.body).equals(null);
50+
});
51+
52+
o('should parse body', () => {
53+
const newReq = clone(UrlExample);
54+
newReq.body = JSON.stringify({ key: '🦄' });
55+
newReq.headers['content-type'] = 'application/json';
56+
newReq.requestContext.http.method = 'POST';
57+
58+
const req = new LambdaUrlRequest(newReq, fakeContext, fakeLog);
59+
o(req.body).equals('{"key":"🦄"}');
60+
o(req.json()).deepEquals({ key: '🦄' });
4961
});
5062
});

src/http/request.url.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@ export interface UrlEvent {
99
routeKey: string;
1010

1111
rawPath: string;
12+
/** Body encoded as a string, this could be base64 encoded see isBase64Encoded */
13+
body?: string;
1214
/** Query string without '?' @example "api=abc123" */
1315
rawQueryString: string;
1416
headers: Record<string, string>;
@@ -71,7 +73,8 @@ export class LambdaUrlRequest<T extends Record<string, string>> extends LambdaHt
7173
}
7274

7375
get body(): string | null {
74-
return null;
76+
if (this.event.body == null) return null;
77+
return this.event.body;
7578
}
7679

7780
static is(x: unknown): x is UrlEvent {

0 commit comments

Comments
 (0)