-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathAPIGatewayProxyEventV2StreamRequestHandler.ts
More file actions
38 lines (37 loc) · 1.18 KB
/
Copy pathAPIGatewayProxyEventV2StreamRequestHandler.ts
File metadata and controls
38 lines (37 loc) · 1.18 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
import type { APIGatewayProxyEventV2 } from 'aws-lambda';
import { createStreamRequestHandler } from './_create';
import { HeaderMap } from '@apollo/server';
export const createAPIGatewayProxyEventV2StreamRequestHandler = <
Event extends APIGatewayProxyEventV2 = APIGatewayProxyEventV2,
>() => {
return createStreamRequestHandler<Event>({
parseHttpMethod(event) {
return event.requestContext.http.method;
},
parseHeaders(event) {
const headerMap = new HeaderMap();
for (const [key, value] of Object.entries(event.headers ?? {})) {
headerMap.set(key, value ?? '');
}
return headerMap;
},
parseBody(event, headers) {
if (event.body) {
const contentType = headers.get('content-type');
const parsedBody = event.isBase64Encoded
? Buffer.from(event.body, 'base64').toString('utf8')
: event.body;
if (contentType?.startsWith('application/json')) {
return JSON.parse(parsedBody);
}
if (contentType?.startsWith('text/plain')) {
return parsedBody;
}
}
return '';
},
parseQueryParams(event) {
return event.rawQueryString;
},
});
};