bxo 0.0.5-dev.33 → 0.0.5-dev.34

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 (3) hide show
  1. package/example.ts +33 -0
  2. package/index.ts +16 -8
  3. package/package.json +1 -1
package/example.ts ADDED
@@ -0,0 +1,33 @@
1
+ import BXO, { createCookie, redirect } from 'bxo';
2
+
3
+ const app = new BXO();
4
+
5
+ // 1) Explicit return redirect (302 default)
6
+ app.get('/old', ({ redirect }) => {
7
+ return redirect('/new');
8
+ });
9
+
10
+ // 2) Implicit redirect (no return needed)
11
+ app.get('/implicit', (ctx) => {
12
+ ctx.redirect('/new');
13
+ });
14
+
15
+ // 3) Custom status (303 after POST)
16
+ app.post('/submit', (ctx) => {
17
+ // ...handle form...
18
+ return ctx.redirect('/thanks', 303);
19
+ });
20
+
21
+ // 4) Redirect with cookie
22
+ app.get('/login', (ctx) => {
23
+ ctx.set.cookies = [
24
+ createCookie('sid', 'abc123', { httpOnly: true, path: '/' })
25
+ ];
26
+ ctx.redirect('/dashboard'); // no return required
27
+ });
28
+
29
+ // 5) Using top-level helper (outside ctx)
30
+ app.get('/go-external', () => redirect('https://example.com', 307));
31
+
32
+ await app.start(3000);
33
+ console.log('Server running at http://localhost:3000');
package/index.ts CHANGED
@@ -67,6 +67,7 @@ export type Context<TConfig extends RouteConfig = {}> = {
67
67
  status?: number;
68
68
  headers?: Record<string, string>;
69
69
  cookies?: Cookie[];
70
+ redirect?: { location: string; status?: number };
70
71
  };
71
72
  status: <T extends number>(
72
73
  code: TConfig['response'] extends StatusResponseSchema
@@ -606,6 +607,7 @@ export default class BXO {
606
607
  ...(ctx.set.headers || {}),
607
608
  Location: location
608
609
  };
610
+ ctx.set.redirect = { location, status };
609
611
 
610
612
  // Prepare headers for immediate Response return (merging any existing headers)
611
613
  const responseHeaders: Record<string, string> = { ...ctx.set.headers };
@@ -691,13 +693,19 @@ export default class BXO {
691
693
 
692
694
  // If the handler did not return a response, but a redirect was configured via ctx.set,
693
695
  // automatically create a redirect Response so users can call ctx.redirect(...) or set headers without returning.
694
- if ((response === undefined || response === null)
695
- && typeof ctx.set.status === 'number'
696
- && ctx.set.status >= 300
697
- && ctx.set.status < 400) {
698
- const hasLocationHeader = !!(ctx.set.headers && Object.keys(ctx.set.headers).some(k => k.toLowerCase() === 'location'));
699
- if (hasLocationHeader) {
700
- let responseHeaders = ctx.set.headers ? { ...ctx.set.headers } : {};
696
+ const hasImplicitRedirectIntent = !!ctx.set.redirect
697
+ || (typeof ctx.set.status === 'number' && ctx.set.status >= 300 && ctx.set.status < 400);
698
+ if ((response === undefined || response === null) && hasImplicitRedirectIntent) {
699
+ const locationFromHeaders = ctx.set.headers && Object.entries(ctx.set.headers).find(([k]) => k.toLowerCase() === 'location')?.[1];
700
+ const location = ctx.set.redirect?.location || locationFromHeaders;
701
+ if (location) {
702
+ // Build headers, ensuring Location is present
703
+ let responseHeaders: Record<string, string> = ctx.set.headers ? { ...ctx.set.headers } : {};
704
+ if (!Object.keys(responseHeaders).some(k => k.toLowerCase() === 'location')) {
705
+ responseHeaders['Location'] = location;
706
+ }
707
+ // Determine status precedence: redirect.status > set.status > 302
708
+ const status = ctx.set.redirect?.status ?? ctx.set.status ?? 302;
701
709
 
702
710
  // Handle cookies if any are set
703
711
  if (ctx.set.cookies && ctx.set.cookies.length > 0) {
@@ -718,7 +726,7 @@ export default class BXO {
718
726
  }
719
727
 
720
728
  return new Response(null, {
721
- status: ctx.set.status,
729
+ status,
722
730
  headers: responseHeaders
723
731
  });
724
732
  }
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.33",
4
+ "version": "0.0.5-dev.34",
5
5
  "description": "A simple and lightweight web framework for Bun",
6
6
  "type": "module",
7
7
  "devDependencies": {