burger-api 0.1.0 → 0.1.4
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 +278 -246
- package/dist/src/core/api-router.d.ts +1 -1
- package/dist/src/core/api-router.js +15 -15
- package/dist/src/core/openapi.d.ts +2 -2
- package/dist/src/core/openapi.js +17 -17
- package/dist/src/core/page-router.d.ts +1 -1
- package/dist/src/core/page-router.js +20 -19
- package/dist/src/core/response.js +6 -6
- package/dist/src/core/server.d.ts +4 -2
- package/dist/src/core/server.js +8 -5
- package/dist/src/index.d.ts +3 -3
- package/dist/src/index.js +48 -39
- package/dist/src/middleware/validator.d.ts +1 -1
- package/dist/src/middleware/validator.js +9 -6
- package/dist/src/types/index.d.ts +8 -2
- package/dist/src/types/index.js +1 -1
- package/dist/src/utils/error.js +9 -9
- package/dist/src/utils/index.d.ts +1 -1
- package/dist/src/utils/index.js +6 -6
- package/dist/src/utils/routing.d.ts +1 -1
- package/dist/src/utils/routing.js +15 -15
- package/package.json +83 -82
package/README.md
CHANGED
|
@@ -1,116 +1,129 @@
|
|
|
1
|
-
|
|
2
1
|
<p align="center">
|
|
3
2
|
<img src="https://github.com/user-attachments/assets/0d9b376e-1d89-479a-aa7f-e7ee3c6b2342" alt="BurgerAPI logo"/>
|
|
4
3
|
</p>
|
|
5
4
|
|
|
5
|
+
[](https://github.com/isfhan/burger-api)
|
|
6
|
+
[](LICENSE)
|
|
7
|
+
[](https://bun.sh)
|
|
6
8
|
|
|
7
|
-
|
|
9
|
+
**burger-api** is a modern, open source API framework built on
|
|
10
|
+
[Bun.js](https://bun.sh). It combines the simplicity of file-based routing with
|
|
11
|
+
powerful features like built-in middleware, Zod-based schema validation, and
|
|
12
|
+
automatic OpenAPI generation. Designed for high performance and ease-of-use,
|
|
13
|
+
burger-api leverages Bun's native modules to deliver blazing-fast API responses
|
|
14
|
+
while keeping your codebase clean and maintainable.
|
|
8
15
|
|
|
9
|
-
**
|
|
10
|
-
|
|
11
|
-
**This project is under active development and should not be used in production yet.**
|
|
16
|
+
**This project is under active development and should not be used in production
|
|
17
|
+
yet.**
|
|
12
18
|
|
|
13
19
|
## 📚 Table of Contents
|
|
14
20
|
|
|
15
|
-
-
|
|
16
|
-
-
|
|
17
|
-
|
|
18
|
-
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
-
|
|
22
|
-
-
|
|
23
|
-
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
-
|
|
31
|
-
-
|
|
32
|
-
- [License](#-license)
|
|
33
|
-
- [FAQs & Resources](#-faqs--additional-resources)
|
|
21
|
+
- [Overview](#-overview)
|
|
22
|
+
- [Features](#-features)
|
|
23
|
+
- [Core Features](#core-features)
|
|
24
|
+
- [Changelog](#-changelog)
|
|
25
|
+
- [What's Coming Next](#-whats-coming-next)
|
|
26
|
+
- [Installation](#-installation)
|
|
27
|
+
- [How to Use burger-api](#-how-to-use-burger-api)
|
|
28
|
+
- [Basic Usage Example](#basic-usage-example)
|
|
29
|
+
- [Project Structure](#recommended-project-structure)
|
|
30
|
+
- [Implementation Examples](#example-implementation)
|
|
31
|
+
- [File-Based Routing](#file-based-routing-examples)
|
|
32
|
+
- [Page Directory Usage](#page-directory-structure)
|
|
33
|
+
- [Route Examples](#route-file-example)
|
|
34
|
+
- [API Documentation](#api-documentation-endpoints)
|
|
35
|
+
- [Contributing](#-contributing)
|
|
36
|
+
- [License](#-license)
|
|
37
|
+
- [FAQs & Resources](#-faqs--additional-resources)
|
|
34
38
|
|
|
35
39
|
## 🚀 Overview
|
|
36
40
|
|
|
37
41
|
burger-api is built to offer a robust developer experience through:
|
|
38
42
|
|
|
39
|
-
-
|
|
40
|
-
|
|
43
|
+
- ⚡ **Bun-Native Performance:**
|
|
44
|
+
Leverages Bun's high-performance HTTP server.
|
|
41
45
|
|
|
42
|
-
-
|
|
43
|
-
|
|
46
|
+
- 📁 **File-Based Routing:**
|
|
47
|
+
Automatically registers API routes from your file structure using a clear
|
|
48
|
+
naming convention.
|
|
44
49
|
|
|
45
|
-
-
|
|
46
|
-
|
|
50
|
+
- 🔄 **Middleware Architecture:**
|
|
51
|
+
Supports both global and route-specific middleware with a familiar
|
|
52
|
+
`req, res, next` pattern.
|
|
47
53
|
|
|
48
|
-
-
|
|
49
|
-
|
|
54
|
+
- ✅ **Type-Safe Validation:**
|
|
55
|
+
Utilizes Zod for request validation, ensuring full type safety and automatic
|
|
56
|
+
error reporting.
|
|
50
57
|
|
|
51
|
-
-
|
|
52
|
-
|
|
58
|
+
- 📚 **Automatic OpenAPI Generation:**
|
|
59
|
+
Generates a complete OpenAPI 3.0 specification (with support for tags,
|
|
60
|
+
summaries, descriptions, operationId, deprecated status, externalDocs, and
|
|
61
|
+
more) directly from your routes and Zod schemas.
|
|
53
62
|
|
|
54
|
-
-
|
|
55
|
-
|
|
63
|
+
- 🔍 **Swagger UI Integration:**
|
|
64
|
+
Provides an out-of-the-box Swagger UI endpoint for interactive API
|
|
65
|
+
documentation.
|
|
56
66
|
|
|
57
67
|
## ✨ Features
|
|
58
68
|
|
|
59
69
|
### Core Features
|
|
60
70
|
|
|
61
|
-
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
- 📁 **File-Based Routing Engine:**
|
|
65
|
-
Automatically scans directories to register routes.
|
|
71
|
+
- ⚡ **Bun-Native HTTP Server:**
|
|
72
|
+
Built on Bun's native APIs for exceptional performance.
|
|
66
73
|
|
|
67
|
-
|
|
74
|
+
- 📁 **File-Based Routing Engine:**
|
|
75
|
+
Automatically scans directories to register routes.
|
|
68
76
|
|
|
69
|
-
-
|
|
77
|
+
- Supports dynamic routes via folder names like `[id]` for
|
|
78
|
+
`/api/product/:id`.
|
|
70
79
|
|
|
71
|
-
|
|
72
|
-
- `BurgerResponse` provides methods to set headers, status, and build responses (JSON, text, HTML, redirects, files).
|
|
80
|
+
- 🛠️ **Request & Response Enhancements:**
|
|
73
81
|
|
|
74
|
-
-
|
|
82
|
+
- `BurgerRequest` extends the native Request to include query, params, and
|
|
83
|
+
validated data.
|
|
84
|
+
- `BurgerResponse` provides methods to set headers, status, and build
|
|
85
|
+
responses (JSON, text, HTML, redirects, files).
|
|
75
86
|
|
|
76
|
-
|
|
77
|
-
- **Route-Specific Middleware:** Defined in individual route files.
|
|
78
|
-
- **Validation Middleware:** Automatically validates request data using Zod schemas exported from routes.
|
|
87
|
+
- 🔄 **Middleware System:**
|
|
79
88
|
|
|
80
|
-
-
|
|
81
|
-
|
|
89
|
+
- **Global Middleware:** Runs on every request.
|
|
90
|
+
- **Route-Specific Middleware:** Defined in individual route files.
|
|
91
|
+
- **Validation Middleware:** Automatically validates request data using
|
|
92
|
+
Zod schemas exported from routes.
|
|
82
93
|
|
|
83
|
-
|
|
94
|
+
- ✅ **Zod-Based Schema Validation:**
|
|
95
|
+
Automatically validates request params, query, and body using Zod.
|
|
84
96
|
|
|
85
|
-
-
|
|
86
|
-
|
|
97
|
+
- Supports preprocessing (e.g., converting URL parameters from string to
|
|
98
|
+
number).
|
|
87
99
|
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
- Per-route metadata (summary, description, tags, operationId, deprecated, externalDocs)
|
|
91
|
-
- Auto-generated parameters and requestBody schemas (using `zod-to-json-schema` for detailed inline JSON schema definitions)
|
|
100
|
+
- 📚 **Automatic OpenAPI Specification Generation:**
|
|
101
|
+
Generates an OpenAPI 3.0 document using route metadata and Zod schemas.
|
|
92
102
|
|
|
93
|
-
-
|
|
94
|
-
|
|
103
|
+
- **OpenAPI Options:**
|
|
104
|
+
- Global metadata (title, description, version)
|
|
105
|
+
- Per-route metadata (summary, description, tags, operationId,
|
|
106
|
+
deprecated, externalDocs)
|
|
107
|
+
- Auto-generated parameters and requestBody schemas (using
|
|
108
|
+
`zod-to-json-schema` for detailed inline JSON schema definitions)
|
|
95
109
|
|
|
96
|
-
|
|
110
|
+
- 🔍 **Swagger UI Integration:**
|
|
111
|
+
Provides a `/docs` endpoint serving an interactive Swagger UI that reads
|
|
112
|
+
from `/openapi.json`.
|
|
97
113
|
|
|
98
|
-
|
|
114
|
+
## 📣 Changelog
|
|
99
115
|
|
|
100
|
-
|
|
101
|
-
- Basic support for serving static `.html` files
|
|
102
|
-
- File-based routing for pages
|
|
103
|
-
- Support for route grouping with `(group)` syntax
|
|
104
|
-
- Support for dynamic route with `[slug]` syntax
|
|
116
|
+
For detailed release notes, please refer to the [Changelog](CHANGELOG.md) file.
|
|
105
117
|
|
|
106
|
-
### Version 0.
|
|
118
|
+
### Version 0.1.3 (March 15, 2024)
|
|
107
119
|
|
|
108
|
-
-
|
|
109
|
-
-
|
|
110
|
-
-
|
|
111
|
-
-
|
|
112
|
-
-
|
|
113
|
-
-
|
|
120
|
+
- 🔄 **Bug Fixes and Improvements:**
|
|
121
|
+
- Enhanced page routing and server response handling
|
|
122
|
+
- Improved import paths configuration in tsconfig.json
|
|
123
|
+
- Updated request/response handling
|
|
124
|
+
- Enhanced server initialization process
|
|
125
|
+
- Improved native types handling
|
|
126
|
+
- Updated server response functionality
|
|
114
127
|
|
|
115
128
|
## 🎯 What's Coming Next?
|
|
116
129
|
|
|
@@ -118,17 +131,21 @@ We're actively enhancing burger-api with powerful new features:
|
|
|
118
131
|
|
|
119
132
|
### 🎨 Page Serving Enhancements (In Development)
|
|
120
133
|
|
|
121
|
-
-
|
|
122
|
-
-
|
|
123
|
-
|
|
124
|
-
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
-
|
|
130
|
-
|
|
131
|
-
|
|
134
|
+
- 🔥 **TSX Support:** Adding React/TSX rendering capabilities
|
|
135
|
+
- 🔐 **Global Middleware:** Applies to all routes for tasks like logging and
|
|
136
|
+
authentication.
|
|
137
|
+
- 🔐 **Page-Specific Middleware:** Defined in individual route files for
|
|
138
|
+
tailored processing.
|
|
139
|
+
- 🛠️ **Advanced Middleware:** More built-in middleware for common use cases:
|
|
140
|
+
- CORS handling
|
|
141
|
+
- Rate limiting
|
|
142
|
+
- Request logging
|
|
143
|
+
- Security headers
|
|
144
|
+
- 🎯 **Performance Optimizations:** Further leveraging Bun's capabilities for
|
|
145
|
+
faster page serving
|
|
146
|
+
|
|
147
|
+
Stay tuned for updates as we continue to build and improve burger-api! We're
|
|
148
|
+
committed to making it the best API framework for Bun.js.
|
|
132
149
|
|
|
133
150
|
## 📦 Installation
|
|
134
151
|
|
|
@@ -143,42 +160,44 @@ bun add burger-api
|
|
|
143
160
|
### **Basic Usage Example**
|
|
144
161
|
|
|
145
162
|
```ts
|
|
146
|
-
import { Burger } from
|
|
163
|
+
import { Burger } from 'burger-api';
|
|
147
164
|
|
|
148
165
|
// Global middleware example: a simple logger.
|
|
149
166
|
const globalLogger = async (req, res, next) => {
|
|
150
|
-
|
|
151
|
-
|
|
167
|
+
console.log(`[Global Logger] ${req.method} ${req.url}`);
|
|
168
|
+
return next();
|
|
152
169
|
};
|
|
153
170
|
|
|
154
171
|
const burger = new Burger({
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
172
|
+
title: 'My Custom API',
|
|
173
|
+
description: 'Custom API with auto-generated docs and validation',
|
|
174
|
+
apiDir: 'api',
|
|
175
|
+
globalMiddleware: [globalLogger],
|
|
176
|
+
version: '1.0.0',
|
|
177
|
+
debug: true, // Enable debug mode for detailed logging and stack trace page
|
|
161
178
|
});
|
|
162
179
|
|
|
163
180
|
// Start the server on port 4000 with a callback
|
|
164
181
|
burger.serve(4000, (port) => {
|
|
165
|
-
|
|
182
|
+
console.log(`Server is running on port ${port}`);
|
|
166
183
|
});
|
|
167
184
|
```
|
|
168
185
|
|
|
169
186
|
The `debug` option enables:
|
|
170
187
|
|
|
171
|
-
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
188
|
+
- 🔍 Interactive stack trace page at when errors occur
|
|
189
|
+
- Shows detailed error information
|
|
190
|
+
- Displays the full stack trace
|
|
191
|
+
- Highlights the exact line where the error occurred
|
|
192
|
+
- Provides request context and environment details
|
|
176
193
|
|
|
177
|
-
This is particularly useful during development to understand how your API is
|
|
194
|
+
This is particularly useful during development to understand how your API is
|
|
195
|
+
working and troubleshoot issues.
|
|
178
196
|
|
|
179
197
|
### **Recommended Project Structure**
|
|
180
198
|
|
|
181
|
-
Here's a recommended project structure that helps keep your code organized and
|
|
199
|
+
Here's a recommended project structure that helps keep your code organized and
|
|
200
|
+
maintainable:
|
|
182
201
|
|
|
183
202
|
```
|
|
184
203
|
my-api/
|
|
@@ -215,132 +234,135 @@ Here's how to implement this structure:
|
|
|
215
234
|
1. **Global Middleware** (`src/middleware/global/logger.ts`):
|
|
216
235
|
|
|
217
236
|
```ts
|
|
218
|
-
import type { BurgerRequest, BurgerResponse } from
|
|
237
|
+
import type { BurgerRequest, BurgerResponse, BurgerNext } from 'burger-api';
|
|
219
238
|
|
|
220
239
|
export const logger = async (
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
240
|
+
req: BurgerRequest,
|
|
241
|
+
res: BurgerResponse,
|
|
242
|
+
next: BurgerNext
|
|
224
243
|
) => {
|
|
225
|
-
|
|
226
|
-
|
|
244
|
+
console.log(`[${new Date().toISOString()}] ${req.method} ${req.url}`);
|
|
245
|
+
return next();
|
|
227
246
|
};
|
|
228
247
|
```
|
|
229
248
|
|
|
230
249
|
2. **Route-Specific Middleware** (`src/middleware/routes/products.ts`):
|
|
231
250
|
|
|
232
251
|
```ts
|
|
233
|
-
import type { BurgerRequest, BurgerResponse } from
|
|
252
|
+
import type { BurgerRequest, BurgerResponse, BurgerNext } from 'burger-api';
|
|
234
253
|
|
|
235
254
|
export const validateProductAccess = async (
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
255
|
+
req: BurgerRequest,
|
|
256
|
+
res: BurgerResponse,
|
|
257
|
+
next: BurgerNext
|
|
239
258
|
) => {
|
|
240
|
-
|
|
241
|
-
|
|
259
|
+
// Your middleware logic here
|
|
260
|
+
return next();
|
|
242
261
|
};
|
|
243
262
|
```
|
|
244
263
|
|
|
245
264
|
3. **Schemas** (`src/schemas/product.ts`):
|
|
246
265
|
|
|
247
266
|
```ts
|
|
248
|
-
import { z } from
|
|
267
|
+
import { z } from 'zod';
|
|
249
268
|
|
|
250
269
|
export const productSchema = {
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
|
|
270
|
+
create: z.object({
|
|
271
|
+
name: z.string().min(1),
|
|
272
|
+
price: z.number().positive(),
|
|
273
|
+
description: z.string().optional(),
|
|
274
|
+
}),
|
|
275
|
+
update: z.object({
|
|
276
|
+
name: z.string().min(1).optional(),
|
|
277
|
+
price: z.number().positive().optional(),
|
|
278
|
+
description: z.string().optional(),
|
|
279
|
+
}),
|
|
261
280
|
};
|
|
262
281
|
```
|
|
263
282
|
|
|
264
283
|
4. **Route File** (`src/api/products/route.ts`):
|
|
265
284
|
|
|
266
285
|
```ts
|
|
267
|
-
import type { BurgerRequest, BurgerResponse } from
|
|
268
|
-
import { validateProductAccess } from
|
|
269
|
-
import { productSchema } from
|
|
286
|
+
import type { BurgerRequest, BurgerResponse } from 'burger-api';
|
|
287
|
+
import { validateProductAccess } from '../../middleware/routes/products';
|
|
288
|
+
import { productSchema } from '../../schemas/product';
|
|
270
289
|
|
|
271
290
|
export const middleware = [validateProductAccess];
|
|
272
291
|
export const schema = {
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
|
|
292
|
+
post: {
|
|
293
|
+
body: productSchema.create,
|
|
294
|
+
},
|
|
295
|
+
put: {
|
|
296
|
+
body: productSchema.update,
|
|
297
|
+
},
|
|
279
298
|
};
|
|
280
299
|
|
|
281
300
|
export async function GET(req: BurgerRequest, res: BurgerResponse) {
|
|
282
|
-
|
|
301
|
+
return res.json({ message: 'List of products' });
|
|
283
302
|
}
|
|
284
303
|
|
|
285
304
|
export async function POST(req: BurgerRequest, res: BurgerResponse) {
|
|
286
|
-
|
|
287
|
-
|
|
305
|
+
const body = req.validated?.body;
|
|
306
|
+
return res.json({ message: 'Product created', data: body });
|
|
288
307
|
}
|
|
289
308
|
```
|
|
290
309
|
|
|
291
310
|
5. **Main Application** (`src/index.ts`):
|
|
292
311
|
|
|
293
312
|
```ts
|
|
294
|
-
import { Burger } from
|
|
295
|
-
import { logger } from
|
|
313
|
+
import { Burger } from 'burger-api';
|
|
314
|
+
import { logger } from './middleware/global/logger';
|
|
296
315
|
|
|
297
316
|
const burger = new Burger({
|
|
298
|
-
|
|
299
|
-
|
|
300
|
-
|
|
301
|
-
|
|
302
|
-
|
|
317
|
+
title: 'Product API',
|
|
318
|
+
description: 'API for managing products',
|
|
319
|
+
apiDir: 'api',
|
|
320
|
+
globalMiddleware: [logger],
|
|
321
|
+
version: '1.0.0',
|
|
303
322
|
});
|
|
304
323
|
|
|
305
324
|
burger.serve(4000, (port) => {
|
|
306
|
-
|
|
325
|
+
console.log(`Server is running on port ${port}`);
|
|
307
326
|
});
|
|
308
327
|
```
|
|
309
328
|
|
|
310
329
|
This structure provides several benefits:
|
|
311
330
|
|
|
312
|
-
-
|
|
313
|
-
-
|
|
314
|
-
-
|
|
315
|
-
-
|
|
316
|
-
-
|
|
331
|
+
- 🎯 Clear separation of concerns
|
|
332
|
+
- 📁 Easy to find and maintain code
|
|
333
|
+
- 🔄 Reusable components
|
|
334
|
+
- 🧹 Clean and organized codebase
|
|
335
|
+
- 📚 Better scalability
|
|
317
336
|
|
|
318
337
|
### **File-Based Routing Examples**
|
|
319
338
|
|
|
320
|
-
-
|
|
321
|
-
|
|
339
|
+
- 📄 **Static API Route:**
|
|
340
|
+
Place a file at `src/api/route.ts` to handle the root API endpoint (e.g.,
|
|
341
|
+
`/api`).
|
|
322
342
|
|
|
323
|
-
-
|
|
324
|
-
|
|
325
|
-
|
|
326
|
-
|
|
327
|
-
|
|
328
|
-
|
|
329
|
-
|
|
330
|
-
|
|
343
|
+
- 🔄 **Dynamic API Route:**
|
|
344
|
+
For routes with dynamic segments, create folders with square brackets. For
|
|
345
|
+
example:
|
|
346
|
+
```
|
|
347
|
+
src/api/product/
|
|
348
|
+
├── route.ts // Handles /api/product
|
|
349
|
+
└── [id]/
|
|
350
|
+
└── route.ts // Handles /api/product/:id
|
|
351
|
+
```
|
|
331
352
|
|
|
332
353
|
### **Page Directory Structure**
|
|
333
354
|
|
|
334
|
-
burger-api supports serving static pages alongside your API routes. Here's how
|
|
355
|
+
burger-api supports serving static pages alongside your API routes. Here's how
|
|
356
|
+
to use the `pageDir` feature:
|
|
335
357
|
|
|
336
358
|
```ts
|
|
337
359
|
const burger = new Burger({
|
|
338
|
-
|
|
339
|
-
|
|
340
|
-
|
|
341
|
-
|
|
342
|
-
|
|
343
|
-
|
|
360
|
+
title: 'My Custom API',
|
|
361
|
+
description: 'Custom API with auto-generated docs and validation',
|
|
362
|
+
apiDir: 'api',
|
|
363
|
+
pageDir: 'pages', // Enable page serving from the pages directory
|
|
364
|
+
globalMiddleware: [globalLogger],
|
|
365
|
+
version: '1.0.0',
|
|
344
366
|
});
|
|
345
367
|
```
|
|
346
368
|
|
|
@@ -361,14 +383,14 @@ my-api/
|
|
|
361
383
|
|
|
362
384
|
#### **Page Routing Features:**
|
|
363
385
|
|
|
364
|
-
-
|
|
365
|
-
-
|
|
366
|
-
|
|
367
|
-
-
|
|
368
|
-
|
|
369
|
-
-
|
|
370
|
-
|
|
371
|
-
|
|
386
|
+
- 📄 **Index Pages:** Files named `index.html` serve as directory index pages
|
|
387
|
+
- 🔄 **Clean URLs:**
|
|
388
|
+
- `/pages/about.html` → `/about`
|
|
389
|
+
- 📁 **Dynamic Routes:** Use `[param]` syntax in folder names
|
|
390
|
+
- `/pages/products/[id]/index.html` → `/products/:id`
|
|
391
|
+
- 🎯 **Route Grouping:** Use `(group)` syntax for logical grouping
|
|
392
|
+
- `/pages/(auth)/login.html` → `/login`
|
|
393
|
+
- `/pages/(auth)/register.html` → `/register`
|
|
372
394
|
|
|
373
395
|
#### **Example Page Structure:**
|
|
374
396
|
|
|
@@ -376,123 +398,133 @@ my-api/
|
|
|
376
398
|
<!-- pages/products/[id]/index.html -->
|
|
377
399
|
<!DOCTYPE html>
|
|
378
400
|
<html>
|
|
379
|
-
|
|
380
|
-
|
|
381
|
-
|
|
382
|
-
|
|
383
|
-
|
|
384
|
-
|
|
385
|
-
|
|
401
|
+
<head>
|
|
402
|
+
<title>Product Details</title>
|
|
403
|
+
</head>
|
|
404
|
+
<body>
|
|
405
|
+
<h1>Product Details</h1>
|
|
406
|
+
<!-- Content here -->
|
|
407
|
+
</body>
|
|
386
408
|
</html>
|
|
387
409
|
```
|
|
388
410
|
|
|
389
411
|
### **Route File Example**
|
|
390
412
|
|
|
391
|
-
Below is an example route file demonstrating schema validation, route-specific
|
|
413
|
+
Below is an example route file demonstrating schema validation, route-specific
|
|
414
|
+
middleware, and OpenAPI metadata.
|
|
392
415
|
|
|
393
416
|
```ts
|
|
394
417
|
// examples/demo/api/product/[id]/route.ts
|
|
395
418
|
|
|
396
|
-
import { z } from
|
|
397
|
-
import type { BurgerRequest, BurgerResponse } from
|
|
419
|
+
import { z } from 'zod';
|
|
420
|
+
import type { BurgerRequest, BurgerResponse, BurgerNext } from 'burger-api';
|
|
398
421
|
|
|
399
422
|
// OpenAPI metadata for this route
|
|
400
423
|
export const openapi = {
|
|
401
|
-
|
|
402
|
-
|
|
403
|
-
|
|
404
|
-
|
|
405
|
-
|
|
406
|
-
|
|
424
|
+
get: {
|
|
425
|
+
summary: 'Get Product Details',
|
|
426
|
+
description: 'Retrieves product details by product ID.',
|
|
427
|
+
tags: ['Product'],
|
|
428
|
+
operationId: 'getProductDetails',
|
|
429
|
+
},
|
|
407
430
|
};
|
|
408
431
|
|
|
409
432
|
// Validation Schemas for GET and POST requests
|
|
410
433
|
export const schema = {
|
|
411
|
-
|
|
412
|
-
|
|
413
|
-
|
|
414
|
-
|
|
415
|
-
|
|
416
|
-
|
|
417
|
-
|
|
418
|
-
|
|
419
|
-
|
|
420
|
-
|
|
421
|
-
|
|
422
|
-
|
|
423
|
-
|
|
424
|
-
|
|
425
|
-
|
|
426
|
-
|
|
427
|
-
|
|
434
|
+
get: {
|
|
435
|
+
params: z.object({
|
|
436
|
+
id: z.preprocess(
|
|
437
|
+
(val) => (typeof val === 'string' ? parseInt(val, 10) : val),
|
|
438
|
+
z.number().min(1, 'ID is required')
|
|
439
|
+
),
|
|
440
|
+
}),
|
|
441
|
+
query: z.object({
|
|
442
|
+
search: z.string().optional(),
|
|
443
|
+
}),
|
|
444
|
+
},
|
|
445
|
+
post: {
|
|
446
|
+
body: z.object({
|
|
447
|
+
name: z.string().min(1, 'Name is required'),
|
|
448
|
+
price: z.number().positive('Price must be positive'),
|
|
449
|
+
}),
|
|
450
|
+
},
|
|
428
451
|
};
|
|
429
452
|
|
|
430
453
|
// Route-specific middleware
|
|
431
454
|
export const middleware = [
|
|
432
|
-
|
|
433
|
-
|
|
434
|
-
|
|
435
|
-
|
|
436
|
-
) => {
|
|
437
|
-
console.log("[Product Middleware] Executing product route middleware");
|
|
438
|
-
return next();
|
|
439
|
-
},
|
|
455
|
+
async (req: BurgerRequest, res: BurgerResponse, next: BurgerNext) => {
|
|
456
|
+
console.log('[Product Middleware] Executing product route middleware');
|
|
457
|
+
return next();
|
|
458
|
+
},
|
|
440
459
|
];
|
|
441
460
|
|
|
442
461
|
// GET handler: returns product details
|
|
443
462
|
export async function GET(
|
|
444
|
-
|
|
445
|
-
|
|
446
|
-
|
|
463
|
+
req: BurgerRequest,
|
|
464
|
+
res: BurgerResponse,
|
|
465
|
+
params: { id: number }
|
|
447
466
|
) {
|
|
448
|
-
|
|
449
|
-
|
|
450
|
-
|
|
451
|
-
|
|
452
|
-
|
|
453
|
-
|
|
454
|
-
|
|
455
|
-
|
|
467
|
+
console.log('[GET] Product route invoked');
|
|
468
|
+
const validatedParams = (req.validated?.params as { id: number }) || params;
|
|
469
|
+
const query =
|
|
470
|
+
req.validated?.query || Object.fromEntries(req.query.entries());
|
|
471
|
+
return res.json({
|
|
472
|
+
id: validatedParams.id,
|
|
473
|
+
query,
|
|
474
|
+
name: 'Sample Product',
|
|
475
|
+
});
|
|
456
476
|
}
|
|
457
477
|
|
|
458
478
|
// POST handler: creates a new product
|
|
459
479
|
export async function POST(req: BurgerRequest, res: BurgerResponse) {
|
|
460
|
-
|
|
461
|
-
|
|
462
|
-
|
|
480
|
+
console.log('[POST] Product route invoked');
|
|
481
|
+
const body = req.validated?.body || (await req.json());
|
|
482
|
+
return res.json(body);
|
|
463
483
|
}
|
|
464
484
|
```
|
|
465
485
|
|
|
466
486
|
### **API Documentation Endpoints**
|
|
467
487
|
|
|
468
|
-
-
|
|
469
|
-
|
|
488
|
+
- 📚 **OpenAPI JSON:**
|
|
489
|
+
Access `http://localhost:4000/openapi.json` to view the auto-generated
|
|
490
|
+
OpenAPI specification.
|
|
470
491
|
|
|
471
|
-
-
|
|
472
|
-
|
|
492
|
+
- 🔍 **Swagger UI:**
|
|
493
|
+
Access `http://localhost:4000/docs` to view interactive API documentation
|
|
494
|
+
via Swagger UI.
|
|
473
495
|
|
|
474
496
|
## 🤝 Contributing
|
|
475
497
|
|
|
476
|
-
We welcome contributions from the community! If you have suggestions or
|
|
498
|
+
We welcome contributions from the community! If you have suggestions or
|
|
499
|
+
improvements, please open an issue or submit a pull request. Let's build
|
|
500
|
+
something amazing together.
|
|
477
501
|
|
|
478
502
|
## 📄 License
|
|
479
503
|
|
|
480
|
-
This project is licensed under the MIT License - see the [LICENSE](LICENSE) file
|
|
504
|
+
This project is licensed under the MIT License - see the [LICENSE](LICENSE) file
|
|
505
|
+
for details.
|
|
481
506
|
|
|
482
|
-
The MIT License is a permissive license that is short and to the point. It lets
|
|
507
|
+
The MIT License is a permissive license that is short and to the point. It lets
|
|
508
|
+
people do anything they want with your code as long as they provide attribution
|
|
509
|
+
back to you and don't hold you liable.
|
|
483
510
|
|
|
484
511
|
## ❓ FAQs & Additional Resources
|
|
485
512
|
|
|
486
|
-
-
|
|
487
|
-
|
|
513
|
+
- 🔄 **How do I add custom middleware?**
|
|
514
|
+
You can pass an array of global middleware in the Burger options or export
|
|
515
|
+
route-specific middleware in your route files.
|
|
488
516
|
|
|
489
|
-
-
|
|
490
|
-
|
|
517
|
+
- 📁 **How does file-based routing work?**
|
|
518
|
+
Place your route files under `src/api/` or just `/api` using folder and file
|
|
519
|
+
naming conventions (e.g., `[id]` for dynamic routes).
|
|
491
520
|
|
|
492
|
-
-
|
|
493
|
-
|
|
521
|
+
- ✅ **How is validation handled?**
|
|
522
|
+
burger-api uses Zod for schema validation. Define your schemas in your route
|
|
523
|
+
files and they are automatically used to validate incoming requests.
|
|
494
524
|
|
|
495
|
-
-
|
|
496
|
-
|
|
525
|
+
- 📚 **How can I customize the OpenAPI documentation?**
|
|
526
|
+
Override the default auto-generated summaries, descriptions, tags, and
|
|
527
|
+
operationIds by exporting an `openapi` object in your route files.
|
|
497
528
|
|
|
498
|
-
_burger-api_ aims to revolutionize your API development experience with
|
|
529
|
+
_burger-api_ aims to revolutionize your API development experience with
|
|
530
|
+
simplicity, speed, and cutting-edge features. Happy coding! 🚀
|