@busy-hour/blaze
Advanced tools
Comparing version 1.0.0 to 1.1.0
@@ -5,2 +5,19 @@ # Changelog | ||
## [1.1.0](https://github.com/Busy-Hour-Studio/blaze/compare/v1.0.0...v1.1.0) (2024-02-22) | ||
### ⚠ BREAKING CHANGES | ||
* service,action,event,hooks creator | ||
### Features | ||
* header validations ([377eee0](https://github.com/Busy-Hour-Studio/blaze/commit/377eee07e9d195c9e14cbaba416da3298410d2a8)) | ||
* zod validations support ([4c98c73](https://github.com/Busy-Hour-Studio/blaze/commit/4c98c739b2a044e68efa35d3e5ef5d006fe51123)) | ||
* zod validator types ([27c7506](https://github.com/Busy-Hour-Studio/blaze/commit/27c75060e6073ccf29a76a0bae5a6fa4d4c1cdbc)) | ||
* zod-openapi doc generator ([91cbe0e](https://github.com/Busy-Hour-Studio/blaze/commit/91cbe0ef2b34f9fa74d6076c7496a717652ccf13)) | ||
* service,action,event,hooks creator ([5f4242b](https://github.com/Busy-Hour-Studio/blaze/commit/5f4242bf6bd541710ea93c96d4e8eaea600430f0)) | ||
## 1.0.0 (2024-02-18) | ||
@@ -7,0 +24,0 @@ |
import type { BlazeErrorOption } from '../types/error'; | ||
import type { RecordUnknown } from '../types/helper'; | ||
export declare class BlazeError extends Error { | ||
status: number; | ||
errors: Record<string, unknown> | unknown | null; | ||
errors: RecordUnknown | unknown | null; | ||
constructor(err: BlazeErrorOption); | ||
} | ||
//# sourceMappingURL=BlazeError.d.ts.map |
@@ -11,2 +11,3 @@ "use strict"; | ||
this.status = 500; | ||
this.name = 'BlazeError'; | ||
} | ||
@@ -17,7 +18,6 @@ else { | ||
this.errors = err.errors; | ||
this.name = err.name ?? 'BlazeError'; | ||
} | ||
this.name = 'BlazeError'; | ||
} | ||
} | ||
exports.BlazeError = BlazeError; | ||
//# sourceMappingURL=BlazeError.js.map |
@@ -11,2 +11,1 @@ import type { ActionCallResult as Result } from '../types/action'; | ||
} | ||
//# sourceMappingURL=BlazeBroker.d.ts.map |
@@ -41,2 +41,1 @@ "use strict"; | ||
exports.BlazeBroker = BlazeBroker; | ||
//# sourceMappingURL=BlazeBroker.js.map |
/// <reference types="node" /> | ||
import type { CreateContextOption } from '../types/context'; | ||
import type { ContextConstructorOption, CreateContextOption } from '../types/context'; | ||
import type { ContextValidation, RecordString, RecordUnknown, ValidationResult } from '../types/helper'; | ||
import qs from 'node:querystring'; | ||
import type { ZodObject, ZodRawShape } from 'zod'; | ||
import { BlazeBroker } from './BlazeBroker'; | ||
export declare class BlazeContext<Meta extends Record<string, unknown> = Record<string, unknown>, Body extends Record<string, unknown> = Record<string, unknown>, Params extends Record<string, unknown> = Record<string, unknown>, Headers extends Record<string, string> = Record<string, string>> { | ||
export declare class BlazeContext<Meta extends RecordUnknown = RecordUnknown, Body extends RecordUnknown = RecordUnknown, Params extends RecordUnknown = RecordUnknown, Headers extends RecordString = RecordString> { | ||
private $honoCtx; | ||
@@ -16,3 +18,4 @@ private $meta; | ||
private $broker; | ||
constructor(options: CreateContextOption<Body, Params, Headers>); | ||
private $validations; | ||
constructor(options: ContextConstructorOption<Body, Params, Headers>); | ||
private getMeta; | ||
@@ -28,2 +31,3 @@ private setMeta; | ||
emit: (eventName: string, ...values: unknown[]) => false | undefined; | ||
event: <T, U = T extends (infer T_1)[] ? import("../types/action").ActionCallResult<T_1> : import("../types/action").ActionCallResult<T>>(eventName: string, ...values: unknown[]) => Promise<void>; | ||
private getHeader; | ||
@@ -38,5 +42,5 @@ private setHeader; | ||
}; | ||
get body(): Body | null; | ||
get validations(): ValidationResult | null; | ||
get query(): qs.ParsedUrlQuery; | ||
get params(): {}; | ||
get params(): Body & Params; | ||
get isRest(): boolean; | ||
@@ -46,7 +50,6 @@ get request(): { | ||
query: qs.ParsedUrlQuery; | ||
params: Body | Params | null; | ||
params: Params | null; | ||
body: Body | null; | ||
}; | ||
static create<Meta extends Record<string, unknown> = Record<string, unknown>, Body extends Record<string, unknown> = Record<string, unknown>, Params extends Record<string, unknown> = Record<string, unknown>, Headers extends Record<string, string> = Record<string, string>>(options: CreateContextOption<Body, Params, Headers>): Promise<BlazeContext<Meta, Body, Params, Headers>>; | ||
static create<Meta extends RecordUnknown = RecordUnknown, Body extends RecordUnknown = RecordUnknown, Params extends RecordUnknown = RecordUnknown, Headers extends RecordString = RecordString, BodyValidation extends ZodObject<ZodRawShape> = ZodObject<ZodRawShape>, ParamsValidation extends ZodObject<ZodRawShape> = ZodObject<ZodRawShape>, HeaderValidation extends ZodObject<ZodRawShape> = ZodObject<ZodRawShape>, Validator extends Partial<ContextValidation<BodyValidation, ParamsValidation, HeaderValidation>> = Partial<ContextValidation<BodyValidation, ParamsValidation, HeaderValidation>>>(options: CreateContextOption<Body, Params, Headers, BodyValidation, ParamsValidation, HeaderValidation, Validator>): Promise<BlazeContext<Meta, Body, Params, Headers>>; | ||
} | ||
//# sourceMappingURL=BlazeContext.d.ts.map |
@@ -7,3 +7,5 @@ "use strict"; | ||
exports.BlazeContext = void 0; | ||
const BlazeError_1 = require("../errors/BlazeError"); | ||
const context_1 = require("../utils/helper/context"); | ||
const validator_1 = require("../utils/helper/validator"); | ||
const node_querystring_1 = __importDefault(require("node:querystring")); | ||
@@ -22,4 +24,5 @@ const BlazeBroker_1 = require("./BlazeBroker"); | ||
$broker; | ||
$validations; | ||
constructor(options) { | ||
const { honoCtx, body, params, headers } = options; | ||
const { honoCtx, body, params, headers, validations } = options; | ||
this.$honoCtx = honoCtx; | ||
@@ -34,2 +37,3 @@ this.$meta = {}; | ||
this.$isRest = !!options.honoCtx; | ||
this.$validations = validations; | ||
this.$broker = new BlazeBroker_1.BlazeBroker(); | ||
@@ -39,2 +43,3 @@ this.call = this.$broker.call.bind(this.$broker); | ||
this.emit = this.$broker.emit.bind(this.$broker); | ||
this.event = this.$broker.event.bind(this.$broker); | ||
} | ||
@@ -70,2 +75,3 @@ getMeta(key) { | ||
emit = this.broker?.emit; | ||
event = this.broker?.event; | ||
getHeader(key) { | ||
@@ -97,4 +103,4 @@ if (key) { | ||
} | ||
get body() { | ||
return this.$body; | ||
get validations() { | ||
return this.$validations; | ||
} | ||
@@ -113,4 +119,2 @@ get query() { | ||
return this.$params; | ||
if (!this.$body && !this.$reqParams) | ||
return {}; | ||
const body = this.$body ?? {}; | ||
@@ -131,3 +135,3 @@ const param = this.$reqParams ?? {}; | ||
query: this.query, | ||
params: this.$params, | ||
params: this.$reqParams, | ||
body: this.$body, | ||
@@ -137,6 +141,11 @@ }; | ||
static async create(options) { | ||
const { honoCtx } = options; | ||
const { honoCtx, validator, throwOnValidationError } = options; | ||
let body = null; | ||
let params = null; | ||
let headers = null; | ||
const validations = { | ||
body: true, | ||
params: true, | ||
header: true, | ||
}; | ||
if (options.body) { | ||
@@ -160,2 +169,41 @@ body = options.body; | ||
} | ||
if (validator?.header) { | ||
const result = (0, validator_1.validateInput)(headers, validator.header); | ||
validations.header = result.success; | ||
if (result.success) | ||
headers = result.data; | ||
else if (!result.success && throwOnValidationError) | ||
throw new BlazeError_1.BlazeError({ | ||
errors: result.error, | ||
message: 'Invalid header', | ||
status: 400, | ||
name: 'Invalid header', | ||
}); | ||
} | ||
if (validator?.body) { | ||
const result = (0, validator_1.validateInput)(body, validator.body); | ||
validations.body = result.success; | ||
if (result.success) | ||
body = result.data; | ||
else if (!result.success && throwOnValidationError) | ||
throw new BlazeError_1.BlazeError({ | ||
errors: result.error, | ||
message: 'Invalid body', | ||
status: 400, | ||
name: 'Invalid body', | ||
}); | ||
} | ||
if (validator?.params) { | ||
const result = (0, validator_1.validateInput)(params, validator.params); | ||
validations.params = result.success; | ||
if (result.success) | ||
params = result.data; | ||
else if (!result.success && throwOnValidationError) | ||
throw new BlazeError_1.BlazeError({ | ||
errors: result.error, | ||
message: 'Invalid params', | ||
status: 400, | ||
name: 'Invalid params', | ||
}); | ||
} | ||
const ctx = new BlazeContext({ | ||
@@ -166,2 +214,3 @@ body, | ||
headers, | ||
validations, | ||
}); | ||
@@ -172,2 +221,1 @@ return ctx; | ||
exports.BlazeContext = BlazeContext; | ||
//# sourceMappingURL=BlazeContext.js.map |
import { BlazeEventEmitter } from './BlazeEventEmitter'; | ||
export declare const BlazeEvent: BlazeEventEmitter; | ||
//# sourceMappingURL=BlazeEvent.d.ts.map |
@@ -17,2 +17,1 @@ "use strict"; | ||
exports.BlazeEvent = EventStore.instance; | ||
//# sourceMappingURL=BlazeEvent.js.map |
import type { EventListener, EventName } from '../types/event'; | ||
type Option = { | ||
maxListener?: number; | ||
maxListener?: number | null; | ||
}; | ||
@@ -32,2 +32,1 @@ export declare class BlazeEventEmitter { | ||
export {}; | ||
//# sourceMappingURL=BlazeEventEmitter.d.ts.map |
@@ -65,4 +65,4 @@ "use strict"; | ||
return; | ||
this.$emitter.get(eventName)?.add(onceListener); | ||
this.$onceEmitter.get(eventName)?.set(onceListener, listener); | ||
this.$emitter.get(eventName)?.add?.(onceListener); | ||
this.$onceEmitter.get(eventName)?.set?.(onceListener, listener); | ||
} | ||
@@ -113,2 +113,1 @@ off(eventName, listener) { | ||
exports.BlazeEventEmitter = BlazeEventEmitter; | ||
//# sourceMappingURL=BlazeEventEmitter.js.map |
@@ -1,4 +0,8 @@ | ||
export type { Action, ActionHandler, Actions, Event, Events, } from './types/action'; | ||
export type { Action, ActionHandler, ActionOpenAPI, ActionValidator, Actions, OpenAPIBody, } from './types/action'; | ||
export type { Event, Events } from './types/event'; | ||
export type { AcceptedAfterHook, AcceptedBeforeHook, ActionHook, } from './types/hooks'; | ||
export type { Method, RestParam, RestRoute } from './types/rest'; | ||
export type { Service } from './types/service'; | ||
export * from './creator'; | ||
export * from './router'; | ||
export { initializeServices } from './utils/setup'; | ||
//# sourceMappingURL=index.d.ts.map |
"use strict"; | ||
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) { | ||
if (k2 === undefined) k2 = k; | ||
var desc = Object.getOwnPropertyDescriptor(m, k); | ||
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) { | ||
desc = { enumerable: true, get: function() { return m[k]; } }; | ||
} | ||
Object.defineProperty(o, k2, desc); | ||
}) : (function(o, m, k, k2) { | ||
if (k2 === undefined) k2 = k; | ||
o[k2] = m[k]; | ||
})); | ||
var __exportStar = (this && this.__exportStar) || function(m, exports) { | ||
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p); | ||
}; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.initializeServices = void 0; | ||
__exportStar(require("./creator"), exports); | ||
__exportStar(require("./router"), exports); | ||
var setup_1 = require("./utils/setup"); | ||
Object.defineProperty(exports, "initializeServices", { enumerable: true, get: function () { return setup_1.initializeServices; } }); | ||
//# sourceMappingURL=index.js.map |
import type { BlazeContext } from '../event/BlazeContext'; | ||
import type { ResponseConfig } from '@asteasolutions/zod-to-openapi'; | ||
import type { ZodObject, ZodRawShape } from 'zod'; | ||
import type { Random, RecordString, RecordUnknown } from './helper'; | ||
import type { ActionHook } from './hooks'; | ||
import type { RestParam } from './rest'; | ||
export interface Event { | ||
(ctx: BlazeContext): Promise<void> | void; | ||
export interface ActionValidator<Body extends ZodObject<ZodRawShape> = ZodObject<ZodRawShape>, Params extends ZodObject<ZodRawShape> = ZodObject<ZodRawShape>, Header extends ZodObject<ZodRawShape> = ZodObject<ZodRawShape>> { | ||
body?: Body | null; | ||
params?: Params | null; | ||
header?: Header | null; | ||
} | ||
export interface Events { | ||
[key: string]: Event; | ||
export interface ActionHandler<Meta extends RecordUnknown = RecordUnknown, Body extends RecordUnknown = RecordUnknown, Params extends RecordUnknown = RecordUnknown, Header extends RecordString = RecordString> { | ||
(ctx: BlazeContext<Meta, Body, Params, Header>): Promise<unknown | void> | unknown | void; | ||
} | ||
export interface ActionHandler { | ||
(ctx: BlazeContext): Promise<unknown | void> | unknown | void; | ||
export interface OpenAPIBody { | ||
required?: boolean; | ||
description?: string; | ||
type: 'application/json' | 'multipart/form-data' | 'application/x-www-form-urlencoded'; | ||
} | ||
export interface BeforeHookHandler { | ||
(ctx: BlazeContext): Promise<void> | void; | ||
export interface ActionOpenAPI { | ||
responses?: Record<number, ResponseConfig> | null; | ||
body?: OpenAPIBody | null; | ||
} | ||
export interface AfterHookHandler { | ||
(ctx: BlazeContext, res: unknown): Promise<unknown | void> | unknown | void; | ||
export interface Action<Meta extends RecordUnknown = RecordUnknown, Header extends ZodObject<ZodRawShape> = ZodObject<ZodRawShape>, Body extends ZodObject<ZodRawShape> = ZodObject<ZodRawShape>, Params extends ZodObject<ZodRawShape> = ZodObject<ZodRawShape>, FinalHeader extends RecordString = Header['_output'] & RecordString, FinalBody extends RecordUnknown = Body['_output'] & RecordUnknown, FinalParams extends RecordUnknown = Params['_output'] & RecordUnknown> { | ||
openapi?: ActionOpenAPI | null; | ||
validator?: ActionValidator<Body, Params, Header> | null; | ||
handler: ActionHandler<Meta, FinalBody, FinalParams, FinalHeader>; | ||
rest?: RestParam | null; | ||
hooks?: ActionHook<Meta, FinalBody, FinalParams, FinalHeader> | null; | ||
throwOnValidationError?: boolean | null; | ||
} | ||
export interface ActionHook { | ||
before?: BeforeHookHandler | BeforeHookHandler[]; | ||
after?: AfterHookHandler | AfterHookHandler[]; | ||
} | ||
export interface Action { | ||
handler: ActionHandler; | ||
rest?: RestParam; | ||
hooks?: ActionHook; | ||
} | ||
export interface AfterHookHandlerOption { | ||
result: unknown; | ||
hooks: AfterHookHandler | AfterHookHandler[]; | ||
blazeCtx: BlazeContext; | ||
} | ||
export interface BeforeHookHandlerOption { | ||
hooks: BeforeHookHandler | BeforeHookHandler[]; | ||
blazeCtx: BlazeContext; | ||
} | ||
export type ActionCallResult<U> = { | ||
@@ -44,4 +40,3 @@ error: Error; | ||
export interface Actions { | ||
[key: string]: Action; | ||
[key: string]: Action<RecordUnknown, Random, Random, Random>; | ||
} | ||
//# sourceMappingURL=action.d.ts.map |
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
//# sourceMappingURL=action.js.map |
import type { Context as HonoCtx } from 'hono'; | ||
export interface CreateContextOption<Body extends Record<string, unknown> = Record<string, unknown>, Params extends Record<string, unknown> = Record<string, unknown>, Headers extends Record<string, string> = Record<string, string>> { | ||
import type { ZodObject, ZodRawShape } from 'zod'; | ||
import type { ContextValidation, RecordString, RecordUnknown, ValidationResult } from './helper'; | ||
export interface CreateContextOption<Body extends RecordUnknown = RecordUnknown, Params extends RecordUnknown = RecordUnknown, Headers extends RecordString = RecordString, BodyValidation extends ZodObject<ZodRawShape> = ZodObject<ZodRawShape>, ParamsValidation extends ZodObject<ZodRawShape> = ZodObject<ZodRawShape>, HeaderValidation extends ZodObject<ZodRawShape> = ZodObject<ZodRawShape>, Validator extends ContextValidation<BodyValidation, ParamsValidation, HeaderValidation> = ContextValidation<BodyValidation, ParamsValidation, HeaderValidation>> { | ||
honoCtx: HonoCtx | null; | ||
@@ -7,3 +9,7 @@ body: Body | null; | ||
headers: Headers | null; | ||
validator: Validator | null; | ||
throwOnValidationError: boolean | null; | ||
} | ||
//# sourceMappingURL=context.d.ts.map | ||
export interface ContextConstructorOption<Body extends RecordUnknown = RecordUnknown, Params extends RecordUnknown = RecordUnknown, Headers extends RecordString = RecordString> extends Omit<CreateContextOption<Body, Params, Headers>, 'validator' | 'throwOnValidationError'> { | ||
validations: ValidationResult | null; | ||
} |
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
//# sourceMappingURL=context.js.map |
@@ -0,6 +1,7 @@ | ||
import type { RecordString, RecordUnknown } from './helper'; | ||
export type BlazeErrorOption = string | { | ||
errors: Record<string, unknown> | unknown | null; | ||
errors: RecordUnknown | RecordString | unknown | null; | ||
status: number; | ||
message: string; | ||
name?: string | null; | ||
}; | ||
//# sourceMappingURL=error.d.ts.map |
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
//# sourceMappingURL=error.js.map |
@@ -0,3 +1,6 @@ | ||
import type { BlazeContext } from '../event/BlazeContext'; | ||
import type { ZodObject, ZodRawShape } from 'zod'; | ||
import type { ActionHandler } from './action'; | ||
export interface EventHandler { | ||
import type { Random, RecordString, RecordUnknown } from './helper'; | ||
export interface EventActionHandler { | ||
name: string; | ||
@@ -7,5 +10,15 @@ handler(...values: unknown[]): ReturnType<ActionHandler>; | ||
export interface EventListener { | ||
(...values: any[]): Promise<void | unknown> | void | unknown; | ||
(...values: Random[]): Promise<void | unknown> | void | unknown; | ||
} | ||
export type EventName = string; | ||
//# sourceMappingURL=event.d.ts.map | ||
export interface EventHandler<Meta extends RecordUnknown = RecordUnknown, Params extends RecordUnknown = RecordUnknown, Header extends RecordString = RecordString> { | ||
(ctx: BlazeContext<Meta, Params, RecordUnknown, Header>): Promise<void> | void; | ||
} | ||
export interface Event<Meta extends RecordUnknown = RecordUnknown, Params extends ZodObject<ZodRawShape> = ZodObject<ZodRawShape>, FinalParams extends RecordUnknown = Params['_output'] & RecordUnknown> { | ||
validator?: Params | null; | ||
handler: EventHandler<Meta, FinalParams>; | ||
throwOnValidationError?: boolean | null; | ||
} | ||
export interface Events { | ||
[key: string]: Event<RecordUnknown, Random>; | ||
} |
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
//# sourceMappingURL=event.js.map |
import type { BlazeContext } from '../event/BlazeContext'; | ||
import type { Hono, Context as HonoCtx } from 'hono'; | ||
import type { Action, AfterHookHandlerOption, BeforeHookHandlerOption } from './action'; | ||
import type { Blaze } from '../router'; | ||
import type { Context as HonoCtx } from 'hono'; | ||
import type { Action } from './action'; | ||
export type Method = 'ALL' | 'POST' | 'GET' | 'PUT' | 'PATCH' | 'OPTIONS' | 'DELETE' | 'USE'; | ||
export type RestRoute = `${Method} /${string}` | `/${string}`; | ||
export interface RestParamOption { | ||
method?: Method; | ||
method?: Method | null; | ||
path: string; | ||
} | ||
export type RestParam = RestParamOption | RestRoute; | ||
export interface RestHandlerOption extends Omit<Action, 'name' | 'rest'> { | ||
router: Hono; | ||
rest: RestParam; | ||
export interface RestHandlerOption { | ||
action: Omit<Action, 'name'>; | ||
router: Blaze; | ||
} | ||
export interface CreateRestHandlerOption extends Pick<Action, 'handler' | 'hooks'> { | ||
} | ||
export interface AfterHookRestHandlerOption extends AfterHookHandlerOption { | ||
honoCtx: HonoCtx; | ||
} | ||
export interface BeforeHookRestHandlerOption extends BeforeHookHandlerOption { | ||
honoCtx: HonoCtx; | ||
} | ||
export interface RestErrorHandlerOption { | ||
@@ -28,2 +21,1 @@ err: Error | unknown; | ||
} | ||
//# sourceMappingURL=rest.d.ts.map |
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
//# sourceMappingURL=rest.js.map |
@@ -1,25 +0,36 @@ | ||
import type { Hono } from 'hono'; | ||
import type { BlazeContext } from '../event/BlazeContext'; | ||
import type { Blaze } from '../router'; | ||
import type { Router } from 'hono/router'; | ||
import type { RouterRoute } from 'hono/types'; | ||
import type { ActionHandler, Actions, Events } from './action'; | ||
import type { EventHandler } from './event'; | ||
import type { Action, ActionHandler, Actions } from './action'; | ||
import type { Event, EventActionHandler, Events } from './event'; | ||
export interface Service { | ||
name: string; | ||
prefix?: string; | ||
version?: number; | ||
actions?: Actions; | ||
events?: Events; | ||
onCreated?: ActionHandler; | ||
onStarted?: ActionHandler; | ||
onStopped?(handlers: EventHandler[]): void; | ||
version?: number | null; | ||
actions?: Actions | null; | ||
events?: Events | null; | ||
onCreated?: ActionHandler | null; | ||
onStarted?: ActionHandler | null; | ||
onStopped?(handlers: EventActionHandler[]): void; | ||
router?: Router<[never, RouterRoute]>; | ||
} | ||
export interface LoadServiceOption { | ||
app: Hono; | ||
app: Blaze; | ||
path: string; | ||
} | ||
export interface AssignActionOption { | ||
service: Service; | ||
router: Hono; | ||
export interface CreateServiceOption { | ||
sourcePath: string; | ||
servicePath: string; | ||
blazeCtx: BlazeContext; | ||
app: Blaze; | ||
} | ||
//# sourceMappingURL=service.d.ts.map | ||
export interface CreateActionOption { | ||
action: Action; | ||
serviceName: string; | ||
actionAlias: string; | ||
} | ||
export interface CreateEventOption { | ||
event: Event; | ||
serviceName: string; | ||
eventAlias: string; | ||
} |
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
//# sourceMappingURL=service.js.map |
@@ -0,1 +1,4 @@ | ||
import { BlazeContext } from '../event/BlazeContext'; | ||
import type { ActionCallResult } from '../types/action'; | ||
import type { CreateContextOption } from '../types/context'; | ||
import type { Service } from '../types/service'; | ||
@@ -8,2 +11,2 @@ export declare function hasOwnProperty<Z extends NonNullable<unknown>, X extends NonNullable<unknown> = NonNullable<unknown>, Y extends PropertyKey = PropertyKey>(obj: X, property: Y): obj is X & Record<Y, Z>; | ||
export declare function toArray<T>(value: T | T[]): T[]; | ||
//# sourceMappingURL=common.d.ts.map | ||
export declare function createContext(options: CreateContextOption): Promise<ActionCallResult<BlazeContext>>; |
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.toArray = exports.resolvePromise = exports.getServiceName = exports.getRestPath = exports.removeTrailingSlash = exports.hasOwnProperty = void 0; | ||
exports.createContext = exports.toArray = exports.resolvePromise = exports.getServiceName = exports.getRestPath = exports.removeTrailingSlash = exports.hasOwnProperty = void 0; | ||
const BlazeContext_1 = require("../event/BlazeContext"); | ||
function hasOwnProperty(obj, property) { | ||
@@ -14,4 +15,3 @@ return Object.hasOwn(obj, property); | ||
const version = service.version ? `v${service.version}` : ''; | ||
const prefix = service.prefix ?? ''; | ||
return [version, prefix, service.name] | ||
return [version, service.name] | ||
.map((val) => removeTrailingSlash(val)) | ||
@@ -24,4 +24,3 @@ .filter(Boolean) | ||
const version = service.version ? `v${service.version}` : ''; | ||
const prefix = service.prefix ?? ''; | ||
return [version, prefix, service.name].filter(Boolean).join('.'); | ||
return [version, service.name].filter(Boolean).join('.'); | ||
} | ||
@@ -43,2 +42,15 @@ exports.getServiceName = getServiceName; | ||
exports.toArray = toArray; | ||
//# sourceMappingURL=common.js.map | ||
async function createContext(options) { | ||
const [blazeCtx, blazeErr] = await resolvePromise(BlazeContext_1.BlazeContext.create(options)); | ||
if (!blazeCtx || blazeErr) { | ||
return { | ||
error: blazeErr, | ||
ok: false, | ||
}; | ||
} | ||
return { | ||
result: blazeCtx, | ||
ok: true, | ||
}; | ||
} | ||
exports.createContext = createContext; |
@@ -9,2 +9,1 @@ export declare const RESERVED_KEYWORD: { | ||
}; | ||
//# sourceMappingURL=constant.d.ts.map |
@@ -12,2 +12,1 @@ "use strict"; | ||
}; | ||
//# sourceMappingURL=constant.js.map |
@@ -5,2 +5,1 @@ import type { BlazeContext } from '../../event/BlazeContext'; | ||
export declare function getStatusCode(honoCtx: BlazeContext, defaultStatusCode: number): number; | ||
//# sourceMappingURL=context.d.ts.map |
@@ -39,2 +39,1 @@ "use strict"; | ||
exports.getStatusCode = getStatusCode; | ||
//# sourceMappingURL=context.js.map |
@@ -1,5 +0,3 @@ | ||
/// <reference types="node" /> | ||
import type { CreateRestHandlerOption } from '../../types/rest'; | ||
import type { Context as HonoCtx } from 'hono'; | ||
export declare function createRestHandler(options: CreateRestHandlerOption): (honoCtx: HonoCtx) => Promise<Response>; | ||
//# sourceMappingURL=handler.d.ts.map | ||
import { BlazeContext } from '../../event/BlazeContext'; | ||
import { Action, ActionCallResult } from '../../types/action'; | ||
export declare function eventHandler(action: Action, blazeCtx: BlazeContext): Promise<ActionCallResult<unknown>>; |
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.createRestHandler = void 0; | ||
const BlazeContext_1 = require("../../event/BlazeContext"); | ||
exports.eventHandler = void 0; | ||
const common_1 = require("../common"); | ||
const context_1 = require("./context"); | ||
const hooks_1 = require("./hooks"); | ||
const rest_1 = require("./rest"); | ||
function createRestHandler(options) { | ||
const { handler, hooks } = options; | ||
return async function routeHandler(honoCtx) { | ||
const [blazeCtx, blazeErr] = await (0, common_1.resolvePromise)(BlazeContext_1.BlazeContext.create({ | ||
honoCtx, | ||
// NULL => automatically use honoCtx value instead | ||
body: null, | ||
params: null, | ||
headers: null, | ||
})); | ||
if (!blazeCtx || blazeErr) { | ||
return honoCtx.json(blazeErr, { | ||
status: 500, | ||
}); | ||
} | ||
if (hooks?.before) { | ||
const beforeHooks = Array.isArray(hooks.before) | ||
? hooks.before | ||
: [hooks.before]; | ||
const beforeHookResult = await (0, hooks_1.handleRestBeforeHook)({ | ||
hooks: beforeHooks, | ||
blazeCtx, | ||
honoCtx, | ||
}); | ||
if (!beforeHookResult.ok) { | ||
return (0, rest_1.handleRestError)({ | ||
ctx: blazeCtx, | ||
err: beforeHookResult.error, | ||
honoCtx, | ||
}); | ||
} | ||
} | ||
// eslint-disable-next-line prefer-const | ||
let [result, handlerErr] = await (0, common_1.resolvePromise)(handler(blazeCtx)); | ||
if (handlerErr) { | ||
return (0, rest_1.handleRestError)({ | ||
ctx: blazeCtx, | ||
err: handlerErr, | ||
honoCtx, | ||
}); | ||
} | ||
if (hooks?.after) { | ||
const afterHooks = Array.isArray(hooks.after) | ||
? hooks.after | ||
: [hooks.after]; | ||
const afterHookResult = await (0, hooks_1.handleRestAfterHook)({ | ||
result, | ||
hooks: afterHooks, | ||
blazeCtx, | ||
honoCtx, | ||
}); | ||
if (!afterHookResult.ok) { | ||
return (0, rest_1.handleRestError)({ | ||
ctx: blazeCtx, | ||
err: afterHookResult.error, | ||
honoCtx, | ||
}); | ||
} | ||
result = afterHookResult.result; | ||
} | ||
if (!result) { | ||
return honoCtx.body(null, 204); | ||
} | ||
const status = (0, context_1.getStatusCode)(blazeCtx, 200); | ||
return honoCtx.json(result, { | ||
status, | ||
// Reuseable action handler for Call/Emit/REST | ||
async function eventHandler(action, blazeCtx) { | ||
if (action?.hooks?.before) { | ||
const beforeHooksRes = await (0, hooks_1.beforeActionHookHandler)({ | ||
blazeCtx, | ||
hooks: action.hooks.before, | ||
}); | ||
if (!beforeHooksRes.ok) | ||
return beforeHooksRes; | ||
} | ||
// eslint-disable-next-line prefer-const | ||
let [result, err] = await (0, common_1.resolvePromise)(action.handler(blazeCtx)); | ||
if (err) { | ||
return { | ||
error: err, | ||
ok: false, | ||
}; | ||
} | ||
if (action?.hooks?.after) { | ||
const afterHooksRes = await (0, hooks_1.afterActionHookHandler)({ | ||
blazeCtx, | ||
hooks: action.hooks.after, | ||
result, | ||
}); | ||
return afterHooksRes; | ||
} | ||
return { | ||
ok: true, | ||
result, | ||
}; | ||
} | ||
exports.createRestHandler = createRestHandler; | ||
//# sourceMappingURL=handler.js.map | ||
exports.eventHandler = eventHandler; |
@@ -1,7 +0,4 @@ | ||
import type { ActionCallResult, AfterHookHandlerOption, BeforeHookHandlerOption } from '../../types/action'; | ||
import type { AfterHookRestHandlerOption, BeforeHookRestHandlerOption } from '../../types/rest'; | ||
export declare function handleBeforeActionHook(options: BeforeHookHandlerOption): Promise<ActionCallResult<unknown>>; | ||
export declare function handleAfterActionHook(options: AfterHookHandlerOption): Promise<ActionCallResult<unknown>>; | ||
export declare function handleRestBeforeHook(options: BeforeHookRestHandlerOption): Promise<ActionCallResult<unknown>>; | ||
export declare function handleRestAfterHook(options: AfterHookRestHandlerOption): Promise<ActionCallResult<unknown>>; | ||
//# sourceMappingURL=hooks.d.ts.map | ||
import type { ActionCallResult } from '../../types/action'; | ||
import type { AfterHookHandlerOption, BeforeHookHandlerOption } from '../../types/hooks'; | ||
export declare function beforeActionHookHandler(options: BeforeHookHandlerOption): Promise<ActionCallResult<unknown>>; | ||
export declare function afterActionHookHandler(options: AfterHookHandlerOption): Promise<ActionCallResult<unknown>>; |
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.handleRestAfterHook = exports.handleRestBeforeHook = exports.handleAfterActionHook = exports.handleBeforeActionHook = void 0; | ||
exports.afterActionHookHandler = exports.beforeActionHookHandler = void 0; | ||
const common_1 = require("../common"); | ||
async function handleBeforeActionHook(options) { | ||
async function beforeActionHookHandler(options) { | ||
const hooks = (0, common_1.toArray)(options.hooks); | ||
@@ -21,4 +21,4 @@ for (const hook of hooks) { | ||
} | ||
exports.handleBeforeActionHook = handleBeforeActionHook; | ||
async function handleAfterActionHook(options) { | ||
exports.beforeActionHookHandler = beforeActionHookHandler; | ||
async function afterActionHookHandler(options) { | ||
const hooks = (0, common_1.toArray)(options.hooks); | ||
@@ -42,40 +42,2 @@ // eslint-disable-next-line prefer-destructuring | ||
} | ||
exports.handleAfterActionHook = handleAfterActionHook; | ||
async function handleRestBeforeHook(options) { | ||
const hooks = (0, common_1.toArray)(options.hooks); | ||
for (const hook of hooks) { | ||
const [, hookErr] = await (0, common_1.resolvePromise)(hook(options.blazeCtx)); | ||
if (hookErr) { | ||
return { | ||
ok: false, | ||
error: hookErr, | ||
}; | ||
} | ||
} | ||
return { | ||
ok: true, | ||
result: null, | ||
}; | ||
} | ||
exports.handleRestBeforeHook = handleRestBeforeHook; | ||
async function handleRestAfterHook(options) { | ||
const hooks = (0, common_1.toArray)(options.hooks); | ||
// eslint-disable-next-line prefer-destructuring | ||
let result = options.result; | ||
for (const hook of hooks) { | ||
const [hookRes, hookErr] = await (0, common_1.resolvePromise)(hook(options.blazeCtx, result)); | ||
if (hookErr) { | ||
return { | ||
ok: false, | ||
error: hookErr, | ||
}; | ||
} | ||
result = hookRes; | ||
} | ||
return { | ||
ok: true, | ||
result, | ||
}; | ||
} | ||
exports.handleRestAfterHook = handleRestAfterHook; | ||
//# sourceMappingURL=hooks.js.map | ||
exports.afterActionHookHandler = afterActionHookHandler; |
@@ -15,2 +15,1 @@ /// <reference types="node" /> | ||
}>; | ||
//# sourceMappingURL=rest.d.ts.map |
@@ -39,2 +39,1 @@ "use strict"; | ||
exports.handleRestError = handleRestError; | ||
//# sourceMappingURL=rest.js.map |
import type { LoadServiceOption } from '../types/service'; | ||
export declare function initializeServices(options: LoadServiceOption): void; | ||
//# sourceMappingURL=setup.d.ts.map |
@@ -7,9 +7,10 @@ "use strict"; | ||
exports.initializeServices = void 0; | ||
const BlazeError_1 = require("../errors/BlazeError"); | ||
const BlazeContext_1 = require("../event/BlazeContext"); | ||
const node_fs_1 = __importDefault(require("node:fs")); | ||
const setup_1 = require("./helper/setup"); | ||
const service_1 = require("./setup/service"); | ||
function initializeServices(options) { | ||
const { app, path: servicePath } = options; | ||
if (!node_fs_1.default.existsSync(servicePath)) { | ||
throw new Error("Service path doesn't exist"); | ||
const { app, path: sourcePath } = options; | ||
if (!node_fs_1.default.existsSync(sourcePath)) { | ||
throw new BlazeError_1.BlazeError("Service path doesn't exist"); | ||
} | ||
@@ -21,8 +22,16 @@ const blazeCtx = new BlazeContext_1.BlazeContext({ | ||
honoCtx: null, | ||
validations: null, | ||
}); | ||
const services = node_fs_1.default.readdirSync(servicePath); | ||
const pendingServices = services.map((0, setup_1.initializeService)(app, servicePath, blazeCtx)); | ||
pendingServices.forEach((onStarted) => onStarted()); | ||
const serviceFiles = node_fs_1.default.readdirSync(sourcePath); | ||
const pendingServices = serviceFiles.map((servicePath) => { | ||
const service = new service_1.BlazeService({ | ||
app, | ||
servicePath, | ||
blazeCtx, | ||
sourcePath, | ||
}); | ||
return service; | ||
}); | ||
pendingServices.forEach((service) => service.onStarted()); | ||
} | ||
exports.initializeServices = initializeServices; | ||
//# sourceMappingURL=setup.js.map |
@@ -5,10 +5,11 @@ { | ||
"types": "dist/index.d.ts", | ||
"version": "1.0.0", | ||
"version": "1.1.0", | ||
"license": "MIT", | ||
"devDependencies": { | ||
"@commitlint/cli": "^17.8.1", | ||
"@commitlint/config-conventional": "^17.8.1", | ||
"@hono/node-server": "^1.7.0", | ||
"@types/node": "^20.11.16", | ||
"@typescript-eslint/eslint-plugin": "^6.20.0", | ||
"@typescript-eslint/parser": "^6.20.0", | ||
"@hono/node-server": "^1.8.1", | ||
"@types/node": "^20.11.19", | ||
"@typescript-eslint/eslint-plugin": "^6.21.0", | ||
"@typescript-eslint/parser": "^6.21.0", | ||
"eslint": "^8.56.0", | ||
@@ -23,4 +24,4 @@ "eslint-config-airbnb-base": "^15.0.0", | ||
"lint-staged": "^14.0.1", | ||
"prettier": "^3.2.4", | ||
"resolve-tspaths": "^0.8.17", | ||
"prettier": "^3.2.5", | ||
"resolve-tspaths": "^0.8.18", | ||
"rimraf": "^5.0.5", | ||
@@ -32,3 +33,5 @@ "standard-version": "^9.5.0", | ||
"dependencies": { | ||
"hono": "^3.12.10" | ||
"@asteasolutions/zod-to-openapi": "^6.3.1", | ||
"hono": "^3.12.10", | ||
"zod": "^3.22.4" | ||
}, | ||
@@ -51,3 +54,2 @@ "keywords": [ | ||
"dist", | ||
"src", | ||
"README.md", | ||
@@ -54,0 +56,0 @@ "LICENSE", |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
78557
3
83
1686
1
+ Addedzod@^3.22.4
+ Added@asteasolutions/zod-to-openapi@6.4.0(transitive)
+ Addedopenapi3-ts@4.4.0(transitive)
+ Addedyaml@2.5.1(transitive)
+ Addedzod@3.23.8(transitive)