bxo 0.0.5-dev.46 → 0.0.5-dev.48

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/example.ts CHANGED
@@ -1,12 +1,39 @@
1
- import BXO from ".";
2
- import { cors } from "./plugins";
1
+ import BXO from './index';
2
+ import { cors } from './plugins/cors';
3
3
 
4
4
  const app = new BXO();
5
5
 
6
- app.use(cors());
6
+ // Use CORS plugin
7
+ app.use(cors({
8
+ origin: ['http://localhost:3000', 'http://localhost:3001'],
9
+ credentials: true,
10
+ methods: ['GET', 'POST', 'PUT', 'DELETE', 'OPTIONS'],
11
+ allowedHeaders: ['Content-Type', 'Authorization', 'X-Custom-Header']
12
+ }));
7
13
 
8
- app.get('/', (ctx) => {
9
- return { message: 'Hello, world!' };
14
+ // Test route that returns a Response object
15
+ app.get('/test-response', () => {
16
+ return new Response(JSON.stringify({ message: 'Hello from Response object' }), {
17
+ status: 200,
18
+ headers: {
19
+ 'Content-Type': 'application/json',
20
+ 'X-Custom-Response-Header': 'test-value'
21
+ }
22
+ });
23
+ });
24
+
25
+ // Test route that returns plain data
26
+ app.get('/test-data', () => {
27
+ return { message: 'Hello from plain data' };
28
+ });
29
+
30
+ // Test route that sets custom headers via ctx.set
31
+ app.get('/test-headers', (ctx) => {
32
+ ctx.set.headers = {
33
+ 'X-Custom-Header': 'set-via-context',
34
+ 'Cache-Control': 'no-cache'
35
+ };
36
+ return { message: 'Headers set via context' };
10
37
  });
11
38
 
12
39
  app.get("/api/actions/nodula.auth.login", (ctx) => {
@@ -15,6 +42,17 @@ app.get("/api/actions/nodula.auth.login", (ctx) => {
15
42
 
16
43
  app.post("/api/actions/nodula.auth.login", (ctx) => {
17
44
  return { message: 'Hello, world!' };
18
- });
45
+ })
46
+ // Note: OPTIONS requests are handled automatically by the CORS plugin
19
47
 
20
- app.listen(3000);
48
+ // Start the server
49
+ app.start(3000, 'localhost').then(() => {
50
+ console.log('šŸš€ Server started at http://localhost:3000');
51
+ console.log('šŸ“‹ Test endpoints:');
52
+ console.log(' GET /test-response - Returns Response object');
53
+ console.log(' GET /test-data - Returns plain data');
54
+ console.log(' GET /test-headers - Sets headers via ctx.set');
55
+ console.log(' OPTIONS /* - Handled automatically by CORS plugin');
56
+ console.log('\nšŸ” Check CORS headers in browser dev tools or curl:');
57
+ console.log(' curl -H "Origin: http://localhost:3001" -H "Access-Control-Request-Method: GET" -X OPTIONS http://localhost:3000/test-response');
58
+ });
package/index.ts CHANGED
@@ -1055,6 +1055,47 @@ export default class BXO {
1055
1055
 
1056
1056
  // Convert response to Response object
1057
1057
  if (response instanceof Response) {
1058
+ // If there are headers set via ctx.set.headers, merge them with the Response headers
1059
+ if (ctx.set.headers && Object.keys(ctx.set.headers).length > 0) {
1060
+ const newHeaders = new Headers(response.headers);
1061
+
1062
+ // Add headers from ctx.set.headers
1063
+ Object.entries(ctx.set.headers).forEach(([key, value]) => {
1064
+ newHeaders.set(key, value);
1065
+ });
1066
+
1067
+ // Handle cookies if any are set
1068
+ if (ctx.set.cookies && ctx.set.cookies.length > 0) {
1069
+ const cookieHeaders = ctx.set.cookies.map(cookie => {
1070
+ let cookieString = `${encodeURIComponent(cookie.name)}=${encodeURIComponent(cookie.value)}`;
1071
+ if (cookie.domain) cookieString += `; Domain=${cookie.domain}`;
1072
+ if (cookie.path) cookieString += `; Path=${cookie.path}`;
1073
+ if (cookie.expires) cookieString += `; Expires=${cookie.expires.toUTCString()}`;
1074
+ if (cookie.maxAge) cookieString += `; Max-Age=${cookie.maxAge}`;
1075
+ if (cookie.secure) cookieString += `; Secure`;
1076
+ if (cookie.httpOnly) cookieString += `; HttpOnly`;
1077
+ if (cookie.sameSite) cookieString += `; SameSite=${cookie.sameSite}`;
1078
+ return cookieString;
1079
+ });
1080
+
1081
+ // Add Set-Cookie headers
1082
+ cookieHeaders.forEach((cookieHeader, index) => {
1083
+ if (index === 0) {
1084
+ newHeaders.set('Set-Cookie', cookieHeader);
1085
+ } else {
1086
+ newHeaders.set(`Set-Cookie-${index}`, cookieHeader);
1087
+ }
1088
+ });
1089
+ }
1090
+
1091
+ // Create new Response with merged headers
1092
+ return new Response(response.body, {
1093
+ status: ctx.set.status || response.status,
1094
+ statusText: response.statusText,
1095
+ headers: newHeaders
1096
+ });
1097
+ }
1098
+
1058
1099
  return response;
1059
1100
  }
1060
1101
 
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.46",
4
+ "version": "0.0.5-dev.48",
5
5
  "description": "A simple and lightweight web framework for Bun",
6
6
  "type": "module",
7
7
  "exports": {
package/plugins/cors.ts CHANGED
@@ -78,8 +78,6 @@ export function cors(options: CORSOptions = {}): Plugin {
78
78
  ctx.set.headers['Access-Control-Allow-Credentials'] = 'true';
79
79
  }
80
80
 
81
- console.log('ctx.set.headers', ctx.set.headers);
82
-
83
81
  // Return a proper Response for OPTIONS requests
84
82
  return new Response(null, {
85
83
  status: 204,
@@ -87,29 +85,23 @@ export function cors(options: CORSOptions = {}): Plugin {
87
85
  });
88
86
  }
89
87
  },
90
- onResponse: async (ctx, response) => {
88
+ onResponse: async (ctx) => {
91
89
  // Handle CORS headers for actual requests
92
90
  const requestOrigin = getRequestOrigin(ctx.request);
93
91
  const allowedOrigin = validateOrigin(requestOrigin, origin);
94
92
 
95
- // Clone the response to modify headers
96
- const newResponse = new Response(response.body, {
97
- status: response.status,
98
- statusText: response.statusText,
99
- headers: new Headers(response.headers)
100
- });
101
-
102
- // Set CORS headers
103
- newResponse.headers.set('Access-Control-Allow-Origin', allowedOrigin || '*');
104
- newResponse.headers.set('Access-Control-Allow-Methods', methods.join(', '));
105
- newResponse.headers.set('Access-Control-Allow-Headers', allowedHeaders.join(', '));
106
- newResponse.headers.set('Access-Control-Max-Age', maxAge.toString());
93
+ // Set CORS headers for all responses
94
+ ctx.set.headers = {
95
+ ...ctx.set.headers,
96
+ 'Access-Control-Allow-Origin': allowedOrigin || '*',
97
+ 'Access-Control-Allow-Methods': methods.join(', '),
98
+ 'Access-Control-Allow-Headers': allowedHeaders.join(', '),
99
+ 'Access-Control-Max-Age': maxAge.toString()
100
+ };
107
101
 
108
102
  if (credentials) {
109
- newResponse.headers.set('Access-Control-Allow-Credentials', 'true');
103
+ ctx.set.headers['Access-Control-Allow-Credentials'] = 'true';
110
104
  }
111
-
112
- return newResponse;
113
105
  }
114
106
  };
115
107
  }
package/test-cors.ts ADDED
File without changes