bxo 0.0.5-dev.27 → 0.0.5-dev.29
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/index.ts +21 -13
- package/package.json +1 -1
package/index.ts
CHANGED
@@ -125,6 +125,11 @@ interface LifecycleHooks {
|
|
125
125
|
onError?: (ctx: Context, error: Error, instance: BXO) => Promise<any> | any;
|
126
126
|
}
|
127
127
|
|
128
|
+
// Add global options for BXO
|
129
|
+
interface BXOOptions {
|
130
|
+
enableValidation?: boolean;
|
131
|
+
}
|
132
|
+
|
128
133
|
export default class BXO {
|
129
134
|
private _routes: Route[] = [];
|
130
135
|
private _wsRoutes: WSRoute[] = [];
|
@@ -134,8 +139,11 @@ export default class BXO {
|
|
134
139
|
private isRunning: boolean = false;
|
135
140
|
private serverPort?: number;
|
136
141
|
private serverHostname?: string;
|
142
|
+
private enableValidation: boolean = true;
|
137
143
|
|
138
|
-
constructor() {
|
144
|
+
constructor(options?: BXOOptions) {
|
145
|
+
this.enableValidation = options?.enableValidation ?? true;
|
146
|
+
}
|
139
147
|
|
140
148
|
// Lifecycle hook methods
|
141
149
|
onBeforeStart(handler: (instance: BXO) => Promise<void> | void): this {
|
@@ -332,7 +340,7 @@ export default class BXO {
|
|
332
340
|
if (routeSegment === '*' && i === routeSegments.length - 1) {
|
333
341
|
// Wildcard at end matches remaining path segments
|
334
342
|
const remainingPath = pathSegments.slice(i).join('/');
|
335
|
-
params['*'] = remainingPath;
|
343
|
+
params['*'] = decodeURIComponent(remainingPath);
|
336
344
|
break;
|
337
345
|
}
|
338
346
|
|
@@ -347,7 +355,7 @@ export default class BXO {
|
|
347
355
|
} else if (routeSegment === '*') {
|
348
356
|
// Single segment wildcard
|
349
357
|
params['*'] = decodeURIComponent(pathSegment);
|
350
|
-
} else if (routeSegment !== pathSegment) {
|
358
|
+
} else if (routeSegment !== decodeURIComponent(pathSegment)) {
|
351
359
|
isMatch = false;
|
352
360
|
break;
|
353
361
|
}
|
@@ -396,7 +404,7 @@ export default class BXO {
|
|
396
404
|
if (routeSegment === '*' && i === routeSegments.length - 1) {
|
397
405
|
// Wildcard at end matches remaining path segments
|
398
406
|
const remainingPath = pathSegments.slice(i).join('/');
|
399
|
-
params['*'] = remainingPath;
|
407
|
+
params['*'] = decodeURIComponent(remainingPath);
|
400
408
|
break;
|
401
409
|
}
|
402
410
|
|
@@ -411,7 +419,7 @@ export default class BXO {
|
|
411
419
|
} else if (routeSegment === '*') {
|
412
420
|
// Single segment wildcard
|
413
421
|
params['*'] = decodeURIComponent(pathSegment);
|
414
|
-
} else if (routeSegment !== pathSegment) {
|
422
|
+
} else if (routeSegment !== decodeURIComponent(pathSegment)) {
|
415
423
|
isMatch = false;
|
416
424
|
break;
|
417
425
|
}
|
@@ -468,7 +476,7 @@ export default class BXO {
|
|
468
476
|
|
469
477
|
// Validate response against response config (supports both simple and status-based schemas)
|
470
478
|
private validateResponse(responseConfig: ResponseConfig | undefined, data: any, status: number = 200): any {
|
471
|
-
if (!responseConfig) return data;
|
479
|
+
if (!responseConfig || !this.enableValidation) return data;
|
472
480
|
|
473
481
|
// If it's a simple schema (not status-based)
|
474
482
|
if ('parse' in responseConfig && typeof responseConfig.parse === 'function') {
|
@@ -565,11 +573,11 @@ export default class BXO {
|
|
565
573
|
let ctx: Context;
|
566
574
|
try {
|
567
575
|
// Validate each part separately to get better error messages
|
568
|
-
const validatedParams = route.config?.params ? this.validateData(route.config.params, params) : params;
|
569
|
-
const validatedQuery = route.config?.query ? this.validateData(route.config.query, query) : query;
|
570
|
-
const validatedBody = route.config?.body ? this.validateData(route.config.body, body) : body;
|
571
|
-
const validatedHeaders = route.config?.headers ? this.validateData(route.config.headers, headers) : headers;
|
572
|
-
const validatedCookies = route.config?.cookies ? this.validateData(route.config.cookies, cookies) : cookies;
|
576
|
+
const validatedParams = this.enableValidation && route.config?.params ? this.validateData(route.config.params, params) : params;
|
577
|
+
const validatedQuery = this.enableValidation && route.config?.query ? this.validateData(route.config.query, query) : query;
|
578
|
+
const validatedBody = this.enableValidation && route.config?.body ? this.validateData(route.config.body, body) : body;
|
579
|
+
const validatedHeaders = this.enableValidation && route.config?.headers ? this.validateData(route.config.headers, headers) : headers;
|
580
|
+
const validatedCookies = this.enableValidation && route.config?.cookies ? this.validateData(route.config.cookies, cookies) : cookies;
|
573
581
|
|
574
582
|
ctx = {
|
575
583
|
params: validatedParams,
|
@@ -641,7 +649,7 @@ export default class BXO {
|
|
641
649
|
}
|
642
650
|
|
643
651
|
// Validate response against schema if provided
|
644
|
-
if (route.config?.response && !(response instanceof Response)) {
|
652
|
+
if (this.enableValidation && route.config?.response && !(response instanceof Response)) {
|
645
653
|
try {
|
646
654
|
const status = ctx.set.status || 200;
|
647
655
|
response = this.validateResponse(route.config.response, response, status);
|
@@ -1035,7 +1043,7 @@ const file = (path: string, options?: { type?: string; headers?: Record<string,
|
|
1035
1043
|
export { z, error, file };
|
1036
1044
|
|
1037
1045
|
// Export types for external use
|
1038
|
-
export type { RouteConfig, RouteDetail, Handler, WebSocketHandler, WSRoute, Cookie };
|
1046
|
+
export type { RouteConfig, RouteDetail, Handler, WebSocketHandler, WSRoute, Cookie, BXOOptions };
|
1039
1047
|
|
1040
1048
|
// Helper function to create a cookie
|
1041
1049
|
export const createCookie = (
|