@busy-hour/blaze
Version:
<h1 align='center'>🔥 Blaze</h1> <div align='center'> An event driven framework for 🔥 Hono.js </div>
166 lines (165 loc) • 5.36 kB
JavaScript
"use strict";
var __create = Object.create;
var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __getProtoOf = Object.getPrototypeOf;
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 __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
// If the importer is in node compatibility mode or this is not an ESM
// file that has been converted to a CommonJS file using a Babel-
// compatible transform (i.e. "__esModule" has not been set), then set
// "default" to the CommonJS "module.exports" for node compatibility.
isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
mod
));
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
var service_exports = {};
__export(service_exports, {
BlazeService: () => BlazeService
});
module.exports = __toCommonJS(service_exports);
var import_node_path = __toESM(require("node:path"), 1);
var import_service = require("../extractor/service/index");
var import_internal = require("../internal/index");
var import_BlazeRouter = require("../router/BlazeRouter");
var import_service2 = require("../utils/helper/service");
var import_action = require("./action");
var import_event = require("./event");
var import_rest = require("./rest");
class BlazeService {
servicePath;
serviceName;
restPath;
mainRouter;
actions;
events;
rests;
handlers;
middlewares;
router;
ctx;
service;
isStarted;
constructor(options) {
const { service, ctx, servicePath, app, middlewares } = options;
this.service = service;
this.servicePath = servicePath;
this.ctx = ctx;
this.serviceName = (0, import_service.getServiceName)(service);
this.restPath = (0, import_service.getRestPath)(service);
this.mainRouter = app;
this.isStarted = false;
this.actions = [];
this.events = [];
this.rests = [];
this.handlers = [];
this.middlewares = service.middlewares ?? [];
if (middlewares) {
this.middlewares.unshift(...middlewares);
}
this.router = null;
this.loadServiceActions();
this.loadServiceEvents();
}
loadRest(action) {
if (!this.router) {
this.router = new import_BlazeRouter.BlazeRouter({
router: this.service.router
});
}
const restInstance = new import_rest.BlazeServiceRest({
action,
router: this.router,
service: this.service,
middlewares: this.middlewares
});
this.rests.push(restInstance);
}
loadServiceActions() {
if (!this.service.actions)
return this.actions;
for (const actionAlias in this.service.actions) {
const action = this.service.actions[actionAlias];
if (action.rest)
this.loadRest(action);
const instance = new import_action.BlazeServiceAction({
action,
actionAlias,
serviceName: this.serviceName
});
this.actions.push(instance);
}
return this.actions;
}
loadServiceEvents() {
if (!this.service.events)
return this.events;
for (const eventAlias in this.service.events) {
const event = this.service.events[eventAlias];
const instance = new import_event.BlazeServiceEvent({
event,
eventAlias,
serviceName: this.serviceName
});
this.events.push(instance);
}
return this.events;
}
assignRestRoute() {
if (!this.router)
return;
this.mainRouter.route(`/${this.restPath}`, this.router);
}
onStopped() {
this.service.onStopped?.(this.handlers);
this.actions.forEach((action) => {
import_internal.BlazeEvent.off(action.actionName, action.actionHandler);
});
}
onRestarted() {
this.rests.forEach((rest) => {
const method = rest.method ?? "ALL";
this.router?.on?.(method, rest.path, rest.restHandler);
});
this.actions.forEach((action) => {
import_internal.BlazeEvent.offAll(action.actionName);
import_internal.BlazeEvent.on(action.actionName, action.actionHandler);
});
}
onStarted() {
if (this.isStarted)
return;
this.assignRestRoute();
this.service.onStarted?.(this.ctx);
this.isStarted = true;
}
static async create(options) {
const servicePath = import_node_path.default.resolve(options.sourcePath, options.servicePath);
const serviceFile = await (0, import_service2.loadService)(servicePath);
const service = new BlazeService({
app: options.app,
ctx: options.ctx,
servicePath,
service: serviceFile,
middlewares: options.middlewares
});
return service;
}
}
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
BlazeService
});