bxo 0.0.5-dev.29 → 0.0.5-dev.30

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.
Files changed (2) hide show
  1. package/index.ts +39 -1
  2. package/package.json +1 -1
package/index.ts CHANGED
@@ -636,6 +636,15 @@ export default class BXO {
636
636
  // Execute route handler
637
637
  let response = await route.handler(ctx);
638
638
 
639
+ // If handler returned a Response, expose its status and headers to hooks
640
+ if (response instanceof Response) {
641
+ if (ctx.set.status === undefined) {
642
+ ctx.set.status = response.status;
643
+ }
644
+ const existingHeaders = Object.fromEntries(response.headers);
645
+ ctx.set.headers = { ...(existingHeaders || {}), ...(ctx.set.headers || {}) };
646
+ }
647
+
639
648
  // Run global onResponse hook
640
649
  if (this.hooks.onResponse) {
641
650
  response = await this.hooks.onResponse(ctx, response, this) || response;
@@ -683,7 +692,36 @@ export default class BXO {
683
692
 
684
693
  // Convert response to Response object
685
694
  if (response instanceof Response) {
686
- return response;
695
+ // Merge existing response headers with any set via hooks
696
+ let responseHeaders: Record<string, string> = {
697
+ ...Object.fromEntries(response.headers),
698
+ ...(ctx.set.headers || {})
699
+ };
700
+
701
+ // Handle cookies if any are set
702
+ if (ctx.set.cookies && ctx.set.cookies.length > 0) {
703
+ const cookieHeaders = ctx.set.cookies.map(cookie => {
704
+ let cookieString = `${encodeURIComponent(cookie.name)}=${encodeURIComponent(cookie.value)}`;
705
+
706
+ if (cookie.domain) cookieString += `; Domain=${cookie.domain}`;
707
+ if (cookie.path) cookieString += `; Path=${cookie.path}`;
708
+ if (cookie.expires) cookieString += `; Expires=${cookie.expires.toUTCString()}`;
709
+ if (cookie.maxAge) cookieString += `; Max-Age=${cookie.maxAge}`;
710
+ if (cookie.secure) cookieString += `; Secure`;
711
+ if (cookie.httpOnly) cookieString += `; HttpOnly`;
712
+ if (cookie.sameSite) cookieString += `; SameSite=${cookie.sameSite}`;
713
+
714
+ return cookieString;
715
+ });
716
+
717
+ // Add Set-Cookie headers
718
+ cookieHeaders.forEach((cookieHeader, index) => {
719
+ responseHeaders[index === 0 ? 'Set-Cookie' : `Set-Cookie-${index}`] = cookieHeader;
720
+ });
721
+ }
722
+
723
+ const mergedStatus = ctx.set.status || response.status || 200;
724
+ return new Response(response.body, { status: mergedStatus, headers: responseHeaders });
687
725
  }
688
726
 
689
727
  // Handle File response (like Elysia)
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "bxo",
3
3
  "module": "index.ts",
4
- "version": "0.0.5-dev.29",
4
+ "version": "0.0.5-dev.30",
5
5
  "description": "A simple and lightweight web framework for Bun",
6
6
  "type": "module",
7
7
  "devDependencies": {