burger-api 0.5.0 → 0.5.2

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 CHANGED
@@ -5,7 +5,7 @@
5
5
  [![Under Development](https://img.shields.io/badge/under%20development-red.svg)](https://github.com/isfhan/burger-api)
6
6
  [![License](https://img.shields.io/badge/license-MIT-blue.svg)](LICENSE)
7
7
  [![Bun](https://img.shields.io/badge/Bun-1.2.20-black?logo=bun)](https://bun.sh)
8
- [![Version](https://img.shields.io/badge/version-0.5.0-green.svg)](https://github.com/isfhan/burger-api/releases)
8
+ [![Version](https://img.shields.io/badge/version-0.5.1-green.svg)](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
@@ -66,7 +66,13 @@ burger-api is built to offer a robust developer experience through:
66
66
 
67
67
  ## 📣 Changelog
68
68
 
69
- ### Latest Version: 0.5.0 (November 1, 2025)
69
+ ### Latest Version: 0.5.2 (November 9, 2025)
70
+
71
+ - 🔧 **Internal Improvements:**
72
+ - Refactored wildcard parameter extraction logic into reusable utility functions
73
+ - Added test suites and README files for all example projects
74
+
75
+ ### Previous Version: 0.5.0 (November 1, 2025)
70
76
 
71
77
  - 🌟 **Feature:** Auto-injected OPTIONS handler for CORS preflight:
72
78
 
package/dist/src/index.js CHANGED
@@ -7,6 +7,7 @@ import { swaggerHtml } from './core/swagger-ui';
7
7
  // Import utils
8
8
  import { collectRoutes } from './utils/index';
9
9
  import { METHOD_NOT_ALLOWED, NOT_FOUND, OPENAPI_ERROR } from './utils/response';
10
+ import { extractPathnameFromUrl, extractWildcardParams, } from './utils/wildcard';
10
11
  // Import middleware
11
12
  import { createValidationMiddleware } from './middleware/validator';
12
13
  export class Burger {
@@ -157,69 +158,8 @@ export class Burger {
157
158
  routes[path] = (request) => {
158
159
  // Extract wildcard params if this is a wildcard route
159
160
  if (isWildcard) {
160
- // Step 1: Get the pathname from the URL
161
- // Example: "http://localhost:4000/api/users/123/profile?id=1" → "/api/users/123/profile"
162
- const url = request.url;
163
- // Find where the path starts (after protocol and domain)
164
- // Example: "http://localhost:4000/api/..." → protocolEnd = 4 (after "http")
165
- const protocolEnd = url.indexOf('://');
166
- // Find first "/" after the domain
167
- // Example: "http://localhost:4000/api/..." → pathStart = 21 (the "/" before "api")
168
- const pathStart = url.indexOf('/', protocolEnd + 3);
169
- // Find where query parameters start (if any)
170
- // Example: "/api/users/123?id=1" → pathEnd = 15 (at the "?")
171
- const pathEnd = url.indexOf('?', pathStart);
172
- // Extract just the pathname without query parameters
173
- // Example: "/api/users/123/profile"
174
- const pathname = pathEnd === -1
175
- ? url.substring(pathStart) // No query params
176
- : url.substring(pathStart, pathEnd); // Has query params, stop at "?"
177
- // Step 2: Extract wildcard segments efficiently
178
- // Example route: "/api/users/:userId/*" has baseSegmentCount = 3
179
- // - Segment 0: "api"
180
- // - Segment 1: "users"
181
- // - Segment 2: ":userId" (or "123" in actual URL)
182
- // - Segment 3+: wildcard segments we want!
183
- if (baseSegmentCount === 0) {
184
- // Fast path: The entire path is wildcard
185
- // Example: "/api/docs/*" where all segments after domain are wildcards
186
- // Just split everything and we're done!
187
- request.wildcardParams = pathname
188
- .split('/')
189
- .filter(Boolean);
190
- }
191
- else {
192
- // Optimized path: Skip base segments and only collect wildcard segments
193
- // Example: "/api/users/123/settings/privacy"
194
- // - We need to skip 3 segments (api, users, 123)
195
- // - Only collect: ["settings", "privacy"]
196
- const wildcardParams = [];
197
- let segmentCount = 0; // Counts how many segments we've seen
198
- let start = pathname[0] === '/' ? 1 : 0; // Start after leading "/" if present
199
- // Loop through each character in the pathname
200
- // When we hit "/" or end of string, we know we found a segment
201
- for (let i = start; i <= pathname.length; i++) {
202
- // Found end of a segment (either "/" or end of string)
203
- if (i === pathname.length ||
204
- pathname[i] === '/') {
205
- // Make sure it's not an empty segment (from double slashes)
206
- if (i > start) {
207
- // Only save segments AFTER we've skipped the base segments
208
- // Example: If baseSegmentCount=3, save segment 3, 4, 5, etc.
209
- if (segmentCount >= baseSegmentCount) {
210
- // Extract this segment from the pathname
211
- // Example: pathname.substring(5, 13) might give "settings"
212
- wildcardParams.push(pathname.substring(start, i));
213
- }
214
- segmentCount++; // Increment count for next segment
215
- }
216
- start = i + 1; // Move start position to character after "/"
217
- }
218
- }
219
- // Done! wildcardParams now contains only the wildcard segments
220
- // Example: ["settings", "privacy"] instead of ["api", "users", "123", "settings", "privacy"]
221
- request.wildcardParams = wildcardParams;
222
- }
161
+ const pathname = extractPathnameFromUrl(request.url);
162
+ extractWildcardParams(request, pathname, baseSegmentCount);
223
163
  }
224
164
  const handler = handlers[request.method];
225
165
  return handler ? handler(request) : this.METHOD_NOT_ALLOWED;
@@ -260,69 +200,8 @@ export class Burger {
260
200
  routes[path] = (request) => {
261
201
  // Extract wildcard params if this is a wildcard route
262
202
  if (isWildcard) {
263
- // Step 1: Get the pathname from the URL
264
- // Example: "http://localhost:4000/api/users/123/profile?id=1" → "/api/users/123/profile"
265
- const url = request.url;
266
- // Find where the path starts (after protocol and domain)
267
- // Example: "http://localhost:4000/api/..." → protocolEnd = 4 (after "http")
268
- const protocolEnd = url.indexOf('://');
269
- // Find first "/" after the domain
270
- // Example: "http://localhost:4000/api/..." → pathStart = 21 (the "/" before "api")
271
- const pathStart = url.indexOf('/', protocolEnd + 3);
272
- // Find where query parameters start (if any)
273
- // Example: "/api/users/123?id=1" → pathEnd = 15 (at the "?")
274
- const pathEnd = url.indexOf('?', pathStart);
275
- // Extract just the pathname without query parameters
276
- // Example: "/api/users/123/profile"
277
- const pathname = pathEnd === -1
278
- ? url.substring(pathStart) // No query params
279
- : url.substring(pathStart, pathEnd); // Has query params, stop at "?"
280
- // Step 2: Extract wildcard segments efficiently
281
- // Example route: "/api/users/:userId/*" has baseSegmentCount = 3
282
- // - Segment 0: "api"
283
- // - Segment 1: "users"
284
- // - Segment 2: ":userId" (or "123" in actual URL)
285
- // - Segment 3+: wildcard segments we want!
286
- if (baseSegmentCount === 0) {
287
- // Fast path: The entire path is wildcard
288
- // Example: "/api/docs/*" where all segments after domain are wildcards
289
- // Just split everything and we're done!
290
- request.wildcardParams = pathname
291
- .split('/')
292
- .filter(Boolean);
293
- }
294
- else {
295
- // Optimized path: Skip base segments and only collect wildcard segments
296
- // Example: "/api/users/123/settings/privacy"
297
- // - We need to skip 3 segments (api, users, 123)
298
- // - Only collect: ["settings", "privacy"]
299
- const wildcardParams = [];
300
- let segmentCount = 0; // Counts how many segments we've seen
301
- let start = pathname[0] === '/' ? 1 : 0; // Start after leading "/" if present
302
- // Loop through each character in the pathname
303
- // When we hit "/" or end of string, we know we found a segment
304
- for (let i = start; i <= pathname.length; i++) {
305
- // Found end of a segment (either "/" or end of string)
306
- if (i === pathname.length ||
307
- pathname[i] === '/') {
308
- // Make sure it's not an empty segment (from double slashes)
309
- if (i > start) {
310
- // Only save segments AFTER we've skipped the base segments
311
- // Example: If baseSegmentCount=3, save segment 3, 4, 5, etc.
312
- if (segmentCount >= baseSegmentCount) {
313
- // Extract this segment from the pathname
314
- // Example: pathname.substring(5, 13) might give "settings"
315
- wildcardParams.push(pathname.substring(start, i));
316
- }
317
- segmentCount++; // Increment count for next segment
318
- }
319
- start = i + 1; // Move start position to character after "/"
320
- }
321
- }
322
- // Done! wildcardParams now contains only the wildcard segments
323
- // Example: ["settings", "privacy"] instead of ["api", "users", "123", "settings", "privacy"]
324
- request.wildcardParams = wildcardParams;
325
- }
203
+ const pathname = extractPathnameFromUrl(request.url);
204
+ extractWildcardParams(request, pathname, baseSegmentCount);
326
205
  }
327
206
  const handler = handlers[request.method];
328
207
  if (!handler)
@@ -0,0 +1,14 @@
1
+ import type { BurgerRequest } from '../types/index';
2
+ /**
3
+ * Extracts the pathname from a full URL string, removing query parameters.
4
+ * @param url - The full URL string (e.g., "http://localhost:4000/api/users/123/profile?id=1")
5
+ * @returns The extracted pathname (e.g., "/api/users/123/profile")
6
+ */
7
+ export declare function extractPathnameFromUrl(url: string): string;
8
+ /**
9
+ * Extracts wildcard parameters from a request pathname and sets them on the request object.
10
+ * @param request - The request object to modify
11
+ * @param pathname - The pathname extracted from the URL (e.g., "/api/users/123/profile")
12
+ * @param baseSegmentCount - Number of segments before the wildcard (e.g., 3 for "/api/users/:userId/*")
13
+ */
14
+ export declare function extractWildcardParams(request: BurgerRequest, pathname: string, baseSegmentCount: number): void;
@@ -0,0 +1,66 @@
1
+ /**
2
+ * Extracts the pathname from a full URL string, removing query parameters.
3
+ * @param url - The full URL string (e.g., "http://localhost:4000/api/users/123/profile?id=1")
4
+ * @returns The extracted pathname (e.g., "/api/users/123/profile")
5
+ */
6
+ export function extractPathnameFromUrl(url) {
7
+ // Find where the path starts (after protocol and domain)
8
+ // Example: "http://localhost:4000/api/..." → protocolEnd = 4 (after "http")
9
+ const protocolEnd = url.indexOf('://');
10
+ // Find first "/" after the domain
11
+ // Example: "http://localhost:4000/api/..." → pathStart = 21 (the "/" before "api")
12
+ const pathStart = url.indexOf('/', protocolEnd + 3);
13
+ // Find where query parameters start (if any)
14
+ // Example: "/api/users/123?id=1" → pathEnd = 15 (at the "?")
15
+ const pathEnd = url.indexOf('?', pathStart);
16
+ // Extract just the pathname without query parameters
17
+ // Example: "/api/users/123/profile"
18
+ return pathEnd === -1
19
+ ? url.substring(pathStart) // No query params
20
+ : url.substring(pathStart, pathEnd); // Has query params, stop at "?"
21
+ }
22
+ /**
23
+ * Extracts wildcard parameters from a request pathname and sets them on the request object.
24
+ * @param request - The request object to modify
25
+ * @param pathname - The pathname extracted from the URL (e.g., "/api/users/123/profile")
26
+ * @param baseSegmentCount - Number of segments before the wildcard (e.g., 3 for "/api/users/:userId/*")
27
+ */
28
+ export function extractWildcardParams(request, pathname, baseSegmentCount) {
29
+ if (baseSegmentCount === 0) {
30
+ // Fast path: The entire path is wildcard
31
+ // Example: "/api/docs/*" where all segments after domain are wildcards
32
+ // Just split everything and we're done!
33
+ request.wildcardParams = pathname.split('/').filter(Boolean);
34
+ }
35
+ else {
36
+ // Optimized path: Skip base segments and only collect wildcard segments
37
+ // Example: "/api/users/123/settings/privacy"
38
+ // - We need to skip 3 segments (api, users, 123)
39
+ // - Only collect: ["settings", "privacy"]
40
+ const wildcardParams = [];
41
+ let segmentCount = 0; // Counts how many segments we've seen
42
+ let start = pathname[0] === '/' ? 1 : 0; // Start after leading "/" if present
43
+ // Loop through each character in the pathname
44
+ // When we hit "/" or end of string, we know we found a segment
45
+ for (let i = start; i <= pathname.length; i++) {
46
+ // Found end of a segment (either "/" or end of string)
47
+ if (i === pathname.length || pathname[i] === '/') {
48
+ // Make sure it's not an empty segment (from double slashes)
49
+ if (i > start) {
50
+ // Only save segments AFTER we've skipped the base segments
51
+ // Example: If baseSegmentCount=3, save segment 3, 4, 5, etc.
52
+ if (segmentCount >= baseSegmentCount) {
53
+ // Extract this segment from the pathname
54
+ // Example: pathname.substring(5, 13) might give "settings"
55
+ wildcardParams.push(pathname.substring(start, i));
56
+ }
57
+ segmentCount++; // Increment count for next segment
58
+ }
59
+ start = i + 1; // Move start position to character after "/"
60
+ }
61
+ }
62
+ // Done! wildcardParams now contains only the wildcard segments
63
+ // Example: ["settings", "privacy"] instead of ["api", "users", "123", "settings", "privacy"]
64
+ request.wildcardParams = wildcardParams;
65
+ }
66
+ }
package/package.json CHANGED
@@ -1,89 +1,89 @@
1
- {
2
- "name": "burger-api",
3
- "version": "0.5.0",
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
- "prepare": "bun run build",
15
- "test": "bun test",
16
- "dev": "bun run examples/file-base-static-page-routing-app/index.ts"
17
- },
18
- "keywords": [
19
- "burger-api",
20
- "burger-api-framework",
21
- "bun",
22
- "bunjs",
23
- "bun-framework",
24
- "fast-backend",
25
- "javascript-backend",
26
- "high-performance",
27
- "esm",
28
- "typescript",
29
- "openapi",
30
- "swagger-ui",
31
- "declarative-routing",
32
- "api-framework",
33
- "modern-backend",
34
- "backend-framework",
35
- "server",
36
- "bun-native",
37
- "js-backend",
38
- "fast-api",
39
- "scalable-backend",
40
- "backend-development",
41
- "modern-api",
42
- "api-development",
43
- "backend-engine",
44
- "efficient",
45
- "minimalist-backend",
46
- "bun-powered",
47
- "open-source",
48
- "typescript-backend",
49
- "performance",
50
- "rapid-development",
51
- "web-framework",
52
- "api-server",
53
- "dynamic-routing",
54
- "backend-routing",
55
- "extensible",
56
- "modular",
57
- "declarative-api",
58
- "lightweight",
59
- "contemporary-backend",
60
- "fast-javascript",
61
- "bun-js-framework",
62
- "bun-backend",
63
- "bun-api-framework",
64
- "pakistan-backend",
65
- "pakistan-api-framework",
66
- "pakistan-api-server",
67
- "pakistan-backend-framework",
68
- "pakistan-bunjs-framework",
69
- "pakistan-javascript-framework"
70
- ],
71
- "license": "MIT",
72
- "repository": {
73
- "type": "git",
74
- "url": "git+https://github.com/isfhan/burger-api.git"
75
- },
76
- "bugs": {
77
- "url": "https://github.com/isfhan/burger-api/issues"
78
- },
79
- "homepage": "https://burger-api.com",
80
- "dependencies": {
81
- "zod": "^4.0.17"
82
- },
83
- "devDependencies": {
84
- "@types/bun": "latest"
85
- },
86
- "peerDependencies": {
87
- "typescript": "^5.7.3"
88
- }
1
+ {
2
+ "name": "burger-api",
3
+ "version": "0.5.2",
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
+ "prepare": "bun run build",
15
+ "test": "bun test",
16
+ "dev": "bun run examples/file-base-static-page-routing-app/index.ts"
17
+ },
18
+ "keywords": [
19
+ "burger-api",
20
+ "burger-api-framework",
21
+ "bun",
22
+ "bunjs",
23
+ "bun-framework",
24
+ "fast-backend",
25
+ "javascript-backend",
26
+ "high-performance",
27
+ "esm",
28
+ "typescript",
29
+ "openapi",
30
+ "swagger-ui",
31
+ "declarative-routing",
32
+ "api-framework",
33
+ "modern-backend",
34
+ "backend-framework",
35
+ "server",
36
+ "bun-native",
37
+ "js-backend",
38
+ "fast-api",
39
+ "scalable-backend",
40
+ "backend-development",
41
+ "modern-api",
42
+ "api-development",
43
+ "backend-engine",
44
+ "efficient",
45
+ "minimalist-backend",
46
+ "bun-powered",
47
+ "open-source",
48
+ "typescript-backend",
49
+ "performance",
50
+ "rapid-development",
51
+ "web-framework",
52
+ "api-server",
53
+ "dynamic-routing",
54
+ "backend-routing",
55
+ "extensible",
56
+ "modular",
57
+ "declarative-api",
58
+ "lightweight",
59
+ "contemporary-backend",
60
+ "fast-javascript",
61
+ "bun-js-framework",
62
+ "bun-backend",
63
+ "bun-api-framework",
64
+ "pakistan-backend",
65
+ "pakistan-api-framework",
66
+ "pakistan-api-server",
67
+ "pakistan-backend-framework",
68
+ "pakistan-bunjs-framework",
69
+ "pakistan-javascript-framework"
70
+ ],
71
+ "license": "MIT",
72
+ "repository": {
73
+ "type": "git",
74
+ "url": "git+https://github.com/isfhan/burger-api.git"
75
+ },
76
+ "bugs": {
77
+ "url": "https://github.com/isfhan/burger-api/issues"
78
+ },
79
+ "homepage": "https://burger-api.com",
80
+ "dependencies": {
81
+ "zod": "^4.0.17"
82
+ },
83
+ "devDependencies": {
84
+ "@types/bun": "latest"
85
+ },
86
+ "peerDependencies": {
87
+ "typescript": "^5.7.3"
88
+ }
89
89
  }