bxo 0.0.5-dev.6 → 0.0.5-dev.8
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 +48 -1
- package/package.json +1 -1
package/index.ts
CHANGED
@@ -31,6 +31,7 @@ export type Context<TConfig extends RouteConfig = {}> = {
|
|
31
31
|
query: TConfig['query'] extends z.ZodSchema<any> ? InferZodType<TConfig['query']> : Record<string, string | undefined>;
|
32
32
|
body: TConfig['body'] extends z.ZodSchema<any> ? InferZodType<TConfig['body']> : unknown;
|
33
33
|
headers: TConfig['headers'] extends z.ZodSchema<any> ? InferZodType<TConfig['headers']> : Record<string, string>;
|
34
|
+
path: string;
|
34
35
|
request: Request;
|
35
36
|
set: {
|
36
37
|
status?: number;
|
@@ -400,6 +401,7 @@ export default class BXO {
|
|
400
401
|
query: route.config?.query ? this.validateData(route.config.query, query) : query,
|
401
402
|
body: route.config?.body ? this.validateData(route.config.body, body) : body,
|
402
403
|
headers: route.config?.headers ? this.validateData(route.config.headers, headers) : headers,
|
404
|
+
path: pathname,
|
403
405
|
request,
|
404
406
|
set: {}
|
405
407
|
};
|
@@ -451,6 +453,35 @@ export default class BXO {
|
|
451
453
|
return response;
|
452
454
|
}
|
453
455
|
|
456
|
+
// Handle File response (like Elysia)
|
457
|
+
if (response instanceof File || (typeof Bun !== 'undefined' && response instanceof Bun.file('').constructor)) {
|
458
|
+
const file = response as File;
|
459
|
+
const responseInit: ResponseInit = {
|
460
|
+
status: ctx.set.status || 200,
|
461
|
+
headers: {
|
462
|
+
'Content-Type': file.type || 'application/octet-stream',
|
463
|
+
'Content-Length': file.size.toString(),
|
464
|
+
...ctx.set.headers
|
465
|
+
}
|
466
|
+
};
|
467
|
+
return new Response(file, responseInit);
|
468
|
+
}
|
469
|
+
|
470
|
+
// Handle Bun.file() response
|
471
|
+
if (typeof response === 'object' && response && 'stream' in response && 'size' in response) {
|
472
|
+
const bunFile = response as any;
|
473
|
+
const responseInit: ResponseInit = {
|
474
|
+
status: ctx.set.status || 200,
|
475
|
+
headers: {
|
476
|
+
'Content-Type': bunFile.type || 'application/octet-stream',
|
477
|
+
'Content-Length': bunFile.size?.toString() || '',
|
478
|
+
...ctx.set.headers,
|
479
|
+
...(bunFile.headers || {}) // Support custom headers from file helper
|
480
|
+
}
|
481
|
+
};
|
482
|
+
return new Response(bunFile, responseInit);
|
483
|
+
}
|
484
|
+
|
454
485
|
const responseInit: ResponseInit = {
|
455
486
|
status: ctx.set.status || 200,
|
456
487
|
headers: ctx.set.headers || {}
|
@@ -781,8 +812,24 @@ const error = (error: Error | string, status: number = 500) => {
|
|
781
812
|
return new Response(JSON.stringify({ error: error instanceof Error ? error.message : error }), { status });
|
782
813
|
}
|
783
814
|
|
815
|
+
// File helper function (like Elysia)
|
816
|
+
const file = (path: string, options?: { type?: string; headers?: Record<string, string> }) => {
|
817
|
+
const bunFile = Bun.file(path);
|
818
|
+
|
819
|
+
if (options?.type) {
|
820
|
+
// Create a wrapper to override the MIME type
|
821
|
+
return {
|
822
|
+
...bunFile,
|
823
|
+
type: options.type,
|
824
|
+
headers: options.headers
|
825
|
+
};
|
826
|
+
}
|
827
|
+
|
828
|
+
return bunFile;
|
829
|
+
}
|
830
|
+
|
784
831
|
// Export Zod for convenience
|
785
|
-
export { z, error };
|
832
|
+
export { z, error, file };
|
786
833
|
|
787
834
|
// Export types for external use
|
788
835
|
export type { RouteConfig, RouteDetail, Handler, WebSocketHandler, WSRoute };
|