@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.
Files changed (2) hide show
  1. package/README.md +32 -15
  2. 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 and typed rules
76
+ ## Context: data shared along the chain
77
77
 
78
- A handler that needs per-request state is written with `defineProxy()`. It receives a context object as its second argument:
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?: string }
84
+ type Session = { user: string }
84
85
 
85
- const readSession = defineProxy<Session>('read-session', (request, session) => {
86
- session.user = request.cookies.get('user')?.value
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
- const requireUser = defineProxy<Session>('require-user', (request, session) => {
91
- if (!session.user) return NextResponse.redirect(new URL('/login', request.url))
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
- - The context is created for each request and is not shared between requests.
102
- - `createContext` is required when a rule declares a context type. Passing a rule that expects a different type is a compile-time error.
103
- - Rules run in array order. Put the rule that sets a value before the rule that reads it.
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
- Use `defineProxy()` only when you need the context or a named rule. A plain function is enough for everything else.
122
+ Rules declared with `defineProxy<Ctx>` need a matching `createContext`. TypeScript reports an error otherwise.
106
123
 
107
124
  ## Continuing and stopping
108
125
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@klnap/next-proxy-chain",
3
- "version": "1.1.0",
3
+ "version": "1.1.1",
4
4
  "description": "Compose independent request rules for Next.js proxy.ts and middleware.ts.",
5
5
  "repository": {
6
6
  "type": "git",