bxo 0.0.5-dev.45 → 0.0.5-dev.46
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 +20 -0
- package/package.json +1 -1
- package/plugins/cors.ts +24 -27
package/example.ts
ADDED
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
import BXO from ".";
|
|
2
|
+
import { cors } from "./plugins";
|
|
3
|
+
|
|
4
|
+
const app = new BXO();
|
|
5
|
+
|
|
6
|
+
app.use(cors());
|
|
7
|
+
|
|
8
|
+
app.get('/', (ctx) => {
|
|
9
|
+
return { message: 'Hello, world!' };
|
|
10
|
+
});
|
|
11
|
+
|
|
12
|
+
app.get("/api/actions/nodula.auth.login", (ctx) => {
|
|
13
|
+
return { message: 'Hello, world!' };
|
|
14
|
+
});
|
|
15
|
+
|
|
16
|
+
app.post("/api/actions/nodula.auth.login", (ctx) => {
|
|
17
|
+
return { message: 'Hello, world!' };
|
|
18
|
+
});
|
|
19
|
+
|
|
20
|
+
app.listen(3000);
|
package/package.json
CHANGED
package/plugins/cors.ts
CHANGED
|
@@ -60,12 +60,11 @@ export function cors(options: CORSOptions = {}): Plugin {
|
|
|
60
60
|
return {
|
|
61
61
|
name: 'cors',
|
|
62
62
|
onRequest: async (ctx) => {
|
|
63
|
-
console.log('onRequest', ctx.request.method);
|
|
64
63
|
// Handle preflight OPTIONS requests
|
|
65
64
|
if (ctx.request.method === 'OPTIONS') {
|
|
66
65
|
const requestOrigin = getRequestOrigin(ctx.request);
|
|
67
66
|
const allowedOrigin = validateOrigin(requestOrigin, origin);
|
|
68
|
-
|
|
67
|
+
|
|
69
68
|
// Set CORS headers for preflight
|
|
70
69
|
ctx.set.headers = {
|
|
71
70
|
...ctx.set.headers,
|
|
@@ -74,11 +73,13 @@ export function cors(options: CORSOptions = {}): Plugin {
|
|
|
74
73
|
'Access-Control-Allow-Headers': allowedHeaders.join(', '),
|
|
75
74
|
'Access-Control-Max-Age': maxAge.toString()
|
|
76
75
|
};
|
|
77
|
-
|
|
76
|
+
|
|
78
77
|
if (credentials) {
|
|
79
78
|
ctx.set.headers['Access-Control-Allow-Credentials'] = 'true';
|
|
80
79
|
}
|
|
81
80
|
|
|
81
|
+
console.log('ctx.set.headers', ctx.set.headers);
|
|
82
|
+
|
|
82
83
|
// Return a proper Response for OPTIONS requests
|
|
83
84
|
return new Response(null, {
|
|
84
85
|
status: 204,
|
|
@@ -88,31 +89,27 @@ export function cors(options: CORSOptions = {}): Plugin {
|
|
|
88
89
|
},
|
|
89
90
|
onResponse: async (ctx, response) => {
|
|
90
91
|
// Handle CORS headers for actual requests
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
newResponse.headers.set('Access-Control-Allow-Credentials', 'true');
|
|
110
|
-
}
|
|
111
|
-
|
|
112
|
-
return newResponse;
|
|
92
|
+
const requestOrigin = getRequestOrigin(ctx.request);
|
|
93
|
+
const allowedOrigin = validateOrigin(requestOrigin, origin);
|
|
94
|
+
|
|
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());
|
|
107
|
+
|
|
108
|
+
if (credentials) {
|
|
109
|
+
newResponse.headers.set('Access-Control-Allow-Credentials', 'true');
|
|
113
110
|
}
|
|
114
|
-
|
|
115
|
-
return
|
|
111
|
+
|
|
112
|
+
return newResponse;
|
|
116
113
|
}
|
|
117
114
|
};
|
|
118
115
|
}
|