burger-api 0.0.36 → 0.0.38

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/LICENSE +21 -0
  2. package/README.md +193 -41
  3. package/package.json +82 -76
package/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2024 Isfhan Ahmed
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md CHANGED
@@ -1,61 +1,60 @@
1
- # burger-api [![Under Development](https://img.shields.io/badge/under%20development-red.svg)](https://github.com/isfhan/burger-api)
1
+ # 🍔 burger-api [![Under Development](https://img.shields.io/badge/under%20development-red.svg)](https://github.com/isfhan/burger-api) [![License](https://img.shields.io/badge/license-MIT-blue.svg)](LICENSE) [![Bun](https://img.shields.io/badge/Bun-1.0.0-black?logo=bun)](https://bun.sh)
2
2
 
3
- **burger-api** is a modern, open source API framework built on [Bun.js](https://bun.sh). It combines the simplicity of file-based routing (inspired by Next.js) with powerful features like built-in middleware, Zod-based schema validation, and automatic OpenAPI generation. Designed for high performance and ease-of-use, burger-api leverages Bun’s native modules to deliver blazing-fast API responses while keeping your codebase clean and maintainable.
3
+ **burger-api** is a modern, open source API framework built on [Bun.js](https://bun.sh). It combines the simplicity of file-based routing with powerful features like built-in middleware, Zod-based schema validation, and automatic OpenAPI generation. Designed for high performance and ease-of-use, burger-api leverages Bun's native modules to deliver blazing-fast API responses while keeping your codebase clean and maintainable.
4
4
 
5
5
  **This project is under active development and should not be used in production yet.**
6
6
 
7
- ## Overview
7
+ ## 🚀 Overview
8
8
 
9
9
  burger-api is built to offer a robust developer experience through:
10
10
 
11
- - **Bun-Native Performance:**
12
- Leverages Bun’s high-performance HTTP server.
11
+ - ⚡ **Bun-Native Performance:**
12
+ Leverages Bun's high-performance HTTP server.
13
13
 
14
- - **File-Based Routing:**
15
- Automatically registers API and page routes from your file structure using a clear naming convention.
14
+ - 📁 **File-Based Routing:**
15
+ Automatically registers API routes from your file structure using a clear naming convention.
16
16
 
17
- - **Middleware Architecture:**
17
+ - 🔄 **Middleware Architecture:**
18
18
  Supports both global and route-specific middleware with a familiar `req, res, next` pattern.
19
19
 
20
- - **Type-Safe Validation:**
20
+ - ✅ **Type-Safe Validation:**
21
21
  Utilizes Zod for request validation, ensuring full type safety and automatic error reporting.
22
22
 
23
- - **Automatic OpenAPI Generation:**
23
+ - 📚 **Automatic OpenAPI Generation:**
24
24
  Generates a complete OpenAPI 3.0 specification (with support for tags, summaries, descriptions, operationId, deprecated status, externalDocs, and more) directly from your routes and Zod schemas.
25
25
 
26
- - **Swagger UI Integration:**
26
+ - 🔍 **Swagger UI Integration:**
27
27
  Provides an out-of-the-box Swagger UI endpoint for interactive API documentation.
28
28
 
29
- ## Features
29
+ ## ✨ Features
30
30
 
31
31
  ### Core Features
32
32
 
33
- - **Bun-Native HTTP Server:**
34
- Built on Bun’s native APIs for exceptional performance.
33
+ - ⚡ **Bun-Native HTTP Server:**
34
+ Built on Bun's native APIs for exceptional performance.
35
35
 
36
- - **File-Based Routing Engine:**
36
+ - 📁 **File-Based Routing Engine:**
37
37
  Automatically scans directories to register routes.
38
38
 
39
39
  - Supports dynamic routes via folder names like `[id]` for `/api/product/:id`.
40
- - Differentiates between API routes (`route.ts`) and page routes (`page.ts`).
41
40
 
42
- - **Request & Response Enhancements:**
41
+ - 🛠️ **Request & Response Enhancements:**
43
42
 
44
43
  - `BurgerRequest` extends the native Request to include query, params, and validated data.
45
44
  - `BurgerResponse` provides methods to set headers, status, and build responses (JSON, text, HTML, redirects, files).
46
45
 
47
- - **Middleware System:**
46
+ - 🔄 **Middleware System:**
48
47
 
49
48
  - **Global Middleware:** Runs on every request.
50
49
  - **Route-Specific Middleware:** Defined in individual route files.
51
50
  - **Validation Middleware:** Automatically validates request data using Zod schemas exported from routes.
52
51
 
53
- - **Zod-Based Schema Validation:**
52
+ - ✅ **Zod-Based Schema Validation:**
54
53
  Automatically validates request params, query, and body using Zod.
55
54
 
56
55
  - Supports preprocessing (e.g., converting URL parameters from string to number).
57
56
 
58
- - **Automatic OpenAPI Specification Generation:**
57
+ - 📚 **Automatic OpenAPI Specification Generation:**
59
58
  Generates an OpenAPI 3.0 document using route metadata and Zod schemas.
60
59
 
61
60
  - **OpenAPI Options:**
@@ -63,10 +62,22 @@ burger-api is built to offer a robust developer experience through:
63
62
  - Per-route metadata (summary, description, tags, operationId, deprecated, externalDocs)
64
63
  - Auto-generated parameters and requestBody schemas (using `zod-to-json-schema` for detailed inline JSON schema definitions)
65
64
 
66
- - **Swagger UI Integration:**
65
+ - 🔍 **Swagger UI Integration:**
67
66
  Provides a `/docs` endpoint serving an interactive Swagger UI that reads from `/openapi.json`.
68
67
 
69
- ## Installation
68
+ ## 🎯 What's Coming Next?
69
+
70
+ We're enhancing burger-api to make it even more powerful! While our core focus remains on building fast, type-safe APIs, we're adding the ability to serve simple pages when needed:
71
+
72
+ ### 🎨 Simple Page Serving (Coming Soon!)
73
+ - Optional page serving capability for simple views
74
+ - Perfect for API documentation, admin panels, or simple dashboards
75
+ - Not meant to replace full-stack frameworks like Next.js
76
+ - For complex frontend applications, we recommend using dedicated frontend frameworks
77
+
78
+ Stay tuned for updates as we continue to build and improve burger-api! We're committed to making it the best API framework for Bun.js.
79
+
80
+ ## 📦 Installation
70
81
 
71
82
  Install burger-api via bun:
72
83
 
@@ -74,12 +85,12 @@ Install burger-api via bun:
74
85
  bun add burger-api
75
86
  ```
76
87
 
77
- ## How to Use burger-api
88
+ ## 🚀 How to Use burger-api
78
89
 
79
90
  ### **Basic Usage Example**
80
91
 
81
92
  ```ts
82
- import { Burger, setDir } from "burger-api";
93
+ import { Burger } from "burger-api";
83
94
 
84
95
  // Global middleware example: a simple logger.
85
96
  const globalLogger = async (req, res, next) => {
@@ -88,11 +99,12 @@ const globalLogger = async (req, res, next) => {
88
99
  };
89
100
 
90
101
  const burger = new Burger({
91
- apiDir: setDir(__dirname, "api"),
92
102
  title: "My Custom API",
93
103
  description: "Custom API with auto-generated docs and validation",
94
- version: "1.0.0",
104
+ apiDir: "api",
95
105
  globalMiddleware: [globalLogger],
106
+ version: "1.0.0",
107
+ debug: true, // Enable debug mode for detailed logging and stack trace page
96
108
  });
97
109
 
98
110
  // Start the server on port 4000 with a callback
@@ -101,12 +113,150 @@ burger.serve(4000, (port) => {
101
113
  });
102
114
  ```
103
115
 
116
+ The `debug` option enables:
117
+ - 🔍 Interactive stack trace page at when errors occur
118
+ - Shows detailed error information
119
+ - Displays the full stack trace
120
+ - Highlights the exact line where the error occurred
121
+ - Provides request context and environment details
122
+
123
+ This is particularly useful during development to understand how your API is working and troubleshoot issues.
124
+
125
+ ### **Recommended Project Structure**
126
+
127
+ Here's a recommended project structure that helps keep your code organized and maintainable:
128
+
129
+ ```
130
+ my-api/
131
+ ├── src/
132
+ │ ├── api/ # API routes
133
+ │ │ ├── products/
134
+ │ │ │ ├── route.ts # Product routes
135
+ │ │ │ └── [id]/
136
+ │ │ │ └── route.ts # Product detail routes
137
+ │ │ └── users/
138
+ │ │ └── route.ts # User routes
139
+ │ ├── middleware/ # Middleware
140
+ │ │ ├── global/ # Global middleware
141
+ │ │ │ ├── logger.ts
142
+ │ │ │ └── auth.ts
143
+ │ │ └── routes/ # Route-specific middleware
144
+ │ │ ├── products.ts
145
+ │ │ └── users.ts
146
+ │ ├── schemas/ # Zod schemas
147
+ │ │ ├── product.ts
148
+ │ │ └── user.ts
149
+ │ ├── utils/ # Utility functions
150
+ │ │ ├── errors.ts
151
+ │ │ └── helpers.ts
152
+ │ └── index.ts # Main application file
153
+ ├── package.json
154
+ └── tsconfig.json
155
+ ```
156
+
157
+ ### **Example Implementation**
158
+
159
+ Here's how to implement this structure:
160
+
161
+ 1. **Global Middleware** (`src/middleware/global/logger.ts`):
162
+ ```ts
163
+ import type { BurgerRequest, BurgerResponse } from "burger-api";
164
+
165
+ export const logger = async (req: BurgerRequest, res: BurgerResponse, next: () => Promise<Response>) => {
166
+ console.log(`[${new Date().toISOString()}] ${req.method} ${req.url}`);
167
+ return next();
168
+ };
169
+ ```
170
+
171
+ 2. **Route-Specific Middleware** (`src/middleware/routes/products.ts`):
172
+ ```ts
173
+ import type { BurgerRequest, BurgerResponse } from "burger-api";
174
+
175
+ export const validateProductAccess = async (
176
+ req: BurgerRequest,
177
+ res: BurgerResponse,
178
+ next: () => Promise<Response>
179
+ ) => {
180
+ // Your middleware logic here
181
+ return next();
182
+ };
183
+ ```
184
+
185
+ 3. **Schemas** (`src/schemas/product.ts`):
186
+ ```ts
187
+ import { z } from "zod";
188
+
189
+ export const productSchema = {
190
+ create: z.object({
191
+ name: z.string().min(1),
192
+ price: z.number().positive(),
193
+ description: z.string().optional(),
194
+ }),
195
+ update: z.object({
196
+ name: z.string().min(1).optional(),
197
+ price: z.number().positive().optional(),
198
+ description: z.string().optional(),
199
+ }),
200
+ };
201
+ ```
202
+
203
+ 4. **Route File** (`src/api/products/route.ts`):
204
+ ```ts
205
+ import type { BurgerRequest, BurgerResponse } from "burger-api";
206
+ import { validateProductAccess } from "../../middleware/routes/products";
207
+ import { productSchema } from "../../schemas/product";
208
+
209
+ export const middleware = [validateProductAccess];
210
+ export const schema = {
211
+ post: {
212
+ body: productSchema.create,
213
+ },
214
+ put: {
215
+ body: productSchema.update,
216
+ },
217
+ };
218
+
219
+ export async function GET(req: BurgerRequest, res: BurgerResponse) {
220
+ return res.json({ message: "List of products" });
221
+ }
222
+
223
+ export async function POST(req: BurgerRequest, res: BurgerResponse) {
224
+ const body = req.validated?.body;
225
+ return res.json({ message: "Product created", data: body });
226
+ }
227
+ ```
228
+
229
+ 5. **Main Application** (`src/index.ts`):
230
+ ```ts
231
+ import { Burger } from "burger-api";
232
+ import { logger } from "./middleware/global/logger";
233
+
234
+ const burger = new Burger({
235
+ title: "Product API",
236
+ description: "API for managing products",
237
+ apiDir: "api",
238
+ globalMiddleware: [logger],
239
+ version: "1.0.0",
240
+ });
241
+
242
+ burger.serve(4000, (port) => {
243
+ console.log(`Server is running on port ${port}`);
244
+ });
245
+ ```
246
+
247
+ This structure provides several benefits:
248
+ - 🎯 Clear separation of concerns
249
+ - 📁 Easy to find and maintain code
250
+ - 🔄 Reusable components
251
+ - 🧹 Clean and organized codebase
252
+ - 📚 Better scalability
253
+
104
254
  ### **File-Based Routing Examples**
105
255
 
106
- - **Static API Route:**
256
+ - 📄 **Static API Route:**
107
257
  Place a file at `src/api/route.ts` to handle the root API endpoint (e.g., `/api`).
108
258
 
109
- - **Dynamic API Route:**
259
+ - 🔄 **Dynamic API Route:**
110
260
  For routes with dynamic segments, create folders with square brackets. For example:
111
261
  ```
112
262
  src/api/product/
@@ -194,32 +344,34 @@ export async function POST(req: BurgerRequest, res: BurgerResponse) {
194
344
 
195
345
  ### **API Documentation Endpoints**
196
346
 
197
- - **OpenAPI JSON:**
347
+ - 📚 **OpenAPI JSON:**
198
348
  Access `http://localhost:4000/openapi.json` to view the auto-generated OpenAPI specification.
199
349
 
200
- - **Swagger UI:**
350
+ - 🔍 **Swagger UI:**
201
351
  Access `http://localhost:4000/docs` to view interactive API documentation via Swagger UI.
202
352
 
203
- ## Contributing
353
+ ## 🤝 Contributing
354
+
355
+ We welcome contributions from the community! If you have suggestions or improvements, please open an issue or submit a pull request. Let's build something amazing together.
204
356
 
205
- We welcome contributions from the community! If you have suggestions or improvements, please open an issue or submit a pull request. Let’s build something amazing together.
357
+ ## 📄 License
206
358
 
207
- ## License
359
+ This project is licensed under the MIT License - see the [LICENSE](LICENSE) file for details.
208
360
 
209
- This project is licensed under the MIT License.
361
+ The MIT License is a permissive license that is short and to the point. It lets people do anything they want with your code as long as they provide attribution back to you and don't hold you liable.
210
362
 
211
- ## FAQs & Additional Resources
363
+ ## ❓ FAQs & Additional Resources
212
364
 
213
- - **How do I add custom middleware?**
365
+ - 🔄 **How do I add custom middleware?**
214
366
  You can pass an array of global middleware in the Burger options or export route-specific middleware in your route files.
215
367
 
216
- - **How does file-based routing work?**
217
- Place your route files under `src/api/` (or `src/pages/`) using folder and file naming conventions (e.g., `[id]` for dynamic routes).
368
+ - 📁 **How does file-based routing work?**
369
+ Place your route files under `src/api/` or just `/api` using folder and file naming conventions (e.g., `[id]` for dynamic routes).
218
370
 
219
- - **How is validation handled?**
371
+ - ✅ **How is validation handled?**
220
372
  burger-api uses Zod for schema validation. Define your schemas in your route files and they are automatically used to validate incoming requests.
221
373
 
222
- - **How can I customize the OpenAPI documentation?**
374
+ - 📚 **How can I customize the OpenAPI documentation?**
223
375
  Override the default auto-generated summaries, descriptions, tags, and operationIds by exporting an `openapi` object in your route files.
224
376
 
225
- _burger-api_ aims to revolutionize your API development experience with simplicity, speed, and cutting-edge features. Happy coding!
377
+ _burger-api_ aims to revolutionize your API development experience with simplicity, speed, and cutting-edge features. Happy coding! 🚀
package/package.json CHANGED
@@ -1,76 +1,82 @@
1
- {
2
- "name": "burger-api",
3
- "version": "0.0.36",
4
- "author": "Isfhan Ahmed",
5
- "type": "module",
6
- "module": "dist/src/index.js",
7
- "types": "dist/src/index.d.ts",
8
- "files": [
9
- "dist"
10
- ],
11
- "scripts": {
12
- "typecheck": "tsc --noEmit",
13
- "build": "tsc --project tsconfig.build.json",
14
- "test": "bun test",
15
- "dev": "bun run examples/native-server/index.ts"
16
- },
17
- "keywords": [
18
- "burger-api",
19
- "burger-api-framework",
20
- "bun",
21
- "bunjs",
22
- "bun-framework",
23
- "fast-backend",
24
- "javascript-backend",
25
- "high-performance",
26
- "esm",
27
- "typescript",
28
- "file-based routing",
29
- "middleware",
30
- "zod-validation",
31
- "openapi",
32
- "swagger-ui",
33
- "declarative-routing",
34
- "api-framework",
35
- "modern-backend",
36
- "backend-framework",
37
- "server",
38
- "bun-native",
39
- "js-backend",
40
- "fast-api",
41
- "scalable-backend",
42
- "backend-development",
43
- "modern-api",
44
- "api-development",
45
- "backend-engine",
46
- "efficient",
47
- "minimalist-backend",
48
- "bun-powered",
49
- "open-source",
50
- "typescript-backend",
51
- "performance",
52
- "rapid-development",
53
- "web-framework",
54
- "api-server",
55
- "dynamic-routing",
56
- "backend-routing",
57
- "extensible",
58
- "modular",
59
- "declarative-api",
60
- "lightweight",
61
- "contemporary-backend",
62
- "fast-javascript",
63
- "bun-js-framework",
64
- "bun-backend"
65
- ],
66
- "dependencies": {
67
- "zod": "^3.24.2",
68
- "zod-to-json-schema": "^3.24.2"
69
- },
70
- "devDependencies": {
71
- "@types/bun": "latest"
72
- },
73
- "peerDependencies": {
74
- "typescript": "^5.0.0"
75
- }
76
- }
1
+ {
2
+ "name": "burger-api",
3
+ "version": "0.0.38",
4
+ "author": "Isfhan Ahmed",
5
+ "type": "module",
6
+ "module": "dist/src/index.js",
7
+ "types": "dist/src/index.d.ts",
8
+ "files": [
9
+ "dist"
10
+ ],
11
+ "scripts": {
12
+ "typecheck": "tsc --noEmit",
13
+ "build": "tsc --project tsconfig.build.json",
14
+ "test": "bun test",
15
+ "dev": "bun run examples/native-server/index.ts"
16
+ },
17
+ "keywords": [
18
+ "burger-api",
19
+ "burger-api-framework",
20
+ "bun",
21
+ "bunjs",
22
+ "bun-framework",
23
+ "fast-backend",
24
+ "javascript-backend",
25
+ "high-performance",
26
+ "esm",
27
+ "typescript",
28
+ "openapi",
29
+ "swagger-ui",
30
+ "declarative-routing",
31
+ "api-framework",
32
+ "modern-backend",
33
+ "backend-framework",
34
+ "server",
35
+ "bun-native",
36
+ "js-backend",
37
+ "fast-api",
38
+ "scalable-backend",
39
+ "backend-development",
40
+ "modern-api",
41
+ "api-development",
42
+ "backend-engine",
43
+ "efficient",
44
+ "minimalist-backend",
45
+ "bun-powered",
46
+ "open-source",
47
+ "typescript-backend",
48
+ "performance",
49
+ "rapid-development",
50
+ "web-framework",
51
+ "api-server",
52
+ "dynamic-routing",
53
+ "backend-routing",
54
+ "extensible",
55
+ "modular",
56
+ "declarative-api",
57
+ "lightweight",
58
+ "contemporary-backend",
59
+ "fast-javascript",
60
+ "bun-js-framework",
61
+ "bun-backend"
62
+ ],
63
+ "license": "MIT",
64
+ "repository": {
65
+ "type": "git",
66
+ "url": "git+https://github.com/isfhan/burger-api.git"
67
+ },
68
+ "bugs": {
69
+ "url": "https://github.com/isfhan/burger-api/issues"
70
+ },
71
+ "homepage": "https://github.com/isfhan/burger-api#readme",
72
+ "dependencies": {
73
+ "zod": "^3.24.2",
74
+ "zod-to-json-schema": "^3.24.2"
75
+ },
76
+ "devDependencies": {
77
+ "@types/bun": "latest"
78
+ },
79
+ "peerDependencies": {
80
+ "typescript": "^5.0.0"
81
+ }
82
+ }