1 | import { AWS, Response } from './types'
|
2 |
|
3 | function stringify(obj: object | string) {
|
4 | return typeof obj === 'object' ? JSON.stringify(obj) : obj
|
5 | }
|
6 |
|
7 | export function normalizeResponse(response: Partial<Response> | string): AWS['HandlerResponse'] {
|
8 | const {
|
9 | isBase64Encoded = false,
|
10 | statusCode = 200,
|
11 | headers = {},
|
12 | multiValueHeaders = {},
|
13 | body = '',
|
14 | html = undefined,
|
15 | json = undefined,
|
16 | xml = undefined,
|
17 | } = typeof response === 'string'
|
18 | ? {
|
19 | body: response,
|
20 | }
|
21 | : response
|
22 |
|
23 | let contentType = 'text/html; charset=utf-8'
|
24 |
|
25 | if (!!json) {
|
26 | contentType = 'application/json; charset=utf-8'
|
27 | } else if (!!xml) {
|
28 | contentType = 'application/xml; charset=utf-8'
|
29 | }
|
30 |
|
31 | return {
|
32 | isBase64Encoded,
|
33 | statusCode,
|
34 | headers: {
|
35 | 'Content-Type': contentType,
|
36 | ...headers,
|
37 | },
|
38 | multiValueHeaders,
|
39 | body: stringify(body || html || json || xml || ''),
|
40 | }
|
41 | }
|