summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/hero.mjs18
1 files changed, 13 insertions, 5 deletions
diff --git a/src/hero.mjs b/src/hero.mjs
index 5169284..b378887 100644
--- a/src/hero.mjs
+++ b/src/hero.mjs
@@ -71,15 +71,23 @@ export const interceptorsFn = ({
},
contentType: async (req, next) => {
const response = await next(req);
- const [mimeType, body] = typeof response.body === "string" ?
- ["text/html", response.body] :
- ["application/json", JSON.stringify(response.body) || ""];
+ const { status, body, headers } = response;
+ assert.equal(typeof status, "number");
+ const mappings = {
+ string: () => [ "text/html", body ],
+ undefined: () => [ "text/plain", http.STATUS_CODES[status] + "\n" ],
+ FALLBACK: () => [ "application/json", JSON.stringify(body) ],
+ };
+ const type = typeof body;
+ assert.notEqual(type, "FALLBACK");
+ const [mimeType, renderedBody] =
+ (mappings[type] || mappings.FALLBACK)();
return {
...response,
- body,
+ body: renderedBody,
headers: {
"Content-Type": mimeType,
- "Content-Length": Buffer.byteLength(body),
+ "Content-Length": Buffer.byteLength(renderedBody),
...(response.headers || {})
},
};