@busy-hour/blaze
Version:
<h1 align='center'>🔥 Blaze</h1> <div align='center'> An event driven framework for 🔥 Hono.js </div>
202 lines (201 loc) • 4.83 kB
JavaScript
// src/internal/context/index.ts
import { getReqBody, getReqQuery } from "../../extractor/rest/index.js";
import { validateAll } from "../../validator/index.js";
import { BlazeBroker } from "../broker/instance.js";
var BlazeContext = 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 = BlazeBroker;
this.call = BlazeBroker.call.bind(BlazeBroker);
this.emit = BlazeBroker.emit.bind(BlazeBroker);
this.event = BlazeBroker.event.bind(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 = 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 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 validateAll({
ctx,
validator: options.validator,
setter
});
return ctx;
}
};
export {
BlazeContext
};