burger-api 0.9.7 → 1.0.0-beta.1
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 +84 -241
- package/dist/src/adapter/bun/index.d.ts +14 -0
- package/dist/src/adapter/bun/index.js +73 -0
- package/dist/src/adapter/bun/types.d.ts +15 -0
- package/dist/src/adapter/bun/types.js +1 -0
- package/dist/src/adapter/index.d.ts +1 -0
- package/dist/src/adapter/index.js +1 -0
- package/dist/src/adapter/types.d.ts +49 -0
- package/dist/src/adapter/types.js +1 -0
- package/dist/src/adapter/web-standard/fetch-handler.d.ts +39 -0
- package/dist/src/adapter/web-standard/fetch-handler.js +54 -0
- package/dist/src/adapter/web-standard/index.d.ts +8 -0
- package/dist/src/adapter/web-standard/index.js +7 -0
- package/dist/src/analysis/route-access-analyzer.d.ts +10 -0
- package/dist/src/analysis/route-access-analyzer.js +287 -0
- package/dist/src/chain/chain.d.ts +16 -0
- package/dist/src/chain/chain.js +17 -0
- package/dist/src/chain/flattener.d.ts +3 -0
- package/dist/src/chain/flattener.js +78 -0
- package/dist/src/chain/node.d.ts +25 -0
- package/dist/src/chain/node.js +1 -0
- package/dist/src/compiler/conventions.d.ts +67 -0
- package/dist/src/compiler/conventions.js +129 -0
- package/dist/src/compiler/index.d.ts +13 -0
- package/dist/src/compiler/index.js +11 -0
- package/dist/src/compiler/module-loader.d.ts +75 -0
- package/dist/src/compiler/module-loader.js +276 -0
- package/dist/src/compiler/route-module.d.ts +97 -0
- package/dist/src/compiler/route-module.js +1 -0
- package/dist/src/compiler/route-tree.d.ts +19 -0
- package/dist/src/compiler/route-tree.js +28 -0
- package/dist/src/compiler/scanner.d.ts +42 -0
- package/dist/src/compiler/scanner.js +240 -0
- package/dist/src/context/context-set.d.ts +26 -0
- package/dist/src/context/context-set.js +45 -0
- package/dist/src/context/context.d.ts +357 -0
- package/dist/src/context/context.js +401 -0
- package/dist/src/context/query-parser.d.ts +23 -0
- package/dist/src/context/query-parser.js +132 -0
- package/dist/src/context/route-access.d.ts +7 -0
- package/dist/src/context/route-access.js +18 -0
- package/dist/src/context/types.d.ts +62 -0
- package/dist/src/context/types.js +5 -0
- package/dist/src/core/asset-mime.d.ts +9 -0
- package/dist/src/core/asset-mime.js +35 -0
- package/dist/src/core/assets.d.ts +30 -0
- package/dist/src/core/assets.js +67 -0
- package/dist/src/core/docs-providers.d.ts +13 -0
- package/dist/src/core/docs-providers.js +89 -0
- package/dist/src/core/embedded-assets.d.ts +18 -0
- package/dist/src/core/embedded-assets.js +13 -0
- package/dist/src/core/openapi.d.ts +9 -10
- package/dist/src/core/openapi.js +333 -127
- package/dist/src/core/page-router.d.ts +13 -17
- package/dist/src/core/page-router.js +86 -55
- package/dist/src/core/server.d.ts +20 -11
- package/dist/src/core/server.js +41 -45
- package/dist/src/errors/forbidden.d.ts +14 -0
- package/dist/src/errors/forbidden.js +16 -0
- package/dist/src/errors/http-error.d.ts +49 -0
- package/dist/src/errors/http-error.js +155 -0
- package/dist/src/errors/method-not-allowed.d.ts +13 -0
- package/dist/src/errors/method-not-allowed.js +15 -0
- package/dist/src/errors/not-found.d.ts +14 -0
- package/dist/src/errors/not-found.js +16 -0
- package/dist/src/errors/unauthorized.d.ts +14 -0
- package/dist/src/errors/unauthorized.js +16 -0
- package/dist/src/index.d.ts +275 -50
- package/dist/src/index.js +974 -333
- package/dist/src/lifecycle/executor.d.ts +64 -0
- package/dist/src/lifecycle/executor.js +201 -0
- package/dist/src/lifecycle/hook-runner.d.ts +11 -0
- package/dist/src/lifecycle/hook-runner.js +156 -0
- package/dist/src/lifecycle/jit.d.ts +50 -0
- package/dist/src/lifecycle/jit.js +258 -0
- package/dist/src/lifecycle/transform.d.ts +26 -0
- package/dist/src/lifecycle/transform.js +99 -0
- package/dist/src/lifecycle/types.d.ts +135 -0
- package/dist/src/lifecycle/types.js +1 -0
- package/dist/src/plugin/composer.d.ts +3 -0
- package/dist/src/plugin/composer.js +28 -0
- package/dist/src/plugin/registry.d.ts +25 -0
- package/dist/src/plugin/registry.js +107 -0
- package/dist/src/plugin/types.d.ts +20 -0
- package/dist/src/plugin/types.js +1 -0
- package/dist/src/router/allow-cache.d.ts +20 -0
- package/dist/src/router/allow-cache.js +26 -0
- package/dist/src/router/compiler.d.ts +39 -0
- package/dist/src/router/compiler.js +867 -0
- package/dist/src/router/define.d.ts +67 -0
- package/dist/src/router/define.js +34 -0
- package/dist/src/router/index.d.ts +6 -0
- package/dist/src/router/index.js +5 -0
- package/dist/src/router/param-extract.d.ts +33 -0
- package/dist/src/router/param-extract.js +87 -0
- package/dist/src/router/regex-matcher.d.ts +42 -0
- package/dist/src/router/regex-matcher.js +216 -0
- package/dist/src/router/router.d.ts +214 -0
- package/dist/src/router/router.js +670 -0
- package/dist/src/router/static-map.d.ts +39 -0
- package/dist/src/router/static-map.js +48 -0
- package/dist/src/router/trie.d.ts +69 -0
- package/dist/src/router/trie.js +572 -0
- package/dist/src/router/types.d.ts +104 -0
- package/dist/src/router/types.js +1 -0
- package/dist/src/runtime/capabilities.d.ts +29 -0
- package/dist/src/runtime/capabilities.js +48 -0
- package/dist/src/types/index.d.ts +260 -109
- package/dist/src/types/index.js +1 -1
- package/dist/src/types/inference.d.ts +78 -0
- package/dist/src/types/inference.js +20 -0
- package/dist/src/types/openapi-config.d.ts +164 -0
- package/dist/src/types/openapi-config.js +8 -0
- package/dist/src/utils/env.d.ts +13 -0
- package/dist/src/utils/env.js +24 -0
- package/dist/src/utils/fs.d.ts +29 -0
- package/dist/src/utils/fs.js +75 -0
- package/dist/src/utils/index.d.ts +1 -1
- package/dist/src/utils/index.js +2 -2
- package/dist/src/utils/pathConversion.js +13 -7
- package/dist/src/utils/response.d.ts +42 -6
- package/dist/src/utils/response.js +189 -7
- package/dist/src/utils/routing.d.ts +32 -13
- package/dist/src/utils/routing.js +41 -18
- package/dist/src/utils/thenable.d.ts +8 -0
- package/dist/src/utils/thenable.js +12 -0
- package/dist/src/utils/timing-safe.d.ts +9 -0
- package/dist/src/utils/timing-safe.js +20 -0
- package/dist/src/utils/wildcard.d.ts +6 -7
- package/dist/src/utils/wildcard.js +38 -55
- package/dist/src/validation/adapter.d.ts +54 -0
- package/dist/src/validation/adapter.js +75 -0
- package/dist/src/validation/adapters/standard.d.ts +11 -0
- package/dist/src/validation/adapters/standard.js +110 -0
- package/dist/src/validation/adapters/zod.d.ts +9 -0
- package/dist/src/validation/adapters/zod.js +128 -0
- package/dist/src/validation/cache.d.ts +26 -0
- package/dist/src/validation/cache.js +35 -0
- package/dist/src/validation/coerce.d.ts +28 -0
- package/dist/src/validation/coerce.js +176 -0
- package/dist/src/validation/compiler.d.ts +27 -0
- package/dist/src/validation/compiler.js +191 -0
- package/dist/src/validation/error.d.ts +69 -0
- package/dist/src/validation/error.js +137 -0
- package/dist/src/validation/index.d.ts +16 -0
- package/dist/src/validation/index.js +12 -0
- package/dist/src/validation/response.d.ts +25 -0
- package/dist/src/validation/response.js +85 -0
- package/dist/src/validation/types.d.ts +148 -0
- package/dist/src/validation/types.js +6 -0
- package/dist/src/validation/validator.d.ts +39 -0
- package/dist/src/validation/validator.js +441 -0
- package/dist/src/ws/adapter.d.ts +230 -0
- package/dist/src/ws/adapter.js +660 -0
- package/dist/src/ws/compiler.d.ts +37 -0
- package/dist/src/ws/compiler.js +178 -0
- package/dist/src/ws/index.d.ts +11 -0
- package/dist/src/ws/index.js +8 -0
- package/dist/src/ws/platform.d.ts +82 -0
- package/dist/src/ws/platform.js +122 -0
- package/dist/src/ws/router.d.ts +47 -0
- package/dist/src/ws/router.js +183 -0
- package/dist/src/ws/scanner.d.ts +97 -0
- package/dist/src/ws/scanner.js +253 -0
- package/dist/src/ws/types.d.ts +381 -0
- package/dist/src/ws/types.js +213 -0
- package/package.json +72 -60
- package/dist/src/core/api-router.d.ts +0 -65
- package/dist/src/core/api-router.js +0 -264
- package/dist/src/core/swagger-ui.d.ts +0 -1
- package/dist/src/core/swagger-ui.js +0 -33
- package/dist/src/middleware/validator.d.ts +0 -14
- package/dist/src/middleware/validator.js +0 -133
- package/dist/src/utils/error.d.ts +0 -17
- package/dist/src/utils/error.js +0 -63
|
@@ -0,0 +1,97 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Scans a directory tree for WebSocket convention files.
|
|
3
|
+
*/
|
|
4
|
+
/**
|
|
5
|
+
* Scanned WebSocket route
|
|
6
|
+
*/
|
|
7
|
+
export interface ScannedWebSocketRoute {
|
|
8
|
+
/**
|
|
9
|
+
* Route path (e.g., "/chat", "/notifications/:room")
|
|
10
|
+
*/
|
|
11
|
+
path: string;
|
|
12
|
+
/**
|
|
13
|
+
* Absolute path to ws.ts / ws.js / ws.mjs file
|
|
14
|
+
*/
|
|
15
|
+
wsFile: string;
|
|
16
|
+
/**
|
|
17
|
+
* Absolute path to hooks file (if any)
|
|
18
|
+
*/
|
|
19
|
+
hooksFile?: string;
|
|
20
|
+
/**
|
|
21
|
+
* Absolute path to config file (if any)
|
|
22
|
+
*/
|
|
23
|
+
configFile?: string;
|
|
24
|
+
/**
|
|
25
|
+
* Route parameters
|
|
26
|
+
*/
|
|
27
|
+
params?: Record<string, string>;
|
|
28
|
+
/**
|
|
29
|
+
* Whether this is a wildcard route
|
|
30
|
+
*/
|
|
31
|
+
isWildcard?: boolean;
|
|
32
|
+
/**
|
|
33
|
+
* Whether this is a group directory (URL only, no inheritance)
|
|
34
|
+
*/
|
|
35
|
+
isGroup?: boolean;
|
|
36
|
+
/**
|
|
37
|
+
* Group name (if isGroup)
|
|
38
|
+
*/
|
|
39
|
+
groupName?: string;
|
|
40
|
+
}
|
|
41
|
+
/**
|
|
42
|
+
* WebSocket scan result
|
|
43
|
+
*/
|
|
44
|
+
export interface WebSocketScanResult {
|
|
45
|
+
/**
|
|
46
|
+
* Scanned WebSocket routes
|
|
47
|
+
*/
|
|
48
|
+
routes: ScannedWebSocketRoute[];
|
|
49
|
+
/**
|
|
50
|
+
* Global hooks file path (if any)
|
|
51
|
+
*/
|
|
52
|
+
globalHooks?: string;
|
|
53
|
+
}
|
|
54
|
+
/**
|
|
55
|
+
* WebSocket directory scanner
|
|
56
|
+
*/
|
|
57
|
+
export declare class WebSocketScanner {
|
|
58
|
+
private readonly wsDir;
|
|
59
|
+
private readonly prefix;
|
|
60
|
+
constructor(wsDir: string, prefix?: string);
|
|
61
|
+
/**
|
|
62
|
+
* Scans the WebSocket directory tree and returns scanned routes
|
|
63
|
+
*/
|
|
64
|
+
scan(): Promise<WebSocketScanResult>;
|
|
65
|
+
/**
|
|
66
|
+
* Recursively walks the directory, emitting a route per ws file found.
|
|
67
|
+
*/
|
|
68
|
+
private walk;
|
|
69
|
+
/**
|
|
70
|
+
* Builds the route path from the directory structure
|
|
71
|
+
*/
|
|
72
|
+
private buildRoutePath;
|
|
73
|
+
/**
|
|
74
|
+
* Extracts route parameters from the path
|
|
75
|
+
*/
|
|
76
|
+
private extractParams;
|
|
77
|
+
/**
|
|
78
|
+
* Checks if a directory name is a dynamic parameter folder
|
|
79
|
+
*/
|
|
80
|
+
private isDynamicDir;
|
|
81
|
+
/**
|
|
82
|
+
* Extracts the parameter name from a dynamic folder
|
|
83
|
+
*/
|
|
84
|
+
private extractParamName;
|
|
85
|
+
/**
|
|
86
|
+
* Checks if a directory name is a wildcard folder
|
|
87
|
+
*/
|
|
88
|
+
private isWildcardDir;
|
|
89
|
+
/**
|
|
90
|
+
* Checks if a directory name is a group folder
|
|
91
|
+
*/
|
|
92
|
+
private isGroupDir;
|
|
93
|
+
/**
|
|
94
|
+
* Extracts the group name from a group folder
|
|
95
|
+
*/
|
|
96
|
+
private extractGroupName;
|
|
97
|
+
}
|
|
@@ -0,0 +1,253 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Scans a directory tree for WebSocket convention files.
|
|
3
|
+
*/
|
|
4
|
+
import { readdir } from 'node:fs/promises';
|
|
5
|
+
import * as path from 'node:path';
|
|
6
|
+
import { resolveScanDir } from '../utils/fs.js';
|
|
7
|
+
import { ROUTE_CONSTANTS } from '../utils/routing.js';
|
|
8
|
+
import { splitConventionName, } from '../compiler/conventions.js';
|
|
9
|
+
/** WebSocket convention stems (extension is `.ts` / `.js` / `.mjs`). */
|
|
10
|
+
const WS_CONVENTION_FILES = ['ws', 'hooks', 'config'];
|
|
11
|
+
/**
|
|
12
|
+
* WebSocket directory scanner
|
|
13
|
+
*/
|
|
14
|
+
export class WebSocketScanner {
|
|
15
|
+
wsDir;
|
|
16
|
+
prefix;
|
|
17
|
+
constructor(wsDir, prefix = '') {
|
|
18
|
+
this.wsDir = wsDir;
|
|
19
|
+
this.prefix = prefix;
|
|
20
|
+
if (!wsDir) {
|
|
21
|
+
throw new Error('WebSocket directory path is required');
|
|
22
|
+
}
|
|
23
|
+
this.wsDir = resolveScanDir(wsDir, 'WebSocket', 'wsDir');
|
|
24
|
+
}
|
|
25
|
+
/**
|
|
26
|
+
* Scans the WebSocket directory tree and returns scanned routes
|
|
27
|
+
*/
|
|
28
|
+
async scan() {
|
|
29
|
+
const routes = [];
|
|
30
|
+
await this.walk(this.wsDir, routes);
|
|
31
|
+
// Groups strip from the URL, so different folders can collide. Detect
|
|
32
|
+
// both exact duplicates and duplicate dynamic shapes (`/:x` vs `/:y`).
|
|
33
|
+
const seenPaths = new Set();
|
|
34
|
+
for (const route of routes) {
|
|
35
|
+
const shape = route.path
|
|
36
|
+
.split('/')
|
|
37
|
+
.map((segment) => segment.startsWith(ROUTE_CONSTANTS.DYNAMIC_SEGMENT_PREFIX)
|
|
38
|
+
? ROUTE_CONSTANTS.DYNAMIC_SEGMENT_PREFIX
|
|
39
|
+
: segment)
|
|
40
|
+
.join('/');
|
|
41
|
+
if (seenPaths.has(shape)) {
|
|
42
|
+
throw new Error(`Duplicate route path registered: "${route.path}". ` +
|
|
43
|
+
`Two route directories resolve to the same URL.`);
|
|
44
|
+
}
|
|
45
|
+
seenPaths.add(shape);
|
|
46
|
+
}
|
|
47
|
+
// Detect global hooks at the parent directory
|
|
48
|
+
let globalHooks;
|
|
49
|
+
const parentDir = path.dirname(this.wsDir);
|
|
50
|
+
let rootEntries = [];
|
|
51
|
+
try {
|
|
52
|
+
rootEntries = await readdir(parentDir, {
|
|
53
|
+
withFileTypes: true,
|
|
54
|
+
});
|
|
55
|
+
}
|
|
56
|
+
catch {
|
|
57
|
+
// Parent directory may not exist
|
|
58
|
+
}
|
|
59
|
+
for (const entry of rootEntries) {
|
|
60
|
+
if (!entry.isFile())
|
|
61
|
+
continue;
|
|
62
|
+
const split = splitConventionName(entry.name);
|
|
63
|
+
if (!split || split.stem !== 'hooks')
|
|
64
|
+
continue;
|
|
65
|
+
if (globalHooks) {
|
|
66
|
+
throw new Error(`Conflicting app-level hooks files "${globalHooks}" and "${path.join(parentDir, entry.name)}" — ` +
|
|
67
|
+
`use only one of .ts/.js/.mjs.`);
|
|
68
|
+
}
|
|
69
|
+
globalHooks = path.resolve(path.join(parentDir, entry.name));
|
|
70
|
+
}
|
|
71
|
+
return { routes, globalHooks };
|
|
72
|
+
}
|
|
73
|
+
/**
|
|
74
|
+
* Recursively walks the directory, emitting a route per ws file found.
|
|
75
|
+
*/
|
|
76
|
+
async walk(dir, out) {
|
|
77
|
+
let dynamicFolderFound = false;
|
|
78
|
+
let wildcardFolderFound = false;
|
|
79
|
+
const entries = await readdir(dir, { withFileTypes: true });
|
|
80
|
+
const subDirs = [];
|
|
81
|
+
// Validate folder-level conflicts before recursing, matching the
|
|
82
|
+
// HTTP scanner's fail-loud rules.
|
|
83
|
+
for (const entry of entries) {
|
|
84
|
+
if (!entry.isDirectory())
|
|
85
|
+
continue;
|
|
86
|
+
const name = entry.name;
|
|
87
|
+
if (name.startsWith(ROUTE_CONSTANTS.WILDCARD_START) &&
|
|
88
|
+
name !== ROUTE_CONSTANTS.WILDCARD_SIMPLE) {
|
|
89
|
+
throw new Error(`Named wildcard folder '${name}' is not supported — ` +
|
|
90
|
+
`use '${ROUTE_CONSTANTS.WILDCARD_SIMPLE}' (anonymous) instead.`);
|
|
91
|
+
}
|
|
92
|
+
const isDynamic = name.startsWith(ROUTE_CONSTANTS.DYNAMIC_FOLDER_START) &&
|
|
93
|
+
name.endsWith(ROUTE_CONSTANTS.DYNAMIC_FOLDER_END) &&
|
|
94
|
+
!name.startsWith(ROUTE_CONSTANTS.WILDCARD_START);
|
|
95
|
+
const isWildcard = name === ROUTE_CONSTANTS.WILDCARD_SIMPLE;
|
|
96
|
+
// Dynamic + wildcard siblings are legal — the router resolves
|
|
97
|
+
// them by priority (static > `:param` > `*`).
|
|
98
|
+
if (isDynamic && dynamicFolderFound) {
|
|
99
|
+
throw new Error(`Multiple dynamic route folders found in the same directory: ` +
|
|
100
|
+
`'${name}' conflicts with another dynamic folder in '${dir}'.`);
|
|
101
|
+
}
|
|
102
|
+
if (isWildcard && wildcardFolderFound) {
|
|
103
|
+
throw new Error(`Multiple wildcard route folders found in the same directory: ` +
|
|
104
|
+
`'${name}' conflicts with another wildcard folder in '${dir}'.`);
|
|
105
|
+
}
|
|
106
|
+
if (isDynamic)
|
|
107
|
+
dynamicFolderFound = true;
|
|
108
|
+
if (isWildcard)
|
|
109
|
+
wildcardFolderFound = true;
|
|
110
|
+
}
|
|
111
|
+
// Collect ws / hooks / config files across all accepted extensions.
|
|
112
|
+
const wsFiles = new Map();
|
|
113
|
+
for (const entry of entries) {
|
|
114
|
+
if (entry.isDirectory()) {
|
|
115
|
+
subDirs.push(entry.name);
|
|
116
|
+
continue;
|
|
117
|
+
}
|
|
118
|
+
if (!entry.isFile())
|
|
119
|
+
continue;
|
|
120
|
+
const split = splitConventionName(entry.name);
|
|
121
|
+
if (!split ||
|
|
122
|
+
!WS_CONVENTION_FILES.includes(split.stem)) {
|
|
123
|
+
continue;
|
|
124
|
+
}
|
|
125
|
+
const abs = path.resolve(path.join(dir, entry.name));
|
|
126
|
+
const existing = wsFiles.get(split.stem);
|
|
127
|
+
if (existing) {
|
|
128
|
+
throw new Error(`Conflicting convention files "${existing}" and "${abs}" in "${dir}" — ` +
|
|
129
|
+
`use only one of .ts/.js/.mjs per convention file.`);
|
|
130
|
+
}
|
|
131
|
+
wsFiles.set(split.stem, abs);
|
|
132
|
+
}
|
|
133
|
+
const wsFile = wsFiles.get('ws');
|
|
134
|
+
if (wsFile) {
|
|
135
|
+
const dirName = path.basename(dir);
|
|
136
|
+
const hooksFile = wsFiles.get('hooks');
|
|
137
|
+
const configFile = wsFiles.get('config');
|
|
138
|
+
// Build route path
|
|
139
|
+
const routePath = this.buildRoutePath(dir);
|
|
140
|
+
const params = this.extractParams(routePath);
|
|
141
|
+
const isWildcard = this.isWildcardDir(dirName);
|
|
142
|
+
const isGroup = this.isGroupDir(dirName);
|
|
143
|
+
const groupName = isGroup
|
|
144
|
+
? this.extractGroupName(dirName)
|
|
145
|
+
: undefined;
|
|
146
|
+
out.push({
|
|
147
|
+
path: routePath,
|
|
148
|
+
wsFile,
|
|
149
|
+
hooksFile,
|
|
150
|
+
configFile,
|
|
151
|
+
params,
|
|
152
|
+
isWildcard,
|
|
153
|
+
isGroup,
|
|
154
|
+
groupName,
|
|
155
|
+
});
|
|
156
|
+
}
|
|
157
|
+
else if (wsFiles.size > 0) {
|
|
158
|
+
// No `ws.ts`, so sibling hooks/config apply to nothing — routes
|
|
159
|
+
// never inherit from parent folders.
|
|
160
|
+
const relativeFolder = path
|
|
161
|
+
.relative(this.wsDir, dir)
|
|
162
|
+
.split(path.sep)
|
|
163
|
+
.join('/');
|
|
164
|
+
const fileNames = [...wsFiles.values()]
|
|
165
|
+
.map((file) => path.basename(file))
|
|
166
|
+
.sort();
|
|
167
|
+
console.warn(`[burger-api] ${relativeFolder || '.'} has ${fileNames.join(', ')} but no ws.ts, so they apply to nothing. ` +
|
|
168
|
+
`Convention files are not inherited by child routes: put hooks.ts/config.ts next to each ws.ts.`);
|
|
169
|
+
}
|
|
170
|
+
// Process subdirectories. Group directories only affect the URL but
|
|
171
|
+
// are still walked.
|
|
172
|
+
for (const subDir of subDirs) {
|
|
173
|
+
await this.walk(path.join(dir, subDir), out);
|
|
174
|
+
}
|
|
175
|
+
}
|
|
176
|
+
/**
|
|
177
|
+
* Builds the route path from the directory structure
|
|
178
|
+
*/
|
|
179
|
+
buildRoutePath(dir) {
|
|
180
|
+
const relativePath = path.relative(this.wsDir, dir);
|
|
181
|
+
const parts = relativePath.split(path.sep);
|
|
182
|
+
// Convert directory names to route path
|
|
183
|
+
const routeParts = [];
|
|
184
|
+
for (const part of parts) {
|
|
185
|
+
// Skip group directories in URL
|
|
186
|
+
if (this.isGroupDir(part))
|
|
187
|
+
continue;
|
|
188
|
+
// Convert dynamic folders
|
|
189
|
+
if (this.isWildcardDir(part)) {
|
|
190
|
+
routeParts.push('*');
|
|
191
|
+
continue;
|
|
192
|
+
}
|
|
193
|
+
if (this.isDynamicDir(part)) {
|
|
194
|
+
const paramName = this.extractParamName(part);
|
|
195
|
+
routeParts.push(`:${paramName}`);
|
|
196
|
+
continue;
|
|
197
|
+
}
|
|
198
|
+
routeParts.push(part);
|
|
199
|
+
}
|
|
200
|
+
const routePath = '/' + routeParts.join('/');
|
|
201
|
+
return this.prefix
|
|
202
|
+
? `/${this.prefix}${routePath}`.replace(/\/+/g, '/')
|
|
203
|
+
: routePath;
|
|
204
|
+
}
|
|
205
|
+
/**
|
|
206
|
+
* Extracts route parameters from the path
|
|
207
|
+
*/
|
|
208
|
+
extractParams(routePath) {
|
|
209
|
+
const params = {};
|
|
210
|
+
const parts = routePath.split('/');
|
|
211
|
+
let hasParams = false;
|
|
212
|
+
for (const part of parts) {
|
|
213
|
+
if (part.startsWith(':')) {
|
|
214
|
+
const paramName = part.slice(1);
|
|
215
|
+
params[paramName] = paramName;
|
|
216
|
+
hasParams = true;
|
|
217
|
+
}
|
|
218
|
+
}
|
|
219
|
+
return hasParams ? params : undefined;
|
|
220
|
+
}
|
|
221
|
+
/**
|
|
222
|
+
* Checks if a directory name is a dynamic parameter folder
|
|
223
|
+
*/
|
|
224
|
+
isDynamicDir(name) {
|
|
225
|
+
return /^\[([^\]]+)\]$/.test(name);
|
|
226
|
+
}
|
|
227
|
+
/**
|
|
228
|
+
* Extracts the parameter name from a dynamic folder
|
|
229
|
+
*/
|
|
230
|
+
extractParamName(name) {
|
|
231
|
+
const match = name.match(/^\[([^\]]+)\]$/);
|
|
232
|
+
return match ? match[1] : name;
|
|
233
|
+
}
|
|
234
|
+
/**
|
|
235
|
+
* Checks if a directory name is a wildcard folder
|
|
236
|
+
*/
|
|
237
|
+
isWildcardDir(name) {
|
|
238
|
+
return /^\[\.\.\.([^\]]*)\]$/.test(name);
|
|
239
|
+
}
|
|
240
|
+
/**
|
|
241
|
+
* Checks if a directory name is a group folder
|
|
242
|
+
*/
|
|
243
|
+
isGroupDir(name) {
|
|
244
|
+
return /^\(.+\)$/.test(name);
|
|
245
|
+
}
|
|
246
|
+
/**
|
|
247
|
+
* Extracts the group name from a group folder
|
|
248
|
+
*/
|
|
249
|
+
extractGroupName(name) {
|
|
250
|
+
const match = name.match(/^\((.+)\)$/);
|
|
251
|
+
return match ? (match[1] ?? name) : name;
|
|
252
|
+
}
|
|
253
|
+
}
|