burger-api 0.0.37 โ†’ 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 (2) hide show
  1. package/README.md +139 -0
  2. package/package.json +1 -1
package/README.md CHANGED
@@ -104,6 +104,7 @@ const burger = new Burger({
104
104
  apiDir: "api",
105
105
  globalMiddleware: [globalLogger],
106
106
  version: "1.0.0",
107
+ debug: true, // Enable debug mode for detailed logging and stack trace page
107
108
  });
108
109
 
109
110
  // Start the server on port 4000 with a callback
@@ -112,6 +113,144 @@ burger.serve(4000, (port) => {
112
113
  });
113
114
  ```
114
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
+
115
254
  ### **File-Based Routing Examples**
116
255
 
117
256
  - ๐Ÿ“„ **Static API Route:**
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "burger-api",
3
- "version": "0.0.37",
3
+ "version": "0.0.38",
4
4
  "author": "Isfhan Ahmed",
5
5
  "type": "module",
6
6
  "module": "dist/src/index.js",