-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathrouteDefinition.ts
More file actions
84 lines (73 loc) · 2.34 KB
/
Copy pathrouteDefinition.ts
File metadata and controls
84 lines (73 loc) · 2.34 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
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
import { createBrowserHistory } from 'history'
import { XRoute, XRouter } from 'xroute'
/** A simple route, matches the `/`, the root page */
export const HomeRoute = XRoute('home')
.Resource('/') // /
.Type<{
pathname: {}
search: {}
}>()
export const AdminRoute = XRoute('admin')
.Resource(
`/admin`, // /admin
)
.Type<{
pathname: {}
search: { isAdvancedView?: boolean }
}>()
enum AdminAnalyticsSubSections {
TopPages = 'topPages',
TopUsers = 'topUsers',
RawData = 'rawData',
}
const AdminAnalyticsSubsectionsURI = `:subSection(${AdminAnalyticsSubSections.TopPages}|${AdminAnalyticsSubSections.TopUsers}|${AdminAnalyticsSubSections.RawData})?`
//
// OR:
// if you dont care about type safety, do this:
//
const AdminAnalyticsSubsectionsURILoose = `:subSection(${Object.values(
AdminAnalyticsSubSections,
).join('|')})?` as const
export const AdminAnalyticsRoute = AdminRoute.Extend('adminAnalytics')
.Resource(`/analytics/${AdminAnalyticsSubsectionsURI}`) // /admin/analytics/:subSection(topPages|topUsers|rawData)
.Type<{
pathname: { subSection?: AdminAnalyticsSubSections }
search: {}
}>()
export const AdminUsersRoute = AdminRoute.Extend('adminUsers')
.Resource(`/users`) // /admin/users
.Type<{
pathname: {}
// You don't need to use the pathname at all if you want to keep it simple
// Can even nest objects and arrays.
search: {
userId?: string // ends up as ?userId=123
editor?: {
line?: string
activeToolId?: string
selectedItems?: string[]
} // ?editor[line]=1&editor[activeToolId]=2&editor[selectedItems]=3&editor[selectedItems]=4
}
}>()
export const NotFoundRoute = XRoute('notFound')
.Resource('/:someGarbage(.*)?') // /:someGarbage(.*)?
.Type<{
pathname: {
/** The pathname that didnt match any route */
someGarbage?: string
}
search: {}
}>()
export function createRouter() {
return new XRouter(
// Order matters, notice the `notFound` route is at the end, to act as a fallback
[
AdminAnalyticsRoute, // /admin/analytics/topPages
AdminUsersRoute, // /admin/users?userId=123&editor[line]=1&editor[activeToolId]=2&editor[selectedItems]=3&editor[selectedItems]=4
AdminRoute, // /admin
HomeRoute, // /
NotFoundRoute, // /asdaskjdkalsdjklasd
],
createBrowserHistory(),
)
}