UNPKG

@busy-hour/blaze

Version:

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

125 lines (124 loc) • 3.57 kB
// src/loader/rest.ts import { extractRestParams } from "../extractor/rest/index.js"; import { eventHandler } from "../handler/index.js"; import { handleRest } from "../handler/rest.js"; import { BlazeContext } from "../internal/context/index.js"; import { BlazeError } from "../internal/errors/index.js"; import { BlazeValidationError } from "../internal/errors/validation.js"; import { Logger } from "../internal/logger/index.js"; import { isEmpty } from "../utils/common.js"; import { REST_METHOD } from "../utils/constant/rest/index.js"; var BlazeServiceRest = class { path; method; service; action; constructor(options) { const { router, action, service } = options; if (!action.rest) { throw Logger.throw("Rest property is required"); } const [method, path] = extractRestParams(action.rest); this.path = path; this.method = method; this.action = action; this.service = service; const { request, responses } = this.openAPIConfig; const serviceMiddlewares = options.middlewares; const afterMiddlewares = action.afterMiddlewares ?? []; const middlewares = action.middlewares ?? []; const tags = []; if (this.service.tags) { if (typeof this.service.tags === "string") { tags.push(this.service.tags); } else { tags.push(...this.service.tags); } } else if (this.service.name) { tags.push(this.service.name); } router.openapi({ method: method || REST_METHOD.ALL, handler: this.restHandler.bind(this), path, request, responses, middlewares, serviceMiddlewares, afterMiddlewares, tags }); } async restHandler(...args) { const [honoCtx, next] = args; try { const ctx = await BlazeContext.create({ honoCtx, // NULL => automatically use honoCtx value instead body: null, headers: null, params: null, query: null, validator: this.action.validator ?? null, meta: this.action.meta ?? null }); const rest = await handleRest({ ctx, honoCtx, promise: eventHandler(this.action, ctx) }); if (!this.action.afterMiddlewares || isEmpty(this.action.afterMiddlewares) || !rest.ok) { return rest.resp; } return next(); } catch (err) { if (err instanceof BlazeValidationError && this.action.onRestError) { const rest = await handleRest({ ctx: err.ctx, honoCtx, promise: this.action.onRestError(err.ctx, err.errors) }); return rest.resp; } const status = err instanceof BlazeError ? err.status : 500; return honoCtx.json(err, status); } } get openAPIConfig() { if (!this.action.openapi) return { request: {}, responses: {} }; const { openapi, validator } = this.action; const request = {}; const responses = openapi.responses ?? {}; if (validator?.body && openapi.body) { request.body = { content: { [openapi.body.type]: { schema: validator.body } }, description: openapi.body?.description, required: openapi.body?.required }; } if (validator?.params) { request.params = validator.params; } if (validator?.query) { request.query = validator.query; } if (validator?.header) { request.headers = validator.header; } return { request, responses }; } }; export { BlazeServiceRest };