UNPKG

@busy-hour/blaze

Version:

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

225 lines (224 loc) • 5.94 kB
"use strict"; 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 context_exports = {}; __export(context_exports, { BlazeContext: () => BlazeContext }); module.exports = __toCommonJS(context_exports); var import_rest = require("../../extractor/rest/index"); var import_validator = require("../../validator/index"); var import_instance = require("../broker/instance"); class BlazeContext { $honoCtx; $meta; $query; $body; $params; $reqHeaders; /** * Set the typeof of the REST response such as `json`, `body`, `text`, or `html`. */ response; /** * Set the status code of the REST response, such as `200`, `404`, `500`, etc. */ status; $resHeaders; /** * Flag that indicates whether the context is from a REST request or not. */ isRest; broker; // Aliases for broker call; emit; event; constructor(options) { const { honoCtx, body, params, headers, query, meta } = options; this.$honoCtx = honoCtx; this.$reqHeaders = headers; this.$params = params; this.$query = query; this.$body = body; this.response = null; this.status = null; this.$meta = meta ? structuredClone(meta) : null; this.$resHeaders = null; this.isRest = !!honoCtx; this.broker = import_instance.BlazeBroker; this.call = import_instance.BlazeBroker.call.bind(import_instance.BlazeBroker); this.emit = import_instance.BlazeBroker.emit.bind(import_instance.BlazeBroker); this.event = import_instance.BlazeBroker.event.bind(import_instance.BlazeBroker); } get meta() { if (!this.$meta) this.$meta = {}; const meta = this.$meta; return { set(key, value) { meta[key] = value; return this; }, get(key) { return meta[key]; }, entries() { return Object.entries(meta); } }; } get headers() { if (!this.$resHeaders) this.$resHeaders = {}; const headers = this.$resHeaders; return { append(key, value) { const current = headers[key]; if (!headers[key]) { headers[key] = value; return; } if (Array.isArray(current)) { headers[key] = [...current, ...value]; return; } headers[key] = [current, ...value]; }, set(key, value) { headers[key] = value; return this; }, get(key) { return headers[key]; }, entries() { return Object.entries(headers); } }; } get query() { if (this.$query) return this.$query; if (!this.$honoCtx) { this.$query = {}; } else { this.$query = (0, import_rest.getReqQuery)(this.$honoCtx); } return this.$query; } get params() { if (this.$params) return this.$params; if (!this.$honoCtx) { this.$params = {}; } else { this.$params = this.$honoCtx.req.param(); } return this.$params; } get reqHeaders() { if (this.$reqHeaders) return this.$reqHeaders; if (!this.$honoCtx) { this.$reqHeaders = {}; } else { this.$reqHeaders = this.$honoCtx.req.header(); } return this.$reqHeaders; } async getBody() { if (this.$body) return this.$body; if (!this.$honoCtx) { this.$body = {}; } else { this.$body = await (0, import_rest.getReqBody)(this.$honoCtx) ?? {}; } return this.$body; } /** * @description Access the request information from the context such as `headers`, `query`, `params`, and `body`. */ get request() { return { header: this.reqHeaders, headers: this.reqHeaders, query: this.query, params: this.params, body: this.getBody.bind(this), url: this.$honoCtx?.req?.url ?? null, method: this.$honoCtx?.req?.method ?? null, path: this.$honoCtx?.req?.path ?? null }; } /** * @description Shorthand for `this.request` * @description Access the request information from the context such as `headers`, `query`, `params`, and `body`. */ get req() { return this.request; } /** * @description Access the response information from the context such as `headers`, `status`, and `response`. * @description It exists for the developer's convenience on handling the response. */ get res() { return { headers: this.headers, status: this.status, response: this.response }; } static setter(ctx) { return { meta(meta) { ctx.$meta = meta; }, header(headers) { ctx.$reqHeaders = headers; }, headers(headers) { ctx.$reqHeaders = headers; }, params(params) { ctx.$params = params; }, query(query) { ctx.$query = query; }, body(body) { ctx.$body = body; } }; } static async create(options) { const ctx = new BlazeContext(options); const setter = BlazeContext.setter(ctx); await (0, import_validator.validateAll)({ ctx, validator: options.validator, setter }); return ctx; } } // Annotate the CommonJS export names for ESM import in node: 0 && (module.exports = { BlazeContext });