bxo 0.0.5-dev.45 → 0.0.5-dev.47
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 +22 -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,7 +73,7 @@ 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
|
}
|
|
@@ -88,31 +87,27 @@ export function cors(options: CORSOptions = {}): Plugin {
|
|
|
88
87
|
},
|
|
89
88
|
onResponse: async (ctx, response) => {
|
|
90
89
|
// 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;
|
|
90
|
+
const requestOrigin = getRequestOrigin(ctx.request);
|
|
91
|
+
const allowedOrigin = validateOrigin(requestOrigin, origin);
|
|
92
|
+
|
|
93
|
+
// Clone the response to modify headers
|
|
94
|
+
const newResponse = new Response(response.body, {
|
|
95
|
+
status: response.status,
|
|
96
|
+
statusText: response.statusText,
|
|
97
|
+
headers: new Headers(response.headers)
|
|
98
|
+
});
|
|
99
|
+
|
|
100
|
+
// Set CORS headers
|
|
101
|
+
newResponse.headers.set('Access-Control-Allow-Origin', allowedOrigin || '*');
|
|
102
|
+
newResponse.headers.set('Access-Control-Allow-Methods', methods.join(', '));
|
|
103
|
+
newResponse.headers.set('Access-Control-Allow-Headers', allowedHeaders.join(', '));
|
|
104
|
+
newResponse.headers.set('Access-Control-Max-Age', maxAge.toString());
|
|
105
|
+
|
|
106
|
+
if (credentials) {
|
|
107
|
+
newResponse.headers.set('Access-Control-Allow-Credentials', 'true');
|
|
113
108
|
}
|
|
114
|
-
|
|
115
|
-
return
|
|
109
|
+
|
|
110
|
+
return newResponse;
|
|
116
111
|
}
|
|
117
112
|
};
|
|
118
113
|
}
|