@busy-hour/blaze
Version:
<h1 align='center'>🔥 Blaze</h1> <div align='center'> An event driven framework for 🔥 Hono.js </div>
124 lines (123 loc) • 4.27 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 BlazeRouter_exports = {};
__export(BlazeRouter_exports, {
BlazeRouter: () => BlazeRouter
});
module.exports = __toCommonJS(BlazeRouter_exports);
var import_hono = require("hono");
var import_instance = require("../internal/config/instance");
var import_logger = require("../internal/logger/index");
var import_common = require("../utils/common");
var import_config = require("../utils/constant/config");
var import_router = require("../utils/helper/router");
class BlazeRouter extends import_hono.Hono {
openAPIRegistry;
zodApi;
constructor(options = {}) {
super({ strict: false, router: options.router });
this.zodApi = import_instance.BlazeConfig.modules[import_config.ExternalModule.ZodApi];
if (!this.zodApi) {
this.openAPIRegistry = null;
return;
}
this.openAPIRegistry = new this.zodApi.OpenAPIRegistry();
}
openapi(route) {
const newRoute = (0, import_router.createOpenApiRouter)(route);
const mws = route.serviceMiddlewares.reduce(
(acc, curr) => {
if (!acc[curr[0]])
acc[curr[0]] = /* @__PURE__ */ new Set();
acc[curr[0]].add(curr[1]);
return acc;
},
{}
);
Object.entries(mws).forEach(
([method, value]) => this.on(method, route.path, ...value)
);
const handlers = [route.handler];
if (!(0, import_common.isNil)(route.middlewares) && !(0, import_common.isEmpty)(route.middlewares)) {
handlers.unshift(...route.middlewares);
}
if (!(0, import_common.isNil)(route.afterMiddlewares) && !(0, import_common.isEmpty)(route.afterMiddlewares)) {
handlers.push(...route.afterMiddlewares);
}
this.on(route.method, route.path, ...handlers);
if (!this.openAPIRegistry)
return;
this.openAPIRegistry.registerPath(newRoute);
}
getOpenAPIDocument(config) {
if (!this.zodApi || !this.openAPIRegistry) {
throw import_logger.Logger.throw(`${import_config.ExternalModule.ZodApi} is not installed`);
}
const generator = new this.zodApi.OpenApiGeneratorV3(
this.openAPIRegistry.definitions
);
const document = generator.generateDocument(config);
return document;
}
getOpenAPI31Document(config) {
if (!this.zodApi || !this.openAPIRegistry) {
throw import_logger.Logger.throw(`${import_config.ExternalModule.ZodApi} is not installed`);
}
const generator = new this.zodApi.OpenApiGeneratorV31(
this.openAPIRegistry.definitions
);
const document = generator.generateDocument(config);
return document;
}
doc(path, config) {
this.get(path, (ctx) => {
try {
const document = this.getOpenAPIDocument(config);
return ctx.json(document);
} catch (e) {
return ctx.json(e, 500);
}
});
}
doc31(path, config) {
this.get(path, (ctx) => {
try {
const document = this.getOpenAPI31Document(config);
return ctx.json(document);
} catch (e) {
return ctx.json(e, 500);
}
});
}
route(path, app) {
super.route(path, app);
if (!(app instanceof BlazeRouter) || !app.openAPIRegistry) {
return this;
}
const docPath = (0, import_router.fixOpenApiPath)(path);
app.openAPIRegistry.definitions.forEach((def) => {
(0, import_router.assignOpenAPIRegistry)(this, docPath, def);
});
return this;
}
}
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
BlazeRouter
});