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.
- package/README.md +139 -0
- 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:**
|