UNPKG

@gabliam/koa

Version:
50 lines (49 loc) 1.47 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.converterValue = void 0; const web_core_1 = require("@gabliam/web-core"); const converterValue = (ctx, execCtx, result) => { const methodInfo = execCtx.getMethodInfo(); const sendJsonValue = (value = '') => { let val; try { val = JSON.stringify(value); } catch (_a) { /* istanbul ignore next */ val = value; } ctx.type = 'application/json'; ctx.body = val; }; // response handler if the result is a ResponseEntity function responseEntityHandler(value) { if (value.hasHeader()) { Object.keys(value.headers).forEach((k) => ctx.set(k, `${value.headers[k]}`)); } if (methodInfo.json) { sendJsonValue(value.body); } else { ctx.body = value.body; } ctx.status = value.status; } if (!ctx.headersSent) { if (result !== undefined) { if (result instanceof web_core_1.ResponseEntity) { responseEntityHandler(result); } else if (methodInfo.json) { sendJsonValue(result); } else { ctx.body = result; } if (methodInfo.json) { ctx.type = 'application/json'; } } } }; exports.converterValue = converterValue;