burger-api 0.5.2 → 0.6.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/README.md +43 -15
- package/dist/src/index.d.ts +20 -2
- package/dist/src/index.js +155 -89
- package/dist/src/types/index.d.ts +8 -9
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -5,7 +5,7 @@
|
|
|
5
5
|
[](https://github.com/isfhan/burger-api)
|
|
6
6
|
[](LICENSE)
|
|
7
7
|
[](https://bun.sh)
|
|
8
|
-
[](https://github.com/isfhan/burger-api/releases)
|
|
9
9
|
|
|
10
10
|
**burger-api** is a modern, open source API framework built on
|
|
11
11
|
[Bun.js](https://bun.sh). It combines the simplicity of file-based routing with
|
|
@@ -42,34 +42,62 @@ burger-api is built to offer a robust developer experience through:
|
|
|
42
42
|
naming convention.
|
|
43
43
|
|
|
44
44
|
- 🔄 **Middleware Architecture:**
|
|
45
|
-
Supports both global and route-specific middleware.
|
|
45
|
+
Supports both global and route-specific middleware. Simple, powerful
|
|
46
|
+
middleware system with three return types:
|
|
46
47
|
|
|
47
|
-
-
|
|
48
|
-
|
|
49
|
-
-
|
|
50
|
-
post-processing is reliable.
|
|
51
|
-
- They run in reverse order and are useful for adding CORS headers,
|
|
52
|
-
logging, or tweaking the final Response.
|
|
48
|
+
- `Response` - Stop here, send this response
|
|
49
|
+
- `Function` - Transform the final response after handler runs
|
|
50
|
+
- `undefined` - Continue to next middleware
|
|
53
51
|
|
|
54
52
|
- ✅ **Type-Safe Validation:**
|
|
55
53
|
Utilizes Zod for request validation, ensuring full type safety and automatic
|
|
56
54
|
error reporting.
|
|
57
55
|
|
|
58
56
|
- 📚 **Automatic OpenAPI Generation:**
|
|
59
|
-
Generates a complete OpenAPI 3.0 specification
|
|
60
|
-
|
|
61
|
-
more) directly from your routes and Zod schemas.
|
|
57
|
+
Generates a complete OpenAPI 3.0 specification directly from your routes and
|
|
58
|
+
Zod schemas.
|
|
62
59
|
|
|
63
60
|
- 🔍 **Swagger UI Integration:**
|
|
64
|
-
|
|
65
|
-
documentation.
|
|
61
|
+
Out-of-the-box Swagger UI endpoint for interactive API documentation.
|
|
66
62
|
|
|
67
63
|
## 📣 Changelog
|
|
68
64
|
|
|
69
|
-
### Latest Version: 0.
|
|
65
|
+
### Latest Version: 0.6.0 (November 12, 2025)
|
|
66
|
+
|
|
67
|
+
### Version 0.6.0 (November 11, 2025)
|
|
68
|
+
|
|
69
|
+
- ⚡ **Major Performance Improvements:**
|
|
70
|
+
|
|
71
|
+
- middleware execution with specialized fast paths
|
|
72
|
+
- AOT compilation with pre-computed middleware arrays
|
|
73
|
+
- Zero runtime allocations (pre-allocated arrays)
|
|
74
|
+
- Manual loop unrolling for 2-middleware case
|
|
75
|
+
- Reduced code from ~110 to ~80 lines
|
|
76
|
+
|
|
77
|
+
- 🎯 **Simplified Middleware System:**
|
|
78
|
+
|
|
79
|
+
- Clearer return types: Response, Function, or undefined
|
|
80
|
+
- Removed complex "around" middleware pattern
|
|
81
|
+
- Dedicated fast paths for 0, 1, and 2 middlewares
|
|
82
|
+
- Better JIT optimization
|
|
83
|
+
|
|
84
|
+
- 📦 **Monorepo Structure:**
|
|
85
|
+
|
|
86
|
+
- Converted to Bun workspace monorepo
|
|
87
|
+
- Core framework in `packages/burger-api`
|
|
88
|
+
- CLI tool in `packages/cli` (under development)
|
|
89
|
+
- Ecosystem middleware at root level
|
|
90
|
+
|
|
91
|
+
- 🔧 **Developer Experience:**
|
|
92
|
+
- 100% backward compatible
|
|
93
|
+
- Clearer documentation
|
|
94
|
+
- Easier to understand codebase
|
|
95
|
+
|
|
96
|
+
### Previous Version: 0.5.2 (November 9, 2025)
|
|
70
97
|
|
|
71
98
|
- 🔧 **Internal Improvements:**
|
|
72
|
-
- Refactored wildcard parameter extraction logic into reusable utility
|
|
99
|
+
- Refactored wildcard parameter extraction logic into reusable utility
|
|
100
|
+
functions
|
|
73
101
|
- Added test suites and README files for all example projects
|
|
74
102
|
|
|
75
103
|
### Previous Version: 0.5.0 (November 1, 2025)
|
package/dist/src/index.d.ts
CHANGED
|
@@ -58,12 +58,30 @@ export declare class Burger {
|
|
|
58
58
|
*/
|
|
59
59
|
private processApiRoutes;
|
|
60
60
|
/**
|
|
61
|
-
* Process
|
|
61
|
+
* Process single middleware
|
|
62
62
|
* @param request - The request object
|
|
63
|
-
* @param
|
|
63
|
+
* @param middleware - The middleware function
|
|
64
64
|
* @param handler - The handler function
|
|
65
65
|
* @returns A promise that resolves to a response
|
|
66
66
|
*/
|
|
67
|
+
private processSingleMiddleware;
|
|
68
|
+
/**
|
|
69
|
+
* Process middleware and handler
|
|
70
|
+
*
|
|
71
|
+
* How it works:
|
|
72
|
+
* 1. Run each middleware in order
|
|
73
|
+
* 2. If middleware returns Response → stop and send that response (but still apply "after" functions)
|
|
74
|
+
* 3. If middleware returns undefined → continue to next middleware
|
|
75
|
+
* 4. If middleware returns function → save it to transform the final response later
|
|
76
|
+
* 5. After all middlewares, run the handler
|
|
77
|
+
* 6. Apply all saved "after" functions to the response (in reverse order)
|
|
78
|
+
*
|
|
79
|
+
* Performance optimizations:
|
|
80
|
+
* - Pre-allocated array for "after" functions (avoids dynamic resizing)
|
|
81
|
+
* - Fast paths for 0, 1, and 2 middlewares (most common cases)
|
|
82
|
+
* - Manual loop unrolling for small counts
|
|
83
|
+
* - Minimal branching in hot path
|
|
84
|
+
*/
|
|
67
85
|
private processMiddleware;
|
|
68
86
|
/**
|
|
69
87
|
* Starts the server and begins listening for incoming requests.
|
package/dist/src/index.js
CHANGED
|
@@ -145,69 +145,98 @@ export class Burger {
|
|
|
145
145
|
const hasSchema = !!schema;
|
|
146
146
|
// Optimize middleware array initialization by pre-allocating size
|
|
147
147
|
const totalMiddlewareCount = globalMiddlewareLen + (hasSchema ? 1 : 0) + routeMiddlewareLen;
|
|
148
|
-
//
|
|
148
|
+
// Pre-compute wildcard info once (used in all paths)
|
|
149
|
+
const isWildcard = route.isWildcard;
|
|
150
|
+
const baseSegmentCount = isWildcard
|
|
151
|
+
? path.split('/').filter(Boolean).length - 1
|
|
152
|
+
: 0;
|
|
153
|
+
// Create optimized route handler based on middleware count
|
|
149
154
|
if (totalMiddlewareCount === 0) {
|
|
150
|
-
// Ultra-fast path: no middleware
|
|
151
|
-
//
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
const baseSegmentCount = isWildcard
|
|
156
|
-
? path.split('/').filter(Boolean).length - 1 // Subtract 1 for the * itself
|
|
157
|
-
: 0;
|
|
158
|
-
routes[path] = (request) => {
|
|
159
|
-
// Extract wildcard params if this is a wildcard route
|
|
160
|
-
if (isWildcard) {
|
|
155
|
+
// Ultra-fast path: no middleware at all
|
|
156
|
+
// Inline everything for maximum speed
|
|
157
|
+
if (isWildcard) {
|
|
158
|
+
// Wildcard route with no middleware
|
|
159
|
+
routes[path] = (request) => {
|
|
161
160
|
const pathname = extractPathnameFromUrl(request.url);
|
|
162
161
|
extractWildcardParams(request, pathname, baseSegmentCount);
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
162
|
+
const handler = handlers[request.method];
|
|
163
|
+
return handler
|
|
164
|
+
? handler(request)
|
|
165
|
+
: this.METHOD_NOT_ALLOWED;
|
|
166
|
+
};
|
|
167
|
+
}
|
|
168
|
+
else {
|
|
169
|
+
// Regular route with no middleware (most common case)
|
|
170
|
+
routes[path] = (request) => {
|
|
171
|
+
const handler = handlers[request.method];
|
|
172
|
+
return handler
|
|
173
|
+
? handler(request)
|
|
174
|
+
: this.METHOD_NOT_ALLOWED;
|
|
175
|
+
};
|
|
176
|
+
}
|
|
167
177
|
}
|
|
168
178
|
else {
|
|
169
|
-
// Pre-compute middleware array with
|
|
179
|
+
// Pre-compute middleware array with exact size (AOT optimization)
|
|
170
180
|
const middlewares = new Array(totalMiddlewareCount);
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
// Copy global middleware (most common case)
|
|
181
|
+
let idx = 0;
|
|
182
|
+
// Copy global middleware (manual loop is faster than spread/concat)
|
|
174
183
|
for (let j = 0; j < globalMiddlewareLen; j++) {
|
|
175
|
-
middlewares[
|
|
184
|
+
middlewares[idx++] = globalMiddleware[j];
|
|
176
185
|
}
|
|
177
186
|
// Add validation middleware if needed
|
|
178
187
|
if (hasSchema) {
|
|
179
|
-
middlewares[
|
|
188
|
+
middlewares[idx++] = createValidationMiddleware(schema);
|
|
180
189
|
}
|
|
181
190
|
// Add route-specific middlewares
|
|
182
191
|
if (routeMiddleware) {
|
|
183
192
|
for (let j = 0; j < routeMiddlewareLen; j++) {
|
|
184
|
-
middlewares[
|
|
193
|
+
middlewares[idx++] = routeMiddleware[j];
|
|
185
194
|
}
|
|
186
195
|
}
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
*/
|
|
192
|
-
// Pre-compute wildcard segment count for faster extraction
|
|
193
|
-
const isWildcard = route.isWildcard;
|
|
194
|
-
// Calculate how many segments come before the wildcard
|
|
195
|
-
// E.g., "/api/users/:userId/*" has 3 segments before wildcard (api, users, :userId)
|
|
196
|
-
const baseSegmentCount = isWildcard
|
|
197
|
-
? path.split('/').filter(Boolean).length - 1 // Subtract 1 for the * itself
|
|
198
|
-
: 0;
|
|
199
|
-
// Create handler with middleware
|
|
200
|
-
routes[path] = (request) => {
|
|
201
|
-
// Extract wildcard params if this is a wildcard route
|
|
196
|
+
// Create specialized handler based on middleware count
|
|
197
|
+
if (totalMiddlewareCount === 1) {
|
|
198
|
+
// Single middleware fast path (common: just CORS or just auth)
|
|
199
|
+
const singleMiddleware = middlewares[0];
|
|
202
200
|
if (isWildcard) {
|
|
203
|
-
|
|
204
|
-
|
|
201
|
+
routes[path] = (request) => {
|
|
202
|
+
const pathname = extractPathnameFromUrl(request.url);
|
|
203
|
+
extractWildcardParams(request, pathname, baseSegmentCount);
|
|
204
|
+
const handler = handlers[request.method];
|
|
205
|
+
if (!handler)
|
|
206
|
+
return this.METHOD_NOT_ALLOWED;
|
|
207
|
+
return this.processSingleMiddleware(request, singleMiddleware, handler);
|
|
208
|
+
};
|
|
209
|
+
}
|
|
210
|
+
else {
|
|
211
|
+
routes[path] = (request) => {
|
|
212
|
+
const handler = handlers[request.method];
|
|
213
|
+
if (!handler)
|
|
214
|
+
return this.METHOD_NOT_ALLOWED;
|
|
215
|
+
return this.processSingleMiddleware(request, singleMiddleware, handler);
|
|
216
|
+
};
|
|
205
217
|
}
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
218
|
+
}
|
|
219
|
+
else {
|
|
220
|
+
// Multiple middlewares (general case)
|
|
221
|
+
if (isWildcard) {
|
|
222
|
+
routes[path] = (request) => {
|
|
223
|
+
const pathname = extractPathnameFromUrl(request.url);
|
|
224
|
+
extractWildcardParams(request, pathname, baseSegmentCount);
|
|
225
|
+
const handler = handlers[request.method];
|
|
226
|
+
if (!handler)
|
|
227
|
+
return this.METHOD_NOT_ALLOWED;
|
|
228
|
+
return this.processMiddleware(request, middlewares, handler);
|
|
229
|
+
};
|
|
230
|
+
}
|
|
231
|
+
else {
|
|
232
|
+
routes[path] = (request) => {
|
|
233
|
+
const handler = handlers[request.method];
|
|
234
|
+
if (!handler)
|
|
235
|
+
return this.METHOD_NOT_ALLOWED;
|
|
236
|
+
return this.processMiddleware(request, middlewares, handler);
|
|
237
|
+
};
|
|
238
|
+
}
|
|
239
|
+
}
|
|
211
240
|
}
|
|
212
241
|
// For wildcard routes, also register the base path
|
|
213
242
|
// Bun's /* wildcard may not match the base path itself, so we register both
|
|
@@ -233,77 +262,114 @@ export class Burger {
|
|
|
233
262
|
return true;
|
|
234
263
|
}
|
|
235
264
|
/**
|
|
236
|
-
* Process
|
|
265
|
+
* Process single middleware
|
|
237
266
|
* @param request - The request object
|
|
238
|
-
* @param
|
|
267
|
+
* @param middleware - The middleware function
|
|
239
268
|
* @param handler - The handler function
|
|
240
269
|
* @returns A promise that resolves to a response
|
|
241
270
|
*/
|
|
271
|
+
async processSingleMiddleware(request, middleware, handler) {
|
|
272
|
+
const result = await middleware(request);
|
|
273
|
+
// Short-circuit with Response
|
|
274
|
+
if (result instanceof Response) {
|
|
275
|
+
return result;
|
|
276
|
+
}
|
|
277
|
+
// Transform response after handler
|
|
278
|
+
if (typeof result === 'function') {
|
|
279
|
+
return result(await handler(request));
|
|
280
|
+
}
|
|
281
|
+
// Continue to handler
|
|
282
|
+
return handler(request);
|
|
283
|
+
}
|
|
284
|
+
/**
|
|
285
|
+
* Process middleware and handler
|
|
286
|
+
*
|
|
287
|
+
* How it works:
|
|
288
|
+
* 1. Run each middleware in order
|
|
289
|
+
* 2. If middleware returns Response → stop and send that response (but still apply "after" functions)
|
|
290
|
+
* 3. If middleware returns undefined → continue to next middleware
|
|
291
|
+
* 4. If middleware returns function → save it to transform the final response later
|
|
292
|
+
* 5. After all middlewares, run the handler
|
|
293
|
+
* 6. Apply all saved "after" functions to the response (in reverse order)
|
|
294
|
+
*
|
|
295
|
+
* Performance optimizations:
|
|
296
|
+
* - Pre-allocated array for "after" functions (avoids dynamic resizing)
|
|
297
|
+
* - Fast paths for 0, 1, and 2 middlewares (most common cases)
|
|
298
|
+
* - Manual loop unrolling for small counts
|
|
299
|
+
* - Minimal branching in hot path
|
|
300
|
+
*/
|
|
242
301
|
async processMiddleware(request, middlewares, handler) {
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
if (result instanceof Response) {
|
|
251
|
-
return result;
|
|
302
|
+
const len = middlewares.length;
|
|
303
|
+
// Fast path: two middlewares (common: CORS + logger, or auth + logger)
|
|
304
|
+
if (len === 2) {
|
|
305
|
+
// First middleware
|
|
306
|
+
const result1 = await middlewares[0](request);
|
|
307
|
+
if (result1 instanceof Response) {
|
|
308
|
+
return result1;
|
|
252
309
|
}
|
|
253
|
-
//
|
|
254
|
-
|
|
255
|
-
|
|
310
|
+
// Second middleware
|
|
311
|
+
const result2 = await middlewares[1](request);
|
|
312
|
+
if (result2 instanceof Response) {
|
|
313
|
+
// Apply first middleware's after function if exists
|
|
314
|
+
if (typeof result1 === 'function') {
|
|
315
|
+
return result1(result2);
|
|
316
|
+
}
|
|
317
|
+
return result2;
|
|
256
318
|
}
|
|
257
|
-
//
|
|
258
|
-
|
|
319
|
+
// Run handler
|
|
320
|
+
let response = await handler(request);
|
|
321
|
+
// Apply after functions in reverse order (manual unroll)
|
|
322
|
+
if (typeof result2 === 'function') {
|
|
323
|
+
response = await result2(response);
|
|
324
|
+
}
|
|
325
|
+
if (typeof result1 === 'function') {
|
|
326
|
+
response = await result1(response);
|
|
327
|
+
}
|
|
328
|
+
return response;
|
|
259
329
|
}
|
|
260
|
-
//
|
|
261
|
-
//
|
|
262
|
-
const afterStack = new Array(
|
|
263
|
-
// Initialize the after middleware counter
|
|
330
|
+
// General path: 3+ middlewares (less common)
|
|
331
|
+
// Pre-allocate array with exact size to avoid dynamic resizing
|
|
332
|
+
const afterStack = new Array(len);
|
|
264
333
|
let afterCount = 0;
|
|
265
|
-
//
|
|
266
|
-
for (let i = 0; i <
|
|
267
|
-
// Get the current middleware
|
|
334
|
+
// Run each middleware
|
|
335
|
+
for (let i = 0; i < len; i++) {
|
|
268
336
|
const result = await middlewares[i](request);
|
|
269
|
-
//
|
|
337
|
+
// Short-circuit with Response (check first - most common early exit)
|
|
270
338
|
if (result instanceof Response) {
|
|
271
|
-
//
|
|
339
|
+
// Apply collected "after" functions in reverse
|
|
340
|
+
if (afterCount === 0)
|
|
341
|
+
return result;
|
|
342
|
+
if (afterCount === 1)
|
|
343
|
+
return afterStack[0](result);
|
|
344
|
+
// Multiple after functions
|
|
272
345
|
let response = result;
|
|
273
|
-
// Fast paths for after functions
|
|
274
|
-
if (afterCount === 0) {
|
|
275
|
-
return response;
|
|
276
|
-
}
|
|
277
|
-
if (afterCount === 1) {
|
|
278
|
-
return afterStack[0](response);
|
|
279
|
-
}
|
|
280
|
-
// Apply after middlewares in reverse order (LIFO)
|
|
281
346
|
for (let j = afterCount - 1; j >= 0; j--) {
|
|
282
347
|
response = await afterStack[j](response);
|
|
283
348
|
}
|
|
284
349
|
return response;
|
|
285
350
|
}
|
|
286
|
-
//
|
|
287
|
-
|
|
351
|
+
// Save function for later (check once, no double typeof check)
|
|
352
|
+
if (typeof result === 'function') {
|
|
288
353
|
afterStack[afterCount++] = result;
|
|
289
354
|
}
|
|
290
|
-
//
|
|
355
|
+
// undefined - continue (implicit, no check needed)
|
|
291
356
|
}
|
|
292
|
-
//
|
|
357
|
+
// All middlewares passed - run handler
|
|
293
358
|
let response = await handler(request);
|
|
294
|
-
//
|
|
295
|
-
|
|
359
|
+
// Apply "after" functions in reverse order
|
|
360
|
+
// Fast paths for common cases
|
|
361
|
+
if (afterCount === 0)
|
|
296
362
|
return response;
|
|
297
|
-
|
|
298
|
-
|
|
299
|
-
if (afterCount ===
|
|
363
|
+
if (afterCount === 1)
|
|
364
|
+
return afterStack[0](response);
|
|
365
|
+
if (afterCount === 2) {
|
|
366
|
+
response = await afterStack[1](response);
|
|
300
367
|
return afterStack[0](response);
|
|
301
368
|
}
|
|
302
|
-
//
|
|
369
|
+
// General case: 3+ after functions
|
|
303
370
|
for (let i = afterCount - 1; i >= 0; i--) {
|
|
304
371
|
response = await afterStack[i](response);
|
|
305
372
|
}
|
|
306
|
-
// Return the response
|
|
307
373
|
return response;
|
|
308
374
|
}
|
|
309
375
|
/**
|
|
@@ -87,17 +87,16 @@ export interface BurgerRequest<RequestValidatedProperties extends DefaultRequest
|
|
|
87
87
|
}
|
|
88
88
|
/**
|
|
89
89
|
* Represents what a middleware can return to control the request flow:
|
|
90
|
-
* - Response:
|
|
91
|
-
* - Function: Continue processing, but transform the final response
|
|
92
|
-
* - undefined:
|
|
90
|
+
* - Response: Stop here, send this response back to the client
|
|
91
|
+
* - Function(Response): Continue processing, but transform the final response after handler runs
|
|
92
|
+
* - undefined: I'm done, continue to the next middleware or handler
|
|
93
93
|
*/
|
|
94
94
|
export type BurgerNext = Response | ((response: Response) => Promise<Response>) | undefined;
|
|
95
95
|
/**
|
|
96
|
-
* A middleware function that processes HTTP requests
|
|
96
|
+
* A middleware function that processes HTTP requests.
|
|
97
97
|
*
|
|
98
98
|
* What middleware can do:
|
|
99
|
-
* -
|
|
100
|
-
* - Check if the request is valid
|
|
99
|
+
* - Check if the request is valid (auth, validation, etc.)
|
|
101
100
|
* - Stop the request by returning a Response
|
|
102
101
|
* - Let the request continue by returning undefined
|
|
103
102
|
* - Transform the final response by returning a function
|
|
@@ -105,8 +104,8 @@ export type BurgerNext = Response | ((response: Response) => Promise<Response>)
|
|
|
105
104
|
* @param request - The HTTP request with Burger framework enhancements
|
|
106
105
|
* @returns One of three things:
|
|
107
106
|
* - Response: Stop here, send this response back
|
|
108
|
-
* - Function:
|
|
109
|
-
* - undefined:
|
|
107
|
+
* - Function: Transform the final response after handler runs
|
|
108
|
+
* - undefined: Continue to the next middleware or handler
|
|
110
109
|
*/
|
|
111
110
|
export type Middleware = ((request: BurgerRequest) => Promise<BurgerNext>) | ((request: BurgerRequest) => BurgerNext);
|
|
112
111
|
/**
|
|
@@ -120,7 +119,7 @@ export type RequestHandler = (request: BurgerRequest) => Promise<Response> | Res
|
|
|
120
119
|
* This can be a function that returns a Promise of a Response,
|
|
121
120
|
* or a function that returns a Response.
|
|
122
121
|
*/
|
|
123
|
-
export type FetchHandler = (request: Request, server?: Server) => Promise<Response> | Response;
|
|
122
|
+
export type FetchHandler = (request: Request, server?: Server<{}>) => Promise<Response> | Response;
|
|
124
123
|
export interface RouteDefinition {
|
|
125
124
|
/**
|
|
126
125
|
* The path of the route.
|