cross-router-core 1.0.2 → 1.0.3
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 +12 -6
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -26,7 +26,6 @@ If you are building a new adapter, use `createRouter` directly:
|
|
|
26
26
|
import { createRouter, createBrowserHistory } from 'cross-router-core'
|
|
27
27
|
|
|
28
28
|
const router = createRouter(createBrowserHistory(), {
|
|
29
|
-
context: {},
|
|
30
29
|
routes: [...],
|
|
31
30
|
middleware: [...],
|
|
32
31
|
})
|
|
@@ -97,14 +96,17 @@ Middleware runs before every navigation, in order: global middleware first, then
|
|
|
97
96
|
Each middleware receives the accumulated context from the previous middleware and passes an extended context to the next one. This is the primary way to propagate things like authenticated user objects, feature flags, or API clients down to loaders.
|
|
98
97
|
|
|
99
98
|
```ts
|
|
100
|
-
import { defineMiddleware, redirect } from 'cross-router-core'
|
|
99
|
+
import { createContext, defineMiddleware, redirect } from 'cross-router-core'
|
|
100
|
+
|
|
101
|
+
const USER_CONTEXT = createContext<User>()
|
|
101
102
|
|
|
102
103
|
const authMiddleware = defineMiddleware(async ({ context, request }, next) => {
|
|
103
104
|
const user = await getSession()
|
|
104
105
|
if (!user)
|
|
105
106
|
throw redirect('/login')
|
|
106
107
|
// Pass the user into the context — loaders will receive it typed
|
|
107
|
-
|
|
108
|
+
context.set(USER_CONTEXT, user)
|
|
109
|
+
await next()
|
|
108
110
|
})
|
|
109
111
|
```
|
|
110
112
|
|
|
@@ -112,7 +114,6 @@ Pass global middleware to `createRouter`:
|
|
|
112
114
|
|
|
113
115
|
```ts
|
|
114
116
|
createRouter(history, {
|
|
115
|
-
context: {},
|
|
116
117
|
middleware: [authMiddleware],
|
|
117
118
|
routes: [...],
|
|
118
119
|
})
|
|
@@ -126,7 +127,7 @@ Or attach middleware to a specific route:
|
|
|
126
127
|
path: 'admin',
|
|
127
128
|
middleware: [requireAdminMiddleware],
|
|
128
129
|
loader: async ({ context }) => {
|
|
129
|
-
|
|
130
|
+
const user = context.get(USER_CONTEXT) // `user` is properly typed here
|
|
130
131
|
},
|
|
131
132
|
}
|
|
132
133
|
```
|
|
@@ -136,12 +137,17 @@ Or attach middleware to a specific route:
|
|
|
136
137
|
Helper for authoring middleware with full type inference. Without it you would need to annotate `TIn` and `TOut` manually.
|
|
137
138
|
|
|
138
139
|
```ts
|
|
140
|
+
import { createContext, defineMiddleware } from 'cross-router-core'
|
|
141
|
+
|
|
142
|
+
const ORG_CONTEXT = createContext<Organization>()
|
|
143
|
+
|
|
139
144
|
const myMiddleware = defineMiddleware<
|
|
140
145
|
{ user: User }, // TIn — what this middleware expects
|
|
141
146
|
{ user: User, org: Org }
|
|
142
147
|
>(async ({ context }, next) => {
|
|
143
148
|
const org = await fetchOrg(context.user.orgId)
|
|
144
|
-
|
|
149
|
+
context.set(ORG_CONTEXT, org)
|
|
150
|
+
await next()
|
|
145
151
|
})
|
|
146
152
|
```
|
|
147
153
|
|