@busy-hour/blaze
Version:
<h1 align='center'>🔥 Blaze</h1> <div align='center'> An event driven framework for 🔥 Hono.js </div>
148 lines (147 loc) • 4.71 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, {
BlazeServiceRest: () => BlazeServiceRest
});
module.exports = __toCommonJS(rest_exports);
var import_rest = require("../extractor/rest/index");
var import_handler = require("../handler/index");
var import_rest2 = require("../handler/rest");
var import_context = require("../internal/context/index");
var import_errors = require("../internal/errors/index");
var import_validation = require("../internal/errors/validation");
var import_logger = require("../internal/logger/index");
var import_common = require("../utils/common");
var import_rest3 = require("../utils/constant/rest/index");
class BlazeServiceRest {
path;
method;
service;
action;
constructor(options) {
const { router, action, service } = options;
if (!action.rest) {
throw import_logger.Logger.throw("Rest property is required");
}
const [method, path] = (0, import_rest.extractRestParams)(action.rest);
this.path = path;
this.method = method;
this.action = action;
this.service = service;
const { request, responses } = this.openAPIConfig;
const serviceMiddlewares = options.middlewares;
const afterMiddlewares = action.afterMiddlewares ?? [];
const middlewares = action.middlewares ?? [];
const tags = [];
if (this.service.tags) {
if (typeof this.service.tags === "string") {
tags.push(this.service.tags);
} else {
tags.push(...this.service.tags);
}
} else if (this.service.name) {
tags.push(this.service.name);
}
router.openapi({
method: method || import_rest3.REST_METHOD.ALL,
handler: this.restHandler.bind(this),
path,
request,
responses,
middlewares,
serviceMiddlewares,
afterMiddlewares,
tags
});
}
async restHandler(...args) {
const [honoCtx, next] = args;
try {
const ctx = await import_context.BlazeContext.create({
honoCtx,
// NULL => automatically use honoCtx value instead
body: null,
headers: null,
params: null,
query: null,
validator: this.action.validator ?? null,
meta: this.action.meta ?? null
});
const rest = await (0, import_rest2.handleRest)({
ctx,
honoCtx,
promise: (0, import_handler.eventHandler)(this.action, ctx)
});
if (!this.action.afterMiddlewares || (0, import_common.isEmpty)(this.action.afterMiddlewares) || !rest.ok) {
return rest.resp;
}
return next();
} catch (err) {
if (err instanceof import_validation.BlazeValidationError && this.action.onRestError) {
const rest = await (0, import_rest2.handleRest)({
ctx: err.ctx,
honoCtx,
promise: this.action.onRestError(err.ctx, err.errors)
});
return rest.resp;
}
const status = err instanceof import_errors.BlazeError ? err.status : 500;
return honoCtx.json(err, status);
}
}
get openAPIConfig() {
if (!this.action.openapi)
return {
request: {},
responses: {}
};
const { openapi, validator } = this.action;
const request = {};
const responses = openapi.responses ?? {};
if (validator?.body && openapi.body) {
request.body = {
content: {
[openapi.body.type]: {
schema: validator.body
}
},
description: openapi.body?.description,
required: openapi.body?.required
};
}
if (validator?.params) {
request.params = validator.params;
}
if (validator?.query) {
request.query = validator.query;
}
if (validator?.header) {
request.headers = validator.header;
}
return {
request,
responses
};
}
}
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
BlazeServiceRest
});