-
-
Notifications
You must be signed in to change notification settings - Fork 10.5k
/
Copy pathroutes.ts
400 lines (356 loc) · 10.3 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
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
import * as Path from "pathe";
import * as v from "valibot";
import pick from "lodash/pick";
import invariant from "../invariant";
declare global {
var __reactRouterAppDirectory: string;
}
export function setAppDirectory(directory: string) {
globalThis.__reactRouterAppDirectory = directory;
}
/**
* Provides the absolute path to the app directory, for use within `routes.ts`.
* This is designed to support resolving file system routes.
*/
export function getAppDirectory() {
invariant(globalThis.__reactRouterAppDirectory);
return globalThis.__reactRouterAppDirectory;
}
export interface RouteManifestEntry {
/**
* 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.tsx` 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]: RouteManifestEntry;
}
/**
* Configuration for an individual route, for use within `routes.ts`. As a
* convenience, route config entries can be created with the {@link route},
* {@link index} and {@link layout} helper functions.
*/
export interface RouteConfigEntry {
/**
* The unique id for this route.
*/
id?: string;
/**
* 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 path to the entry point for this route, relative to
* `config.appDirectory`.
*/
file: string;
/**
* The child routes.
*/
children?: RouteConfigEntry[];
}
export const routeConfigEntrySchema: v.BaseSchema<
RouteConfigEntry,
any,
v.BaseIssue<unknown>
> = v.pipe(
v.custom<RouteConfigEntry>((value) => {
return !(
typeof value === "object" &&
value !== null &&
"then" in value &&
"catch" in value
);
}, "Invalid type: Expected object but received a promise. Did you forget to await?"),
v.object({
id: v.optional(v.string()),
path: v.optional(v.string()),
index: v.optional(v.boolean()),
caseSensitive: v.optional(v.boolean()),
file: v.string(),
children: v.optional(v.array(v.lazy(() => routeConfigEntrySchema))),
})
);
export const resolvedRouteConfigSchema = v.array(routeConfigEntrySchema);
type ResolvedRouteConfig = v.InferInput<typeof resolvedRouteConfigSchema>;
/**
* Route config to be exported via the default export from `app/routes.ts`.
*/
export type RouteConfig = ResolvedRouteConfig | Promise<ResolvedRouteConfig>;
export function validateRouteConfig({
routeConfigFile,
routeConfig,
}: {
routeConfigFile: string;
routeConfig: unknown;
}): { valid: false; message: string } | { valid: true } {
if (!routeConfig) {
return {
valid: false,
message: `Route config must be the default export in "${routeConfigFile}".`,
};
}
if (!Array.isArray(routeConfig)) {
return {
valid: false,
message: `Route config in "${routeConfigFile}" must be an array.`,
};
}
let { issues } = v.safeParse(resolvedRouteConfigSchema, routeConfig);
if (issues?.length) {
let { root, nested } = v.flatten(issues);
return {
valid: false,
message: [
`Route config in "${routeConfigFile}" is invalid.`,
root ? `${root}` : [],
nested
? Object.entries(nested).map(
([path, message]) => `Path: routes.${path}\n${message}`
)
: [],
]
.flat()
.join("\n\n"),
};
}
return { valid: true };
}
const createConfigRouteOptionKeys = [
"id",
"index",
"caseSensitive",
] as const satisfies Array<keyof RouteConfigEntry>;
type CreateRouteOptions = Pick<
RouteConfigEntry,
(typeof createConfigRouteOptionKeys)[number]
>;
/**
* Helper function for creating a route config entry, for use within
* `routes.ts`.
*/
function route(
path: string | null | undefined,
file: string,
children?: RouteConfigEntry[]
): RouteConfigEntry;
function route(
path: string | null | undefined,
file: string,
options: CreateRouteOptions,
children?: RouteConfigEntry[]
): RouteConfigEntry;
function route(
path: string | null | undefined,
file: string,
optionsOrChildren: CreateRouteOptions | RouteConfigEntry[] | undefined,
children?: RouteConfigEntry[]
): RouteConfigEntry {
let options: CreateRouteOptions = {};
if (Array.isArray(optionsOrChildren) || !optionsOrChildren) {
children = optionsOrChildren;
} else {
options = optionsOrChildren;
}
return {
file,
children,
path: path ?? undefined,
...pick(options, createConfigRouteOptionKeys),
};
}
const createIndexOptionKeys = ["id"] as const satisfies Array<
keyof RouteConfigEntry
>;
type CreateIndexOptions = Pick<
RouteConfigEntry,
(typeof createIndexOptionKeys)[number]
>;
/**
* Helper function for creating a route config entry for an index route, for use
* within `routes.ts`.
*/
function index(file: string, options?: CreateIndexOptions): RouteConfigEntry {
return {
file,
index: true,
...pick(options, createIndexOptionKeys),
};
}
const createLayoutOptionKeys = ["id"] as const satisfies Array<
keyof RouteConfigEntry
>;
type CreateLayoutOptions = Pick<
RouteConfigEntry,
(typeof createLayoutOptionKeys)[number]
>;
/**
* Helper function for creating a route config entry for a layout route, for use
* within `routes.ts`.
*/
function layout(file: string, children?: RouteConfigEntry[]): RouteConfigEntry;
function layout(
file: string,
options: CreateLayoutOptions,
children?: RouteConfigEntry[]
): RouteConfigEntry;
function layout(
file: string,
optionsOrChildren: CreateLayoutOptions | RouteConfigEntry[] | undefined,
children?: RouteConfigEntry[]
): RouteConfigEntry {
let options: CreateLayoutOptions = {};
if (Array.isArray(optionsOrChildren) || !optionsOrChildren) {
children = optionsOrChildren;
} else {
options = optionsOrChildren;
}
return {
file,
children,
...pick(options, createLayoutOptionKeys),
};
}
/**
* Helper function for adding a path prefix to a set of routes without needing
* to introduce a parent route file, for use within `routes.ts`.
*/
function prefix(
prefixPath: string,
routes: RouteConfigEntry[]
): RouteConfigEntry[] {
return routes.map((route) => {
if (route.index || typeof route.path === "string") {
return {
...route,
path: route.path ? joinRoutePaths(prefixPath, route.path) : prefixPath,
children: route.children,
};
} else if (route.children) {
return {
...route,
children: prefix(prefixPath, route.children),
};
}
return route;
});
}
const helpers = { route, index, layout, prefix };
export { route, index, layout, prefix };
/**
* Creates a set of route config helpers that resolve file paths relative to the
* given directory, for use within `routes.ts`. This is designed to support
* splitting route config into multiple files within different directories.
*/
export function relative(directory: string): typeof helpers {
return {
/**
* Helper function for creating a route config entry, for use within
* `routes.ts`. Note that this helper has been scoped, meaning that file
* path will be resolved relative to the directory provided to the
* `relative` call that created this helper.
*/
route: (path, file, ...rest) => {
return route(path, Path.resolve(directory, file), ...(rest as any));
},
/**
* Helper function for creating a route config entry for an index route, for
* use within `routes.ts`. Note that this helper has been scoped, meaning
* that file path will be resolved relative to the directory provided to the
* `relative` call that created this helper.
*/
index: (file, ...rest) => {
return index(Path.resolve(directory, file), ...(rest as any));
},
/**
* Helper function for creating a route config entry for a layout route, for
* use within `routes.ts`. Note that this helper has been scoped, meaning
* that file path will be resolved relative to the directory provided to the
* `relative` call that created this helper.
*/
layout: (file, ...rest) => {
return layout(Path.resolve(directory, file), ...(rest as any));
},
// Passthrough of helper functions that don't need relative scoping so that
// a complete API is still provided.
prefix,
};
}
export function configRoutesToRouteManifest(
appDirectory: string,
routes: RouteConfigEntry[],
rootId = "root"
): RouteManifest {
let routeManifest: RouteManifest = {};
function walk(route: RouteConfigEntry, parentId: string) {
let id = route.id || createRouteId(route.file);
let manifestItem: RouteManifestEntry = {
id,
parentId,
file: Path.isAbsolute(route.file)
? Path.relative(appDirectory, route.file)
: route.file,
path: route.path,
index: route.index,
caseSensitive: route.caseSensitive,
};
if (routeManifest.hasOwnProperty(id)) {
throw new Error(
`Unable to define routes with duplicate route id: "${id}"`
);
}
routeManifest[id] = manifestItem;
if (route.children) {
for (let child of route.children) {
walk(child, id);
}
}
}
for (let route of routes) {
walk(route, rootId);
}
return routeManifest;
}
function createRouteId(file: string) {
return Path.normalize(stripFileExtension(file));
}
function stripFileExtension(file: string) {
return file.replace(/\.[a-z0-9]+$/i, "");
}
function joinRoutePaths(path1: string, path2: string): string {
return [
path1.replace(/\/+$/, ""), // Remove trailing slashes
path2.replace(/^\/+/, ""), // Remove leading slashes
].join("/");
}