@busy-hour/blaze
Version:
<h1 align='center'>🔥 Blaze</h1> <div align='center'> An event driven framework for 🔥 Hono.js </div>
96 lines (95 loc) • 3.07 kB
JavaScript
;
var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __hasOwnProp = Object.prototype.hasOwnProperty;
var __export = (target, all) => {
for (var name in all)
__defProp(target, name, { get: all[name], enumerable: true });
};
var __copyProps = (to, from, except, desc) => {
if (from && typeof from === "object" || typeof from === "function") {
for (let key of __getOwnPropNames(from))
if (!__hasOwnProp.call(to, key) && key !== except)
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
}
return to;
};
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
var rest_exports = {};
__export(rest_exports, {
getRestResponse: () => getRestResponse,
getRouteHandler: () => getRouteHandler,
handleRest: () => handleRest,
handleRestError: () => handleRestError,
handleRestResponse: () => handleRestResponse
});
module.exports = __toCommonJS(rest_exports);
var import_errors = require("../internal/errors/index");
var import_common = require("../utils/common");
var import_rest = require("../utils/constant/rest/index");
function getRouteHandler(router, method) {
if (!method)
return router.all;
return router[method.toLowerCase()];
}
function getRestResponse(options) {
const { status, headers } = options.ctx;
if (!status) {
return [options.result, void 0, void 0];
}
if ((0, import_common.isEmpty)(headers)) {
return [options.result, status, void 0];
}
const resHeaders = (0, import_common.mapToObject)(options.ctx.headers);
return [options.result, status, resHeaders];
}
function handleRestError(options) {
const { err, ctx, honoCtx } = options;
let status = ctx.status ?? 500;
if (err instanceof import_errors.BlazeError) {
status = err.status;
}
return honoCtx.json(err, status);
}
function handleRestResponse(options) {
const { ctx, honoCtx } = options;
const args = getRestResponse(options);
switch (ctx.response) {
case import_rest.RESPONSE_TYPE.TEXT:
return honoCtx.text(...args);
case import_rest.RESPONSE_TYPE.HTML:
return honoCtx.html(...args);
case import_rest.RESPONSE_TYPE.BODY:
return honoCtx.body(...args);
case import_rest.RESPONSE_TYPE.JSON:
default:
return honoCtx.json(...args);
}
}
async function handleRest(options) {
const { ctx, honoCtx, promise } = options;
try {
const result = await promise;
if ((0, import_common.isNil)(result)) {
return { resp: honoCtx.body(null, 204), ok: true };
}
return {
resp: await handleRestResponse({ ctx, honoCtx, result }),
ok: true
};
} catch (err) {
return {
resp: handleRestError({ ctx, err, honoCtx }),
ok: false
};
}
}
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
getRestResponse,
getRouteHandler,
handleRest,
handleRestError,
handleRestResponse
});