alien-middleware 0.10.0 → 0.10.2
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/dist/{index-B3m3gxNA.d.ts → index-CqtSRV4s.d.ts} +5 -3
- package/dist/index.d.ts +1 -1
- package/dist/router.d.ts +2 -2
- package/package.json +1 -1
- package/readme.md +27 -0
|
@@ -113,9 +113,11 @@ type RequestContext<TEnv extends object = any, TProperties extends object = neve
|
|
|
113
113
|
*
|
|
114
114
|
* When type `T` is `never`, a default context is returned.
|
|
115
115
|
*/
|
|
116
|
-
type MiddlewareContext<T extends MiddlewareChain
|
|
117
|
-
|
|
118
|
-
|
|
116
|
+
type MiddlewareContext<T extends MiddlewareChain | Middleware[]> = [
|
|
117
|
+
T
|
|
118
|
+
] extends [never] ? RequestContext<{}, never, unknown> : T extends MiddlewareChain ? RequestContext<Env<T>, Properties<T>, Platform<T>> : T extends Middleware[] ? MiddlewareContext<ApplyMiddlewares<T>> : never;
|
|
119
|
+
type IsolatedContext<T extends MiddlewareChain> = RequestContext<InputEnv<T>, InputProperties<T>, Platform<T>>;
|
|
120
|
+
type RequestMiddleware<T extends MiddlewareChain = MiddlewareChain> = (context: RequestContext<InputEnv<T>, InputProperties<T>, Platform<T>>) => Awaitable<Response | RequestPlugin | void>;
|
|
119
121
|
type ResponseCallback = (response: Response) => Awaitable<Response | void>;
|
|
120
122
|
interface RequestHandler<T extends MiddlewareTypes = any> extends HattipHandler<T['platform']>, MiddlewareChain<T> {
|
|
121
123
|
}
|
package/dist/index.d.ts
CHANGED
|
@@ -1,3 +1,3 @@
|
|
|
1
|
-
export { A as ApplyMiddleware, e as ApplyMiddlewares, g as EnvAccessor, h as ExtractMiddleware, i as Middleware, a as MiddlewareChain, M as MiddlewareContext, j as RequestContext, k as RequestHandler, l as RequestMiddleware, m as RequestPlugin, n as ResponseCallback, d as chain, f as filterPlatform } from './index-
|
|
1
|
+
export { A as ApplyMiddleware, e as ApplyMiddlewares, g as EnvAccessor, h as ExtractMiddleware, i as Middleware, a as MiddlewareChain, M as MiddlewareContext, j as RequestContext, k as RequestHandler, l as RequestMiddleware, m as RequestPlugin, n as ResponseCallback, d as chain, f as filterPlatform } from './index-CqtSRV4s.js';
|
|
2
2
|
import '@hattip/core';
|
|
3
3
|
import 'pathic';
|
package/dist/router.d.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
import { R as Router, M as MiddlewareContext, a as MiddlewareChain, E as EmptyMiddlewareChain } from './index-
|
|
2
|
-
export { b as RouteContext, c as RouteHandler } from './index-
|
|
1
|
+
import { R as Router, M as MiddlewareContext, a as MiddlewareChain, E as EmptyMiddlewareChain } from './index-CqtSRV4s.js';
|
|
2
|
+
export { b as RouteContext, c as RouteHandler } from './index-CqtSRV4s.js';
|
|
3
3
|
import '@hattip/core';
|
|
4
4
|
import 'pathic';
|
|
5
5
|
|
package/package.json
CHANGED
package/readme.md
CHANGED
|
@@ -154,6 +154,14 @@ Request middleware runs sequentially before a `Response` is generated.
|
|
|
154
154
|
const app = chain().use(addApiKey).use(useApiKey)
|
|
155
155
|
```
|
|
156
156
|
|
|
157
|
+
- **Setting Response Headers:** Call the `context.setHeader()` method to set a response header.
|
|
158
|
+
|
|
159
|
+
```typescript
|
|
160
|
+
const app = chain().use(context => {
|
|
161
|
+
context.setHeader('X-Powered-By', 'alien-middleware')
|
|
162
|
+
})
|
|
163
|
+
```
|
|
164
|
+
|
|
157
165
|
> [!NOTE]
|
|
158
166
|
> If you're wondering why you need to return an object to define properties
|
|
159
167
|
> (rather than simply assigning to the context object), it's because TypeScript
|
|
@@ -203,6 +211,25 @@ console.log(response.headers.get('X-Powered-By')) // Output: alien-middleware
|
|
|
203
211
|
|
|
204
212
|
Even if a middleware returns an immutable `Response` (e.g. from a `fetch()` call), your _response callback_ can still modify the headers. We make sure to clone the response before processing it with any response callbacks.
|
|
205
213
|
|
|
214
|
+
#### Non-Blocking Response Callbacks
|
|
215
|
+
|
|
216
|
+
To ensure the client receives a response as soon as possible, your response callbacks should avoid using `await` unless absolutely necessary. Prefer using `context.waitUntil()` to register independent promises that shouldn't block the response from being sent.
|
|
217
|
+
|
|
218
|
+
```typescript
|
|
219
|
+
const app = chain().use(context => {
|
|
220
|
+
context.onResponse(async response => {
|
|
221
|
+
// ❌ Bad! This blocks the response from being sent.
|
|
222
|
+
await myLoggingService.logResponse(response)
|
|
223
|
+
|
|
224
|
+
// ❌ Bad! This may be interrupted by serverless runtimes.
|
|
225
|
+
myLoggingService.logResponse(response).catch(console.error)
|
|
226
|
+
|
|
227
|
+
// ✅ Good! This doesn't block the response from being sent.
|
|
228
|
+
context.waitUntil(myLoggingService.logResponse(response))
|
|
229
|
+
})
|
|
230
|
+
})
|
|
231
|
+
```
|
|
232
|
+
|
|
206
233
|
### Merging a Middleware Chain
|
|
207
234
|
|
|
208
235
|
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.
|