summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorEuAndreh <eu@euandre.org>2024-03-16 05:38:17 -0300
committerEuAndreh <eu@euandre.org>2024-03-16 05:38:17 -0300
commita03abe678afd45e9d139e13c9d17a569c2853547 (patch)
treec1703f22703ed878f1d9af0f4ba1d333e9d8ad2d /src
parentsrc/hero.mjs: Include stacktrace in 500 log entry (diff)
downloadpapod-a03abe678afd45e9d139e13c9d17a569c2853547.tar.gz
papod-a03abe678afd45e9d139e13c9d17a569c2853547.tar.xz
src/hero.mjs: interceptors.contentType(): Generate body from status when missing
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 || {})
},
};