@keenmate/svelte-spa-router 1.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/Router.d.ts ADDED
@@ -0,0 +1,221 @@
1
+ ///<reference types="svelte" />
2
+
3
+ import {ComponentType, SvelteComponent} from "svelte"
4
+ import {Readable, Writable} from "svelte/store"
5
+
6
+ /** Dictionary with route details passed to the pre-conditions functions, as well as the `routeLoading` and `conditionsFailed` events */
7
+ export interface RouteDetail {
8
+ /** Route matched as defined in the route definition (could be a string or a regular expression object) */
9
+ route: string | RegExp
10
+
11
+ /** Location path */
12
+ location: string
13
+
14
+ /** Querystring from the hash */
15
+ querystring: string
16
+
17
+ /** Params matched in the route */
18
+ params: Record<string, string> | null
19
+
20
+ /** Custom data passed by the user */
21
+ userData?: object
22
+ }
23
+
24
+ /** Detail object for the `routeLoaded` event */
25
+ export interface RouteDetailLoaded extends RouteDetail {
26
+ /** Svelte component */
27
+ component: ComponentType
28
+
29
+ /** Name of the Svelte component that was loaded (note: might be minified in production) */
30
+ name: string
31
+ }
32
+
33
+ /**
34
+ * This is a Svelte component loaded asynchronously.
35
+ * It's meant to be used with the `import()` function, such as `() => import('Foo.svelte')}`
36
+ */
37
+ export type AsyncSvelteComponent = () => Promise<{ default: ComponentType }>
38
+
39
+ /**
40
+ * 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.
41
+ * 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.
42
+ *
43
+ * The pre-condition function can be asynchronous too, returning a boolean asynchronously.
44
+ *
45
+ * @param detail Route detail object
46
+ * @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)
47
+ */
48
+ export type RoutePrecondition = (detail: RouteDetail) => (boolean | Promise<boolean>)
49
+
50
+ /** Object returned by the `wrap` method */
51
+ export interface WrappedComponent {
52
+ /** Component to load (this is always asynchronous) */
53
+ component: ComponentType
54
+
55
+ /** Route pre-conditions to validate */
56
+ conditions?: RoutePrecondition[]
57
+
58
+ /** Optional dictionary of static props */
59
+ props?: object
60
+
61
+ /** Optional user data dictionary */
62
+ userData?: object
63
+
64
+ /**
65
+ * Internal flag used by the router to identify wrapped routes
66
+ * @internal
67
+ */
68
+ //_sveltesparouter?: boolean
69
+ }
70
+
71
+ /**
72
+ * Navigates to a new page programmatically.
73
+ *
74
+ * @param location Path to navigate to (must start with `/` or '#/')
75
+ * @returns Promise that resolves after the page navigation has completed
76
+ */
77
+ export function push(location: string): Promise<void>
78
+
79
+ /**
80
+ * Navigates back in history (equivalent to pressing the browser's back button).
81
+ *
82
+ * @returns Promise that resolves after the page navigation has completed
83
+ */
84
+ export function pop(): Promise<void>
85
+
86
+ /**
87
+ * Replaces the current page but without modifying the history stack.
88
+ *
89
+ * @param location - Path to navigate to (must start with `/` or '#/')
90
+ * @returns Promise that resolves after the page navigation has completed
91
+ */
92
+ export function replace(location: string): Promise<void>
93
+
94
+ /** Type for the opts parameter of the link action */
95
+ export type LinkActionOpts = {
96
+ /** A string to use in place of the link's href attribute. Using this allows for updating link's targets reactively. */
97
+ href?: string
98
+ /** If true, link is disabled */
99
+ disabled?: boolean
100
+ }
101
+
102
+ /** Type for the update function of the link action */
103
+ export type LinkActionUpdateFunc = ((opts?: LinkActionOpts) => void) |
104
+ ((hrefVar?: string) => void)
105
+
106
+ /** Type for backwards-compatible (typo: Upate) */
107
+ export type LinkActionUpateFunc = LinkActionUpdateFunc
108
+
109
+ /**
110
+ * Svelte Action that enables a link element (`<a>`) to use our history management.
111
+ *
112
+ * For example:
113
+ *
114
+ * ````html
115
+ * <a href="/books" use:link>View books</a>
116
+ * ````
117
+ *
118
+ * @param node - The target node (automatically set by Svelte). Must be an anchor tag (`<a>`) with a href attribute starting in `/`
119
+ * @param opts - Dictionary with options for the link
120
+ * @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
121
+ */
122
+ export function link(node: HTMLElement, opts?: LinkActionOpts): { update: LinkActionUpdateFunc }
123
+ export function link(node: HTMLElement, hrefVar?: string): { update: LinkActionUpdateFunc }
124
+
125
+ /** Full location from the hash: page and querystring */
126
+ interface Location {
127
+ /** Location (page/view), for example `/book` */
128
+ location: string
129
+
130
+ /** Querystring from the hash, as a string not parsed */
131
+ querystring?: string
132
+ }
133
+
134
+ /**
135
+ * Store to configure whether the router operates on routes hidden behind # or not
136
+ */
137
+ export const HashRoutingEnabled: Writable<boolean>
138
+
139
+ /**
140
+ * Used when hash-based routing is disabled and in normal scenario should be filled with `import.meta.env.BASE_URL` (in case of Vite app)
141
+ * A '/' prefixed path that will be excluded from URL when dealing with route navigation
142
+ */
143
+ export const BasePath: Writable<string>
144
+
145
+ /**
146
+ * Readable store that returns the current full location (incl. querystring)
147
+ */
148
+ export const loc: Readable<Location>
149
+
150
+ /**
151
+ * Readable store that returns the current location
152
+ */
153
+ export const location: Readable<string>
154
+
155
+ /**
156
+ * Readable store that returns the current querystring
157
+ */
158
+ export const querystring: Readable<string | undefined>
159
+
160
+ /**
161
+ * Readable store that returns the current list of params
162
+ */
163
+ export const params: Readable<Record<string, string> | undefined>
164
+ // Note: the above is implemented as writable but exported as readable because consumers should not modify the value
165
+
166
+ /** List of routes */
167
+ export type RouteDefinition = Record<string, ComponentType | WrappedComponent> |
168
+ Map<string | RegExp, ComponentType | WrappedComponent>
169
+
170
+ /** Generic interface for events from the router */
171
+ interface RouterEvent<T> {
172
+ detail: T
173
+ }
174
+
175
+ /** Event type for conditionsFailed */
176
+ export type ConditionsFailedEvent = RouterEvent<RouteDetail>
177
+
178
+ /** Event type for routeLoading */
179
+ export type RouteLoadingEvent = RouterEvent<RouteDetail>
180
+
181
+ /** Event type for routeLoaded */
182
+ export type RouteLoadedEvent = RouterEvent<RouteDetailLoaded>
183
+
184
+ /**
185
+ * Router component
186
+ */
187
+ export default class Router extends SvelteComponent {
188
+ // Props
189
+ $$prop_def: {
190
+ /**
191
+ * Dictionary of all routes, in the format `'/path': component`.
192
+ *
193
+ * For example:
194
+ * ````js
195
+ * import HomeRoute from './routes/HomeRoute.svelte'
196
+ * import BooksRoute from './routes/BooksRoute.svelte'
197
+ * import NotFoundRoute from './routes/NotFoundRoute.svelte'
198
+ * routes = {
199
+ * '/': HomeRoute,
200
+ * '/books': BooksRoute,
201
+ * '*': NotFoundRoute
202
+ * }
203
+ * ````
204
+ */
205
+ routes: RouteDefinition,
206
+ /**
207
+ * Optional prefix for the routes in this router. This is useful for example in the case of nested routers.
208
+ */
209
+ prefix?: string | RegExp,
210
+ /**
211
+ * If set to true, the router will restore scroll positions on back navigation
212
+ * and scroll to top on forward navigation.
213
+ */
214
+ restoreScrollState?: boolean
215
+ }
216
+
217
+ $on(event: "routeEvent", callback: (event: CustomEvent) => void): () => void
218
+ $on(event: "conditionsFailed", callback: (event: ConditionsFailedEvent) => void): () => void
219
+ $on(event: "routeLoading", callback: (event: RouteLoadingEvent) => void): () => void
220
+ $on(event: "routeLoaded", callback: (event: RouteLoadedEvent) => void): () => void
221
+ }