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.
@@ -44,7 +44,7 @@ export function createValidationMiddleware(schema) {
44
44
  validated.params = methodSchema.params.parse(req.params);
45
45
  }
46
46
  catch (e) {
47
- errors.push({ field: "params", error: e.errors });
47
+ errors.push({ field: 'params', error: e.errors });
48
48
  }
49
49
  }
50
50
  // Validate query parameters.
@@ -53,17 +53,17 @@ export function createValidationMiddleware(schema) {
53
53
  validated.query = methodSchema.query.parse(Object.fromEntries(req.query.entries()));
54
54
  }
55
55
  catch (e) {
56
- errors.push({ field: "query", error: e.errors });
56
+ errors.push({ field: 'query', error: e.errors });
57
57
  }
58
58
  }
59
59
  // Validate request body.
60
60
  if (methodSchema.body) {
61
61
  // Check the Content-Type header.
62
- const contentType = req.headers.get("Content-Type") || "";
63
- if (!contentType.includes("application/json")) {
62
+ const contentType = req.headers.get('Content-Type') || '';
63
+ if (!contentType.includes('application/json')) {
64
64
  // If not JSON, push an error indicating the expected content type.
65
65
  errors.push({
66
- field: "body",
66
+ field: 'body',
67
67
  error: "Invalid Content-Type. Expected 'application/json'.",
68
68
  });
69
69
  }
@@ -74,7 +74,10 @@ export function createValidationMiddleware(schema) {
74
74
  validated.body = methodSchema.body.parse(bodyData);
75
75
  }
76
76
  catch (e) {
77
- errors.push({ field: "body", error: e.errors || e.message });
77
+ errors.push({
78
+ field: 'body',
79
+ error: e.errors || e.message,
80
+ });
78
81
  }
79
82
  }
80
83
  }
@@ -1,4 +1,4 @@
1
- import { z } from "zod";
1
+ import { z } from 'zod';
2
2
  export interface ServerOptions {
3
3
  /**
4
4
  * The title of the API. This is an optional property that can be used
@@ -148,6 +148,12 @@ export interface BurgerResponse {
148
148
  */
149
149
  original(body?: BodyInit | null, init?: ResponseInit): Response;
150
150
  }
151
+ /**
152
+ * Represents a next function in the request handling pipeline.
153
+ * This function is used to call the next middleware in the chain.
154
+ * It returns a Promise that resolves with a Response object.
155
+ */
156
+ export type BurgerNext = () => Promise<Response>;
151
157
  /**
152
158
  * Represents a request handler function.
153
159
  * Request handler functions receive a BurgerRequest and BurgerResponse as
@@ -170,7 +176,7 @@ export type RequestHandler = (request: BurgerRequest, response: BurgerResponse)
170
176
  * @param next - A function to call the next middleware in the chain.
171
177
  * @returns A Promise that resolves with the response.
172
178
  */
173
- export type Middleware = (request: BurgerRequest, response: BurgerResponse, next: () => Promise<Response>) => Promise<Response>;
179
+ export type Middleware = (request: BurgerRequest, response: BurgerResponse, next: BurgerNext) => Promise<Response>;
174
180
  export interface RouteDefinition {
175
181
  /**
176
182
  * The path of the route.
@@ -1 +1 @@
1
- import { z } from "zod";
1
+ import { z } from 'zod';
@@ -1,4 +1,4 @@
1
- import { normalizePath } from "./index.js";
1
+ import { normalizePath } from './index.js';
2
2
  /**
3
3
  * Generates an HTTP error response based on the request's "Accept" header.
4
4
  *
@@ -22,14 +22,14 @@ export function errorResponse(error, req, debug) {
22
22
  const logPrefix = `[${method} ${url}]`;
23
23
  // Log detailed error context
24
24
  console.error(`${logPrefix} Error:`, error);
25
- const acceptHeader = req.headers.get("accept") || "";
26
- if (acceptHeader.includes("text/html") && debug) {
27
- const body = `<!doctype html><html lang="en"><meta charset="UTF-8"><title>Error Occurred - BurgerAPI</title><style>body{margin:0;padding:0;background-color:#f4f4f4;font-family:Nunito,sans-serif;color:#333}.whoops-container{width:80%;margin:5% auto;background:#fff;border-radius:8px;box-shadow:0 4px 12px rgba(0,0,0,.15);overflow:hidden}.whoops-header{background-color:#e74c3c;color:#fff;padding:20px;display:flex;align-items:center;justify-content:space-between}.whoops-header .title-container{display:flex;align-items:center}.whoops-header .title{font-size:1.5em;margin-left:10px}.whoops-header .debug-mode{font-weight:600;display:flex;align-items:center;padding:10px;background-color:#fff;color:#e74c3c;box-shadow:0 4px 12px rgba(0,0,0,.15);border-radius:8px}.whoops-header .debug-mode .fa-check-circle{color:#28a745;margin-right:5px;font-size:1.2em}.whoops-content{padding:2em}.tag{font-weight:600;color:#4a5568;background-color:#edf2f7;padding:8px 15px;border-radius:6px;display:inline-block;font-size:14px}.error-message{font-size:1.25em;line-height:1.6;margin-bottom:1.5em;color:#2d3748}h2{color:#e3342f;font-size:1.5em;margin-bottom:.8em;border-bottom:1px solid #edf2f7;padding-bottom:.5em}pre{background:#f9fafc;border-left:4px solid #e3342f;padding:1.5em;border-radius:4px;font-family:"Source Code Pro",Menlo,Monaco,Consolas,"Courier New",monospace;font-size:1em;line-height:1.6;overflow-x:auto;color:#4a5568}.footer{text-align:center;padding-top:25px;font-size:.9em;color:#718096;border-top:1px solid #edf2f7}.error-icon{font-size:2em}.request-info{display:flex;justify-content:space-between;align-items:center}</style><link href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/5.15.4/css/all.min.css"rel="stylesheet"><div class="whoops-container"><div class="whoops-header"><div class="title-container"><i class="fas error-icon fa-exclamation-triangle"></i><div class="title">Whoops! Something went wrong</div></div><div class="debug-mode"><i class="fas fa-check-circle"></i> Debug Mode Enabled</div></div><div class="whoops-content"><div class="request-info"><div><strong>Request:</strong> ${method} ${url}</div><div class="tag">${error.stack?.split("\n")[0].split(":")[0]}</div></div><p class="error-message">${error.message}<h2>Stack Trace</h2><pre>${error.stack || "No stack trace available"}</pre><div class="footer">“It’s not a bug, it’s an undocumented feature.” — Anonymous</div></div></div>
25
+ const acceptHeader = req.headers.get('accept') || '';
26
+ if (acceptHeader.includes('text/html') && debug) {
27
+ const body = `<!doctype html><html lang="en"><meta charset="UTF-8"><title>Error Occurred - BurgerAPI</title><style>body{margin:0;padding:0;background-color:#f4f4f4;font-family:Nunito,sans-serif;color:#333}.whoops-container{width:80%;margin:5% auto;background:#fff;border-radius:8px;box-shadow:0 4px 12px rgba(0,0,0,.15);overflow:hidden}.whoops-header{background-color:#e74c3c;color:#fff;padding:20px;display:flex;align-items:center;justify-content:space-between}.whoops-header .title-container{display:flex;align-items:center}.whoops-header .title{font-size:1.5em;margin-left:10px}.whoops-header .debug-mode{font-weight:600;display:flex;align-items:center;padding:10px;background-color:#fff;color:#e74c3c;box-shadow:0 4px 12px rgba(0,0,0,.15);border-radius:8px}.whoops-header .debug-mode .fa-check-circle{color:#28a745;margin-right:5px;font-size:1.2em}.whoops-content{padding:2em}.tag{font-weight:600;color:#4a5568;background-color:#edf2f7;padding:8px 15px;border-radius:6px;display:inline-block;font-size:14px}.error-message{font-size:1.25em;line-height:1.6;margin-bottom:1.5em;color:#2d3748}h2{color:#e3342f;font-size:1.5em;margin-bottom:.8em;border-bottom:1px solid #edf2f7;padding-bottom:.5em}pre{background:#f9fafc;border-left:4px solid #e3342f;padding:1.5em;border-radius:4px;font-family:"Source Code Pro",Menlo,Monaco,Consolas,"Courier New",monospace;font-size:1em;line-height:1.6;overflow-x:auto;color:#4a5568}.footer{text-align:center;padding-top:25px;font-size:.9em;color:#718096;border-top:1px solid #edf2f7}.error-icon{font-size:2em}.request-info{display:flex;justify-content:space-between;align-items:center}</style><link href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/5.15.4/css/all.min.css"rel="stylesheet"><div class="whoops-container"><div class="whoops-header"><div class="title-container"><i class="fas error-icon fa-exclamation-triangle"></i><div class="title">Whoops! Something went wrong</div></div><div class="debug-mode"><i class="fas fa-check-circle"></i> Debug Mode Enabled</div></div><div class="whoops-content"><div class="request-info"><div><strong>Request:</strong> ${method} ${url}</div><div class="tag">${error.stack?.split('\n')[0].split(':')[0]}</div></div><p class="error-message">${error.message}<h2>Stack Trace</h2><pre>${error.stack || 'No stack trace available'}</pre><div class="footer">“It’s not a bug, it’s an undocumented feature.” — Anonymous</div></div></div>
28
28
  `;
29
29
  // Return the html response
30
30
  return new Response(body, {
31
31
  status: 500,
32
- headers: { "Content-Type": "text/html" },
32
+ headers: { 'Content-Type': 'text/html' },
33
33
  });
34
34
  }
35
35
  else if (debug) {
@@ -38,19 +38,19 @@ export function errorResponse(error, req, debug) {
38
38
  // Create a JSON response
39
39
  const body = JSON.stringify({
40
40
  request: `${method} ${url}`,
41
- error: error instanceof Error ? error.message : "Unknown error",
42
- stack: error instanceof Error ? normalizePath(error.stack || "") : "",
41
+ error: error instanceof Error ? error.message : 'Unknown error',
42
+ stack: error instanceof Error ? normalizePath(error.stack || '') : '',
43
43
  });
44
44
  // Return the JSON response
45
45
  return new Response(body, {
46
46
  status: 500,
47
- headers: { "Content-Type": "application/json" },
47
+ headers: { 'Content-Type': 'application/json' },
48
48
  });
49
49
  }
50
50
  else {
51
51
  // Log detailed error context
52
52
  console.error(`${logPrefix} Error:`, error);
53
53
  // Return a plain text response
54
- return new Response("Internal Server Error", { status: 500 });
54
+ return new Response('Internal Server Error', { status: 500 });
55
55
  }
56
56
  }
@@ -1,4 +1,4 @@
1
- export { ROUTE_CONSTANTS, HTTP_METHODS, compareRoutes, getRouteSpecificity, } from "./routing.js";
1
+ export { ROUTE_CONSTANTS, HTTP_METHODS, compareRoutes, getRouteSpecificity, } from './routing.js';
2
2
  /**
3
3
  * Resolves the given path to the specified directory.
4
4
  * @param directory the directory to resolve to
@@ -1,6 +1,6 @@
1
- import { resolve } from "path";
1
+ import { resolve } from 'path';
2
2
  // Export constants
3
- export { ROUTE_CONSTANTS, HTTP_METHODS, compareRoutes, getRouteSpecificity, } from "./routing.js";
3
+ export { ROUTE_CONSTANTS, HTTP_METHODS, compareRoutes, getRouteSpecificity, } from './routing.js';
4
4
  /**
5
5
  * Resolves the given path to the specified directory.
6
6
  * @param directory the directory to resolve to
@@ -17,11 +17,11 @@ export function setDir(directory, path) {
17
17
  */
18
18
  export function cleanPrefix(prefix) {
19
19
  // Remove all slashes from the beginning
20
- while (prefix.startsWith("/")) {
20
+ while (prefix.startsWith('/')) {
21
21
  prefix = prefix.slice(1);
22
22
  }
23
23
  // Remove all slashes from the end
24
- while (prefix.endsWith("/")) {
24
+ while (prefix.endsWith('/')) {
25
25
  prefix = prefix.slice(0, -1);
26
26
  }
27
27
  return prefix;
@@ -34,9 +34,9 @@ export function cleanPrefix(prefix) {
34
34
  */
35
35
  export function normalizePath(path) {
36
36
  // Replace multiple slashes with a single slash
37
- let normalized = path.replace(/\/+/g, "/");
37
+ let normalized = path.replace(/\/+/g, '/');
38
38
  // Remove trailing slash if it's not the root
39
- if (normalized.length > 1 && normalized.endsWith("/")) {
39
+ if (normalized.length > 1 && normalized.endsWith('/')) {
40
40
  normalized = normalized.slice(0, -1);
41
41
  }
42
42
  return normalized;
@@ -1,4 +1,4 @@
1
- import type { PageDefinition, RouteDefinition } from "src/types";
1
+ import type { PageDefinition, RouteDefinition } from '../types/index.js';
2
2
  /**
3
3
  * Constants for route handling.
4
4
  */
@@ -2,25 +2,25 @@
2
2
  * Constants for route handling.
3
3
  */
4
4
  export const ROUTE_CONSTANTS = {
5
- SUPPORTED_PAGE_EXTENSIONS: [".tsx", ".html"],
6
- PAGE_INDEX_FILES: ["index.tsx", "index.html"],
7
- DYNAMIC_SEGMENT_PREFIX: ":",
8
- DYNAMIC_FOLDER_START: "[",
9
- DYNAMIC_FOLDER_END: "]",
10
- GROUPING_FOLDER_START: "(",
11
- GROUPING_FOLDER_END: ")",
5
+ SUPPORTED_PAGE_EXTENSIONS: ['.tsx', '.html'],
6
+ PAGE_INDEX_FILES: ['index.tsx', 'index.html'],
7
+ DYNAMIC_SEGMENT_PREFIX: ':',
8
+ DYNAMIC_FOLDER_START: '[',
9
+ DYNAMIC_FOLDER_END: ']',
10
+ GROUPING_FOLDER_START: '(',
11
+ GROUPING_FOLDER_END: ')',
12
12
  };
13
13
  /**
14
14
  * Supported HTTP methods
15
15
  */
16
16
  export const HTTP_METHODS = [
17
- "GET",
18
- "POST",
19
- "PUT",
20
- "DELETE",
21
- "PATCH",
22
- "HEAD",
23
- "OPTIONS",
17
+ 'GET',
18
+ 'POST',
19
+ 'PUT',
20
+ 'DELETE',
21
+ 'PATCH',
22
+ 'HEAD',
23
+ 'OPTIONS',
24
24
  ];
25
25
  /**
26
26
  * Calculates the specificity of a route path based on the number of static segments.
@@ -29,7 +29,7 @@ export const HTTP_METHODS = [
29
29
  * @returns The specificity score (higher means more static segments).
30
30
  */
31
31
  export const getRouteSpecificity = (path) => {
32
- const segments = path.split("/").filter(Boolean);
32
+ const segments = path.split('/').filter(Boolean);
33
33
  return segments.reduce((score, segment) => {
34
34
  return segment.startsWith(ROUTE_CONSTANTS.DYNAMIC_SEGMENT_PREFIX)
35
35
  ? score
package/package.json CHANGED
@@ -1,82 +1,83 @@
1
- {
2
- "name": "burger-api",
3
- "version": "0.1.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
- "test": "bun test",
15
- "dev": "bun run examples/file-base-static-page-routing-app/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://burger-api-docs.vercel.app",
72
- "dependencies": {
73
- "zod": "^3.24.2",
74
- "zod-to-json-schema": "^3.24.3"
75
- },
76
- "devDependencies": {
77
- "@types/bun": "latest"
78
- },
79
- "peerDependencies": {
80
- "typescript": "^5.7.3"
81
- }
82
- }
1
+ {
2
+ "name": "burger-api",
3
+ "version": "0.1.4",
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 && tsc-alias -p tsconfig.build.json",
14
+ "test": "bun test",
15
+ "dev": "bun run examples/file-base-static-page-routing-app/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://burger-api-docs.vercel.app",
72
+ "dependencies": {
73
+ "zod": "^3.24.2",
74
+ "zod-to-json-schema": "^3.24.4"
75
+ },
76
+ "devDependencies": {
77
+ "@types/bun": "latest",
78
+ "tsc-alias": "^1.8.11"
79
+ },
80
+ "peerDependencies": {
81
+ "typescript": "^5.7.3"
82
+ }
83
+ }