@busy-hour/blaze
Advanced tools
Comparing version 1.2.1 to 1.2.2
@@ -5,2 +5,9 @@ # Changelog | ||
## [1.2.2](https://github.com/Busy-Hour-Studio/blaze/compare/v1.2.1...v1.2.2) (2024-02-26) | ||
### Bug Fixes | ||
* event broker call ([0a8cdc1](https://github.com/Busy-Hour-Studio/blaze/commit/0a8cdc120427b8557fca6875df760b7a825a0f63)) | ||
## [1.2.1](https://github.com/Busy-Hour-Studio/blaze/compare/v1.2.1-alpha.0...v1.2.1) (2024-02-25) | ||
@@ -7,0 +14,0 @@ |
@@ -56,3 +56,3 @@ "use strict"; | ||
const evtName = [import_constant.RESERVED_KEYWORD.PREFIX.EVENT, eventName].join("."); | ||
this.event(evtName, ...values); | ||
return this.emit(evtName, ...values); | ||
} | ||
@@ -59,0 +59,0 @@ } |
@@ -35,3 +35,2 @@ "use strict"; | ||
var import_node_querystring = __toESM(require("node:querystring"), 1); | ||
var import_BlazeError = require("../errors/BlazeError"); | ||
var import_context = require("../utils/helper/context"); | ||
@@ -72,2 +71,10 @@ var import_validator = require("../utils/helper/validator"); | ||
} | ||
get broker() { | ||
return this.$broker; | ||
} | ||
// Aliases for broker | ||
call = this.broker?.call; | ||
mcall = this.broker?.mcall; | ||
emit = this.broker?.emit; | ||
event = this.broker?.event; | ||
getMeta(key) { | ||
@@ -95,10 +102,2 @@ const value = this.$meta[key]; | ||
} | ||
get broker() { | ||
return this.$broker; | ||
} | ||
// Aliases | ||
call = this.broker?.call; | ||
mcall = this.broker?.mcall; | ||
emit = this.broker?.emit; | ||
event = this.broker?.event; | ||
getHeader(key) { | ||
@@ -202,5 +201,7 @@ if (key) { | ||
const { honoCtx, validator, throwOnValidationError } = options; | ||
let body = options.body; | ||
let params = options.params; | ||
let headers = options.headers; | ||
const data = { | ||
body: null, | ||
params: null, | ||
headers: null | ||
}; | ||
const validations = { | ||
@@ -212,54 +213,33 @@ body: true, | ||
if (validator?.header) { | ||
if (!headers && honoCtx) { | ||
headers = honoCtx.req.header(); | ||
} | ||
const result = (0, import_validator.validateInput)(headers, validator.header); | ||
validations.header = result.success; | ||
if (result.success) | ||
headers = result.data; | ||
else if (!result.success && throwOnValidationError) | ||
throw new import_BlazeError.BlazeError({ | ||
errors: result.error, | ||
message: "Invalid header", | ||
status: 400, | ||
name: "Invalid header" | ||
}); | ||
(0, import_validator.validateHeader)({ | ||
data, | ||
honoCtx, | ||
schema: validator.header, | ||
throwOnValidationError, | ||
validations | ||
}); | ||
} | ||
if (validator?.params) { | ||
(0, import_validator.validateParams)({ | ||
data, | ||
honoCtx, | ||
schema: validator.params, | ||
throwOnValidationError, | ||
validations | ||
}); | ||
} | ||
if (validator?.body) { | ||
if (!body && honoCtx) { | ||
body = await (0, import_context.getReqBody)(honoCtx); | ||
} | ||
const result = (0, import_validator.validateInput)(body, validator.body); | ||
validations.body = result.success; | ||
if (result.success) | ||
body = result.data; | ||
else if (!result.success && throwOnValidationError) | ||
throw new import_BlazeError.BlazeError({ | ||
errors: result.error, | ||
message: "Invalid body", | ||
status: 400, | ||
name: "Invalid body" | ||
}); | ||
await (0, import_validator.validateBody)({ | ||
data, | ||
honoCtx, | ||
schema: validator.body, | ||
throwOnValidationError, | ||
validations | ||
}); | ||
} | ||
if (validator?.params) { | ||
if (!params && honoCtx) { | ||
params = honoCtx.req.param(); | ||
} | ||
const result = (0, import_validator.validateInput)(params, validator.params); | ||
validations.params = result.success; | ||
if (result.success) | ||
params = result.data; | ||
else if (!result.success && throwOnValidationError) | ||
throw new import_BlazeError.BlazeError({ | ||
errors: result.error, | ||
message: "Invalid params", | ||
status: 400, | ||
name: "Invalid params" | ||
}); | ||
} | ||
const ctx = new BlazeContext({ | ||
body, | ||
params, | ||
body: data.body, | ||
params: data.params, | ||
headers: data.headers, | ||
honoCtx, | ||
headers, | ||
validations | ||
@@ -266,0 +246,0 @@ }); |
@@ -49,2 +49,3 @@ "use strict"; | ||
}); | ||
return true; | ||
} | ||
@@ -51,0 +52,0 @@ // eslint-disable-next-line no-use-before-define, @typescript-eslint/no-shadow |
@@ -21,3 +21,5 @@ "use strict"; | ||
__export(constant_exports, { | ||
RESERVED_KEYWORD: () => RESERVED_KEYWORD | ||
FORM_CONTENT_TYPE: () => FORM_CONTENT_TYPE, | ||
RESERVED_KEYWORD: () => RESERVED_KEYWORD, | ||
REST_CONTENT_TYPE: () => REST_CONTENT_TYPE | ||
}); | ||
@@ -33,5 +35,19 @@ module.exports = __toCommonJS(constant_exports); | ||
}; | ||
const REST_CONTENT_TYPE = { | ||
JSON: "application/json", | ||
TEXT: "text/plain", | ||
HTML: "text/html", | ||
BODY: "application/octet-stream", | ||
FORM: "application/x-www-form-urlencoded", | ||
MULTIPART: "multipart/form-data" | ||
}; | ||
const FORM_CONTENT_TYPE = [ | ||
REST_CONTENT_TYPE.FORM, | ||
REST_CONTENT_TYPE.MULTIPART | ||
]; | ||
// Annotate the CommonJS export names for ESM import in node: | ||
0 && (module.exports = { | ||
RESERVED_KEYWORD | ||
FORM_CONTENT_TYPE, | ||
RESERVED_KEYWORD, | ||
REST_CONTENT_TYPE | ||
}); |
@@ -21,6 +21,6 @@ "use strict"; | ||
__export(context_exports, { | ||
getReqBody: () => getReqBody, | ||
getStatusCode: () => getStatusCode | ||
getReqBody: () => getReqBody | ||
}); | ||
module.exports = __toCommonJS(context_exports); | ||
var import_constant = require("../constant"); | ||
async function getReqBody(honoCtx) { | ||
@@ -30,35 +30,26 @@ const contentType = honoCtx.req.header("Content-Type"); | ||
return null; | ||
const formType = ["application/x-www-form-urlencoded", "multipart/form-data"]; | ||
const isFormLike = formType.some((type) => contentType.startsWith(type)); | ||
const isJson = contentType.startsWith("application/json"); | ||
const isText = contentType.startsWith("text/"); | ||
const isBlob = contentType.startsWith("application/octet-stream"); | ||
if (isFormLike) { | ||
return honoCtx.req.parseBody({ | ||
all: true | ||
}); | ||
const isFormLike = import_constant.FORM_CONTENT_TYPE.some( | ||
(type) => contentType.startsWith(type) | ||
); | ||
const isJson = contentType.startsWith(import_constant.REST_CONTENT_TYPE.JSON); | ||
const isText = contentType.startsWith(import_constant.REST_CONTENT_TYPE.TEXT); | ||
const isBlob = contentType.startsWith(import_constant.REST_CONTENT_TYPE.BODY); | ||
switch (true) { | ||
case isFormLike: | ||
return honoCtx.req.parseBody({ | ||
all: true | ||
}); | ||
case isJson: | ||
return honoCtx.req.json(); | ||
case isText: | ||
return honoCtx.req.text(); | ||
case isBlob: | ||
return honoCtx.req.blob(); | ||
default: | ||
return null; | ||
} | ||
if (isJson) { | ||
return honoCtx.req.json(); | ||
} | ||
if (isText) { | ||
return honoCtx.req.text(); | ||
} | ||
if (isBlob) { | ||
return honoCtx.req.blob(); | ||
} | ||
return null; | ||
} | ||
function getStatusCode(ctx, defaultStatusCode) { | ||
const status = ctx.header.get("status"); | ||
let statusCode = Array.isArray(status) ? +status.at(-1) : +status; | ||
if (Number.isNaN(statusCode)) { | ||
statusCode = defaultStatusCode; | ||
} | ||
return statusCode; | ||
} | ||
// Annotate the CommonJS export names for ESM import in node: | ||
0 && (module.exports = { | ||
getReqBody, | ||
getStatusCode | ||
getReqBody | ||
}); |
@@ -24,2 +24,3 @@ "use strict"; | ||
getRouteHandler: () => getRouteHandler, | ||
getStatusCode: () => getStatusCode, | ||
handleRestError: () => handleRestError, | ||
@@ -30,3 +31,2 @@ handleRestResponse: () => handleRestResponse | ||
var import_BlazeError = require("../../errors/BlazeError"); | ||
var import_context = require("./context"); | ||
function extractRestPath(restRoute) { | ||
@@ -49,2 +49,10 @@ const restPath = restRoute.split(" "); | ||
} | ||
function getStatusCode(ctx, defaultStatusCode) { | ||
const status = ctx.header.get("status"); | ||
let statusCode = Array.isArray(status) ? +status.at(-1) : +status; | ||
if (Number.isNaN(statusCode)) { | ||
statusCode = defaultStatusCode; | ||
} | ||
return statusCode; | ||
} | ||
function handleRestError(options) { | ||
@@ -57,3 +65,3 @@ const { err, ctx, honoCtx } = options; | ||
} | ||
const status = (0, import_context.getStatusCode)(ctx, 500); | ||
const status = getStatusCode(ctx, 500); | ||
return honoCtx.json(err, { | ||
@@ -65,3 +73,3 @@ status | ||
const { ctx, honoCtx, result } = options; | ||
const status = (0, import_context.getStatusCode)(ctx, 200); | ||
const status = getStatusCode(ctx, 200); | ||
const headers = ctx.header.get(); | ||
@@ -89,4 +97,5 @@ const respOption = { | ||
getRouteHandler, | ||
getStatusCode, | ||
handleRestError, | ||
handleRestResponse | ||
}); |
@@ -21,5 +21,10 @@ "use strict"; | ||
__export(validator_exports, { | ||
validateInput: () => validateInput | ||
validateBody: () => validateBody, | ||
validateHeader: () => validateHeader, | ||
validateInput: () => validateInput, | ||
validateParams: () => validateParams | ||
}); | ||
module.exports = __toCommonJS(validator_exports); | ||
var import_BlazeError = require("../../errors/BlazeError"); | ||
var import_context = require("./context"); | ||
function validateInput(input, schema) { | ||
@@ -29,5 +34,59 @@ const result = schema.passthrough().safeParse(input); | ||
} | ||
function validateHeader(options) { | ||
const { data, honoCtx, schema, validations, throwOnValidationError } = options; | ||
if (!data.headers && honoCtx) { | ||
data.headers = honoCtx.req.header(); | ||
} | ||
const result = validateInput(data.headers, schema); | ||
validations.header = result.success; | ||
if (result.success) | ||
data.headers = result.data; | ||
else if (!result.success && throwOnValidationError) | ||
throw new import_BlazeError.BlazeError({ | ||
errors: result.error, | ||
message: "Invalid header", | ||
status: 400, | ||
name: "Invalid header" | ||
}); | ||
} | ||
function validateParams(options) { | ||
const { data, honoCtx, schema, validations, throwOnValidationError } = options; | ||
if (!data.params && honoCtx) { | ||
data.params = honoCtx.req.param(); | ||
} | ||
const result = validateInput(data.params, schema); | ||
validations.params = result.success; | ||
if (result.success) | ||
data.params = result.data; | ||
else if (!result.success && throwOnValidationError) | ||
throw new import_BlazeError.BlazeError({ | ||
errors: result.error, | ||
message: "Invalid params", | ||
status: 400, | ||
name: "Invalid params" | ||
}); | ||
} | ||
async function validateBody(options) { | ||
const { data, honoCtx, schema, validations, throwOnValidationError } = options; | ||
if (!data.body && honoCtx) { | ||
data.body = await (0, import_context.getReqBody)(honoCtx); | ||
} | ||
const result = validateInput(data.body, schema); | ||
validations.body = result.success; | ||
if (result.success) | ||
data.body = result.data; | ||
else if (!result.success && throwOnValidationError) | ||
throw new import_BlazeError.BlazeError({ | ||
errors: result.error, | ||
message: "Invalid body", | ||
status: 400, | ||
name: "Invalid body" | ||
}); | ||
} | ||
// Annotate the CommonJS export names for ESM import in node: | ||
0 && (module.exports = { | ||
validateInput | ||
validateBody, | ||
validateHeader, | ||
validateInput, | ||
validateParams | ||
}); |
@@ -34,3 +34,3 @@ // src/event/BlazeBroker.ts | ||
const evtName = [RESERVED_KEYWORD.PREFIX.EVENT, eventName].join("."); | ||
this.event(evtName, ...values); | ||
return this.emit(evtName, ...values); | ||
} | ||
@@ -37,0 +37,0 @@ }; |
// src/event/BlazeContext.ts | ||
import qs from "node:querystring"; | ||
import { BlazeError } from "../errors/BlazeError.js"; | ||
import { getReqBody } from "../utils/helper/context.js"; | ||
import { validateInput } from "../utils/helper/validator.js"; | ||
import { | ||
validateBody, | ||
validateHeader, | ||
validateParams | ||
} from "../utils/helper/validator.js"; | ||
import { BlazeBroker } from "./BlazeBroker.js"; | ||
@@ -39,2 +42,10 @@ var BlazeContext = class _BlazeContext { | ||
} | ||
get broker() { | ||
return this.$broker; | ||
} | ||
// Aliases for broker | ||
call = this.broker?.call; | ||
mcall = this.broker?.mcall; | ||
emit = this.broker?.emit; | ||
event = this.broker?.event; | ||
getMeta(key) { | ||
@@ -62,10 +73,2 @@ const value = this.$meta[key]; | ||
} | ||
get broker() { | ||
return this.$broker; | ||
} | ||
// Aliases | ||
call = this.broker?.call; | ||
mcall = this.broker?.mcall; | ||
emit = this.broker?.emit; | ||
event = this.broker?.event; | ||
getHeader(key) { | ||
@@ -169,5 +172,7 @@ if (key) { | ||
const { honoCtx, validator, throwOnValidationError } = options; | ||
let body = options.body; | ||
let params = options.params; | ||
let headers = options.headers; | ||
const data = { | ||
body: null, | ||
params: null, | ||
headers: null | ||
}; | ||
const validations = { | ||
@@ -179,54 +184,33 @@ body: true, | ||
if (validator?.header) { | ||
if (!headers && honoCtx) { | ||
headers = honoCtx.req.header(); | ||
} | ||
const result = validateInput(headers, validator.header); | ||
validations.header = result.success; | ||
if (result.success) | ||
headers = result.data; | ||
else if (!result.success && throwOnValidationError) | ||
throw new BlazeError({ | ||
errors: result.error, | ||
message: "Invalid header", | ||
status: 400, | ||
name: "Invalid header" | ||
}); | ||
validateHeader({ | ||
data, | ||
honoCtx, | ||
schema: validator.header, | ||
throwOnValidationError, | ||
validations | ||
}); | ||
} | ||
if (validator?.params) { | ||
validateParams({ | ||
data, | ||
honoCtx, | ||
schema: validator.params, | ||
throwOnValidationError, | ||
validations | ||
}); | ||
} | ||
if (validator?.body) { | ||
if (!body && honoCtx) { | ||
body = await getReqBody(honoCtx); | ||
} | ||
const result = validateInput(body, validator.body); | ||
validations.body = result.success; | ||
if (result.success) | ||
body = result.data; | ||
else if (!result.success && throwOnValidationError) | ||
throw new BlazeError({ | ||
errors: result.error, | ||
message: "Invalid body", | ||
status: 400, | ||
name: "Invalid body" | ||
}); | ||
await validateBody({ | ||
data, | ||
honoCtx, | ||
schema: validator.body, | ||
throwOnValidationError, | ||
validations | ||
}); | ||
} | ||
if (validator?.params) { | ||
if (!params && honoCtx) { | ||
params = honoCtx.req.param(); | ||
} | ||
const result = validateInput(params, validator.params); | ||
validations.params = result.success; | ||
if (result.success) | ||
params = result.data; | ||
else if (!result.success && throwOnValidationError) | ||
throw new BlazeError({ | ||
errors: result.error, | ||
message: "Invalid params", | ||
status: 400, | ||
name: "Invalid params" | ||
}); | ||
} | ||
const ctx = new _BlazeContext({ | ||
body, | ||
params, | ||
body: data.body, | ||
params: data.params, | ||
headers: data.headers, | ||
honoCtx, | ||
headers, | ||
validations | ||
@@ -233,0 +217,0 @@ }); |
@@ -27,2 +27,3 @@ // src/event/BlazeEventEmitter.ts | ||
}); | ||
return true; | ||
} | ||
@@ -29,0 +30,0 @@ // eslint-disable-next-line no-use-before-define, @typescript-eslint/no-shadow |
@@ -8,4 +8,4 @@ import type { ActionCallResult as Result } from '../types/action'; | ||
mcall<T, U = T extends Array<infer T> ? Result<T> : Result<T>>(...args: [eventName: EventName, ...values: unknown[]][]): Promise<U[][]>; | ||
emit(eventName: EventName, ...values: unknown[]): false | undefined; | ||
event(eventName: EventName, ...values: unknown[]): void; | ||
emit(eventName: EventName, ...values: unknown[]): boolean; | ||
event(eventName: EventName, ...values: unknown[]): boolean; | ||
} |
@@ -22,2 +22,7 @@ /// <reference types="node" /> | ||
constructor(options: ContextConstructorOption<Body, Params, Headers>); | ||
get broker(): BlazeBroker; | ||
call: <T, U = T extends (infer T_1)[] ? import("../types/action").ActionCallResult<T_1> : import("../types/action").ActionCallResult<T>>(eventName: string, ...values: unknown[]) => Promise<U[]>; | ||
mcall: <T, U = T extends (infer T_1)[] ? import("../types/action").ActionCallResult<T_1> : import("../types/action").ActionCallResult<T>>(...args: [eventName: string, ...values: unknown[]][]) => Promise<U[][]>; | ||
emit: (eventName: string, ...values: unknown[]) => boolean; | ||
event: (eventName: string, ...values: unknown[]) => boolean; | ||
private getMeta; | ||
@@ -29,7 +34,2 @@ private setMeta; | ||
}; | ||
get broker(): BlazeBroker; | ||
call: <T, U = T extends (infer T_1)[] ? import("../types/action").ActionCallResult<T_1> : import("../types/action").ActionCallResult<T>>(eventName: string, ...values: unknown[]) => Promise<U[]>; | ||
mcall: <T, U = T extends (infer T_1)[] ? import("../types/action").ActionCallResult<T_1> : import("../types/action").ActionCallResult<T>>(...args: [eventName: string, ...values: unknown[]][]) => Promise<U[][]>; | ||
emit: (eventName: string, ...values: unknown[]) => false | undefined; | ||
event: (eventName: string, ...values: unknown[]) => void; | ||
private getHeader; | ||
@@ -36,0 +36,0 @@ private setHeader; |
@@ -13,3 +13,3 @@ import type { EventListener, EventName } from '../types/event'; | ||
set maxListeners(value: number); | ||
emit(eventName: EventName, ...values: unknown[]): false | undefined; | ||
emit(eventName: EventName, ...values: unknown[]): boolean; | ||
emitAsync<T, U = T extends Array<infer T> ? T : T>(eventName: EventName, ...values: unknown[]): Promise<U[]>; | ||
@@ -16,0 +16,0 @@ on(eventName: EventName, listener: EventListener): void; |
@@ -10,3 +10,3 @@ import type { Context as HonoCtx } from 'hono'; | ||
validator: Validator | null; | ||
throwOnValidationError: boolean | null; | ||
throwOnValidationError: boolean; | ||
} | ||
@@ -13,0 +13,0 @@ export interface ContextConstructorOption<Body extends RecordUnknown = RecordUnknown, Params extends RecordUnknown = RecordUnknown, Headers extends RecordString = RecordString> extends Omit<CreateContextOption<Body, Params, Headers>, 'validator' | 'throwOnValidationError'> { |
@@ -0,1 +1,2 @@ | ||
import type { Context as HonoContext } from 'hono'; | ||
import type { ZodObject, ZodRawShape } from 'zod'; | ||
@@ -15,1 +16,13 @@ export type RecordUnknown = Record<string, unknown>; | ||
} | ||
export interface ContextData<Body extends RecordUnknown = RecordUnknown, Params extends RecordUnknown = RecordUnknown, Headers extends RecordString = RecordString> { | ||
body: Body | null; | ||
params: Params | null; | ||
headers: Headers | null; | ||
} | ||
export interface DataValidatorOption<Body extends RecordUnknown = RecordUnknown, Params extends RecordUnknown = RecordUnknown, Headers extends RecordString = RecordString> { | ||
data: ContextData<Body, Params, Headers>; | ||
schema: ZodObject<ZodRawShape>; | ||
honoCtx: HonoContext | null; | ||
throwOnValidationError: boolean; | ||
validations: ValidationResult; | ||
} |
@@ -9,1 +9,10 @@ export declare const RESERVED_KEYWORD: { | ||
}; | ||
export declare const REST_CONTENT_TYPE: { | ||
readonly JSON: "application/json"; | ||
readonly TEXT: "text/plain"; | ||
readonly HTML: "text/html"; | ||
readonly BODY: "application/octet-stream"; | ||
readonly FORM: "application/x-www-form-urlencoded"; | ||
readonly MULTIPART: "multipart/form-data"; | ||
}; | ||
export declare const FORM_CONTENT_TYPE: readonly ["application/x-www-form-urlencoded", "multipart/form-data"]; |
import type { Context as HonoCtx } from 'hono'; | ||
import type { BlazeContext } from '../../event/BlazeContext'; | ||
export declare function getReqBody(honoCtx: HonoCtx): Promise<any>; | ||
export declare function getStatusCode(ctx: BlazeContext, defaultStatusCode: number): number; |
/// <reference types="node" /> | ||
import { Hono } from 'hono'; | ||
import type { BlazeContext } from '../../event/BlazeContext'; | ||
import type { Method, RestErrorHandlerOption, RestParam, RestResponseHandlerOption, RestRoute } from '../../types/rest'; | ||
@@ -7,2 +8,3 @@ export declare function extractRestPath(restRoute: RestRoute): readonly [null, string] | readonly [Method, string]; | ||
export declare function getRouteHandler(router: Hono, method: Method | null): import("hono/types").MiddlewareHandlerInterface<import("hono").Env, import("hono/types").BlankSchema, "/">; | ||
export declare function getStatusCode(ctx: BlazeContext, defaultStatusCode: number): number; | ||
export declare function handleRestError(options: RestErrorHandlerOption): Response & import("hono").TypedResponse<{ | ||
@@ -9,0 +11,0 @@ status: number; |
import type { ZodObject, ZodRawShape } from 'zod'; | ||
import type { DataValidatorOption } from '../../types/helper'; | ||
export declare function validateInput<T extends ZodObject<ZodRawShape>>(input: unknown, schema: T): import("zod").SafeParseReturnType<import("zod").objectInputType<ZodRawShape, import("zod").ZodTypeAny, "passthrough">, import("zod").objectOutputType<ZodRawShape, import("zod").ZodTypeAny, "passthrough">>; | ||
export declare function validateHeader(options: DataValidatorOption): void; | ||
export declare function validateParams(options: DataValidatorOption): void; | ||
export declare function validateBody(options: DataValidatorOption): Promise<void>; |
@@ -10,4 +10,18 @@ // src/utils/constant.ts | ||
}; | ||
var REST_CONTENT_TYPE = { | ||
JSON: "application/json", | ||
TEXT: "text/plain", | ||
HTML: "text/html", | ||
BODY: "application/octet-stream", | ||
FORM: "application/x-www-form-urlencoded", | ||
MULTIPART: "multipart/form-data" | ||
}; | ||
var FORM_CONTENT_TYPE = [ | ||
REST_CONTENT_TYPE.FORM, | ||
REST_CONTENT_TYPE.MULTIPART | ||
]; | ||
export { | ||
RESERVED_KEYWORD | ||
FORM_CONTENT_TYPE, | ||
RESERVED_KEYWORD, | ||
REST_CONTENT_TYPE | ||
}; |
// src/utils/helper/context.ts | ||
import { FORM_CONTENT_TYPE, REST_CONTENT_TYPE } from "../constant.js"; | ||
async function getReqBody(honoCtx) { | ||
@@ -6,34 +7,25 @@ const contentType = honoCtx.req.header("Content-Type"); | ||
return null; | ||
const formType = ["application/x-www-form-urlencoded", "multipart/form-data"]; | ||
const isFormLike = formType.some((type) => contentType.startsWith(type)); | ||
const isJson = contentType.startsWith("application/json"); | ||
const isText = contentType.startsWith("text/"); | ||
const isBlob = contentType.startsWith("application/octet-stream"); | ||
if (isFormLike) { | ||
return honoCtx.req.parseBody({ | ||
all: true | ||
}); | ||
const isFormLike = FORM_CONTENT_TYPE.some( | ||
(type) => contentType.startsWith(type) | ||
); | ||
const isJson = contentType.startsWith(REST_CONTENT_TYPE.JSON); | ||
const isText = contentType.startsWith(REST_CONTENT_TYPE.TEXT); | ||
const isBlob = contentType.startsWith(REST_CONTENT_TYPE.BODY); | ||
switch (true) { | ||
case isFormLike: | ||
return honoCtx.req.parseBody({ | ||
all: true | ||
}); | ||
case isJson: | ||
return honoCtx.req.json(); | ||
case isText: | ||
return honoCtx.req.text(); | ||
case isBlob: | ||
return honoCtx.req.blob(); | ||
default: | ||
return null; | ||
} | ||
if (isJson) { | ||
return honoCtx.req.json(); | ||
} | ||
if (isText) { | ||
return honoCtx.req.text(); | ||
} | ||
if (isBlob) { | ||
return honoCtx.req.blob(); | ||
} | ||
return null; | ||
} | ||
function getStatusCode(ctx, defaultStatusCode) { | ||
const status = ctx.header.get("status"); | ||
let statusCode = Array.isArray(status) ? +status.at(-1) : +status; | ||
if (Number.isNaN(statusCode)) { | ||
statusCode = defaultStatusCode; | ||
} | ||
return statusCode; | ||
} | ||
export { | ||
getReqBody, | ||
getStatusCode | ||
getReqBody | ||
}; |
// src/utils/helper/rest.ts | ||
import { BlazeError } from "../../errors/BlazeError.js"; | ||
import { getStatusCode } from "./context.js"; | ||
function extractRestPath(restRoute) { | ||
@@ -21,2 +20,10 @@ const restPath = restRoute.split(" "); | ||
} | ||
function getStatusCode(ctx, defaultStatusCode) { | ||
const status = ctx.header.get("status"); | ||
let statusCode = Array.isArray(status) ? +status.at(-1) : +status; | ||
if (Number.isNaN(statusCode)) { | ||
statusCode = defaultStatusCode; | ||
} | ||
return statusCode; | ||
} | ||
function handleRestError(options) { | ||
@@ -58,4 +65,5 @@ const { err, ctx, honoCtx } = options; | ||
getRouteHandler, | ||
getStatusCode, | ||
handleRestError, | ||
handleRestResponse | ||
}; |
// src/utils/helper/validator.ts | ||
import { BlazeError } from "../../errors/BlazeError.js"; | ||
import { getReqBody } from "./context.js"; | ||
function validateInput(input, schema) { | ||
@@ -6,4 +8,58 @@ const result = schema.passthrough().safeParse(input); | ||
} | ||
function validateHeader(options) { | ||
const { data, honoCtx, schema, validations, throwOnValidationError } = options; | ||
if (!data.headers && honoCtx) { | ||
data.headers = honoCtx.req.header(); | ||
} | ||
const result = validateInput(data.headers, schema); | ||
validations.header = result.success; | ||
if (result.success) | ||
data.headers = result.data; | ||
else if (!result.success && throwOnValidationError) | ||
throw new BlazeError({ | ||
errors: result.error, | ||
message: "Invalid header", | ||
status: 400, | ||
name: "Invalid header" | ||
}); | ||
} | ||
function validateParams(options) { | ||
const { data, honoCtx, schema, validations, throwOnValidationError } = options; | ||
if (!data.params && honoCtx) { | ||
data.params = honoCtx.req.param(); | ||
} | ||
const result = validateInput(data.params, schema); | ||
validations.params = result.success; | ||
if (result.success) | ||
data.params = result.data; | ||
else if (!result.success && throwOnValidationError) | ||
throw new BlazeError({ | ||
errors: result.error, | ||
message: "Invalid params", | ||
status: 400, | ||
name: "Invalid params" | ||
}); | ||
} | ||
async function validateBody(options) { | ||
const { data, honoCtx, schema, validations, throwOnValidationError } = options; | ||
if (!data.body && honoCtx) { | ||
data.body = await getReqBody(honoCtx); | ||
} | ||
const result = validateInput(data.body, schema); | ||
validations.body = result.success; | ||
if (result.success) | ||
data.body = result.data; | ||
else if (!result.success && throwOnValidationError) | ||
throw new BlazeError({ | ||
errors: result.error, | ||
message: "Invalid body", | ||
status: 400, | ||
name: "Invalid body" | ||
}); | ||
} | ||
export { | ||
validateInput | ||
validateBody, | ||
validateHeader, | ||
validateInput, | ||
validateParams | ||
}; |
@@ -7,3 +7,3 @@ { | ||
"type": "module", | ||
"version": "1.2.1", | ||
"version": "1.2.2", | ||
"license": "MIT", | ||
@@ -10,0 +10,0 @@ "devDependencies": { |
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
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
151081
4053