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.
Files changed (3) hide show
  1. package/dist/index.js +1 -1
  2. package/package.json +1 -1
  3. 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
- return new Response("Not Found", { status: 404 });
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
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "alien-middleware",
3
3
  "type": "module",
4
- "version": "0.1.0",
4
+ "version": "0.2.0",
5
5
  "exports": {
6
6
  "types": "./dist/index.d.ts",
7
7
  "default": "./dist/index.js"
package/readme.md CHANGED
@@ -51,7 +51,7 @@ const secondMiddleware = (ctx: RequestContext) => {
51
51
  }
52
52
 
53
53
  // Add middleware sequentially
54
- const finalApp = app.use(firstMiddleware).use(secondMiddleware)
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, call the chain instance itself with a context object. This object typically comes from an adapter like Hattip's [Node adapter](https://www.npmjs.com/package/@hattip/adapter-node).
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
- // Simplified context for demonstration (requires necessary imports like 'noop' if run directly)
66
- const context: AdapterRequestContext = {
67
- request: new Request('http://localhost/test'),
68
- ip: '127.0.0.1',
69
- platform: {},
70
- waitUntil: (promise: Promise<any>) => {},
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
- // Execute the chain
76
- const response = await finalApp(context) // Assuming finalApp from previous example
72
+ // Create a server
73
+ const server = createServer(app)
77
74
 
78
- console.log(await response.text()) // Output: Hello from middleware!
79
- console.log(response.status) // Output: 200
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, except for nested chains.
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 userApp = app.use(addUser).use(greetUser)
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 envApp = app.use(addApiKey).use(useApiKey)
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 addHeader = (ctx: RequestContext, response: Response) => {
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
- // `addHeader` runs after `mainHandler` generates a response
159
- const headerApp = app.use(mainHandler).use(addHeader)
158
+ // `poweredByMiddleware` runs after `mainHandler` generates a response
159
+ const app = chain().use(mainHandler).use(poweredByMiddleware)
160
160
 
161
- // Assuming `context` is defined as in the "Executing the Chain" example
162
- const responseWithHeader = await headerApp(context)
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 nestedApp = app.use(innerChain).use(outerMiddleware)
193
-
194
- // Assuming `context` is defined as in the "Executing the Chain" example
195
- await nestedApp(context)
196
- // Output:
197
- // Inner chain start
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.