@kaito-http/core 4.0.0-beta.8 → 4.0.0-beta.9
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/dist/cors/cors.cjs +19 -9
- package/dist/cors/cors.d.cts +40 -25
- package/dist/cors/cors.d.ts +40 -25
- package/dist/cors/cors.js +19 -9
- package/package.json +1 -1
package/dist/cors/cors.cjs
CHANGED
|
@@ -48,15 +48,25 @@ function experimental_createOriginMatcher(origins) {
|
|
|
48
48
|
return (origin) => regex.test(origin);
|
|
49
49
|
}
|
|
50
50
|
function experimental_createCORSTransform(origins) {
|
|
51
|
-
|
|
52
|
-
return
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
51
|
+
let matcher = experimental_createOriginMatcher(origins);
|
|
52
|
+
return {
|
|
53
|
+
before: (request) => {
|
|
54
|
+
if (request.method === "OPTIONS") {
|
|
55
|
+
return new Response(null, { status: 204 });
|
|
56
|
+
}
|
|
57
|
+
},
|
|
58
|
+
transform: (request, response) => {
|
|
59
|
+
const origin = request.headers.get("Origin");
|
|
60
|
+
if (origin && matcher(origin)) {
|
|
61
|
+
response.headers.set("Access-Control-Allow-Origin", origin);
|
|
62
|
+
response.headers.set("Access-Control-Allow-Methods", "GET, POST, PUT, PATCH, DELETE, OPTIONS");
|
|
63
|
+
response.headers.set("Access-Control-Allow-Headers", "Content-Type, Authorization");
|
|
64
|
+
response.headers.set("Access-Control-Max-Age", "86400");
|
|
65
|
+
response.headers.set("Access-Control-Allow-Credentials", "true");
|
|
66
|
+
}
|
|
67
|
+
},
|
|
68
|
+
setOrigins: (newOrigins) => {
|
|
69
|
+
matcher = experimental_createOriginMatcher(newOrigins);
|
|
60
70
|
}
|
|
61
71
|
};
|
|
62
72
|
}
|
package/dist/cors/cors.d.cts
CHANGED
|
@@ -55,42 +55,57 @@
|
|
|
55
55
|
*/
|
|
56
56
|
declare function experimental_createOriginMatcher(origins: string[]): (origin: string) => boolean;
|
|
57
57
|
/**
|
|
58
|
-
* Create a
|
|
58
|
+
* Create a CORS handler with sane defaults for most apps.
|
|
59
59
|
*
|
|
60
60
|
* **⚠️ This API is experimental and may change or even be removed in the future. ⚠️**
|
|
61
61
|
*
|
|
62
|
-
* @param
|
|
63
|
-
*
|
|
62
|
+
* @param origins Array of allowed origin patterns. Each pattern must include protocol (http:// or https://).
|
|
63
|
+
* Supports both exact matches and wildcard subdomain patterns. See {@link experimental_createOriginMatcher}
|
|
64
|
+
* for detailed pattern matching rules.
|
|
65
|
+
*
|
|
66
|
+
* @returns An object containing:
|
|
67
|
+
* - `before`: A handler for OPTIONS requests that returns a 204 response
|
|
68
|
+
* - `transform`: A function that applies CORS headers to the response if origin matches
|
|
69
|
+
* - `setOrigins`: A function to dynamically update the allowed origins
|
|
70
|
+
*
|
|
64
71
|
* @example
|
|
65
72
|
* ```ts
|
|
66
|
-
* const
|
|
67
|
-
*
|
|
68
|
-
*
|
|
69
|
-
*
|
|
73
|
+
* const corsHandler = experimental_createCORSTransform([
|
|
74
|
+
* // Exact matches
|
|
75
|
+
* 'https://example.com',
|
|
76
|
+
* 'http://localhost:3000',
|
|
70
77
|
*
|
|
71
|
-
*
|
|
72
|
-
*
|
|
73
|
-
*
|
|
78
|
+
* // Wildcard subdomain matches
|
|
79
|
+
* 'https://*.myapp.com', // matches https://dashboard.myapp.com
|
|
80
|
+
* 'http://*.myapp.com', // matches http://dashboard.myapp.com
|
|
74
81
|
*
|
|
75
|
-
*
|
|
76
|
-
*
|
|
77
|
-
*
|
|
78
|
-
*
|
|
82
|
+
* // Match both subdomain and root domain
|
|
83
|
+
* 'https://*.staging.com', // matches https://app.staging.com
|
|
84
|
+
* 'https://staging.com' // matches https://staging.com
|
|
85
|
+
* ]);
|
|
79
86
|
*
|
|
80
87
|
* const router = create({
|
|
81
|
-
*
|
|
82
|
-
*
|
|
83
|
-
*
|
|
84
|
-
*
|
|
85
|
-
*
|
|
86
|
-
*
|
|
87
|
-
*
|
|
88
|
-
*
|
|
89
|
-
*
|
|
90
|
-
*
|
|
88
|
+
* // Handle preflight requests
|
|
89
|
+
* before: corsHandler.before,
|
|
90
|
+
*
|
|
91
|
+
* // Or expanded
|
|
92
|
+
* before: (request) => {
|
|
93
|
+
* const res = cors.before(request);
|
|
94
|
+
* if (res) return res;
|
|
95
|
+
* },
|
|
96
|
+
*
|
|
97
|
+
* // Apply CORS headers to all responses
|
|
98
|
+
* transform: corsHandler.transform,
|
|
91
99
|
* });
|
|
100
|
+
*
|
|
101
|
+
* // Optionally update allowed origins later
|
|
102
|
+
* corsHandler.setOrigins(['https://newdomain.com']);
|
|
92
103
|
* ```
|
|
93
104
|
*/
|
|
94
|
-
declare function experimental_createCORSTransform(origins: string[]):
|
|
105
|
+
declare function experimental_createCORSTransform(origins: string[]): {
|
|
106
|
+
before: (request: Request) => Response | undefined;
|
|
107
|
+
transform: (request: Request, response: Response) => void;
|
|
108
|
+
setOrigins: (newOrigins: string[]) => void;
|
|
109
|
+
};
|
|
95
110
|
|
|
96
111
|
export { experimental_createCORSTransform, experimental_createOriginMatcher };
|
package/dist/cors/cors.d.ts
CHANGED
|
@@ -55,42 +55,57 @@
|
|
|
55
55
|
*/
|
|
56
56
|
declare function experimental_createOriginMatcher(origins: string[]): (origin: string) => boolean;
|
|
57
57
|
/**
|
|
58
|
-
* Create a
|
|
58
|
+
* Create a CORS handler with sane defaults for most apps.
|
|
59
59
|
*
|
|
60
60
|
* **⚠️ This API is experimental and may change or even be removed in the future. ⚠️**
|
|
61
61
|
*
|
|
62
|
-
* @param
|
|
63
|
-
*
|
|
62
|
+
* @param origins Array of allowed origin patterns. Each pattern must include protocol (http:// or https://).
|
|
63
|
+
* Supports both exact matches and wildcard subdomain patterns. See {@link experimental_createOriginMatcher}
|
|
64
|
+
* for detailed pattern matching rules.
|
|
65
|
+
*
|
|
66
|
+
* @returns An object containing:
|
|
67
|
+
* - `before`: A handler for OPTIONS requests that returns a 204 response
|
|
68
|
+
* - `transform`: A function that applies CORS headers to the response if origin matches
|
|
69
|
+
* - `setOrigins`: A function to dynamically update the allowed origins
|
|
70
|
+
*
|
|
64
71
|
* @example
|
|
65
72
|
* ```ts
|
|
66
|
-
* const
|
|
67
|
-
*
|
|
68
|
-
*
|
|
69
|
-
*
|
|
73
|
+
* const corsHandler = experimental_createCORSTransform([
|
|
74
|
+
* // Exact matches
|
|
75
|
+
* 'https://example.com',
|
|
76
|
+
* 'http://localhost:3000',
|
|
70
77
|
*
|
|
71
|
-
*
|
|
72
|
-
*
|
|
73
|
-
*
|
|
78
|
+
* // Wildcard subdomain matches
|
|
79
|
+
* 'https://*.myapp.com', // matches https://dashboard.myapp.com
|
|
80
|
+
* 'http://*.myapp.com', // matches http://dashboard.myapp.com
|
|
74
81
|
*
|
|
75
|
-
*
|
|
76
|
-
*
|
|
77
|
-
*
|
|
78
|
-
*
|
|
82
|
+
* // Match both subdomain and root domain
|
|
83
|
+
* 'https://*.staging.com', // matches https://app.staging.com
|
|
84
|
+
* 'https://staging.com' // matches https://staging.com
|
|
85
|
+
* ]);
|
|
79
86
|
*
|
|
80
87
|
* const router = create({
|
|
81
|
-
*
|
|
82
|
-
*
|
|
83
|
-
*
|
|
84
|
-
*
|
|
85
|
-
*
|
|
86
|
-
*
|
|
87
|
-
*
|
|
88
|
-
*
|
|
89
|
-
*
|
|
90
|
-
*
|
|
88
|
+
* // Handle preflight requests
|
|
89
|
+
* before: corsHandler.before,
|
|
90
|
+
*
|
|
91
|
+
* // Or expanded
|
|
92
|
+
* before: (request) => {
|
|
93
|
+
* const res = cors.before(request);
|
|
94
|
+
* if (res) return res;
|
|
95
|
+
* },
|
|
96
|
+
*
|
|
97
|
+
* // Apply CORS headers to all responses
|
|
98
|
+
* transform: corsHandler.transform,
|
|
91
99
|
* });
|
|
100
|
+
*
|
|
101
|
+
* // Optionally update allowed origins later
|
|
102
|
+
* corsHandler.setOrigins(['https://newdomain.com']);
|
|
92
103
|
* ```
|
|
93
104
|
*/
|
|
94
|
-
declare function experimental_createCORSTransform(origins: string[]):
|
|
105
|
+
declare function experimental_createCORSTransform(origins: string[]): {
|
|
106
|
+
before: (request: Request) => Response | undefined;
|
|
107
|
+
transform: (request: Request, response: Response) => void;
|
|
108
|
+
setOrigins: (newOrigins: string[]) => void;
|
|
109
|
+
};
|
|
95
110
|
|
|
96
111
|
export { experimental_createCORSTransform, experimental_createOriginMatcher };
|
package/dist/cors/cors.js
CHANGED
|
@@ -23,15 +23,25 @@ function experimental_createOriginMatcher(origins) {
|
|
|
23
23
|
return (origin) => regex.test(origin);
|
|
24
24
|
}
|
|
25
25
|
function experimental_createCORSTransform(origins) {
|
|
26
|
-
|
|
27
|
-
return
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
26
|
+
let matcher = experimental_createOriginMatcher(origins);
|
|
27
|
+
return {
|
|
28
|
+
before: (request) => {
|
|
29
|
+
if (request.method === "OPTIONS") {
|
|
30
|
+
return new Response(null, { status: 204 });
|
|
31
|
+
}
|
|
32
|
+
},
|
|
33
|
+
transform: (request, response) => {
|
|
34
|
+
const origin = request.headers.get("Origin");
|
|
35
|
+
if (origin && matcher(origin)) {
|
|
36
|
+
response.headers.set("Access-Control-Allow-Origin", origin);
|
|
37
|
+
response.headers.set("Access-Control-Allow-Methods", "GET, POST, PUT, PATCH, DELETE, OPTIONS");
|
|
38
|
+
response.headers.set("Access-Control-Allow-Headers", "Content-Type, Authorization");
|
|
39
|
+
response.headers.set("Access-Control-Max-Age", "86400");
|
|
40
|
+
response.headers.set("Access-Control-Allow-Credentials", "true");
|
|
41
|
+
}
|
|
42
|
+
},
|
|
43
|
+
setOrigins: (newOrigins) => {
|
|
44
|
+
matcher = experimental_createOriginMatcher(newOrigins);
|
|
35
45
|
}
|
|
36
46
|
};
|
|
37
47
|
}
|