alien-middleware 0.7.0 → 0.7.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/package.json +1 -1
- package/readme.md +54 -28
package/package.json
CHANGED
package/readme.md
CHANGED
|
@@ -186,45 +186,71 @@ request middlewares, **except** when the middleware chain is nested inside
|
|
|
186
186
|
another chain, since the outer chain will still have a chance to return a
|
|
187
187
|
`Response`.
|
|
188
188
|
|
|
189
|
-
###
|
|
189
|
+
### Merging a Middleware Chain
|
|
190
190
|
|
|
191
|
-
|
|
191
|
+
By passing a middleware chain to `.use()`, you can merge it with the existing chain. Its middlewares will be executed _after_ any existing middlewares in this chain and _before_ any new middlewares you add later.
|
|
192
192
|
|
|
193
193
|
```typescript
|
|
194
194
|
const innerChain = chain((context: RequestContext) => {
|
|
195
|
-
|
|
196
|
-
return { define: { innerData: 'secret' } } // Only available inside innerChain
|
|
197
|
-
}).use((context: RequestContext<{ innerData: string }>) => {
|
|
198
|
-
console.log('Accessing inner data:', context.innerData)
|
|
195
|
+
return { helloFromInner: true }
|
|
199
196
|
})
|
|
200
197
|
|
|
201
|
-
const
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
}
|
|
207
|
-
return new Response('Finished')
|
|
208
|
-
}
|
|
209
|
-
|
|
210
|
-
const finalApp = chain().use(innerChain).use(outerMiddleware)
|
|
211
|
-
// Output when executing the finalApp chain:
|
|
212
|
-
// Inner chain start
|
|
213
|
-
// Accessing inner data: secret
|
|
214
|
-
// Outer middleware after inner chain
|
|
215
|
-
// innerData is correctly scoped.
|
|
198
|
+
const app = chain()
|
|
199
|
+
.use(innerChain)
|
|
200
|
+
.use(context => {
|
|
201
|
+
context.helloFromInner // Output: true
|
|
202
|
+
})
|
|
216
203
|
```
|
|
217
204
|
|
|
218
|
-
|
|
205
|
+
### Isolating a Middleware Chain
|
|
219
206
|
|
|
220
|
-
|
|
207
|
+
When adding a middleware chain to another, you may use the `isolate()` method to isolate the nested chain from the outer chain.
|
|
221
208
|
|
|
222
|
-
|
|
209
|
+
```typescript
|
|
210
|
+
const isolatedChain = chain().isolate()
|
|
211
|
+
```
|
|
212
|
+
|
|
213
|
+
This prevents the nested chain from affecting middleware in the outer chain (e.g. through _request plugins_).
|
|
223
214
|
|
|
224
215
|
```typescript
|
|
225
|
-
const
|
|
226
|
-
|
|
227
|
-
|
|
216
|
+
const innerChain = chain()
|
|
217
|
+
.use(() => ({
|
|
218
|
+
foo: true,
|
|
219
|
+
}))
|
|
220
|
+
.use(context => {
|
|
221
|
+
context.foo // Output: true
|
|
222
|
+
})
|
|
223
|
+
|
|
224
|
+
const outerChain = chain()
|
|
225
|
+
.use(innerChain.isolate())
|
|
226
|
+
.use(context => {
|
|
227
|
+
context.foo // Output: undefined
|
|
228
|
+
})
|
|
229
|
+
```
|
|
230
|
+
|
|
231
|
+
If an isolated chain does not return a `Response`, execution continues with the next middleware in the outer chain.
|
|
232
|
+
|
|
233
|
+
### Escaping a Middleware Chain
|
|
234
|
+
|
|
235
|
+
To stop processing a request (e.g. skip any remaining middlewares), use the `context.passThrough()` method. The Hattip adapter is responsible for deciding the appropriate action based on the request.
|
|
236
|
+
|
|
237
|
+
In the context of an isolated chain, `context.passThrough()` will skip remaining middlewares in the isolated chain, but the outer chain will continue execution with the next middleware.
|
|
238
|
+
|
|
239
|
+
```ts
|
|
240
|
+
const app = chain()
|
|
241
|
+
.use(context => {
|
|
242
|
+
if (!context.request.headers.has('Authorization')) {
|
|
243
|
+
// It's best practice to return immediately after
|
|
244
|
+
// calling passThrough()
|
|
245
|
+
return context.passThrough()
|
|
246
|
+
}
|
|
247
|
+
return new Response('Authorized', { status: 200 })
|
|
248
|
+
})
|
|
249
|
+
.use(context => {
|
|
250
|
+
// This will never run, since the previous middleware
|
|
251
|
+
// either returns a Response or calls passThrough()
|
|
252
|
+
throw new Error('This request middleware will never run')
|
|
253
|
+
})
|
|
228
254
|
```
|
|
229
255
|
|
|
230
256
|
### Safe Environment Variables
|
|
@@ -338,7 +364,7 @@ import { routes } from 'alien-middleware/router'
|
|
|
338
364
|
// Define a middleware that adds a user to the context
|
|
339
365
|
const addUserMiddleware = (context: RequestContext): RequestPlugin => {
|
|
340
366
|
const user = { id: 123, name: 'Alice' }
|
|
341
|
-
return {
|
|
367
|
+
return { user }
|
|
342
368
|
}
|
|
343
369
|
|
|
344
370
|
// Create a chain with the middleware
|