burger-api 0.0.39 → 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.
@@ -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,167 +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 = `
28
- <!DOCTYPE html>
29
- <html lang="en">
30
- <head>
31
- <meta charset="UTF-8">
32
- <title>Error Occurred - BurgerAPI</title>
33
- <style>
34
- body {
35
- margin: 0;
36
- padding: 0;
37
- background-color: #f4f4f4;
38
- font-family: 'Nunito', sans-serif;
39
- color: #333;
40
- }
41
-
42
- .whoops-container {
43
- width: 80%;
44
- margin: 5% auto;
45
- background: #fff;
46
- border-radius: 8px;
47
- box-shadow: 0 4px 12px rgba(0, 0, 0, 0.15);
48
- overflow: hidden;
49
- }
50
-
51
- .whoops-header {
52
- background-color: #e74c3c;
53
- color: white;
54
- padding: 20px;
55
- display: flex;
56
- align-items: center;
57
- justify-content: space-between;
58
- }
59
-
60
- .whoops-header .title-container {
61
- display: flex;
62
- align-items: center;
63
- }
64
-
65
- .whoops-header .title {
66
- font-size: 1.5em;
67
- margin-left: 10px;
68
- }
69
-
70
- .whoops-header .debug-mode {
71
- font-weight: 600;
72
- display: flex;
73
- align-items: center;
74
- padding: 10px;
75
- background-color: #ffffff;
76
- color: #e74c3c;
77
- box-shadow: 0 4px 12px rgba(0, 0, 0, 0.15);
78
- border-radius: 8px;
79
- }
80
-
81
- .whoops-header .debug-mode .fa-check-circle {
82
- color: #28a745; /* Green color for the check icon */
83
- margin-right: 5px; /* Add some spacing between icon and text */
84
- font-size: 1.2em; /* slightly larger icon */
85
- }
86
-
87
- .whoops-content {
88
- padding: 2em;
89
- }
90
-
91
- .tag {
92
- font-weight: 600;
93
- color: #4a5568;
94
- background-color: #edf2f7;
95
- padding: 8px 15px;
96
- border-radius: 6px;
97
- display: inline-block;
98
- font-size: 14px;
99
- }
100
-
101
- .error-message {
102
- font-size: 1.25em;
103
- line-height: 1.6;
104
- margin-bottom: 1.5em;
105
- color: #2d3748;
106
- }
107
-
108
- h2 {
109
- color: #e3342f;
110
- font-size: 1.5em;
111
- margin-bottom: 0.8em;
112
- border-bottom: 1px solid #edf2f7;
113
- padding-bottom: 0.5em;
114
- }
115
-
116
- pre {
117
- background: #f9fafc;
118
- border-left: 4px solid #e3342f;
119
- padding: 1.5em;
120
- border-radius: 4px;
121
- font-family: "Source Code Pro", Menlo, Monaco, Consolas, "Courier New", monospace;
122
- font-size: 1em;
123
- line-height: 1.6;
124
- overflow-x: auto;
125
- color: #4a5568;
126
- }
127
-
128
- .footer {
129
- text-align: center;
130
- padding-top:25px;
131
- font-size: 0.9em;
132
- color: #718096;
133
- border-top: 1px solid #edf2f7;
134
- }
135
-
136
- .error-icon {
137
- font-size: 2em;
138
- }
139
-
140
- .request-info{
141
- display: flex;
142
- justify-content: space-between;
143
- align-items: center;
144
- }
145
-
146
- /* Font Import (Google Fonts) */
147
- @import url('https://fonts.googleapis.com/css2?family=Source+Code+Pro&display=swap');
148
- @import url('https://fonts.googleapis.com/css2?family=Nunito:wght@400;600;700&display=swap');
149
- </style>
150
- <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/5.15.4/css/all.min.css">
151
- </head>
152
- <body>
153
- <div class="whoops-container">
154
- <div class="whoops-header">
155
- <div class="title-container">
156
- <i class="fas fa-exclamation-triangle error-icon"></i>
157
- <div class="title">Whoops! Something went wrong</div>
158
- </div>
159
- <div class="debug-mode">
160
- <i class="fas fa-check-circle"></i> Debug Mode Enabled
161
- </div>
162
- </div>
163
- <div class="whoops-content">
164
- <div class="request-info">
165
- <div><strong>Request:</strong> ${method} ${url}</div>
166
- <div class="tag">
167
- ${error.stack?.split("\n")[0].split(":")[0]}
168
- </div>
169
- </div>
170
- <p class="error-message">
171
- ${error.message}
172
- </p>
173
- <h2>Stack Trace</h2>
174
- <pre>${error.stack || "No stack trace available"}</pre>
175
- <div class="footer">
176
- “It’s not a bug, it’s an undocumented feature.” — Anonymous
177
- </div>
178
- </div>
179
- </div>
180
- </body>
181
- </html>`;
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
+ `;
182
29
  // Return the html response
183
30
  return new Response(body, {
184
31
  status: 500,
185
- headers: { "Content-Type": "text/html" },
32
+ headers: { 'Content-Type': 'text/html' },
186
33
  });
187
34
  }
188
35
  else if (debug) {
@@ -191,19 +38,19 @@ export function errorResponse(error, req, debug) {
191
38
  // Create a JSON response
192
39
  const body = JSON.stringify({
193
40
  request: `${method} ${url}`,
194
- error: error instanceof Error ? error.message : "Unknown error",
195
- stack: error instanceof Error ? normalizePath(error.stack || "") : "",
41
+ error: error instanceof Error ? error.message : 'Unknown error',
42
+ stack: error instanceof Error ? normalizePath(error.stack || '') : '',
196
43
  });
197
44
  // Return the JSON response
198
45
  return new Response(body, {
199
46
  status: 500,
200
- headers: { "Content-Type": "application/json" },
47
+ headers: { 'Content-Type': 'application/json' },
201
48
  });
202
49
  }
203
50
  else {
204
51
  // Log detailed error context
205
52
  console.error(`${logPrefix} Error:`, error);
206
53
  // Return a plain text response
207
- return new Response("Internal Server Error", { status: 500 });
54
+ return new Response('Internal Server Error', { status: 500 });
208
55
  }
209
56
  }
@@ -1,3 +1,4 @@
1
+ export { ROUTE_CONSTANTS, HTTP_METHODS, compareRoutes, getRouteSpecificity, } from './routing.js';
1
2
  /**
2
3
  * Resolves the given path to the specified directory.
3
4
  * @param directory the directory to resolve to
@@ -1,4 +1,6 @@
1
- import { resolve } from "path";
1
+ import { resolve } from 'path';
2
+ // Export constants
3
+ export { ROUTE_CONSTANTS, HTTP_METHODS, compareRoutes, getRouteSpecificity, } from './routing.js';
2
4
  /**
3
5
  * Resolves the given path to the specified directory.
4
6
  * @param directory the directory to resolve to
@@ -15,11 +17,11 @@ export function setDir(directory, path) {
15
17
  */
16
18
  export function cleanPrefix(prefix) {
17
19
  // Remove all slashes from the beginning
18
- while (prefix.startsWith("/")) {
20
+ while (prefix.startsWith('/')) {
19
21
  prefix = prefix.slice(1);
20
22
  }
21
23
  // Remove all slashes from the end
22
- while (prefix.endsWith("/")) {
24
+ while (prefix.endsWith('/')) {
23
25
  prefix = prefix.slice(0, -1);
24
26
  }
25
27
  return prefix;
@@ -32,9 +34,9 @@ export function cleanPrefix(prefix) {
32
34
  */
33
35
  export function normalizePath(path) {
34
36
  // Replace multiple slashes with a single slash
35
- let normalized = path.replace(/\/+/g, "/");
37
+ let normalized = path.replace(/\/+/g, '/');
36
38
  // Remove trailing slash if it's not the root
37
- if (normalized.length > 1 && normalized.endsWith("/")) {
39
+ if (normalized.length > 1 && normalized.endsWith('/')) {
38
40
  normalized = normalized.slice(0, -1);
39
41
  }
40
42
  return normalized;
@@ -0,0 +1,32 @@
1
+ import type { PageDefinition, RouteDefinition } from '../types/index.js';
2
+ /**
3
+ * Constants for route handling.
4
+ */
5
+ export declare const ROUTE_CONSTANTS: {
6
+ SUPPORTED_PAGE_EXTENSIONS: string[];
7
+ PAGE_INDEX_FILES: string[];
8
+ DYNAMIC_SEGMENT_PREFIX: string;
9
+ DYNAMIC_FOLDER_START: string;
10
+ DYNAMIC_FOLDER_END: string;
11
+ GROUPING_FOLDER_START: string;
12
+ GROUPING_FOLDER_END: string;
13
+ };
14
+ /**
15
+ * Supported HTTP methods
16
+ */
17
+ export declare const HTTP_METHODS: string[];
18
+ /**
19
+ * Calculates the specificity of a route path based on the number of static segments.
20
+ * Static segments increase the score, while dynamic segments (:param) do not.
21
+ * @param path The route path to evaluate.
22
+ * @returns The specificity score (higher means more static segments).
23
+ */
24
+ export declare const getRouteSpecificity: (path: string) => number;
25
+ /**
26
+ * Compares two routes for sorting, prioritizing those with higher specificity (more static segments).
27
+ * If specificity is equal, sorts alphabetically by path.
28
+ * @param a The first route to compare.
29
+ * @param b The second route to compare.
30
+ * @returns Negative if a comes before b, positive if b comes before a, zero if equal.
31
+ */
32
+ export declare const compareRoutes: (a: PageDefinition | RouteDefinition, b: PageDefinition | RouteDefinition) => number;
@@ -0,0 +1,54 @@
1
+ /**
2
+ * Constants for route handling.
3
+ */
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: ')',
12
+ };
13
+ /**
14
+ * Supported HTTP methods
15
+ */
16
+ export const HTTP_METHODS = [
17
+ 'GET',
18
+ 'POST',
19
+ 'PUT',
20
+ 'DELETE',
21
+ 'PATCH',
22
+ 'HEAD',
23
+ 'OPTIONS',
24
+ ];
25
+ /**
26
+ * Calculates the specificity of a route path based on the number of static segments.
27
+ * Static segments increase the score, while dynamic segments (:param) do not.
28
+ * @param path The route path to evaluate.
29
+ * @returns The specificity score (higher means more static segments).
30
+ */
31
+ export const getRouteSpecificity = (path) => {
32
+ const segments = path.split('/').filter(Boolean);
33
+ return segments.reduce((score, segment) => {
34
+ return segment.startsWith(ROUTE_CONSTANTS.DYNAMIC_SEGMENT_PREFIX)
35
+ ? score
36
+ : score + 1;
37
+ }, 0);
38
+ };
39
+ /**
40
+ * Compares two routes for sorting, prioritizing those with higher specificity (more static segments).
41
+ * If specificity is equal, sorts alphabetically by path.
42
+ * @param a The first route to compare.
43
+ * @param b The second route to compare.
44
+ * @returns Negative if a comes before b, positive if b comes before a, zero if equal.
45
+ */
46
+ export const compareRoutes = (a, b) => {
47
+ const aSpecificity = getRouteSpecificity(a.path);
48
+ const bSpecificity = getRouteSpecificity(b.path);
49
+ if (aSpecificity > bSpecificity)
50
+ return -1;
51
+ if (aSpecificity < bSpecificity)
52
+ return 1;
53
+ return a.path.localeCompare(b.path);
54
+ };
package/package.json CHANGED
@@ -1,82 +1,83 @@
1
- {
2
- "name": "burger-api",
3
- "version": "0.0.39",
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
- }
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
+ }