burger-api 0.0.38 → 0.1.0
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 +129 -8
- package/dist/src/core/{router.d.ts → api-router.d.ts} +11 -18
- package/dist/src/core/api-router.js +209 -0
- package/dist/src/core/openapi.d.ts +2 -2
- package/dist/src/core/page-router.d.ts +59 -0
- package/dist/src/core/page-router.js +231 -0
- package/dist/src/core/server.d.ts +5 -4
- package/dist/src/core/server.js +7 -5
- package/dist/src/index.d.ts +3 -2
- package/dist/src/index.js +28 -12
- package/dist/src/types/index.d.ts +5 -5
- package/dist/src/utils/error.js +2 -155
- package/dist/src/utils/index.d.ts +1 -0
- package/dist/src/utils/index.js +2 -0
- package/dist/src/utils/routing.d.ts +32 -0
- package/dist/src/utils/routing.js +54 -0
- package/package.json +5 -5
- package/dist/src/core/router.js +0 -197
|
@@ -0,0 +1,231 @@
|
|
|
1
|
+
// Import stuff from node
|
|
2
|
+
import { readdirSync } from "fs";
|
|
3
|
+
import * as path from "path";
|
|
4
|
+
// Import utils
|
|
5
|
+
import { cleanPrefix, normalizePath, compareRoutes, ROUTE_CONSTANTS, } from "../utils/index.js";
|
|
6
|
+
/**
|
|
7
|
+
* PageRouter class for handling file-based page routing.
|
|
8
|
+
* Loads pages from a directory structure and matches requests to the appropriate page handlers.
|
|
9
|
+
* Supports dynamic segments (e.g., [id]) and uses default exports as page handlers.
|
|
10
|
+
*/
|
|
11
|
+
export class PageRouter {
|
|
12
|
+
pagesDir;
|
|
13
|
+
prefix;
|
|
14
|
+
/** Array of loaded page definitions */
|
|
15
|
+
pages = [];
|
|
16
|
+
/**
|
|
17
|
+
* Constructor for the PageRouter class.
|
|
18
|
+
* @param pagesDir The directory path where page modules are located.
|
|
19
|
+
* @param prefix Optional prefix to prepend to all routes (e.g., "pages" becomes "/pages/...").
|
|
20
|
+
*/
|
|
21
|
+
constructor(pagesDir, prefix = "") {
|
|
22
|
+
this.pagesDir = pagesDir;
|
|
23
|
+
this.prefix = prefix;
|
|
24
|
+
if (!pagesDir) {
|
|
25
|
+
throw new Error("Pages directory path must be provided");
|
|
26
|
+
}
|
|
27
|
+
// Normalize the pagesDir path
|
|
28
|
+
this.pagesDir = path.normalize(pagesDir);
|
|
29
|
+
// Normalize the prefix if provided
|
|
30
|
+
if (prefix) {
|
|
31
|
+
this.prefix = cleanPrefix(prefix);
|
|
32
|
+
}
|
|
33
|
+
}
|
|
34
|
+
/**
|
|
35
|
+
* Loads page modules from the specified directory and adds them to the pages array.
|
|
36
|
+
* After loading, sorts the pages to prioritize static routes over dynamic ones based on specificity.
|
|
37
|
+
* @returns A promise that resolves when all page modules have been loaded and sorted.
|
|
38
|
+
*/
|
|
39
|
+
async loadPages() {
|
|
40
|
+
// Clear the pages array
|
|
41
|
+
this.pages = [];
|
|
42
|
+
try {
|
|
43
|
+
await this.scanDirectory(this.pagesDir);
|
|
44
|
+
// Sort pages to ensure static routes are matched before dynamic ones
|
|
45
|
+
this.pages.sort((a, b) => compareRoutes(a, b));
|
|
46
|
+
}
|
|
47
|
+
catch (error) {
|
|
48
|
+
console.error("Failed to load pages:", error);
|
|
49
|
+
throw new Error(`Failed to load pages: ${error instanceof Error ? error.message : String(error)}`);
|
|
50
|
+
}
|
|
51
|
+
}
|
|
52
|
+
/**
|
|
53
|
+
* Recursively scans the directory for page modules and adds them to the pages array.
|
|
54
|
+
* @param dir The current directory to scan.
|
|
55
|
+
* @param basePath The base path for constructing the route path.
|
|
56
|
+
*/
|
|
57
|
+
async scanDirectory(dir, basePath = "") {
|
|
58
|
+
// Track if a dynamic folder has been found at this directory level
|
|
59
|
+
let dynamicFolderFound = false;
|
|
60
|
+
try {
|
|
61
|
+
const entries = readdirSync(dir, { withFileTypes: true });
|
|
62
|
+
for (const entry of entries) {
|
|
63
|
+
const entryPath = path.join(dir, entry.name);
|
|
64
|
+
const relativePath = path.join(basePath, entry.name);
|
|
65
|
+
if (entry.isDirectory()) {
|
|
66
|
+
// Handle dynamic directories (e.g., [id])
|
|
67
|
+
if (entry.name.startsWith(ROUTE_CONSTANTS.DYNAMIC_FOLDER_START) &&
|
|
68
|
+
entry.name.endsWith(ROUTE_CONSTANTS.DYNAMIC_FOLDER_END)) {
|
|
69
|
+
if (dynamicFolderFound) {
|
|
70
|
+
throw new Error(`Multiple dynamic page folders found in the same directory: '${entry.name}' conflicts with another dynamic folder.`);
|
|
71
|
+
}
|
|
72
|
+
dynamicFolderFound = true;
|
|
73
|
+
}
|
|
74
|
+
await this.scanDirectory(entryPath, relativePath);
|
|
75
|
+
}
|
|
76
|
+
else if (entry.isFile() &&
|
|
77
|
+
ROUTE_CONSTANTS.SUPPORTED_PAGE_EXTENSIONS.some((ext) => entry.name.endsWith(ext))) {
|
|
78
|
+
// Convert file path to route path and load the module
|
|
79
|
+
const routePath = this.convertFilePathToRoute(relativePath);
|
|
80
|
+
// Clean the route path
|
|
81
|
+
const cleanedRoutePath = this.cleanRoutePath(routePath);
|
|
82
|
+
// Get the module path
|
|
83
|
+
const modulePath = path.resolve(entryPath);
|
|
84
|
+
try {
|
|
85
|
+
// Import the module
|
|
86
|
+
const pageModule = await import(modulePath);
|
|
87
|
+
// Get the default export as the page handler
|
|
88
|
+
if (entry.name.endsWith(".tsx") &&
|
|
89
|
+
typeof pageModule.default !== "function") {
|
|
90
|
+
throw new Error(`Page at ${entryPath} must export a default function as its handler.`);
|
|
91
|
+
}
|
|
92
|
+
// Create page definition
|
|
93
|
+
const pageDefWithSlash = {
|
|
94
|
+
path: cleanedRoutePath + "/",
|
|
95
|
+
handler: pageModule.default,
|
|
96
|
+
middleware: pageModule.middleware,
|
|
97
|
+
};
|
|
98
|
+
// Create page definition
|
|
99
|
+
const pageDef = {
|
|
100
|
+
path: cleanedRoutePath,
|
|
101
|
+
handler: pageModule.default,
|
|
102
|
+
middleware: pageModule.middleware,
|
|
103
|
+
};
|
|
104
|
+
// Add the page definition to the pages array
|
|
105
|
+
this.pages.push(pageDefWithSlash, pageDef);
|
|
106
|
+
}
|
|
107
|
+
catch (importError) {
|
|
108
|
+
console.error(`Failed to import module at ${modulePath}:`, importError);
|
|
109
|
+
}
|
|
110
|
+
}
|
|
111
|
+
}
|
|
112
|
+
}
|
|
113
|
+
catch (error) {
|
|
114
|
+
console.error(`Error scanning directory ${dir}:`, error);
|
|
115
|
+
throw error;
|
|
116
|
+
}
|
|
117
|
+
}
|
|
118
|
+
/**
|
|
119
|
+
* Converts a file path to a route path by processing dynamic segments,
|
|
120
|
+
* removing certain directory indicators, and applying a prefix if specified.
|
|
121
|
+
* @param filePath The file path to convert into a route path.
|
|
122
|
+
* @returns The converted route path string (before extension cleaning).
|
|
123
|
+
*/
|
|
124
|
+
convertFilePathToRoute(filePath) {
|
|
125
|
+
// Split path into segments
|
|
126
|
+
const segments = filePath.split(path.sep);
|
|
127
|
+
const resultSegments = [];
|
|
128
|
+
for (let segment of segments) {
|
|
129
|
+
if (!segment)
|
|
130
|
+
continue; // Skip empty segments
|
|
131
|
+
// Skip grouping segments (e.g., (group))
|
|
132
|
+
if (segment.startsWith(ROUTE_CONSTANTS.GROUPING_FOLDER_START) &&
|
|
133
|
+
segment.endsWith(ROUTE_CONSTANTS.GROUPING_FOLDER_END))
|
|
134
|
+
continue;
|
|
135
|
+
// Convert dynamic segments from [param] to :param
|
|
136
|
+
if (segment.startsWith(ROUTE_CONSTANTS.DYNAMIC_FOLDER_START) &&
|
|
137
|
+
segment.endsWith(ROUTE_CONSTANTS.DYNAMIC_FOLDER_END)) {
|
|
138
|
+
const param = segment.slice(1, -1);
|
|
139
|
+
resultSegments.push(ROUTE_CONSTANTS.DYNAMIC_SEGMENT_PREFIX + param);
|
|
140
|
+
}
|
|
141
|
+
else {
|
|
142
|
+
resultSegments.push(segment);
|
|
143
|
+
}
|
|
144
|
+
}
|
|
145
|
+
// Construct the route with leading slash
|
|
146
|
+
let route = "/" + resultSegments.join("/");
|
|
147
|
+
// Apply prefix if provided
|
|
148
|
+
if (this.prefix) {
|
|
149
|
+
const cleanPrefixStr = cleanPrefix(this.prefix);
|
|
150
|
+
route = "/" + cleanPrefixStr + route;
|
|
151
|
+
}
|
|
152
|
+
// Remove trailing slash unless it's the root
|
|
153
|
+
if (route !== "/" && route.endsWith("/")) {
|
|
154
|
+
route = route.slice(0, -1);
|
|
155
|
+
}
|
|
156
|
+
return route;
|
|
157
|
+
}
|
|
158
|
+
/**
|
|
159
|
+
* Cleans a route path by handling index files and removing extensions.
|
|
160
|
+
* @param routePath The route path to clean
|
|
161
|
+
* @returns The cleaned route path
|
|
162
|
+
*/
|
|
163
|
+
cleanRoutePath(routePath) {
|
|
164
|
+
// Break the route path into segments
|
|
165
|
+
const pathSegments = routePath.split("/");
|
|
166
|
+
// Get the last segment
|
|
167
|
+
if (pathSegments.length > 0) {
|
|
168
|
+
const lastSegment = pathSegments[pathSegments.length - 1];
|
|
169
|
+
// Check if the last segment is an index file
|
|
170
|
+
if (ROUTE_CONSTANTS.PAGE_INDEX_FILES.includes(lastSegment)) {
|
|
171
|
+
// Remove the last segment for index files
|
|
172
|
+
pathSegments.pop();
|
|
173
|
+
}
|
|
174
|
+
else {
|
|
175
|
+
// Remove file extension for non-index files
|
|
176
|
+
const extensionIndex = lastSegment.lastIndexOf(".");
|
|
177
|
+
if (extensionIndex > 0) {
|
|
178
|
+
pathSegments[pathSegments.length - 1] = lastSegment.substring(0, extensionIndex);
|
|
179
|
+
}
|
|
180
|
+
}
|
|
181
|
+
}
|
|
182
|
+
// Join the segments back together
|
|
183
|
+
const finalPath = pathSegments.join("/") === "" ? "/" : pathSegments.join("/");
|
|
184
|
+
return finalPath;
|
|
185
|
+
}
|
|
186
|
+
/**
|
|
187
|
+
* Resolves the given request by finding a matching page and extracting dynamic parameters.
|
|
188
|
+
* @param request The request to resolve.
|
|
189
|
+
* @returns An object containing the matched page and parameters, or an empty params object if no match.
|
|
190
|
+
*/
|
|
191
|
+
resolve(request) {
|
|
192
|
+
const url = new URL(request.url);
|
|
193
|
+
const reqPath = normalizePath(url.pathname);
|
|
194
|
+
console.debug(`Resolving route for path: ${reqPath}`);
|
|
195
|
+
for (const page of this.pages) {
|
|
196
|
+
const match = this.matchRoute(reqPath, page.path);
|
|
197
|
+
if (match) {
|
|
198
|
+
console.debug(`Route matched: ${page.path} with params:`, match);
|
|
199
|
+
return { page, params: match };
|
|
200
|
+
}
|
|
201
|
+
}
|
|
202
|
+
console.debug(`No matching route found for path: ${reqPath}`);
|
|
203
|
+
return { params: {} };
|
|
204
|
+
}
|
|
205
|
+
/**
|
|
206
|
+
* Checks if the request path matches the page path, extracting dynamic parameters if matched.
|
|
207
|
+
* @param requestPath The request path to check.
|
|
208
|
+
* @param pagePath The page path to match against.
|
|
209
|
+
* @returns A record of dynamic parameters if matched, otherwise null.
|
|
210
|
+
*/
|
|
211
|
+
matchRoute(requestPath, pagePath) {
|
|
212
|
+
const reqSegments = requestPath.split("/").filter(Boolean);
|
|
213
|
+
const pageSegments = pagePath.split("/").filter(Boolean);
|
|
214
|
+
if (reqSegments.length !== pageSegments.length) {
|
|
215
|
+
return null;
|
|
216
|
+
}
|
|
217
|
+
const params = {};
|
|
218
|
+
for (let i = 0; i < reqSegments.length; i++) {
|
|
219
|
+
const pSegment = pageSegments[i];
|
|
220
|
+
const reqSegment = reqSegments[i];
|
|
221
|
+
if (pSegment.startsWith(ROUTE_CONSTANTS.DYNAMIC_SEGMENT_PREFIX)) {
|
|
222
|
+
const paramName = pSegment.slice(ROUTE_CONSTANTS.DYNAMIC_SEGMENT_PREFIX.length);
|
|
223
|
+
params[paramName] = reqSegment;
|
|
224
|
+
}
|
|
225
|
+
else if (pSegment !== reqSegment) {
|
|
226
|
+
return null;
|
|
227
|
+
}
|
|
228
|
+
}
|
|
229
|
+
return params;
|
|
230
|
+
}
|
|
231
|
+
}
|
|
@@ -8,12 +8,13 @@ export declare class Server {
|
|
|
8
8
|
*/
|
|
9
9
|
constructor(options: ServerOptions);
|
|
10
10
|
/**
|
|
11
|
-
* Starts the server with the given
|
|
11
|
+
* Starts the server with the given routes, handler, port, and callback.
|
|
12
|
+
* @param routes - The routes to be used by the server.
|
|
13
|
+
* @param handler - The handler to be used by the server.
|
|
12
14
|
* @param port - The port to listen on.
|
|
13
|
-
* @param
|
|
14
|
-
* @param cb - The callback function to be called when the server starts.
|
|
15
|
+
* @param cb - An optional callback function to be called when the server starts.
|
|
15
16
|
*/
|
|
16
|
-
start(
|
|
17
|
+
start(routes: any, handler: RequestHandler, port: number, cb?: () => void): void;
|
|
17
18
|
/**
|
|
18
19
|
* Stops the server.
|
|
19
20
|
* If the server is currently running, this method will stop the server and
|
package/dist/src/core/server.js
CHANGED
|
@@ -14,15 +14,16 @@ export class Server {
|
|
|
14
14
|
this.options = options;
|
|
15
15
|
}
|
|
16
16
|
/**
|
|
17
|
-
* Starts the server with the given
|
|
17
|
+
* Starts the server with the given routes, handler, port, and callback.
|
|
18
|
+
* @param routes - The routes to be used by the server.
|
|
19
|
+
* @param handler - The handler to be used by the server.
|
|
18
20
|
* @param port - The port to listen on.
|
|
19
|
-
* @param
|
|
20
|
-
* @param cb - The callback function to be called when the server starts.
|
|
21
|
+
* @param cb - An optional callback function to be called when the server starts.
|
|
21
22
|
*/
|
|
22
|
-
start(
|
|
23
|
+
start(routes, handler, port, cb) {
|
|
23
24
|
// Start Bun's native server using Bun.serve
|
|
24
25
|
this.server = Bun.serve({
|
|
25
|
-
|
|
26
|
+
routes,
|
|
26
27
|
fetch: async (request) => {
|
|
27
28
|
try {
|
|
28
29
|
// Wrap the native Request with HttpRequest to get a BurgerRequest
|
|
@@ -37,6 +38,7 @@ export class Server {
|
|
|
37
38
|
return errorResponse(error, request, this.options.debug ?? false);
|
|
38
39
|
}
|
|
39
40
|
},
|
|
41
|
+
port,
|
|
40
42
|
});
|
|
41
43
|
if (cb) {
|
|
42
44
|
cb();
|
package/dist/src/index.d.ts
CHANGED
|
@@ -2,7 +2,8 @@ import type { ServerOptions, Middleware } from "./types";
|
|
|
2
2
|
export declare class Burger {
|
|
3
3
|
private options;
|
|
4
4
|
private server;
|
|
5
|
-
private
|
|
5
|
+
private apiRouter?;
|
|
6
|
+
private pageRouter?;
|
|
6
7
|
private globalMiddleware;
|
|
7
8
|
/**
|
|
8
9
|
* Constructor for the Burger class.
|
|
@@ -28,4 +29,4 @@ export declare class Burger {
|
|
|
28
29
|
serve(port?: number, cb?: () => void): Promise<void>;
|
|
29
30
|
}
|
|
30
31
|
export { setDir } from "./utils/index.js";
|
|
31
|
-
export type { ServerOptions, RequestHandler, BurgerRequest, BurgerResponse, Middleware, } from "./types/index.js";
|
|
32
|
+
export type { ServerOptions, RequestHandler, BurgerRequest, BurgerResponse, Middleware, openapi, } from "./types/index.js";
|
package/dist/src/index.js
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
// Import stuff from core
|
|
2
2
|
import { Server } from "./core/server.js";
|
|
3
|
-
import {
|
|
3
|
+
import { ApiRouter } from "./core/api-router.js";
|
|
4
|
+
import { PageRouter } from "./core/page-router.js";
|
|
4
5
|
import { HttpRequest } from "./core/request.js";
|
|
5
6
|
import { HttpResponse } from "./core/response.js";
|
|
6
7
|
import { generateOpenAPIDocument } from "./core/openapi.js";
|
|
@@ -10,7 +11,8 @@ import { createValidationMiddleware } from "./middleware/validator.js";
|
|
|
10
11
|
export class Burger {
|
|
11
12
|
options;
|
|
12
13
|
server;
|
|
13
|
-
|
|
14
|
+
apiRouter;
|
|
15
|
+
pageRouter;
|
|
14
16
|
globalMiddleware = [];
|
|
15
17
|
/**
|
|
16
18
|
* Constructor for the Burger class.
|
|
@@ -26,7 +28,11 @@ export class Burger {
|
|
|
26
28
|
this.server = new Server(options);
|
|
27
29
|
// Initialize API router
|
|
28
30
|
if (options.apiDir) {
|
|
29
|
-
this.
|
|
31
|
+
this.apiRouter = new ApiRouter(options.apiDir, "api");
|
|
32
|
+
}
|
|
33
|
+
// Initialize page router
|
|
34
|
+
if (options.pageDir) {
|
|
35
|
+
this.pageRouter = new PageRouter(options.pageDir, "");
|
|
30
36
|
}
|
|
31
37
|
// Add global middleware
|
|
32
38
|
if (options.globalMiddleware) {
|
|
@@ -48,9 +54,19 @@ export class Burger {
|
|
|
48
54
|
*/
|
|
49
55
|
async serve(port = 4000, cb) {
|
|
50
56
|
// File-based routing mode
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
57
|
+
const routes = {};
|
|
58
|
+
// Load Page routes
|
|
59
|
+
if (this.pageRouter) {
|
|
60
|
+
await this.pageRouter.loadPages();
|
|
61
|
+
this.pageRouter.pages.forEach((page) => {
|
|
62
|
+
routes[page.path] = page.handler;
|
|
63
|
+
});
|
|
64
|
+
}
|
|
65
|
+
if (this.apiRouter) {
|
|
66
|
+
// Load API routes
|
|
67
|
+
await this.apiRouter.loadRoutes();
|
|
68
|
+
// Start the server
|
|
69
|
+
this.server.start(routes, async (req) => {
|
|
54
70
|
// Wrap the native request with helper methods
|
|
55
71
|
const request = new HttpRequest(req);
|
|
56
72
|
// Create a new instance of BurgerResponse for the handler
|
|
@@ -59,9 +75,9 @@ export class Burger {
|
|
|
59
75
|
const url = new URL(request.url);
|
|
60
76
|
// Check if the request is for /openapi.json
|
|
61
77
|
if (url.pathname === "/openapi.json") {
|
|
62
|
-
if (this.
|
|
78
|
+
if (this.apiRouter) {
|
|
63
79
|
// Generate OpenAPI document
|
|
64
|
-
const doc = generateOpenAPIDocument(this.
|
|
80
|
+
const doc = generateOpenAPIDocument(this.apiRouter, this.options);
|
|
65
81
|
// Return it as JSON
|
|
66
82
|
return response.json(doc);
|
|
67
83
|
}
|
|
@@ -77,7 +93,7 @@ export class Burger {
|
|
|
77
93
|
return response.html(swaggerHtml);
|
|
78
94
|
}
|
|
79
95
|
// Get the route and params for the current request
|
|
80
|
-
const { route, params } = this.
|
|
96
|
+
const { route, params } = this.apiRouter.resolve(req);
|
|
81
97
|
if (!route) {
|
|
82
98
|
// Return a 404 if no route is found
|
|
83
99
|
return response.status(404).json({ error: "Route not found" });
|
|
@@ -123,13 +139,13 @@ export class Burger {
|
|
|
123
139
|
}
|
|
124
140
|
// Execute the full chain
|
|
125
141
|
return await finalHandler();
|
|
126
|
-
}, cb);
|
|
142
|
+
}, port, cb);
|
|
127
143
|
}
|
|
128
144
|
else {
|
|
129
145
|
// Fallback to default handler if no router is provided
|
|
130
|
-
this.server.start(
|
|
146
|
+
this.server.start(null, async (_) => new Response("Hello from burger-api!", {
|
|
131
147
|
headers: { "Content-Type": "text/plain" },
|
|
132
|
-
}), cb);
|
|
148
|
+
}), port, cb);
|
|
133
149
|
}
|
|
134
150
|
}
|
|
135
151
|
}
|
|
@@ -25,10 +25,6 @@ export interface ServerOptions {
|
|
|
25
25
|
* Global middleware to be executed before each request.
|
|
26
26
|
*/
|
|
27
27
|
globalMiddleware?: Middleware[];
|
|
28
|
-
/**
|
|
29
|
-
* The templating engine to use for rendering HTML templates.
|
|
30
|
-
*/
|
|
31
|
-
templatingEngine?: TemplatingEngine;
|
|
32
28
|
/**
|
|
33
29
|
* The version of the API. This is an optional property that can be used
|
|
34
30
|
* to specify the version of the API.
|
|
@@ -248,4 +244,8 @@ export type openapi = {
|
|
|
248
244
|
};
|
|
249
245
|
};
|
|
250
246
|
};
|
|
251
|
-
export
|
|
247
|
+
export interface PageDefinition {
|
|
248
|
+
path: string;
|
|
249
|
+
handler: RequestHandler;
|
|
250
|
+
middleware?: RequestHandler[];
|
|
251
|
+
}
|
package/dist/src/utils/error.js
CHANGED
|
@@ -24,161 +24,8 @@ export function errorResponse(error, req, debug) {
|
|
|
24
24
|
console.error(`${logPrefix} Error:`, error);
|
|
25
25
|
const acceptHeader = req.headers.get("accept") || "";
|
|
26
26
|
if (acceptHeader.includes("text/html") && debug) {
|
|
27
|
-
const body =
|
|
28
|
-
|
|
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>`;
|
|
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,
|
package/dist/src/utils/index.js
CHANGED
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
import type { PageDefinition, RouteDefinition } from "src/types";
|
|
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;
|