-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRouteBuilder.ts
More file actions
46 lines (40 loc) · 1.24 KB
/
RouteBuilder.ts
File metadata and controls
46 lines (40 loc) · 1.24 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
import { toCamelCase } from './utils';
class RouteBuilder {
private path: string = '';
private label: string = '';
private subnodes: RouteBuilder[] = [];
root(options: { nodes: RouteBuilder[], basepath?: string }) {
this.subnodes = options.nodes
if (options?.basepath) {
this.path = options.basepath
}
this.subnodes = options.nodes
return this
// return Object.fromEntries(nodes.map(node => [node.label, node.build()]))
}
node(label: string, path: string, options?: { subnodes?: RouteBuilder[] }): RouteBuilder {
const node = new RouteBuilder();
node.path = path;
node.label = label;
if (options?.subnodes) {
node.subnodes = options.subnodes;
}
return node;
}
build(parentPath?: string): any {
const result: any = {
path: parentPath === undefined ? this.path : `${parentPath}${this.path}`,
label: this.label,
};
if (this.subnodes.length > 0) {
result._ = Object.fromEntries(
this.subnodes.map((sn) => [toCamelCase(sn.label), sn.build(result.path)])
);
}
return result;
}
buildRoute(): any {
return Object.fromEntries(this.subnodes.map((sn) => [toCamelCase(sn.label), sn.build(this.path)]))
}
}
export default RouteBuilder