-
-
Notifications
You must be signed in to change notification settings - Fork 19
/
index.d.ts
252 lines (231 loc) · 6.13 KB
/
index.d.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
import type { ReadableAtom } from 'nanostores'
// Splitting string by delimiter
type Split<S extends string, D extends string> = string extends S
? string[]
: S extends ''
? []
: S extends `${infer T}${D}${infer U}`
? [T, ...Split<U, D>]
: [S]
// Converting path array to object
type PathToParams<PathArray, Params> = PathArray extends [
infer First,
...infer Rest
]
? First extends `:${infer Param}`
? // eslint-disable-next-line @typescript-eslint/no-shadow
First extends `:${infer Param}?`
? PathToParams<Rest, Params & Partial<Record<Param, string>>>
: PathToParams<Rest, Params & Record<Param, string>>
: PathToParams<Rest, Params>
: Params
/* eslint-disable-next-line @typescript-eslint/no-empty-object-type */
type ParseUrl<Path extends string> = PathToParams<Split<Path, '/'>, {}>
export type RouterConfig = Record<string, Pattern<any> | RegExp | string>
export type ConfigFromRouter<SomeRouter> = SomeRouter extends Router<
infer Config
>
? Config
: never
// Converting routes to params
type ParamsFromConfig<K extends RouterConfig> = {
[key in keyof K]: K[key] extends Pattern<infer P>
? P
: K[key] extends string
? ParseUrl<K[key]>
: never
}
// Converting string params to string | number
type Input<T> = {
[P in keyof T]: number | string
}
type MappedC<A, B> = {
[K in keyof A & keyof B]: A[K] extends B[K] ? never : K
}
type OptionalKeys<T> = MappedC<T, Required<T>>[keyof T]
type EmptyObject = Record<string, never>
type SearchParams = Record<string, number | string>
export type ParamsArg<
Config extends RouterConfig,
PageName extends keyof Config
> = keyof ParamsFromConfig<Config>[PageName] extends never
? [EmptyObject?, SearchParams?]
: keyof ParamsFromConfig<Config>[PageName] extends OptionalKeys<
ParamsFromConfig<Config>[PageName]
>
? [Input<ParamsFromConfig<Config>[PageName]>?, SearchParams?]
: [Input<ParamsFromConfig<Config>[PageName]>, SearchParams?]
type Pattern<RouteParams> = Readonly<
[RegExp, (...parts: string[]) => RouteParams]
>
export type InputPage<
Config extends RouterConfig = RouterConfig,
PageName extends keyof Config = any
> = PageName extends any
? Input<ParamsFromConfig<Config>[PageName]> extends EmptyObject
? {
params?: Input<ParamsFromConfig<Config>[PageName]>
route: PageName
}
: {
params: Input<ParamsFromConfig<Config>[PageName]>
route: PageName
}
: never
export type Page<
Config extends RouterConfig = RouterConfig,
PageName extends keyof Config = any
> = PageName extends any
? {
hash: string
params: ParamsFromConfig<Config>[PageName]
path: string
route: PageName
search: Record<string, string>
}
: never
export interface RouterOptions {
links?: boolean
search?: boolean
}
/**
* Router store. Use {@link createRouter} to create it.
*
* It is a simple router without callbacks. Think about it as a URL parser.
*
* ```ts
* import { createRouter } from 'nanostores'
*
* export const router = createRouter({
* home: '/',
* category: '/posts/:categoryId',
* post: '/posts/:categoryId/:id'
* })
* ```
*/
export interface Router<Config extends RouterConfig = RouterConfig>
extends ReadableAtom<Page<Config, keyof Config> | undefined> {
/**
* Open URL without page reloading.
*
* ```js
* router.open('/posts/guides/10')
* ```
*
* @param path Absolute URL (`https://example.com/a`)
* or domain-less URL (`/a`).
* @param redirect Don’t add entry to the navigation history.
*/
open(path: string, redirect?: boolean): void
/**
* Converted routes.
*/
routes: [string, RegExp, (...params: string[]) => object, string?][]
}
/**
* Create {@link Router} store.
*
* ```ts
* import { createRouter } from 'nanostores'
*
* export const router = createRouter({
* home: '/',
* category: '/posts/:categoryId',
* post: '/posts/:categoryId/:id'
* })
* ```
*
* @param routes URL patterns.
* @param opts Options.
*/
export function createRouter<const Config extends RouterConfig>(
routes: Config,
opts?: RouterOptions
): Router<Config>
/**
* Open page by name and parameters. Pushes new state into history.
*
* ```js
* import { openPage } from 'nanostores'
*
* openPage(router, 'post', { categoryId: 'guides', id: '10' })
* ```
*
* @param name Route name.
* @param params Route parameters.
*/
export function openPage<
Config extends RouterConfig,
PageName extends keyof Config
>(
router: Router<Config>,
name: PageName,
...params: ParamsArg<Config, PageName>
): void
export function openPage<
Config extends RouterConfig,
PageName extends keyof Config
>(
router: Router<Config>,
route: InputPage<Config, PageName>,
search?: SearchParams
): void
/**
* Open page by name and parameters. Replaces recent state in history.
*
* ```js
* import { redirectPage } from '@logux/state'
*
* openPage(router, 'login')
* // replace login route, so we don't face it on back navigation
* redirectPage(router, 'post', { categoryId: 'guides', id: '10' })
* ```
*
* @param name Route name.
* @param params Route parameters.
*/
export function redirectPage<
Config extends RouterConfig,
PageName extends keyof Config
>(
router: Router<Config>,
name: PageName,
...params: ParamsArg<Config, PageName>
): void
export function redirectPage<
Config extends RouterConfig,
PageName extends keyof Config
>(
router: Router<Config>,
route: InputPage<Config, PageName>,
search?: SearchParams
): void
/**
* Generates pathname by name and parameters. Useful to render links.
*
* ```js
* import { getPageUrl } from 'nanostores'
*
* getPageUrl(router, 'post', { categoryId: 'guides', id: '10' })
* //=> '/posts/guides/10'
* ```
*
* @param name Route name.
* @param params Route parameters.
*/
export function getPagePath<
Config extends RouterConfig,
PageName extends keyof Config
>(
router: Router<Config>,
name: PageName,
...params: ParamsArg<Config, PageName>
): string
export function getPagePath<
Config extends RouterConfig,
PageName extends keyof Config
>(
router: Router<Config>,
route: InputPage<Config, PageName>,
search?: SearchParams
): string