-
-
Notifications
You must be signed in to change notification settings - Fork 107
/
Router.d.ts
209 lines (175 loc) · 7.1 KB
/
Router.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
///<reference types="svelte" />
import {SvelteComponent, ComponentType} from 'svelte'
import {Readable} from 'svelte/store'
/** Dictionary with route details passed to the pre-conditions functions, as well as the `routeLoading` and `conditionsFailed` events */
export interface RouteDetail {
/** Route matched as defined in the route definition (could be a string or a regular expression object) */
route: string | RegExp
/** Location path */
location: string
/** Querystring from the hash */
querystring: string
/** Params matched in the route */
params: Record<string, string> | null
/** Custom data passed by the user */
userData?: object
}
/** Detail object for the `routeLoaded` event */
export interface RouteDetailLoaded extends RouteDetail {
/** Svelte component */
component: ComponentType
/** Name of the Svelte component that was loaded (note: might be minified in production) */
name: string
}
/**
* This is a Svelte component loaded asynchronously.
* It's meant to be used with the `import()` function, such as `() => import('Foo.svelte')}`
*/
export type AsyncSvelteComponent = () => Promise<{default: ComponentType}>
/**
* Route pre-condition function. This is a callback that receives a RouteDetail object as argument containing information on the route that we're trying to load.
* The function must return a boolean indicating whether the route can be loaded. If the function returns `false`, the route is not loaded, and no other pre-condition function is executed.
*
* The pre-condition function can be asynchronous too, returning a boolean asynchronously.
*
* @param detail Route detail object
* @returns If the callback returns a false-y value, it's interpreted as the precondition failed, so it aborts loading the component (and won't process other pre-condition callbacks)
*/
export type RoutePrecondition = (detail: RouteDetail) => (boolean | Promise<boolean>)
/** Object returned by the `wrap` method */
export interface WrappedComponent {
/** Component to load (this is always asynchronous) */
component: ComponentType
/** Route pre-conditions to validate */
conditions?: RoutePrecondition[]
/** Optional dictionary of static props */
props?: object
/** Optional user data dictionary */
userData?: object
/**
* Internal flag used by the router to identify wrapped routes
* @internal
*/
//_sveltesparouter?: boolean
}
/**
* Navigates to a new page programmatically.
*
* @param location Path to navigate to (must start with `/` or '#/')
* @returns Promise that resolves after the page navigation has completed
*/
export function push(location: string): Promise<void>
/**
* Navigates back in history (equivalent to pressing the browser's back button).
*
* @returns Promise that resolves after the page navigation has completed
*/
export function pop(): Promise<void>
/**
* Replaces the current page but without modifying the history stack.
*
* @param location - Path to navigate to (must start with `/` or '#/')
* @returns Promise that resolves after the page navigation has completed
*/
export function replace(location: string): Promise<void>
/** Type for the opts parameter of the link action */
export type LinkActionOpts = {
/** A string to use in place of the link's href attribute. Using this allows for updating link's targets reactively. */
href?: string
/** If true, link is disabled */
disabled?: boolean
}
/** Type for the update function of the link action */
export type LinkActionUpdateFunc = ((opts?: LinkActionOpts) => void) |
((hrefVar?: string) => void)
/** Type for backwards-compatible (typo: Upate) */
export type LinkActionUpateFunc = LinkActionUpdateFunc
/**
* Svelte Action that enables a link element (`<a>`) to use our history management.
*
* For example:
*
* ````html
* <a href="/books" use:link>View books</a>
* ````
*
* @param node - The target node (automatically set by Svelte). Must be an anchor tag (`<a>`) with a href attribute starting in `/`
* @param opts - Dictionary with options for the link
* @param hrefVar - A string to use in place of the link's href attribute. Using this allows for updating link's targets reactively. This is a shorthand for opts.href
*/
export function link(node: HTMLElement, opts?: LinkActionOpts): {update: LinkActionUpdateFunc}
export function link(node: HTMLElement, hrefVar?: string): {update: LinkActionUpdateFunc}
/** Full location from the hash: page and querystring */
interface Location {
/** Location (page/view), for example `/book` */
location: string
/** Querystring from the hash, as a string not parsed */
querystring?: string
}
/**
* Readable store that returns the current full location (incl. querystring)
*/
export const loc: Readable<Location>
/**
* Readable store that returns the current location
*/
export const location: Readable<string>
/**
* Readable store that returns the current querystring
*/
export const querystring: Readable<string | undefined>
/**
* Readable store that returns the current list of params
*/
export const params: Readable<Record<string, string> | undefined>
// Note: the above is implemented as writable but exported as readable because consumers should not modify the value
/** List of routes */
export type RouteDefinition = Record<string, ComponentType | WrappedComponent> |
Map<string | RegExp, ComponentType | WrappedComponent>
/** Generic interface for events from the router */
interface RouterEvent<T> {
detail: T
}
/** Event type for conditionsFailed */
export type ConditionsFailedEvent = RouterEvent<RouteDetail>
/** Event type for routeLoading */
export type RouteLoadingEvent = RouterEvent<RouteDetail>
/** Event type for routeLoaded */
export type RouteLoadedEvent = RouterEvent<RouteDetailLoaded>
/**
* Router component
*/
export default class Router extends SvelteComponent {
// Props
$$prop_def: {
/**
* Dictionary of all routes, in the format `'/path': component`.
*
* For example:
* ````js
* import HomeRoute from './routes/HomeRoute.svelte'
* import BooksRoute from './routes/BooksRoute.svelte'
* import NotFoundRoute from './routes/NotFoundRoute.svelte'
* routes = {
* '/': HomeRoute,
* '/books': BooksRoute,
* '*': NotFoundRoute
* }
* ````
*/
routes: RouteDefinition,
/**
* Optional prefix for the routes in this router. This is useful for example in the case of nested routers.
*/
prefix?: string | RegExp,
/**
* If set to true, the router will restore scroll positions on back navigation
* and scroll to top on forward navigation.
*/
restoreScrollState?: boolean
}
$on(event: 'routeEvent', callback: (event: CustomEvent) => void): () => void
$on(event: 'conditionsFailed', callback: (event: ConditionsFailedEvent) => void): () => void
$on(event: 'routeLoading', callback: (event: RouteLoadingEvent) => void): () => void
$on(event: 'routeLoaded', callback: (event: RouteLoadedEvent) => void): () => void
}