-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrestApiResponseFormatter.ts
69 lines (61 loc) · 2.03 KB
/
restApiResponseFormatter.ts
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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
import { ALLOWED_ORIGINS } from "./constants";
import { ApiEvent, ApiResponse } from "./types";
export const getAccessControlAllowOriginHeader = <INPUT_EVENT extends ApiEvent>(
event: INPUT_EVENT
):
| {
"Access-Control-Allow-Origin": string;
}
| Record<string, never> => {
const origin = event.headers?.origin;
if (
origin === undefined ||
event.requestContext?.stage === undefined ||
!ALLOWED_ORIGINS?.[event.requestContext?.stage]?.includes(origin)
) {
return {};
}
return {
"Access-Control-Allow-Origin": origin,
};
};
/** Format the response of a REST API event with the PayloadFormatVersion 2.0 of the AWS Lambda Proxy Integration for ApiGatewayV2 (HTTP API).
*
* If the response is not a JSON object or already has a property statusCode, it is expected to be correctly formatted.
*
* @param event Lambda input event from REST API Lambda integration
* @param response Lambda handler response
*
* @returns The response (modified in place) formatted with:
* - statusCode: 200
* - headers:
* - Content-Type: application/json.
* - Access-Control-Allow-Origin: Origin if allowed
* - body: stringified function's response.
*
*/
export const restApiResponseFormatterInPlace = <
REST_API_EVENT extends ApiEvent,
LAMBDA_HANDLER_RESPONSE extends Record<string, unknown> & Partial<ApiResponse>
>(
event: REST_API_EVENT,
response: LAMBDA_HANDLER_RESPONSE
): ApiResponse => {
// If the response is not a JSON object or already has a property statusCode, it is expected to be correctly formatted.
if (typeof response !== "object" || response.statusCode !== undefined) {
return response as ApiResponse;
}
const stringifiedBody = JSON.stringify(response);
// Clean response
for (const key in response) {
delete response[key];
}
response.body = stringifiedBody;
response.statusCode = 200;
response.headers = {
"Content-Type": "application/json",
// Required for CORS support to work
...getAccessControlAllowOriginHeader(event),
};
return response as ApiResponse;
};