@bunary/http 0.0.1 → 0.0.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/CHANGELOG.md +21 -1
- package/README.md +71 -0
- package/package.json +1 -1
package/CHANGELOG.md
CHANGED
|
@@ -5,7 +5,27 @@ All notable changes to `@bunary/http` will be documented in this file.
|
|
|
5
5
|
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.1.0/),
|
|
6
6
|
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
|
|
7
7
|
|
|
8
|
-
## [0.0.
|
|
8
|
+
## [0.0.2] - 2026-01-24
|
|
9
|
+
|
|
10
|
+
### Added
|
|
11
|
+
|
|
12
|
+
- Comprehensive middleware test suite (19 tests)
|
|
13
|
+
- Middleware documentation in README with examples:
|
|
14
|
+
- Basic logging middleware
|
|
15
|
+
- Error handling middleware
|
|
16
|
+
- Authentication middleware pattern
|
|
17
|
+
- Middleware chain execution order
|
|
18
|
+
|
|
19
|
+
### Verified
|
|
20
|
+
|
|
21
|
+
- Middleware pipeline executes in registration order (FR-016)
|
|
22
|
+
- `app.use(middleware)` adds middleware to pipeline (FR-015)
|
|
23
|
+
- Middleware can call `next()` for chain continuation
|
|
24
|
+
- Middleware can return early without calling `next()`
|
|
25
|
+
- Middleware can catch and handle errors from `next()`
|
|
26
|
+
- Middleware errors return 500 response
|
|
27
|
+
|
|
28
|
+
## [0.0.1] - 2026-01-24
|
|
9
29
|
|
|
10
30
|
### Added
|
|
11
31
|
|
package/README.md
CHANGED
|
@@ -144,6 +144,77 @@ const data = await response.json();
|
|
|
144
144
|
// { message: 'hi' }
|
|
145
145
|
```
|
|
146
146
|
|
|
147
|
+
## Middleware
|
|
148
|
+
|
|
149
|
+
Add middleware to handle cross-cutting concerns like logging, authentication, and error handling.
|
|
150
|
+
|
|
151
|
+
### Basic Middleware
|
|
152
|
+
|
|
153
|
+
```typescript
|
|
154
|
+
// Logging middleware
|
|
155
|
+
app.use(async (ctx, next) => {
|
|
156
|
+
const start = Date.now();
|
|
157
|
+
const result = await next();
|
|
158
|
+
console.log(`${ctx.request.method} ${new URL(ctx.request.url).pathname} - ${Date.now() - start}ms`);
|
|
159
|
+
return result;
|
|
160
|
+
});
|
|
161
|
+
```
|
|
162
|
+
|
|
163
|
+
### Middleware Chain
|
|
164
|
+
|
|
165
|
+
Middleware executes in registration order. Each middleware can:
|
|
166
|
+
- Run code before calling `next()`
|
|
167
|
+
- Call `next()` to continue the chain
|
|
168
|
+
- Run code after `next()` returns
|
|
169
|
+
- Return early without calling `next()`
|
|
170
|
+
|
|
171
|
+
```typescript
|
|
172
|
+
app
|
|
173
|
+
.use(async (ctx, next) => {
|
|
174
|
+
console.log('First - before');
|
|
175
|
+
const result = await next();
|
|
176
|
+
console.log('First - after');
|
|
177
|
+
return result;
|
|
178
|
+
})
|
|
179
|
+
.use(async (ctx, next) => {
|
|
180
|
+
console.log('Second - before');
|
|
181
|
+
const result = await next();
|
|
182
|
+
console.log('Second - after');
|
|
183
|
+
return result;
|
|
184
|
+
});
|
|
185
|
+
|
|
186
|
+
// Output order: First-before, Second-before, handler, Second-after, First-after
|
|
187
|
+
```
|
|
188
|
+
|
|
189
|
+
### Error Handling Middleware
|
|
190
|
+
|
|
191
|
+
```typescript
|
|
192
|
+
app.use(async (ctx, next) => {
|
|
193
|
+
try {
|
|
194
|
+
return await next();
|
|
195
|
+
} catch (error) {
|
|
196
|
+
console.error('Error:', error);
|
|
197
|
+
return new Response(JSON.stringify({ error: error.message }), {
|
|
198
|
+
status: 500,
|
|
199
|
+
headers: { 'Content-Type': 'application/json' }
|
|
200
|
+
});
|
|
201
|
+
}
|
|
202
|
+
});
|
|
203
|
+
```
|
|
204
|
+
|
|
205
|
+
### Auth Middleware (Example)
|
|
206
|
+
|
|
207
|
+
```typescript
|
|
208
|
+
app.use(async (ctx, next) => {
|
|
209
|
+
const token = ctx.request.headers.get('Authorization');
|
|
210
|
+
if (!token) {
|
|
211
|
+
return new Response(JSON.stringify({ error: 'Unauthorized' }), { status: 401 });
|
|
212
|
+
}
|
|
213
|
+
// Validate token...
|
|
214
|
+
return await next();
|
|
215
|
+
});
|
|
216
|
+
```
|
|
217
|
+
|
|
147
218
|
## Error Handling
|
|
148
219
|
|
|
149
220
|
Uncaught errors in handlers return a 500 response:
|