crelte 0.4.8 → 0.5.0-alpha.2
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/dist/Crelte.d.ts +7 -6
- package/dist/Crelte.d.ts.map +1 -1
- package/dist/Crelte.js +5 -13
- package/dist/CrelteRequest.d.ts +9 -0
- package/dist/CrelteRequest.d.ts.map +1 -1
- package/dist/CrelteRequest.js +16 -2
- package/dist/blocks/Blocks.svelte +2 -2
- package/dist/blocks/Blocks.svelte.d.ts +3 -19
- package/dist/blocks/Blocks.svelte.d.ts.map +1 -1
- package/dist/cookies/ClientCookies.d.ts +0 -1
- package/dist/cookies/ClientCookies.d.ts.map +1 -1
- package/dist/cookies/ClientCookies.js +0 -1
- package/dist/cookies/ServerCookies.d.ts +1 -2
- package/dist/cookies/ServerCookies.d.ts.map +1 -1
- package/dist/cookies/ServerCookies.js +2 -6
- package/dist/cookies/index.d.ts +0 -2
- package/dist/cookies/index.d.ts.map +1 -1
- package/dist/graphql/GraphQl.d.ts +2 -2
- package/dist/graphql/GraphQl.d.ts.map +1 -1
- package/dist/index.d.ts +9 -3
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +14 -6
- package/dist/init/InternalApp.d.ts +30 -0
- package/dist/init/InternalApp.d.ts.map +1 -0
- package/dist/init/InternalApp.js +71 -0
- package/dist/init/client.d.ts +0 -5
- package/dist/init/client.d.ts.map +1 -1
- package/dist/init/client.js +88 -75
- package/dist/init/crelte-vite-plugin.d.ts +5 -0
- package/dist/init/server.d.ts +0 -5
- package/dist/init/server.d.ts.map +1 -1
- package/dist/init/server.js +49 -20
- package/dist/init/shared.d.ts +7 -18
- package/dist/init/shared.d.ts.map +1 -1
- package/dist/init/shared.js +97 -154
- package/dist/init/svelteComponents.d.ts +3 -0
- package/dist/init/svelteComponents.d.ts.map +1 -0
- package/dist/init/svelteComponents.js +7 -0
- package/dist/loadData/Globals.d.ts +40 -33
- package/dist/loadData/Globals.d.ts.map +1 -1
- package/dist/loadData/Globals.js +99 -88
- package/dist/loadData/index.d.ts +3 -2
- package/dist/loadData/index.d.ts.map +1 -1
- package/dist/loadData/index.js +2 -0
- package/dist/plugins/Events.d.ts +11 -13
- package/dist/plugins/Events.d.ts.map +1 -1
- package/dist/plugins/Events.js +10 -3
- package/dist/routing/BaseRoute.d.ts +255 -0
- package/dist/routing/BaseRoute.d.ts.map +1 -0
- package/dist/routing/BaseRoute.js +349 -0
- package/dist/routing/BaseRouter.d.ts +210 -0
- package/dist/routing/BaseRouter.d.ts.map +1 -0
- package/dist/routing/BaseRouter.js +444 -0
- package/dist/routing/ClientRouter.d.ts +32 -0
- package/dist/routing/ClientRouter.d.ts.map +1 -0
- package/dist/routing/ClientRouter.js +259 -0
- package/dist/routing/LoadRunner.d.ts +39 -0
- package/dist/routing/LoadRunner.d.ts.map +1 -0
- package/dist/routing/{PageLoader.js → LoadRunner.js} +32 -20
- package/dist/routing/Request.d.ts +35 -3
- package/dist/routing/Request.d.ts.map +1 -1
- package/dist/routing/Request.js +64 -5
- package/dist/routing/Route.d.ts +24 -223
- package/dist/routing/Route.d.ts.map +1 -1
- package/dist/routing/Route.js +26 -315
- package/dist/routing/Router.d.ts +49 -73
- package/dist/routing/Router.d.ts.map +1 -1
- package/dist/routing/Router.js +85 -251
- package/dist/routing/ServerRouter.d.ts +23 -0
- package/dist/routing/ServerRouter.d.ts.map +1 -0
- package/dist/routing/ServerRouter.js +57 -0
- package/dist/routing/utils.d.ts +5 -0
- package/dist/routing/utils.d.ts.map +1 -1
- package/dist/routing/utils.js +39 -0
- package/dist/utils.d.ts +1 -0
- package/dist/utils.d.ts.map +1 -1
- package/dist/utils.js +3 -0
- package/package.json +7 -6
- package/src/Crelte.ts +12 -18
- package/src/CrelteRequest.ts +21 -2
- package/src/cookies/ClientCookies.ts +0 -2
- package/src/cookies/ServerCookies.ts +2 -7
- package/src/cookies/index.ts +0 -3
- package/src/graphql/GraphQl.ts +2 -1
- package/src/index.ts +17 -9
- package/src/init/InternalApp.ts +134 -0
- package/src/init/client.ts +104 -93
- package/src/init/crelte-vite-plugin.d.ts +5 -0
- package/src/init/server.ts +67 -35
- package/src/init/shared.ts +107 -227
- package/src/init/svelteComponents.ts +12 -0
- package/src/loadData/Globals.ts +121 -102
- package/src/loadData/index.ts +3 -2
- package/src/plugins/Events.ts +40 -42
- package/src/routing/BaseRoute.ts +422 -0
- package/src/routing/BaseRouter.ts +528 -0
- package/src/routing/ClientRouter.ts +329 -0
- package/src/routing/{PageLoader.ts → LoadRunner.ts} +43 -30
- package/src/routing/Request.ts +97 -12
- package/src/routing/Route.ts +56 -376
- package/src/routing/Router.ts +100 -359
- package/src/routing/ServerRouter.ts +78 -0
- package/src/routing/utils.ts +53 -0
- package/src/utils.ts +4 -0
- package/dist/routing/InnerRouter.d.ts +0 -113
- package/dist/routing/InnerRouter.d.ts.map +0 -1
- package/dist/routing/InnerRouter.js +0 -417
- package/dist/routing/PageLoader.d.ts +0 -36
- package/dist/routing/PageLoader.d.ts.map +0 -1
- package/src/routing/InnerRouter.ts +0 -498
|
@@ -0,0 +1,349 @@
|
|
|
1
|
+
import { objClone } from '../utils.js';
|
|
2
|
+
import { trimSlashEnd } from './utils.js';
|
|
3
|
+
/**
|
|
4
|
+
* A Route contains information about the current page for example the url and
|
|
5
|
+
* the site
|
|
6
|
+
*/
|
|
7
|
+
export default class BaseRoute {
|
|
8
|
+
/**
|
|
9
|
+
* The url of the route
|
|
10
|
+
*/
|
|
11
|
+
url;
|
|
12
|
+
/**
|
|
13
|
+
* The site of the route
|
|
14
|
+
*
|
|
15
|
+
* ## Note
|
|
16
|
+
* The site might not always match with the current route
|
|
17
|
+
* but be the site default site or one that matches the
|
|
18
|
+
* users language.
|
|
19
|
+
*
|
|
20
|
+
* If that is important call `route.siteMatches()` to verify
|
|
21
|
+
*/
|
|
22
|
+
site;
|
|
23
|
+
/**
|
|
24
|
+
* The scroll position of the current route
|
|
25
|
+
*
|
|
26
|
+
* ## Note
|
|
27
|
+
* This does not have to represent the current scroll position
|
|
28
|
+
* should more be used internally.
|
|
29
|
+
*
|
|
30
|
+
* It might be useful for a new request to specify the wanted
|
|
31
|
+
* scroll position
|
|
32
|
+
*/
|
|
33
|
+
scrollY;
|
|
34
|
+
/**
|
|
35
|
+
* the position in the browser history of this route
|
|
36
|
+
* this allows to find out if we can go back
|
|
37
|
+
*/
|
|
38
|
+
index;
|
|
39
|
+
/**
|
|
40
|
+
* The origin of this route, See [[RouteOrigin]]
|
|
41
|
+
*/
|
|
42
|
+
origin;
|
|
43
|
+
/**
|
|
44
|
+
* @hidden
|
|
45
|
+
* State data that can be used to store additional information
|
|
46
|
+
*/
|
|
47
|
+
_state;
|
|
48
|
+
/**
|
|
49
|
+
* @hidden
|
|
50
|
+
* Any data that should be passed to onRoute and onRequest handlers
|
|
51
|
+
* or exchanged between loadData's
|
|
52
|
+
* This context is not persistant and should be considered "valid"
|
|
53
|
+
* only for the current request / route
|
|
54
|
+
*
|
|
55
|
+
* ## Note
|
|
56
|
+
* Consider using state instead. This will not be cloned in the clone
|
|
57
|
+
* call so will always be the same object
|
|
58
|
+
*/
|
|
59
|
+
_context;
|
|
60
|
+
/**
|
|
61
|
+
* Creates a new Route
|
|
62
|
+
*/
|
|
63
|
+
constructor(url, site, opts = {}) {
|
|
64
|
+
this.url = new URL(url);
|
|
65
|
+
this.site = site;
|
|
66
|
+
this.scrollY = opts.scrollY ?? null;
|
|
67
|
+
this.index = opts.index ?? 0;
|
|
68
|
+
this.origin = opts.origin ?? 'manual';
|
|
69
|
+
this._state = opts.state ?? {};
|
|
70
|
+
this._context = opts.context ?? {};
|
|
71
|
+
}
|
|
72
|
+
/**
|
|
73
|
+
* Returns the uri of the route
|
|
74
|
+
*
|
|
75
|
+
* Never ends with a slash
|
|
76
|
+
*
|
|
77
|
+
* ## Example
|
|
78
|
+
* ```
|
|
79
|
+
* const site = _; // site with url https://example.com/fo
|
|
80
|
+
* const route = new Route('https://example.com/foo/bar/', site);
|
|
81
|
+
* console.log(route.uri); // '/bar'
|
|
82
|
+
*
|
|
83
|
+
* const site2 = _; // site with url https://example.com/other
|
|
84
|
+
* const route2 = new Route('https://example.com/foo/bar/?a=1', site2);
|
|
85
|
+
* console.log(route2.uri); // '/foo/bar'
|
|
86
|
+
* ```
|
|
87
|
+
*/
|
|
88
|
+
get uri() {
|
|
89
|
+
if (this.siteMatches()) {
|
|
90
|
+
return trimSlashEnd(this.url.pathname.substring(this.site.uri.length));
|
|
91
|
+
}
|
|
92
|
+
return trimSlashEnd(this.url.pathname);
|
|
93
|
+
}
|
|
94
|
+
/**
|
|
95
|
+
* Returns the base url of the route
|
|
96
|
+
*
|
|
97
|
+
* Never ends with a slash
|
|
98
|
+
*
|
|
99
|
+
* ## Example
|
|
100
|
+
* ```
|
|
101
|
+
* const site = _; // site with url https://example.com/foo
|
|
102
|
+
* const route = new Route('https://example.com/foo/bar/', null);
|
|
103
|
+
* console.log(route.baseUrl); // 'https://example.com/foo'
|
|
104
|
+
*
|
|
105
|
+
* const site2 = _; // site with url https://example.com/other
|
|
106
|
+
* const route2 = new Route('https://example.com/foo/bar/', site2);
|
|
107
|
+
* console.log(route2.baseUrl); // 'https://example.com'
|
|
108
|
+
* ```
|
|
109
|
+
*/
|
|
110
|
+
get baseUrl() {
|
|
111
|
+
if (this.siteMatches())
|
|
112
|
+
return trimSlashEnd(this.site.url.href);
|
|
113
|
+
return this.url.origin;
|
|
114
|
+
}
|
|
115
|
+
/**
|
|
116
|
+
* Returns the search params
|
|
117
|
+
*
|
|
118
|
+
* ## Note
|
|
119
|
+
* You might also have a look at `getSearchParam` and `setSearchParam`
|
|
120
|
+
*
|
|
121
|
+
* ## Example
|
|
122
|
+
* ```
|
|
123
|
+
* const route = new Route('https://example.com/foo/bar/?a=1&b=2', null);
|
|
124
|
+
* console.log(route.search.get('a')); // '1'
|
|
125
|
+
* ```
|
|
126
|
+
*/
|
|
127
|
+
get search() {
|
|
128
|
+
return this.url.searchParams;
|
|
129
|
+
}
|
|
130
|
+
/**
|
|
131
|
+
* Returns the hash of the route
|
|
132
|
+
*
|
|
133
|
+
* ## Example
|
|
134
|
+
* ```
|
|
135
|
+
* const route = new Route('https://example.com/foo/bar/#hash', null);
|
|
136
|
+
* console.log(route.hash); // '#hash'
|
|
137
|
+
* ```
|
|
138
|
+
*/
|
|
139
|
+
get hash() {
|
|
140
|
+
return this.url.hash;
|
|
141
|
+
}
|
|
142
|
+
/**
|
|
143
|
+
* Set the hash of the route
|
|
144
|
+
*
|
|
145
|
+
* ## Example
|
|
146
|
+
* ```
|
|
147
|
+
* const route = new Route('https://example.com/foo/bar/', null);
|
|
148
|
+
* route.hash = '#hash';
|
|
149
|
+
* console.log(route.url.href); // 'https://example.com/foo/bar/#hash'
|
|
150
|
+
* ```
|
|
151
|
+
*/
|
|
152
|
+
set hash(hash) {
|
|
153
|
+
this.url.hash = hash;
|
|
154
|
+
}
|
|
155
|
+
/**
|
|
156
|
+
* Checks if there are previous routes which would allow it to go back
|
|
157
|
+
*/
|
|
158
|
+
canGoBack() {
|
|
159
|
+
return !!this.index;
|
|
160
|
+
}
|
|
161
|
+
/**
|
|
162
|
+
* Gets the search param
|
|
163
|
+
*
|
|
164
|
+
* ## Example
|
|
165
|
+
* ```
|
|
166
|
+
* const route = new Route('https://example.com/foo/bar/?a=1&b=2', null);
|
|
167
|
+
* console.log(route.getSearchParam('a')); // '1'
|
|
168
|
+
* ```
|
|
169
|
+
*/
|
|
170
|
+
getSearchParam(key) {
|
|
171
|
+
return this.search.get(key);
|
|
172
|
+
}
|
|
173
|
+
/**
|
|
174
|
+
* Sets the search param or removes it if the value is null, undefined or an
|
|
175
|
+
* empty string
|
|
176
|
+
*
|
|
177
|
+
* ## Example
|
|
178
|
+
* ```
|
|
179
|
+
* const route = new Route('https://example.com/foo/bar/?a=1&b=2', null);
|
|
180
|
+
* route.setSearchParam('a', '3');
|
|
181
|
+
* console.log(route.url.href); // 'https://example.com/foo/bar/?a=3&b=2'
|
|
182
|
+
*
|
|
183
|
+
* route.setSearchParam('a', null);
|
|
184
|
+
* console.log(route.url.href); // 'https://example.com/foo/bar/?b=2'
|
|
185
|
+
* ```
|
|
186
|
+
*/
|
|
187
|
+
setSearchParam(key, value) {
|
|
188
|
+
const deleteValue = typeof value === 'undefined' ||
|
|
189
|
+
value === null ||
|
|
190
|
+
(typeof value === 'string' && value === '');
|
|
191
|
+
if (!deleteValue) {
|
|
192
|
+
this.search.set(key, value);
|
|
193
|
+
}
|
|
194
|
+
else {
|
|
195
|
+
this.search.delete(key);
|
|
196
|
+
}
|
|
197
|
+
}
|
|
198
|
+
/**
|
|
199
|
+
* Returns a state value if it exists.
|
|
200
|
+
*/
|
|
201
|
+
getState(key) {
|
|
202
|
+
return this._state[key] ?? null;
|
|
203
|
+
}
|
|
204
|
+
/**
|
|
205
|
+
* Sets a state value.
|
|
206
|
+
* If the value is null or undefined, the key will be removed.
|
|
207
|
+
*
|
|
208
|
+
* ## When to use state
|
|
209
|
+
* State is used to store additional information that persists across route changes.
|
|
210
|
+
* The State is only available in the client code since it is stored using window.history.
|
|
211
|
+
*
|
|
212
|
+
* Consider using setSearchParam instead to enable server side rendering.
|
|
213
|
+
*/
|
|
214
|
+
setState(key, value) {
|
|
215
|
+
if (typeof value === 'undefined' || value === null) {
|
|
216
|
+
delete this._state[key];
|
|
217
|
+
}
|
|
218
|
+
else {
|
|
219
|
+
this._state[key] = value;
|
|
220
|
+
}
|
|
221
|
+
}
|
|
222
|
+
/**
|
|
223
|
+
* Returns a context value if it exists.
|
|
224
|
+
*/
|
|
225
|
+
getContext(key) {
|
|
226
|
+
return this._context[key] ?? null;
|
|
227
|
+
}
|
|
228
|
+
/**
|
|
229
|
+
* Sets a context value.
|
|
230
|
+
* If the value is null or undefined, the key will be removed.
|
|
231
|
+
*
|
|
232
|
+
* ## When to use context
|
|
233
|
+
* Context is used to pass data to onRoute and onRequest handlers or exchange data between loadData calls.
|
|
234
|
+
* This context is not persistent and should be considered valid only for the current request/route.
|
|
235
|
+
* The context is not cloned in the clone call and will be the same object.
|
|
236
|
+
*/
|
|
237
|
+
setContext(key, value) {
|
|
238
|
+
if (typeof value === 'undefined' || value === null) {
|
|
239
|
+
delete this._context[key];
|
|
240
|
+
}
|
|
241
|
+
else {
|
|
242
|
+
this._context[key] = value;
|
|
243
|
+
}
|
|
244
|
+
}
|
|
245
|
+
/**
|
|
246
|
+
* Returns true if the route is in live preview mode
|
|
247
|
+
*/
|
|
248
|
+
inLivePreview() {
|
|
249
|
+
return !!this.search.get('x-craft-live-preview');
|
|
250
|
+
}
|
|
251
|
+
/**
|
|
252
|
+
* Returns if the site matches the url
|
|
253
|
+
*/
|
|
254
|
+
siteMatches() {
|
|
255
|
+
if (this.url.origin !== this.site.url.origin)
|
|
256
|
+
return false;
|
|
257
|
+
// now we need to validate the pathname we should make sure both end with a slash
|
|
258
|
+
// todo can we do this better?
|
|
259
|
+
// make sure that urls like pathname: /abcbc and site: /abc don't match
|
|
260
|
+
return (this.url.pathname + '/').startsWith(
|
|
261
|
+
// uri never returns a slash at the end
|
|
262
|
+
this.site.uri + '/');
|
|
263
|
+
}
|
|
264
|
+
/**
|
|
265
|
+
* Checks if the route is equal to another route
|
|
266
|
+
*
|
|
267
|
+
* This checks the url but search params do not have to be in the
|
|
268
|
+
* same order
|
|
269
|
+
*
|
|
270
|
+
* ## Note
|
|
271
|
+
* This only check the url, not site or anything else.
|
|
272
|
+
*/
|
|
273
|
+
eq(route) {
|
|
274
|
+
return (route &&
|
|
275
|
+
this.eqUrl(route) &&
|
|
276
|
+
this.eqSearch(route) &&
|
|
277
|
+
this.eqHash(route));
|
|
278
|
+
}
|
|
279
|
+
/**
|
|
280
|
+
* Checks if the route is equal to another route
|
|
281
|
+
*
|
|
282
|
+
* This does not check the search params or hash
|
|
283
|
+
*/
|
|
284
|
+
eqUrl(route) {
|
|
285
|
+
return (route &&
|
|
286
|
+
this.url.pathname === route.url.pathname &&
|
|
287
|
+
this.url.origin === route.url.origin);
|
|
288
|
+
}
|
|
289
|
+
/**
|
|
290
|
+
* Checks if the search params are equal to another route
|
|
291
|
+
*/
|
|
292
|
+
eqSearch(route) {
|
|
293
|
+
const searchEq = (a, b) => {
|
|
294
|
+
if (a.size !== b.size)
|
|
295
|
+
return false;
|
|
296
|
+
a.sort();
|
|
297
|
+
b.sort();
|
|
298
|
+
const aEntries = Array.from(a.entries());
|
|
299
|
+
const bEntries = Array.from(b.entries());
|
|
300
|
+
return aEntries
|
|
301
|
+
.map((a, i) => [a, bEntries[i]])
|
|
302
|
+
.every(([[ak, av], [bk, bv]]) => ak === bk && av === bv);
|
|
303
|
+
};
|
|
304
|
+
return route && searchEq(this.search, route.search);
|
|
305
|
+
}
|
|
306
|
+
/**
|
|
307
|
+
* Checks if the hash is equal to another route
|
|
308
|
+
*/
|
|
309
|
+
eqHash(route) {
|
|
310
|
+
return route && this.hash === route.hash;
|
|
311
|
+
}
|
|
312
|
+
/**
|
|
313
|
+
* Create a copy of the Route
|
|
314
|
+
*
|
|
315
|
+
* ## Note
|
|
316
|
+
* This does not make a copy of the template or context
|
|
317
|
+
*/
|
|
318
|
+
clone() {
|
|
319
|
+
return new BaseRoute(this.url.href, this.site, {
|
|
320
|
+
scrollY: this.scrollY ?? undefined,
|
|
321
|
+
index: this.index,
|
|
322
|
+
origin: this.origin,
|
|
323
|
+
state: objClone(this._state),
|
|
324
|
+
context: this._context,
|
|
325
|
+
});
|
|
326
|
+
}
|
|
327
|
+
/** @hidden */
|
|
328
|
+
_fillFromState(state) {
|
|
329
|
+
// todo should this be here?
|
|
330
|
+
// not better in the request?
|
|
331
|
+
if (typeof state?.route?.scrollY === 'number')
|
|
332
|
+
this.scrollY = state.route.scrollY;
|
|
333
|
+
if (typeof state?.route?.index === 'number')
|
|
334
|
+
this.index = state.route.index;
|
|
335
|
+
if (typeof state?.state === 'object' && state.state !== null) {
|
|
336
|
+
this._state = state.state;
|
|
337
|
+
}
|
|
338
|
+
}
|
|
339
|
+
/** @hidden */
|
|
340
|
+
_toState() {
|
|
341
|
+
return {
|
|
342
|
+
route: {
|
|
343
|
+
scrollY: this.scrollY,
|
|
344
|
+
index: this.index,
|
|
345
|
+
},
|
|
346
|
+
state: this._state,
|
|
347
|
+
};
|
|
348
|
+
}
|
|
349
|
+
}
|
|
@@ -0,0 +1,210 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* This is the Router which we internally extend from
|
|
3
|
+
* it does not directly get exposed
|
|
4
|
+
*/
|
|
5
|
+
import { Writable } from 'crelte-std/stores';
|
|
6
|
+
import Request, { RequestOptions } from './Request.js';
|
|
7
|
+
import Route from './Route.js';
|
|
8
|
+
import Site, { SiteFromGraphQl } from './Site.js';
|
|
9
|
+
import LoadRunner, { LoadRunnerOptions } from './LoadRunner.js';
|
|
10
|
+
import { Entry, type CrelteRequest } from '../index.js';
|
|
11
|
+
import { Listeners } from 'crelte-std/sync';
|
|
12
|
+
export type BaseRouterOptions = {} & LoadRunnerOptions;
|
|
13
|
+
export default class BaseRouter {
|
|
14
|
+
sites: Site[];
|
|
15
|
+
private sitesByLanguage;
|
|
16
|
+
/**
|
|
17
|
+
* The current route
|
|
18
|
+
*
|
|
19
|
+
* ## Note
|
|
20
|
+
* Will always contain a route except in the first loadData call
|
|
21
|
+
*/
|
|
22
|
+
route: Writable<Route | null>;
|
|
23
|
+
/**
|
|
24
|
+
* The current site
|
|
25
|
+
*
|
|
26
|
+
* ## Note
|
|
27
|
+
* Will always contain a site except in the first loadData call
|
|
28
|
+
*/
|
|
29
|
+
site: Writable<Site | null>;
|
|
30
|
+
/**
|
|
31
|
+
* The current entry
|
|
32
|
+
*
|
|
33
|
+
* ## Note
|
|
34
|
+
* Will always contain an entry except in the first loadData call
|
|
35
|
+
*/
|
|
36
|
+
entry: Writable<Entry | null>;
|
|
37
|
+
request: Request | null;
|
|
38
|
+
/**
|
|
39
|
+
* The loading flag, specifies if a page is currently
|
|
40
|
+
* getting loaded
|
|
41
|
+
*/
|
|
42
|
+
loading: Writable<boolean>;
|
|
43
|
+
/**
|
|
44
|
+
* The loading progress, the value is between 0 and 1
|
|
45
|
+
*/
|
|
46
|
+
loadingProgress: Writable<number>;
|
|
47
|
+
onNewCrelteRequest: (req: Request) => CrelteRequest;
|
|
48
|
+
/**
|
|
49
|
+
* Not sure the naming here is great but
|
|
50
|
+
*/
|
|
51
|
+
onBeforeRequest: (cr: CrelteRequest) => Promise<void> | void;
|
|
52
|
+
onRequestListeners: Listeners<[CrelteRequest]>;
|
|
53
|
+
loadRunner: LoadRunner;
|
|
54
|
+
onRouteListeners: Listeners<[Route]>;
|
|
55
|
+
onRender: (cr: CrelteRequest,
|
|
56
|
+
/**
|
|
57
|
+
* ## Throws
|
|
58
|
+
* if the route is missing entry, template or loadedData
|
|
59
|
+
*/
|
|
60
|
+
readyForRoute: () => Route, domUpdated: (cr: CrelteRequest, route: Route) => void) => Promise<Route> | Route;
|
|
61
|
+
constructor(sites: SiteFromGraphQl[], opts: BaseRouterOptions);
|
|
62
|
+
/**
|
|
63
|
+
* Get the default site
|
|
64
|
+
*/
|
|
65
|
+
primarySite(): Site;
|
|
66
|
+
/**
|
|
67
|
+
* Should be override by environment specific router
|
|
68
|
+
*
|
|
69
|
+
* This should be based on the language
|
|
70
|
+
*
|
|
71
|
+
* todo check that the router uses the correct sites for each function
|
|
72
|
+
*/
|
|
73
|
+
defaultSite(): Site;
|
|
74
|
+
/**
|
|
75
|
+
* Get a site and if possible use the accept lang header.
|
|
76
|
+
*
|
|
77
|
+
* @param acceptLang Accept Language header.
|
|
78
|
+
*/
|
|
79
|
+
siteByAcceptLang(acceptLang?: string | null): Site;
|
|
80
|
+
/**
|
|
81
|
+
* Tries to get a site by it's id
|
|
82
|
+
*/
|
|
83
|
+
siteById(id: number): Site | null;
|
|
84
|
+
/**
|
|
85
|
+
* Resolve a url or Route and convert it to a Request
|
|
86
|
+
*
|
|
87
|
+
* @param target
|
|
88
|
+
* @param opts, any option present will override the value in target
|
|
89
|
+
* @return Returns null if the url does not match our host (the protocol get's ignored)
|
|
90
|
+
*/
|
|
91
|
+
targetToRequest(target: string | URL | Route | Request, opts?: RequestOptions): Request;
|
|
92
|
+
/**
|
|
93
|
+
* Resolve a url and convert it to a Request
|
|
94
|
+
*
|
|
95
|
+
* @param url
|
|
96
|
+
* @return Returns null if the url does not match our host (the protocol get's ignored)
|
|
97
|
+
*/
|
|
98
|
+
requestFromUrl(fullUrl: URL): Request;
|
|
99
|
+
/**
|
|
100
|
+
* Open a new route
|
|
101
|
+
*
|
|
102
|
+
* @param target the target to open can be an url, a route or a request
|
|
103
|
+
* the url needs to start with http or with a / which will be considered as
|
|
104
|
+
* the site baseUrl
|
|
105
|
+
*
|
|
106
|
+
* ## Note
|
|
107
|
+
* The origin will always be set to 'manual'
|
|
108
|
+
*
|
|
109
|
+
* ## Example
|
|
110
|
+
* ```
|
|
111
|
+
* import { getRouter } from 'crelte';
|
|
112
|
+
*
|
|
113
|
+
* const router = getRouter();
|
|
114
|
+
* console.log(router.site.get().url.href); // 'https://example.com/de';
|
|
115
|
+
*
|
|
116
|
+
* router.open('/foo/bar');
|
|
117
|
+
* // the following page will be opened https://example.com/de/foo/bar
|
|
118
|
+
* ```
|
|
119
|
+
*/
|
|
120
|
+
open(target: string | URL | Route | Request, opts?: RequestOptions): Promise<Route | void>;
|
|
121
|
+
openRequest(_req: Request): Promise<Route | void>;
|
|
122
|
+
/**
|
|
123
|
+
* This pushes the new route without triggering a new pageload
|
|
124
|
+
*
|
|
125
|
+
* You can use this when using pagination for example change the route object
|
|
126
|
+
* (search argument) and then call push
|
|
127
|
+
*
|
|
128
|
+
* ## Note
|
|
129
|
+
* This will always set the origin to 'push'
|
|
130
|
+
* And will clear the scrollY value if you not provide a new one via the `opts`
|
|
131
|
+
* This will disableLoadData by default if you not provide an override via the `opts`
|
|
132
|
+
*
|
|
133
|
+
* ## Example using the update function
|
|
134
|
+
* ```
|
|
135
|
+
* import { getRouter } from 'crelte';
|
|
136
|
+
*
|
|
137
|
+
* const router = getRouter();
|
|
138
|
+
*
|
|
139
|
+
* const page = 1;
|
|
140
|
+
* router.push(req => req.setSearchParam('page', page || null));
|
|
141
|
+
* ```
|
|
142
|
+
*
|
|
143
|
+
* ## Example using the route object
|
|
144
|
+
* ```
|
|
145
|
+
* import { getRouter } from 'crelte';
|
|
146
|
+
*
|
|
147
|
+
* const router = getRouter();
|
|
148
|
+
*
|
|
149
|
+
* const page = 1;
|
|
150
|
+
* const route = router.route.get();
|
|
151
|
+
* route.setSearchParam('page', page > 0 ? page : null);
|
|
152
|
+
* router.push(route);
|
|
153
|
+
* ```
|
|
154
|
+
*/
|
|
155
|
+
push(route: Route | Request, opts?: RequestOptions): Promise<void | Route>;
|
|
156
|
+
pushRequest(_req: Request, _opts?: RequestOptions): Promise<Route | void>;
|
|
157
|
+
/**
|
|
158
|
+
* This replaces the state of the route without triggering a new pageload
|
|
159
|
+
*
|
|
160
|
+
* You can use this when using some filters for example a search filter
|
|
161
|
+
*
|
|
162
|
+
* ## Note
|
|
163
|
+
* This will always set the origin to 'replace'
|
|
164
|
+
* And will clear the scrollY value if you don't provide a new one via the `opts`
|
|
165
|
+
* This will disableLoadData by default if you don't provide an override via the `opts`
|
|
166
|
+
*
|
|
167
|
+
* ## Example using the update function
|
|
168
|
+
* ```
|
|
169
|
+
* import { getRouter } from 'crelte';
|
|
170
|
+
*
|
|
171
|
+
* const router = getRouter();
|
|
172
|
+
*
|
|
173
|
+
* const search = 'foo';
|
|
174
|
+
* router.replace(req => req.setSearchParam('search', search));
|
|
175
|
+
* ```
|
|
176
|
+
*
|
|
177
|
+
* ## Example using the route object
|
|
178
|
+
* ```
|
|
179
|
+
* import { getRouter } from 'crelte';
|
|
180
|
+
*
|
|
181
|
+
* const router = getRouter();
|
|
182
|
+
*
|
|
183
|
+
* const search = 'foo';
|
|
184
|
+
* const route = router.route.get();
|
|
185
|
+
* route.setSearchParam('search', search);
|
|
186
|
+
* router.replace(route);
|
|
187
|
+
* ```
|
|
188
|
+
*/
|
|
189
|
+
replace(route: Route | Request, opts?: RequestOptions): Promise<void | Route>;
|
|
190
|
+
replaceRequest(_req: Request, _opts?: RequestOptions): Promise<Route | void>;
|
|
191
|
+
/**
|
|
192
|
+
* Checks if there are previous routes which would allow it to go back
|
|
193
|
+
*/
|
|
194
|
+
canGoBack(): boolean;
|
|
195
|
+
/**
|
|
196
|
+
* Go back in the history
|
|
197
|
+
*/
|
|
198
|
+
back(): void;
|
|
199
|
+
preload(target: string | URL | Route | Request): Promise<void>;
|
|
200
|
+
cancelRequest(): void;
|
|
201
|
+
/**
|
|
202
|
+
* ## Throws
|
|
203
|
+
* If the request completed but had an error
|
|
204
|
+
*/
|
|
205
|
+
handleRequest(req: Request, updateHistory: (route: Route) => void): Promise<Route | void>;
|
|
206
|
+
copyEntryToRequest(req: Request): void;
|
|
207
|
+
triggerRoute(route: Route): void;
|
|
208
|
+
updateScroll(_cr: CrelteRequest, _route: Route): void;
|
|
209
|
+
}
|
|
210
|
+
//# sourceMappingURL=BaseRouter.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"BaseRouter.d.ts","sourceRoot":"","sources":["../../src/routing/BaseRouter.ts"],"names":[],"mappings":"AAAA;;;GAGG;AAEH,OAAO,EAAE,QAAQ,EAAE,MAAM,mBAAmB,CAAC;AAC7C,OAAO,OAAO,EAAE,EAAa,cAAc,EAAE,MAAM,cAAc,CAAC;AAClE,OAAO,KAAK,MAAM,YAAY,CAAC;AAC/B,OAAO,IAAI,EAAE,EAAE,eAAe,EAAe,MAAM,WAAW,CAAC;AAE/D,OAAO,UAAU,EAAE,EAAE,iBAAiB,EAAE,MAAM,iBAAiB,CAAC;AAChE,OAAO,EAAE,KAAK,EAAE,KAAK,aAAa,EAAE,MAAM,aAAa,CAAC;AACxD,OAAO,EAAE,SAAS,EAAE,MAAM,iBAAiB,CAAC;AAG5C,MAAM,MAAM,iBAAiB,GAAG,EAAE,GAAG,iBAAiB,CAAC;AAIvD,MAAM,CAAC,OA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