UNPKG

@busy-hour/blaze

Version:

<h1 align='center'>🔥 Blaze</h1> <div align='center'> An event driven framework for 🔥 Hono.js </div>

232 lines (231 loc) • 7.01 kB
"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 Blaze_exports = {}; __export(Blaze_exports, { Blaze: () => Blaze }); module.exports = __toCommonJS(Blaze_exports); var import_node_fs = __toESM(require("node:fs"), 1); var import_instance = require("../internal/config/instance"); var import_context = require("../internal/context/index"); var import_logger = require("../internal/logger/index"); var import_service = require("../loader/service"); var import_trpc = require("../loader/trpc/index"); var import_common = require("../utils/common"); var import_config = require("../utils/constant/config/index"); var import_BlazeRouter = require("./BlazeRouter"); class Blaze { $services; router; /** * Shorthand for `app.router.doc`. * It allows you to generate OpenAPI documents and serve it at the given path. * @example * ```ts * app.doc('/doc', { * openapi: '3.0.0', * info: { * version: '1.0.0', * title: 'Blaze OpenAPI Example' * } * }) * ``` * @see {@link BlazeRouter.doc} */ doc; /** * Shorthand for `app.router.doc31`. * It allows you to generate OpenAPI documents and serve it at the given path. * @example * ```ts * app.doc31('/doc', { * openapi: '3.1.0', * info: { * version: '1.0.0', * title: 'Blaze OpenAPI Example' * } * }) * ``` * @see {@link BlazeRouter.doc} */ doc31; /** * Shorthand for `app.router.use`. * It allows you to add middleware to the router * @example * ```ts * app.use(cors()) * ``` * @see {@link BlazeRouter.use} */ use; ctx; adapter; fetch; trpc; constructor(options = {}) { this.$services = /* @__PURE__ */ new Map(); this.router = new import_BlazeRouter.BlazeRouter(options); this.doc = this.router.doc.bind(this.router); this.doc31 = this.router.doc31.bind(this.router); this.ctx = new import_context.BlazeContext({ body: null, params: null, headers: null, honoCtx: null, meta: null, query: null }); this.adapter = import_instance.BlazeConfig.modules[import_config.ExternalModule.NodeAdapter]; this.fetch = this.router.fetch.bind(this.router); this.use = this.router.use.bind(this.router); this.trpc = import_trpc.useTrpc.bind(this); if (!options.path) return; this.load({ path: options.path, autoStart: options.autoStart, middlewares: options.middlewares }); } /** * Start all the loaded services * @example * ```ts * app.start() * ``` * passing `autoStart: true` on app creation (`new Blaze({ autoStart: true })`) will also start all the services */ start() { this.$services.forEach((service) => service.onStarted()); } addServices(service) { const services = (0, import_common.toArray)(service); services.forEach((serv) => { if (this.$services.has(serv.serviceName)) return; this.$services.set(serv.serviceName, serv); }); } /** * `load` all the services from the given path * @example * ```ts * app.load({ * path: path.resolve(__dirname, 'services'), * autoStart: true * }) * ``` * `autoStart` options will start all the services when all the services are loaded */ async load(options) { const { autoStart, path: sourcePath, middlewares } = options; if (!import_node_fs.default.existsSync(sourcePath)) { throw import_logger.Logger.throw("Service path doesn't exist"); } const serviceFiles = import_node_fs.default.readdirSync(sourcePath); const services = await Promise.all( serviceFiles.map((servicePath) => { const service = import_service.BlazeService.create({ app: this.router, servicePath, ctx: this.ctx, sourcePath, middlewares: middlewares ?? [] }); return service; }) ); this.addServices(services); if (!autoStart) return; this.start(); } /** * Same as `load` but requires an array of services instead of a path. Recommended if you want to bundle those services with Bun * @example * ```ts * app.import({ * services: [userService, authService, ...], * autoStart: true * }) * ``` */ import(options) { const services = options.services.map((serv) => { const service = new import_service.BlazeService({ app: this.router, ctx: this.ctx, middlewares: options.middlewares ?? [], service: serv, servicePath: "" }); return service; }); this.addServices(services); if (!options.autoStart) return; this.start(); } /** * List of all the loaded services * @see {@link BlazeService} */ get services() { return [...this.$services.values()]; } getServeConfig(port, listener) { const config = { fetch: this.fetch, reusePort: true, port }; return [config, listener]; } serve(port, listener) { const args = this.getServeConfig(port, listener); if (!(0, import_common.isNil)(port)) { if (import_instance.BlazeConfig.runTime === import_config.PossibleRunTime.NODE && this.adapter) { this.adapter.serve(...args); } if ((0, import_common.isNil)(listener)) { return args[0]; } return args; } if (import_instance.BlazeConfig.runTime === import_config.PossibleRunTime.NODE && this.adapter) { this.adapter.serve(...args); } return this.fetch; } } // Annotate the CommonJS export names for ESM import in node: 0 && (module.exports = { Blaze });