forked from remix-run/remix
-
Notifications
You must be signed in to change notification settings - Fork 0
/
routes.ts
190 lines (161 loc) · 4.49 KB
/
routes.ts
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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
import * as path from "path";
/**
* A route that was created using `defineRoutes` or created conventionally from
* looking at the files on the filesystem.
*/
export interface ConfigRoute {
/**
* The path this route uses to match on the URL pathname.
*/
path?: string;
/**
* Should be `true` if it is an index route. This disallows child routes.
*/
index?: boolean;
/**
* Should be `true` if the `path` is case-sensitive. Defaults to `false`.
*/
caseSensitive?: boolean;
/**
* The unique id for this route, named like its `file` but without the
* extension. So `app/routes/gists/$username.jsx` will have an `id` of
* `routes/gists/$username`.
*/
id: string;
/**
* The unique `id` for this route's parent route, if there is one.
*/
parentId?: string;
/**
* The path to the entry point for this route, relative to
* `config.appDirectory`.
*/
file: string;
}
export interface RouteManifest {
[routeId: string]: ConfigRoute;
}
export interface DefineRouteOptions {
/**
* Should be `true` if the route `path` is case-sensitive. Defaults to
* `false`.
*/
caseSensitive?: boolean;
/**
* Should be `true` if this is an index route that does not allow child routes.
*/
index?: boolean;
/**
* An optional unique id string for this route. Use this if you need to aggregate
* two or more routes with the same route file.
*/
id?: string;
}
interface DefineRouteChildren {
(): void;
}
/**
* A function for defining a route that is passed as the argument to the
* `defineRoutes` callback.
*
* Calls to this function are designed to be nested, using the `children`
* callback argument.
*
* defineRoutes(route => {
* route('/', 'pages/layout', () => {
* route('react-router', 'pages/react-router');
* route('reach-ui', 'pages/reach-ui');
* });
* });
*/
export interface DefineRouteFunction {
(
/**
* The path this route uses to match the URL pathname.
*/
path: string | undefined,
/**
* The path to the file that exports the React component rendered by this
* route as its default export, relative to the `app` directory.
*/
file: string,
/**
* Options for defining routes, or a function for defining child routes.
*/
optionsOrChildren?: DefineRouteOptions | DefineRouteChildren,
/**
* A function for defining child routes.
*/
children?: DefineRouteChildren
): void;
}
export type DefineRoutesFunction = typeof defineRoutes;
/**
* A function for defining routes programmatically, instead of using the
* filesystem convention.
*/
export function defineRoutes(
callback: (defineRoute: DefineRouteFunction) => void
): RouteManifest {
let routes: RouteManifest = Object.create(null);
let parentRoutes: ConfigRoute[] = [];
let alreadyReturned = false;
let defineRoute: DefineRouteFunction = (
path,
file,
optionsOrChildren,
children
) => {
if (alreadyReturned) {
throw new Error(
"You tried to define routes asynchronously but started defining " +
"routes before the async work was done. Please await all async " +
"data before calling `defineRoutes()`"
);
}
let options: DefineRouteOptions;
if (typeof optionsOrChildren === "function") {
// route(path, file, children)
options = {};
children = optionsOrChildren;
} else {
// route(path, file, options, children)
// route(path, file, options)
options = optionsOrChildren || {};
}
let route: ConfigRoute = {
path: path ? path : undefined,
index: options.index ? true : undefined,
caseSensitive: options.caseSensitive ? true : undefined,
id: options.id || createRouteId(file),
parentId:
parentRoutes.length > 0
? parentRoutes[parentRoutes.length - 1].id
: undefined,
file,
};
if (route.id in routes) {
throw new Error(
`Unable to define routes with duplicate route id: "${route.id}"`
);
}
routes[route.id] = route;
if (children) {
parentRoutes.push(route);
children();
parentRoutes.pop();
}
};
callback(defineRoute);
alreadyReturned = true;
return routes;
}
export function createRouteId(file: string) {
return normalizeSlashes(stripFileExtension(file));
}
export function normalizeSlashes(file: string) {
return file.split(path.win32.sep).join("/");
}
function stripFileExtension(file: string) {
return file.replace(/\.[a-z0-9]+$/i, "");
}