@klnap/next-proxy-chain 1.1.0 → 1.1.1
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 +32 -15
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -73,36 +73,53 @@ Path patterns:
|
|
|
73
73
|
|
|
74
74
|
Segments are compared whole. `/tenant` does not match `/tenant-other`. A pattern such as `/files/*.pdf` throws when the chain is created, because wildcards must be whole segments.
|
|
75
75
|
|
|
76
|
-
## Context
|
|
76
|
+
## Context: data shared along the chain
|
|
77
77
|
|
|
78
|
-
|
|
78
|
+
The context is a `Map` that exists for one request. A rule stores a value with `ctx.set`, and a later rule reads it with `ctx.get`:
|
|
79
79
|
|
|
80
80
|
```ts
|
|
81
81
|
import { chain, defineProxy, next } from '@klnap/next-proxy-chain'
|
|
82
|
+
import { NextResponse } from 'next/server'
|
|
82
83
|
|
|
83
|
-
type Session = { user
|
|
84
|
+
type Session = { user: string }
|
|
84
85
|
|
|
85
|
-
|
|
86
|
-
|
|
86
|
+
// Sets the value.
|
|
87
|
+
const readSession = defineProxy('read-session', (req, ctx) => {
|
|
88
|
+
const user = req.cookies.get('user')?.value
|
|
89
|
+
if (user) ctx.set('session', { user })
|
|
87
90
|
return next()
|
|
88
91
|
})
|
|
89
92
|
|
|
90
|
-
|
|
91
|
-
|
|
93
|
+
// Reads the value.
|
|
94
|
+
const requireUser = defineProxy('require-user', (req, ctx) => {
|
|
95
|
+
const session = ctx.get('session') as Session | undefined
|
|
96
|
+
if (!session) return NextResponse.redirect(new URL('/login', req.url))
|
|
92
97
|
return next()
|
|
93
98
|
})
|
|
94
99
|
|
|
95
|
-
export default chain(
|
|
96
|
-
[readSession, [requireUser, '/dashboard/**']],
|
|
97
|
-
{ createContext: (): Session => ({}) }
|
|
98
|
-
)
|
|
100
|
+
export default chain([readSession, [requireUser, '/dashboard/**']])
|
|
99
101
|
```
|
|
100
102
|
|
|
101
|
-
-
|
|
102
|
-
- `
|
|
103
|
-
- Rules run in array order.
|
|
103
|
+
- Each request has its own map. Values never leak between requests.
|
|
104
|
+
- `ctx.get` returns `unknown`, so cast it to the type you stored.
|
|
105
|
+
- Rules run in array order. Set the value before the rule that reads it.
|
|
106
|
+
- A native handler reads the same map through its third argument: `(request, event, ctx)`.
|
|
107
|
+
|
|
108
|
+
### Optional: `createContext`
|
|
109
|
+
|
|
110
|
+
`createContext` creates the context object yourself. Use it when every rule should start with the same typed object, such as a request ID:
|
|
111
|
+
|
|
112
|
+
```ts
|
|
113
|
+
type Ctx = { requestId: string }
|
|
114
|
+
|
|
115
|
+
export default chain([requestLog, authProxy], {
|
|
116
|
+
createContext: (req): Ctx => ({
|
|
117
|
+
requestId: req.headers.get('x-request-id') ?? crypto.randomUUID(),
|
|
118
|
+
}),
|
|
119
|
+
})
|
|
120
|
+
```
|
|
104
121
|
|
|
105
|
-
|
|
122
|
+
Rules declared with `defineProxy<Ctx>` need a matching `createContext`. TypeScript reports an error otherwise.
|
|
106
123
|
|
|
107
124
|
## Continuing and stopping
|
|
108
125
|
|