push
github
67 of 67 branches covered (100.0%)
Branch coverage included in aggregate %.
378 of 378 relevant lines covered (100.0%)
18.69 hits per line
| 1 |
export interface ResponseFormatter {
|
1✔ |
| 2 |
(body?: any, options?: object): Response |
1✔ |
| 3 |
} |
1✔ |
| 4 |
|
1✔ |
| 5 |
export interface BodyTransformer {
|
1✔ |
| 6 |
(body: any): string
|
1✔ |
| 7 |
} |
1✔ |
| 8 |
|
1✔ |
| 9 |
type ResponseFormatterOptions = {
|
1✔ |
| 10 |
headers?: object |
1✔ |
| 11 |
} & ResponseInit |
1✔ |
| 12 |
|
1✔ |
| 13 |
export const createResponse = |
1✔ |
| 14 |
( |
1✔ |
| 15 |
format = 'text/plain; charset=utf-8', |
1✔ |
| 16 |
transform?: BodyTransformer |
1✔ |
| 17 |
): ResponseFormatter => |
1✔ |
| 18 |
(body, options: ResponseFormatterOptions = {}) => {
|
|
| 19 |
const { headers = {}, ...rest } = options
|
18✔ |
| 20 |
|
18✔ |
| 21 |
if (body?.constructor.name === 'Response') return body |
|
| 22 |
|
17✔ |
| 23 |
return new Response(transform ? transform(body) : body, { |
|
| 24 |
headers: {
|
18✔ |
| 25 |
'content-type': format, |
18✔ |
| 26 |
...headers, |
18✔ |
| 27 |
}, |
18✔ |
| 28 |
...rest, |
18✔ |
| 29 |
}) |
18✔ |
| 30 |
} |
18✔ |