Skip to content

Commit a983862

Browse files
committed
feat(response): make result optional
1 parent 8d24484 commit a983862

File tree

2 files changed

+16
-11
lines changed

2 files changed

+16
-11
lines changed

src/aResponse.spec.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ void describe('aResponse()', () => {
1717
statusCode: 200,
1818
headers: {
1919
'Cache-Control': `public, max-age=${60 * 10}`,
20+
'content-length': '59',
2021
'content-type': 'application/json',
2122
},
2223
body: JSON.stringify({

src/aResponse.ts

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -6,18 +6,22 @@ import type { HttpStatusCode } from '@hello.nrfcloud.com/proto/hello'
66

77
export const aResponse = (
88
status: HttpStatusCode,
9-
result: {
9+
result?: {
1010
'@context': URL
1111
} & Record<string, unknown>,
1212
cacheForSeconds: number = 60,
1313
headers?: APIGatewayProxyStructuredResultV2['headers'],
14-
): APIGatewayProxyResultV2 => ({
15-
statusCode: status,
16-
headers: {
17-
'content-type': 'application/json',
18-
'Cache-Control':
19-
cacheForSeconds > 0 ? `public, max-age=${cacheForSeconds}` : 'no-store',
20-
...(headers ?? {}),
21-
},
22-
body: JSON.stringify(result),
23-
})
14+
): APIGatewayProxyResultV2 => {
15+
const body = result !== undefined ? JSON.stringify(result) : undefined
16+
return {
17+
statusCode: status,
18+
headers: {
19+
...(body !== undefined ? { 'content-type': 'application/json' } : {}),
20+
'content-length': `${body?.length ?? 0}`,
21+
'Cache-Control':
22+
cacheForSeconds > 0 ? `public, max-age=${cacheForSeconds}` : 'no-store',
23+
...(headers ?? {}),
24+
},
25+
body,
26+
}
27+
}

0 commit comments

Comments
 (0)