@appwarden/middleware 3.0.3 → 3.1.0
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/README.md +1 -1
- package/cloudflare/react-router.d.ts +15 -4
- package/cloudflare/react-router.js +21 -2
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
# @appwarden/middleware
|
|
2
2
|
|
|
3
|
-

|
|
4
4
|
[](https://www.npmjs.com/package/@appwarden/middleware)
|
|
5
5
|
[](https://docs.npmjs.com/generating-provenance-statements)
|
|
6
6
|
[](https://opensource.org/licenses/MIT)
|
|
@@ -6,6 +6,11 @@ interface CloudflareContext {
|
|
|
6
6
|
env: CloudflareEnv;
|
|
7
7
|
ctx: ExecutionContext;
|
|
8
8
|
}
|
|
9
|
+
/**
|
|
10
|
+
* Symbol used to store Cloudflare context in RouterContextProvider.
|
|
11
|
+
* This is used when middleware is enabled with the v8_middleware future flag.
|
|
12
|
+
*/
|
|
13
|
+
declare const cloudflareContextSymbol: unique symbol;
|
|
9
14
|
/**
|
|
10
15
|
* Configuration for the Appwarden middleware.
|
|
11
16
|
*/
|
|
@@ -27,13 +32,15 @@ type ReactRouterConfigFn = (cloudflare: CloudflareContext) => ReactRouterAppward
|
|
|
27
32
|
/**
|
|
28
33
|
* React Router middleware function signature.
|
|
29
34
|
* This matches the unstable_middleware export type in React Router v7.
|
|
35
|
+
*
|
|
36
|
+
* Supports both old and new context APIs:
|
|
37
|
+
* - Old API: context is a plain object with `cloudflare` property
|
|
38
|
+
* - New API (v8_middleware): context is a RouterContextProvider instance
|
|
30
39
|
*/
|
|
31
40
|
interface ReactRouterMiddlewareArgs {
|
|
32
41
|
request: Request;
|
|
33
42
|
params: Record<string, string | undefined>;
|
|
34
|
-
context:
|
|
35
|
-
cloudflare: CloudflareContext;
|
|
36
|
-
};
|
|
43
|
+
context: any;
|
|
37
44
|
}
|
|
38
45
|
type ReactRouterMiddlewareFunction = (args: ReactRouterMiddlewareArgs, next: () => Promise<unknown>) => Promise<unknown>;
|
|
39
46
|
/**
|
|
@@ -42,6 +49,10 @@ type ReactRouterMiddlewareFunction = (args: ReactRouterMiddlewareArgs, next: ()
|
|
|
42
49
|
* This middleware checks if the site is locked and redirects to the lock page if so.
|
|
43
50
|
* It should be exported from your root route (root.tsx) to protect all routes.
|
|
44
51
|
*
|
|
52
|
+
* Supports both old and new React Router context APIs:
|
|
53
|
+
* - Old API: Pass context as plain object with `cloudflare` property
|
|
54
|
+
* - New API (v8_middleware): Use RouterContextProvider with cloudflareContextSymbol
|
|
55
|
+
*
|
|
45
56
|
* @example
|
|
46
57
|
* ```typescript
|
|
47
58
|
* // app/root.tsx
|
|
@@ -60,4 +71,4 @@ type ReactRouterMiddlewareFunction = (args: ReactRouterMiddlewareArgs, next: ()
|
|
|
60
71
|
*/
|
|
61
72
|
declare function createAppwardenMiddleware(configFn: ReactRouterConfigFn): ReactRouterMiddlewareFunction;
|
|
62
73
|
|
|
63
|
-
export { type CloudflareContext, type ReactRouterAppwardenConfig, type ReactRouterConfigFn, type ReactRouterMiddlewareArgs, type ReactRouterMiddlewareFunction, createAppwardenMiddleware };
|
|
74
|
+
export { type CloudflareContext, type ReactRouterAppwardenConfig, type ReactRouterConfigFn, type ReactRouterMiddlewareArgs, type ReactRouterMiddlewareFunction, cloudflareContextSymbol, createAppwardenMiddleware };
|
|
@@ -32,15 +32,33 @@ var ReactRouterCloudflareConfigSchema = z.object({
|
|
|
32
32
|
});
|
|
33
33
|
|
|
34
34
|
// src/adapters/react-router-cloudflare.ts
|
|
35
|
+
var cloudflareContextSymbol = /* @__PURE__ */ Symbol.for(
|
|
36
|
+
"@appwarden/middleware:cloudflare"
|
|
37
|
+
);
|
|
38
|
+
function getCloudflareContext(context) {
|
|
39
|
+
if (context?.cloudflare) {
|
|
40
|
+
return context.cloudflare;
|
|
41
|
+
}
|
|
42
|
+
if (context?.get && typeof context.get === "function") {
|
|
43
|
+
try {
|
|
44
|
+
const cloudflare = context.get(cloudflareContextSymbol);
|
|
45
|
+
if (cloudflare) {
|
|
46
|
+
return cloudflare;
|
|
47
|
+
}
|
|
48
|
+
} catch {
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
return null;
|
|
52
|
+
}
|
|
35
53
|
function createAppwardenMiddleware(configFn) {
|
|
36
54
|
return async (args, next) => {
|
|
37
55
|
const { request, context } = args;
|
|
38
56
|
try {
|
|
39
|
-
const cloudflare = context
|
|
57
|
+
const cloudflare = getCloudflareContext(context);
|
|
40
58
|
if (!cloudflare) {
|
|
41
59
|
console.error(
|
|
42
60
|
printMessage(
|
|
43
|
-
"Cloudflare context not found. Make sure you're running on Cloudflare Workers."
|
|
61
|
+
"Cloudflare context not found. Make sure you're running on Cloudflare Workers and have set up the context correctly."
|
|
44
62
|
)
|
|
45
63
|
);
|
|
46
64
|
return next();
|
|
@@ -83,5 +101,6 @@ function createAppwardenMiddleware(configFn) {
|
|
|
83
101
|
};
|
|
84
102
|
}
|
|
85
103
|
export {
|
|
104
|
+
cloudflareContextSymbol,
|
|
86
105
|
createAppwardenMiddleware
|
|
87
106
|
};
|