burger-api 0.0.39 → 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,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,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "burger-api",
|
|
3
|
-
"version": "0.0
|
|
3
|
+
"version": "0.1.0",
|
|
4
4
|
"author": "Isfhan Ahmed",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"module": "dist/src/index.js",
|
|
@@ -12,7 +12,7 @@
|
|
|
12
12
|
"typecheck": "tsc --noEmit",
|
|
13
13
|
"build": "tsc --project tsconfig.build.json",
|
|
14
14
|
"test": "bun test",
|
|
15
|
-
"dev": "bun run examples/
|
|
15
|
+
"dev": "bun run examples/file-base-static-page-routing-app/index.ts"
|
|
16
16
|
},
|
|
17
17
|
"keywords": [
|
|
18
18
|
"burger-api",
|
|
@@ -68,15 +68,15 @@
|
|
|
68
68
|
"bugs": {
|
|
69
69
|
"url": "https://github.com/isfhan/burger-api/issues"
|
|
70
70
|
},
|
|
71
|
-
"homepage": "https://
|
|
71
|
+
"homepage": "https://burger-api-docs.vercel.app",
|
|
72
72
|
"dependencies": {
|
|
73
73
|
"zod": "^3.24.2",
|
|
74
|
-
"zod-to-json-schema": "^3.24.
|
|
74
|
+
"zod-to-json-schema": "^3.24.3"
|
|
75
75
|
},
|
|
76
76
|
"devDependencies": {
|
|
77
77
|
"@types/bun": "latest"
|
|
78
78
|
},
|
|
79
79
|
"peerDependencies": {
|
|
80
|
-
"typescript": "^5.
|
|
80
|
+
"typescript": "^5.7.3"
|
|
81
81
|
}
|
|
82
82
|
}
|
package/dist/src/core/router.js
DELETED
|
@@ -1,197 +0,0 @@
|
|
|
1
|
-
// Import stuff from node
|
|
2
|
-
import { readdirSync } from "fs";
|
|
3
|
-
import * as path from "path";
|
|
4
|
-
// Import utils
|
|
5
|
-
import { cleanPrefix, normalizePath } from "../utils/index.js";
|
|
6
|
-
/**
|
|
7
|
-
* Router class for handling file-based routing.
|
|
8
|
-
* Loads routes from a directory structure and matches requests to the appropriate route handlers.
|
|
9
|
-
* Supports dynamic segments (e.g., [id]) and HTTP method handlers.
|
|
10
|
-
* Routes are sorted to prioritize static routes over dynamic ones to prevent overlapping route issues.
|
|
11
|
-
*/
|
|
12
|
-
export class Router {
|
|
13
|
-
routesDir;
|
|
14
|
-
prefix;
|
|
15
|
-
/** Array of loaded route definitions */
|
|
16
|
-
routes = [];
|
|
17
|
-
/**
|
|
18
|
-
* Constructor for the Router class.
|
|
19
|
-
* @param routesDir The directory path where route modules are located.
|
|
20
|
-
* @param prefix Optional prefix to prepend to all routes (e.g., "api" becomes "/api/...").
|
|
21
|
-
*/
|
|
22
|
-
constructor(routesDir, prefix = "") {
|
|
23
|
-
this.routesDir = routesDir;
|
|
24
|
-
this.prefix = prefix;
|
|
25
|
-
}
|
|
26
|
-
/**
|
|
27
|
-
* Loads route modules from the specified directory and adds them to the routes array.
|
|
28
|
-
* After loading, sorts the routes to prioritize static routes over dynamic ones based on specificity.
|
|
29
|
-
* @returns A promise that resolves when all route modules have been loaded and sorted.
|
|
30
|
-
*/
|
|
31
|
-
async loadRoutes() {
|
|
32
|
-
this.routes = [];
|
|
33
|
-
await this.scanDirectory(this.routesDir);
|
|
34
|
-
// Sort routes to ensure static routes are matched before dynamic ones
|
|
35
|
-
this.routes.sort((a, b) => this.compareRoutes(a, b));
|
|
36
|
-
}
|
|
37
|
-
/**
|
|
38
|
-
* Recursively scans the directory for route modules and adds them to the routes array.
|
|
39
|
-
* @param dir The current directory to scan.
|
|
40
|
-
* @param basePath The base path for constructing the route path.
|
|
41
|
-
*/
|
|
42
|
-
async scanDirectory(dir, basePath = "") {
|
|
43
|
-
// Track if a dynamic folder has been found at this directory level
|
|
44
|
-
let dynamicFolderFound = false;
|
|
45
|
-
const entries = readdirSync(dir, { withFileTypes: true });
|
|
46
|
-
for (const entry of entries) {
|
|
47
|
-
const entryPath = path.join(dir, entry.name);
|
|
48
|
-
const relativePath = path.join(basePath, entry.name);
|
|
49
|
-
if (entry.isDirectory()) {
|
|
50
|
-
// Handle dynamic directories (e.g., [id])
|
|
51
|
-
if (entry.name.startsWith("[") && entry.name.endsWith("]")) {
|
|
52
|
-
if (dynamicFolderFound) {
|
|
53
|
-
throw new Error(`Multiple dynamic route folders found in the same directory: '${entry.name}' conflicts with another dynamic folder.`);
|
|
54
|
-
}
|
|
55
|
-
dynamicFolderFound = true;
|
|
56
|
-
}
|
|
57
|
-
await this.scanDirectory(entryPath, relativePath);
|
|
58
|
-
}
|
|
59
|
-
else if (entry.isFile() && entry.name === "route.ts") {
|
|
60
|
-
// Convert file path to route path and load the module
|
|
61
|
-
const routePath = this.convertFilePathToRoute(relativePath);
|
|
62
|
-
const modulePath = path.resolve(entryPath);
|
|
63
|
-
const routeModule = await import(modulePath);
|
|
64
|
-
// Collect HTTP method handlers from the module
|
|
65
|
-
const handlers = {};
|
|
66
|
-
const methods = ["GET", "POST", "PUT", "DELETE", "PATCH", "HEAD"];
|
|
67
|
-
for (const method of methods) {
|
|
68
|
-
if (typeof routeModule[method] === "function") {
|
|
69
|
-
handlers[method] = routeModule[method];
|
|
70
|
-
}
|
|
71
|
-
}
|
|
72
|
-
// Create route definition
|
|
73
|
-
const routeDef = {
|
|
74
|
-
path: routePath,
|
|
75
|
-
handlers,
|
|
76
|
-
middleware: routeModule.middleware,
|
|
77
|
-
schema: routeModule.schema,
|
|
78
|
-
openapi: routeModule.openapi,
|
|
79
|
-
};
|
|
80
|
-
this.routes.push(routeDef);
|
|
81
|
-
}
|
|
82
|
-
}
|
|
83
|
-
}
|
|
84
|
-
/**
|
|
85
|
-
* Converts a file path to a route path by processing dynamic segments,
|
|
86
|
-
* removing certain directory indicators, and applying a prefix if specified.
|
|
87
|
-
* @param filePath The file path to convert into a route path.
|
|
88
|
-
* @returns The converted route path string.
|
|
89
|
-
*/
|
|
90
|
-
convertFilePathToRoute(filePath) {
|
|
91
|
-
// Remove the "route.ts" suffix
|
|
92
|
-
if (filePath.endsWith("route.ts")) {
|
|
93
|
-
filePath = filePath.slice(0, -"route.ts".length);
|
|
94
|
-
}
|
|
95
|
-
// Split path into segments
|
|
96
|
-
const segments = filePath.split(path.sep);
|
|
97
|
-
const resultSegments = [];
|
|
98
|
-
for (let segment of segments) {
|
|
99
|
-
if (!segment)
|
|
100
|
-
continue; // Skip empty segments
|
|
101
|
-
// Skip grouping segments (e.g., (group))
|
|
102
|
-
if (segment.startsWith("(") && segment.endsWith(")"))
|
|
103
|
-
continue;
|
|
104
|
-
// Convert dynamic segments from [param] to :param
|
|
105
|
-
if (segment.startsWith("[") && segment.endsWith("]")) {
|
|
106
|
-
const param = segment.slice(1, -1);
|
|
107
|
-
resultSegments.push(":" + param);
|
|
108
|
-
}
|
|
109
|
-
else {
|
|
110
|
-
resultSegments.push(segment);
|
|
111
|
-
}
|
|
112
|
-
}
|
|
113
|
-
// Construct the route with leading slash
|
|
114
|
-
let route = "/" + resultSegments.join("/");
|
|
115
|
-
// Apply prefix if provided
|
|
116
|
-
if (this.prefix) {
|
|
117
|
-
const cleanPrefixStr = cleanPrefix(this.prefix);
|
|
118
|
-
route = "/" + cleanPrefixStr + route;
|
|
119
|
-
}
|
|
120
|
-
// Remove trailing slash unless it's the root
|
|
121
|
-
if (route !== "/" && route.endsWith("/")) {
|
|
122
|
-
route = route.slice(0, -1);
|
|
123
|
-
}
|
|
124
|
-
return route;
|
|
125
|
-
}
|
|
126
|
-
/**
|
|
127
|
-
* Resolves the given request by finding a matching route and extracting dynamic parameters.
|
|
128
|
-
* @param request The request to resolve.
|
|
129
|
-
* @returns An object containing the matched route and parameters, or an empty params object if no match.
|
|
130
|
-
*/
|
|
131
|
-
resolve(request) {
|
|
132
|
-
const url = new URL(request.url);
|
|
133
|
-
const reqPath = normalizePath(url.pathname);
|
|
134
|
-
const method = request.method.toUpperCase();
|
|
135
|
-
for (const route of this.routes) {
|
|
136
|
-
const match = this.matchRoute(reqPath, route.path);
|
|
137
|
-
if (match && route.handlers[method]) {
|
|
138
|
-
return { route, params: match };
|
|
139
|
-
}
|
|
140
|
-
}
|
|
141
|
-
return { params: {} };
|
|
142
|
-
}
|
|
143
|
-
/**
|
|
144
|
-
* Checks if the request path matches the route path, extracting dynamic parameters if matched.
|
|
145
|
-
* @param requestPath The request path to check.
|
|
146
|
-
* @param routePath The route path to match against.
|
|
147
|
-
* @returns A record of dynamic parameters if matched, otherwise null.
|
|
148
|
-
*/
|
|
149
|
-
matchRoute(requestPath, routePath) {
|
|
150
|
-
const reqSegments = requestPath.split("/").filter(Boolean);
|
|
151
|
-
const routeSegments = routePath.split("/").filter(Boolean);
|
|
152
|
-
if (reqSegments.length !== routeSegments.length) {
|
|
153
|
-
return null;
|
|
154
|
-
}
|
|
155
|
-
const params = {};
|
|
156
|
-
for (let i = 0; i < reqSegments.length; i++) {
|
|
157
|
-
const rSegment = routeSegments[i];
|
|
158
|
-
const reqSegment = reqSegments[i];
|
|
159
|
-
if (rSegment.startsWith(":")) {
|
|
160
|
-
const paramName = rSegment.slice(1);
|
|
161
|
-
params[paramName] = reqSegment;
|
|
162
|
-
}
|
|
163
|
-
else if (rSegment !== reqSegment) {
|
|
164
|
-
return null;
|
|
165
|
-
}
|
|
166
|
-
}
|
|
167
|
-
return params;
|
|
168
|
-
}
|
|
169
|
-
/**
|
|
170
|
-
* Calculates the specificity of a route path based on the number of static segments.
|
|
171
|
-
* Static segments increase the score, while dynamic segments (:param) do not.
|
|
172
|
-
* @param path The route path to evaluate.
|
|
173
|
-
* @returns The specificity score (higher means more static segments).
|
|
174
|
-
*/
|
|
175
|
-
getRouteSpecificity(path) {
|
|
176
|
-
const segments = path.split("/").filter(Boolean);
|
|
177
|
-
return segments.reduce((score, segment) => {
|
|
178
|
-
return segment.startsWith(":") ? score : score + 1;
|
|
179
|
-
}, 0);
|
|
180
|
-
}
|
|
181
|
-
/**
|
|
182
|
-
* Compares two routes for sorting, prioritizing those with higher specificity (more static segments).
|
|
183
|
-
* If specificity is equal, sorts alphabetically by path.
|
|
184
|
-
* @param a The first route to compare.
|
|
185
|
-
* @param b The second route to compare.
|
|
186
|
-
* @returns Negative if a comes before b, positive if b comes before a, zero if equal.
|
|
187
|
-
*/
|
|
188
|
-
compareRoutes(a, b) {
|
|
189
|
-
const aSpecificity = this.getRouteSpecificity(a.path);
|
|
190
|
-
const bSpecificity = this.getRouteSpecificity(b.path);
|
|
191
|
-
if (aSpecificity > bSpecificity)
|
|
192
|
-
return -1;
|
|
193
|
-
if (aSpecificity < bSpecificity)
|
|
194
|
-
return 1;
|
|
195
|
-
return a.path.localeCompare(b.path);
|
|
196
|
-
}
|
|
197
|
-
}
|