kequapp
Version:
DEPRECATED: renamed to @kequtech/arbor
32 lines (31 loc) • 821 B
JavaScript
import { Ex } from "../../ex.js";
import { createRenderer } from "../modules.js";
export const rendererJson = createRenderer({
contentType: 'application/json',
action(payload, { req, res }) {
const json = generateJson(payload);
res.setHeader('Content-Length', Buffer.byteLength(json));
if (req.method === 'HEAD') {
res.end();
}
else {
res.end(json);
}
},
});
function generateJson(payload) {
try {
if (process.env.NODE_ENV === 'production') {
return JSON.stringify(payload);
}
else {
return JSON.stringify(payload, null, 2);
}
}
catch (error) {
throw Ex.InternalServerError('Invalid json response', {
payload,
error,
});
}
}