alien-middleware 0.1.0 → 0.2.0
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.js +1 -1
- package/package.json +1 -1
- package/readme.md +43 -42
package/dist/index.js
CHANGED
|
@@ -66,7 +66,7 @@ var MiddlewareChain = class _MiddlewareChain {
|
|
|
66
66
|
if (parentContext[kIgnoreNotFound]) {
|
|
67
67
|
return;
|
|
68
68
|
}
|
|
69
|
-
|
|
69
|
+
response = new Response("Not Found", { status: 404 });
|
|
70
70
|
}
|
|
71
71
|
for (const middleware2 of responseChain) {
|
|
72
72
|
if (cache.has(middleware2)) {
|
package/package.json
CHANGED
package/readme.md
CHANGED
|
@@ -51,7 +51,7 @@ const secondMiddleware = (ctx: RequestContext) => {
|
|
|
51
51
|
}
|
|
52
52
|
|
|
53
53
|
// Add middleware sequentially
|
|
54
|
-
const
|
|
54
|
+
const app = chain().use(firstMiddleware).use(secondMiddleware)
|
|
55
55
|
```
|
|
56
56
|
|
|
57
57
|
> [!NOTE]
|
|
@@ -59,29 +59,28 @@ const finalApp = app.use(firstMiddleware).use(secondMiddleware)
|
|
|
59
59
|
|
|
60
60
|
### Executing the Chain
|
|
61
61
|
|
|
62
|
-
To run the middleware chain,
|
|
62
|
+
To run the middleware chain, pass it to a Hattip adapter like the [Node adapter](https://www.npmjs.com/package/@hattip/adapter-node). The middleware chain is a valid Hattip handler.
|
|
63
63
|
|
|
64
64
|
```typescript
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
passThrough: () => {},
|
|
72
|
-
env: (key: string) => undefined, // Basic env function
|
|
73
|
-
}
|
|
65
|
+
import { createServer } from '@hattip/adapter-node'
|
|
66
|
+
|
|
67
|
+
const app = chain()
|
|
68
|
+
.use(mySessionMiddleware)
|
|
69
|
+
.use(myAuthMiddleware)
|
|
70
|
+
.use(myLoggerMiddleware)
|
|
74
71
|
|
|
75
|
-
//
|
|
76
|
-
const
|
|
72
|
+
// Create a server
|
|
73
|
+
const server = createServer(app)
|
|
77
74
|
|
|
78
|
-
|
|
79
|
-
|
|
75
|
+
// Start the server
|
|
76
|
+
server.listen(3000, () => {
|
|
77
|
+
console.log('Server is running on port 3000')
|
|
78
|
+
})
|
|
80
79
|
```
|
|
81
80
|
|
|
82
81
|
> [!NOTE]
|
|
83
82
|
> If no middleware in the chain returns a `Response`, a `404 Not Found` response
|
|
84
|
-
> is automatically returned
|
|
83
|
+
> is automatically returned.
|
|
85
84
|
|
|
86
85
|
### Request Middleware
|
|
87
86
|
|
|
@@ -115,18 +114,9 @@ Request middleware runs sequentially before a `Response` is generated.
|
|
|
115
114
|
return new Response(`Hello, ${ctx.user.name}!`)
|
|
116
115
|
}
|
|
117
116
|
|
|
118
|
-
const
|
|
117
|
+
const app = chain().use(addUser).use(greetUser)
|
|
119
118
|
```
|
|
120
119
|
|
|
121
|
-
> [!NOTE]
|
|
122
|
-
> If you're wondering why you need to return a `{ define: { … } }` object
|
|
123
|
-
> (rather than using simple assignment), it's because TypeScript is unable to
|
|
124
|
-
> infer the type of the context object downstream if you don't do this.
|
|
125
|
-
>
|
|
126
|
-
> Another thing to note is you don't typically define middlewares outside the
|
|
127
|
-
> `.use(…)` call expression, since that requires you to unnecessarily declare
|
|
128
|
-
> the type of the context object. It's better to define them inline.
|
|
129
|
-
|
|
130
120
|
- **Extending Environment:** Return an object with an `env` property to add environment variables accessible via `ctx.env()`.
|
|
131
121
|
|
|
132
122
|
```typescript
|
|
@@ -139,15 +129,25 @@ Request middleware runs sequentially before a `Response` is generated.
|
|
|
139
129
|
console.log('API Key:', key) // Output: API Key: secret123
|
|
140
130
|
}
|
|
141
131
|
|
|
142
|
-
const
|
|
132
|
+
const app = chain().use(addApiKey).use(useApiKey)
|
|
143
133
|
```
|
|
144
134
|
|
|
135
|
+
> [!NOTE]
|
|
136
|
+
> If you're wondering why you need to return a `{ define: { … } }` object
|
|
137
|
+
> (rather than simply assigning to the context object), it's because TypeScript
|
|
138
|
+
> is unable to infer the type of the context object downstream if you don't do
|
|
139
|
+
> it like this.
|
|
140
|
+
>
|
|
141
|
+
> Another thing to note is you don't typically define middlewares outside the
|
|
142
|
+
> `.use(…)` call expression, since that requires you to unnecessarily declare
|
|
143
|
+
> the type of the context object. It's better to define them inline.
|
|
144
|
+
|
|
145
145
|
### Response Middleware
|
|
146
146
|
|
|
147
147
|
Response middleware runs _after_ a `Response` has been generated by a request middleware or the final handler. It receives both the context and the generated `Response`.
|
|
148
148
|
|
|
149
149
|
```typescript
|
|
150
|
-
const
|
|
150
|
+
const poweredByMiddleware = (ctx: RequestContext, response: Response) => {
|
|
151
151
|
response.headers.set('X-Powered-By', 'alien-middleware')
|
|
152
152
|
}
|
|
153
153
|
|
|
@@ -155,12 +155,11 @@ const mainHandler = (ctx: RequestContext) => {
|
|
|
155
155
|
return new Response('Main content')
|
|
156
156
|
}
|
|
157
157
|
|
|
158
|
-
// `
|
|
159
|
-
const
|
|
158
|
+
// `poweredByMiddleware` runs after `mainHandler` generates a response
|
|
159
|
+
const app = chain().use(mainHandler).use(poweredByMiddleware)
|
|
160
160
|
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
console.log(responseWithHeader.headers.get('X-Powered-By')) // Output: alien-middleware
|
|
161
|
+
const response = await app({…})
|
|
162
|
+
console.log(response.headers.get('X-Powered-By')) // Output: alien-middleware
|
|
164
163
|
```
|
|
165
164
|
|
|
166
165
|
> [!NOTE]
|
|
@@ -168,6 +167,11 @@ console.log(responseWithHeader.headers.get('X-Powered-By')) // Output: alien-mid
|
|
|
168
167
|
> They can only inspect the `Response` or replace it by returning a new
|
|
169
168
|
> `Response`.
|
|
170
169
|
|
|
170
|
+
Your response middlewares will run even if no `Response` is generated by the
|
|
171
|
+
request middlewares, **except** when the middleware chain is nested inside
|
|
172
|
+
another chain, since the outer chain will still have a chance to return a
|
|
173
|
+
`Response`.
|
|
174
|
+
|
|
171
175
|
### Nesting Chains
|
|
172
176
|
|
|
173
177
|
You can compose middleware by nesting chains using `.use()`. Context modifications (`define`, `env`) within a nested chain are scoped to that chain and do not affect middleware outside of it.
|
|
@@ -189,15 +193,12 @@ const outerMiddleware = (ctx: RequestContext) => {
|
|
|
189
193
|
return new Response('Finished')
|
|
190
194
|
}
|
|
191
195
|
|
|
192
|
-
const
|
|
193
|
-
|
|
194
|
-
//
|
|
195
|
-
|
|
196
|
-
//
|
|
197
|
-
//
|
|
198
|
-
// Accessing inner data: secret
|
|
199
|
-
// Outer middleware after inner chain
|
|
200
|
-
// innerData is correctly scoped.
|
|
196
|
+
const finalApp = chain().use(innerChain).use(outerMiddleware)
|
|
197
|
+
// Output when executing the finalApp chain:
|
|
198
|
+
// Inner chain start
|
|
199
|
+
// Accessing inner data: secret
|
|
200
|
+
// Outer middleware after inner chain
|
|
201
|
+
// innerData is correctly scoped.
|
|
201
202
|
```
|
|
202
203
|
|
|
203
204
|
If a nested chain does not return a `Response`, execution continues with the next middleware in the outer chain.
|