crelte 0.5.8 → 0.5.9
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/blocks/Blocks.d.ts +36 -0
- package/dist/blocks/Blocks.d.ts.map +1 -1
- package/dist/blocks/Blocks.js +42 -0
- package/dist/blocks/Blocks.svelte +2 -16
- package/dist/blocks/Blocks.svelte.d.ts +20 -37
- package/dist/blocks/Blocks.svelte.d.ts.map +1 -1
- package/dist/blocks/index.d.ts +2 -2
- package/dist/blocks/index.d.ts.map +1 -1
- package/dist/blocks/index.js +2 -2
- package/dist/cookies/ServerCookies.d.ts +1 -1
- package/dist/cookies/ServerCookies.js +1 -1
- package/dist/cookies/index.d.ts +1 -1
- package/dist/crelte.d.ts +21 -10
- package/dist/crelte.d.ts.map +1 -1
- package/dist/index.d.ts +19 -14
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +17 -13
- package/dist/init/client.d.ts +2 -2
- package/dist/init/client.d.ts.map +1 -1
- package/dist/init/client.js +3 -2
- package/dist/init/server.d.ts +7 -6
- package/dist/init/server.d.ts.map +1 -1
- package/dist/init/server.js +6 -6
- package/dist/loadData/Globals.d.ts +3 -3
- package/dist/loadData/Globals.js +3 -3
- package/dist/loadData/loadData.d.ts +8 -18
- package/dist/loadData/loadData.d.ts.map +1 -1
- package/dist/loadData/loadData.js +2 -2
- package/dist/node/index.d.ts +5 -0
- package/dist/node/index.d.ts.map +1 -1
- package/dist/node/index.js +84 -35
- package/dist/plugins/Events.d.ts +4 -4
- package/dist/plugins/Plugins.d.ts +2 -2
- package/dist/plugins/index.d.ts +2 -3
- package/dist/plugins/index.d.ts.map +1 -1
- package/dist/plugins/index.js +2 -1
- package/dist/queries/Queries.d.ts +9 -0
- package/dist/queries/Queries.d.ts.map +1 -1
- package/dist/queries/Queries.js +29 -17
- package/dist/queries/QueryError.d.ts +2 -0
- package/dist/queries/QueryError.d.ts.map +1 -1
- package/dist/queries/QueryError.js +6 -1
- package/dist/queries/gql.d.ts +2 -2
- package/dist/queries/gql.js +2 -2
- package/dist/queries/index.d.ts +7 -5
- package/dist/queries/index.d.ts.map +1 -1
- package/dist/queries/vars.d.ts +7 -7
- package/dist/queries/vars.d.ts.map +1 -1
- package/dist/queries/vars.js +10 -8
- package/dist/routing/LoadRunner.d.ts +1 -1
- package/dist/routing/LoadRunner.js +1 -1
- package/dist/routing/index.d.ts +4 -4
- package/dist/routing/index.d.ts.map +1 -1
- package/dist/routing/route/BaseRoute.d.ts +25 -25
- package/dist/routing/route/BaseRoute.js +24 -24
- package/dist/routing/route/Request.d.ts +4 -4
- package/dist/routing/route/Request.js +4 -4
- package/dist/routing/route/Route.d.ts +2 -2
- package/dist/routing/route/Route.js +2 -2
- package/dist/routing/router/BaseRouter.d.ts +4 -4
- package/dist/routing/router/BaseRouter.js +3 -3
- package/dist/routing/router/ClientRouter.d.ts +1 -1
- package/dist/routing/router/ClientRouter.js +1 -1
- package/dist/routing/router/Router.d.ts +27 -25
- package/dist/routing/router/Router.d.ts.map +1 -1
- package/dist/routing/router/Router.js +24 -21
- package/dist/routing/router/ServerRouter.d.ts +1 -1
- package/dist/routing/router/ServerRouter.js +1 -1
- package/dist/server/CrelteServer.d.ts +5 -5
- package/dist/server/CrelteServer.d.ts.map +1 -1
- package/dist/server/CrelteServer.js +4 -4
- package/dist/server/Request.d.ts +7 -1
- package/dist/server/Request.d.ts.map +1 -1
- package/dist/server/Request.js +7 -1
- package/dist/server/ServerRouter.d.ts +1 -1
- package/dist/server/ServerRouter.d.ts.map +1 -1
- package/dist/server/index.d.ts +4 -4
- package/dist/server/index.d.ts.map +1 -1
- package/dist/server/index.js +1 -1
- package/dist/server/shared.d.ts +7 -13
- package/dist/server/shared.d.ts.map +1 -1
- package/dist/server/shared.js +14 -16
- package/dist/ssr/SsrCache.d.ts +56 -4
- package/dist/ssr/SsrCache.d.ts.map +1 -1
- package/dist/ssr/SsrCache.js +94 -20
- package/dist/std/index.d.ts +13 -0
- package/dist/std/index.d.ts.map +1 -1
- package/dist/std/index.js +13 -0
- package/dist/std/rand/index.d.ts +34 -0
- package/dist/std/rand/index.d.ts.map +1 -0
- package/dist/std/rand/index.js +45 -0
- package/dist/std/rand/internal.d.ts +8 -0
- package/dist/std/rand/internal.d.ts.map +1 -0
- package/dist/std/rand/internal.js +15 -0
- package/dist/std/stores/Writable.d.ts +1 -1
- package/dist/std/stores/Writable.js +1 -1
- package/dist/translations/index.d.ts +28 -0
- package/dist/translations/index.d.ts.map +1 -0
- package/dist/translations/index.js +31 -0
- package/dist/translations/loadTranslations.d.ts +23 -0
- package/dist/translations/loadTranslations.d.ts.map +1 -0
- package/dist/translations/loadTranslations.js +26 -0
- package/dist/translations/loader/GlobalLoader.d.ts +34 -0
- package/dist/translations/loader/GlobalLoader.d.ts.map +1 -0
- package/dist/translations/loader/GlobalLoader.js +45 -0
- package/dist/translations/loader/fileLoader/ClientFileLoader.d.ts +10 -0
- package/dist/translations/loader/fileLoader/ClientFileLoader.d.ts.map +1 -0
- package/dist/translations/loader/fileLoader/ClientFileLoader.js +24 -0
- package/dist/translations/loader/fileLoader/NodeFileLoader.d.ts +9 -0
- package/dist/translations/loader/fileLoader/NodeFileLoader.d.ts.map +1 -0
- package/dist/translations/loader/fileLoader/NodeFileLoader.js +21 -0
- package/dist/translations/loader/fileLoader/fileLoader.d.ts +5 -0
- package/dist/translations/loader/fileLoader/fileLoader.d.ts.map +1 -0
- package/dist/translations/loader/fileLoader/fileLoader.js +16 -0
- package/dist/translations/loader/index.d.ts +7 -0
- package/dist/translations/loader/index.d.ts.map +1 -0
- package/dist/translations/loader/index.js +1 -0
- package/dist/translations/translationsPlugin.d.ts +64 -0
- package/dist/translations/translationsPlugin.d.ts.map +1 -0
- package/dist/translations/translationsPlugin.js +110 -0
- package/dist/translations/utils.d.ts +8 -0
- package/dist/translations/utils.d.ts.map +1 -0
- package/dist/translations/utils.js +15 -0
- package/dist/vite/index.d.ts.map +1 -1
- package/dist/vite/index.js +30 -38
- package/package.json +9 -1
- package/src/blocks/Blocks.svelte +4 -63
- package/src/blocks/Blocks.ts +53 -0
- package/src/blocks/index.ts +2 -2
- package/src/cookies/ServerCookies.ts +1 -1
- package/src/cookies/index.ts +1 -1
- package/src/crelte.ts +21 -10
- package/src/index.ts +19 -14
- package/src/init/client.ts +3 -2
- package/src/init/server.ts +14 -8
- package/src/loadData/Globals.ts +3 -3
- package/src/loadData/loadData.ts +8 -18
- package/src/node/index.ts +100 -39
- package/src/plugins/Events.ts +4 -4
- package/src/plugins/Plugins.ts +2 -2
- package/src/plugins/index.ts +2 -3
- package/src/queries/Queries.ts +35 -18
- package/src/queries/QueryError.ts +10 -1
- package/src/queries/gql.ts +2 -2
- package/src/queries/index.ts +7 -4
- package/src/queries/vars.ts +12 -8
- package/src/routing/LoadRunner.ts +1 -1
- package/src/routing/index.ts +13 -3
- package/src/routing/route/BaseRoute.ts +25 -25
- package/src/routing/route/Request.ts +4 -4
- package/src/routing/route/Route.ts +2 -2
- package/src/routing/router/BaseRouter.ts +4 -4
- package/src/routing/router/ClientRouter.ts +1 -1
- package/src/routing/router/Router.ts +28 -25
- package/src/routing/router/ServerRouter.ts +1 -1
- package/src/server/CrelteServer.ts +5 -5
- package/src/server/Request.ts +7 -1
- package/src/server/ServerRouter.ts +1 -2
- package/src/server/index.ts +17 -3
- package/src/server/shared.ts +28 -33
- package/src/ssr/SsrCache.ts +104 -22
- package/src/std/index.ts +14 -0
- package/src/std/rand/index.ts +55 -0
- package/src/std/rand/internal.ts +17 -0
- package/src/std/stores/Writable.ts +1 -1
- package/src/translations/index.ts +56 -0
- package/src/translations/loadTranslations.ts +33 -0
- package/src/translations/loader/GlobalLoader.ts +62 -0
- package/src/translations/loader/fileLoader/ClientFileLoader.ts +40 -0
- package/src/translations/loader/fileLoader/NodeFileLoader.ts +32 -0
- package/src/translations/loader/fileLoader/fileLoader.ts +19 -0
- package/src/translations/loader/index.ts +8 -0
- package/src/translations/translationsPlugin.ts +145 -0
- package/src/translations/utils.ts +17 -0
- package/src/vite/index.ts +43 -39
package/src/queries/Queries.ts
CHANGED
|
@@ -91,12 +91,21 @@ export default class Queries {
|
|
|
91
91
|
this.request = request;
|
|
92
92
|
}
|
|
93
93
|
|
|
94
|
+
/**
|
|
95
|
+
* Create a new Queries instance
|
|
96
|
+
*
|
|
97
|
+
* @param endpoint the craft GraphQl endpoint
|
|
98
|
+
* @param frontend the frontend url where the server queries are reachable
|
|
99
|
+
* @param ssrCache the ssrCache to use for caching
|
|
100
|
+
* @param opts options
|
|
101
|
+
* @returns
|
|
102
|
+
*/
|
|
94
103
|
static new(
|
|
95
104
|
endpoint: string,
|
|
96
105
|
frontend: string,
|
|
97
106
|
ssrCache: SsrCache,
|
|
98
107
|
opts: QueriesOptions = {},
|
|
99
|
-
) {
|
|
108
|
+
): Queries {
|
|
100
109
|
return new Queries(
|
|
101
110
|
new Inner(endpoint, frontend, ssrCache, opts),
|
|
102
111
|
opts?.route ?? null,
|
|
@@ -131,6 +140,8 @@ export default class Queries {
|
|
|
131
140
|
// for convenience we set the siteId as variable if it is known
|
|
132
141
|
if (siteId !== null) vars = { siteId, ...vars };
|
|
133
142
|
|
|
143
|
+
// todo, maybe we should set the XCraftSiteId, with the siteId from vars?
|
|
144
|
+
|
|
134
145
|
return this.inner.query(query, vars, {
|
|
135
146
|
previewToken,
|
|
136
147
|
siteToken,
|
|
@@ -217,16 +228,15 @@ class Inner {
|
|
|
217
228
|
|
|
218
229
|
try {
|
|
219
230
|
const resp = await this.queryNotCached(query, vars, opts);
|
|
231
|
+
|
|
220
232
|
if (key) {
|
|
221
233
|
// todo maybe we should objClone here?
|
|
222
234
|
this.ssrCache.set(key, resp);
|
|
223
235
|
|
|
224
|
-
// ! because the listeners get's in the previous
|
|
225
|
-
// always be set when the key is set
|
|
236
|
+
// ! (never null) because the listeners get's in the previous
|
|
237
|
+
// if statement and will always be set when the key is set
|
|
226
238
|
const listeners = this.listeners.get(key)!;
|
|
227
|
-
listeners.forEach(([resolve, _error]) =>
|
|
228
|
-
resolve(resp);
|
|
229
|
-
});
|
|
239
|
+
listeners.forEach(([resolve, _error]) => resolve(resp));
|
|
230
240
|
|
|
231
241
|
this.listeners.delete(key);
|
|
232
242
|
}
|
|
@@ -234,12 +244,10 @@ class Inner {
|
|
|
234
244
|
return resp;
|
|
235
245
|
} catch (e: unknown) {
|
|
236
246
|
if (key) {
|
|
237
|
-
// ! because the listeners get's in the previous
|
|
238
|
-
// always be set when the key is set
|
|
247
|
+
// ! (never null) because the listeners get's in the previous
|
|
248
|
+
// if statement and will always be set when the key is set
|
|
239
249
|
const listeners = this.listeners.get(key)!;
|
|
240
|
-
listeners.forEach(([_resolve, error]) =>
|
|
241
|
-
error(e);
|
|
242
|
-
});
|
|
250
|
+
listeners.forEach(([_resolve, error]) => error(e));
|
|
243
251
|
|
|
244
252
|
this.listeners.delete(key);
|
|
245
253
|
}
|
|
@@ -291,11 +299,17 @@ class Inner {
|
|
|
291
299
|
body = { query: query.query, variables: vars };
|
|
292
300
|
}
|
|
293
301
|
|
|
294
|
-
|
|
295
|
-
|
|
296
|
-
|
|
297
|
-
|
|
298
|
-
|
|
302
|
+
let resp: Response;
|
|
303
|
+
|
|
304
|
+
try {
|
|
305
|
+
resp = await fetch(url, {
|
|
306
|
+
method: 'POST',
|
|
307
|
+
headers,
|
|
308
|
+
body: JSON.stringify(body),
|
|
309
|
+
});
|
|
310
|
+
} catch (e: any) {
|
|
311
|
+
throw new Error(`query to '${url}' failed with: ${e.message}`);
|
|
312
|
+
}
|
|
299
313
|
|
|
300
314
|
if (!resp.ok) {
|
|
301
315
|
throw new QueryError(
|
|
@@ -322,8 +336,11 @@ class Inner {
|
|
|
322
336
|
}
|
|
323
337
|
|
|
324
338
|
if ('errors' in jsonResp) {
|
|
325
|
-
console.
|
|
326
|
-
throw new QueryError(
|
|
339
|
+
console.error(logName + ' errors', jsonResp.errors);
|
|
340
|
+
throw new QueryError(
|
|
341
|
+
{ status: resp.status, errors: jsonResp.errors },
|
|
342
|
+
'received errors',
|
|
343
|
+
);
|
|
327
344
|
}
|
|
328
345
|
|
|
329
346
|
return jsonResp.data ?? null;
|
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
export type QueryErrorResponse = {
|
|
2
2
|
status?: number;
|
|
3
3
|
body?: string;
|
|
4
|
+
errors?: any[];
|
|
4
5
|
};
|
|
5
6
|
|
|
6
7
|
// todo improve this
|
|
@@ -32,6 +33,14 @@ export default class QueryError extends Error {
|
|
|
32
33
|
* The error message in string form
|
|
33
34
|
*/
|
|
34
35
|
get message(): string {
|
|
35
|
-
return 'QueryError: ' + JSON.stringify(this.resp);
|
|
36
|
+
return 'QueryError: ' + JSON.stringify(this.resp, null, ' ');
|
|
36
37
|
}
|
|
37
38
|
}
|
|
39
|
+
|
|
40
|
+
export function isQueryError(err: any): err is QueryError {
|
|
41
|
+
return (
|
|
42
|
+
typeof err === 'object' &&
|
|
43
|
+
err !== null &&
|
|
44
|
+
typeof err.__QueryError__ === 'function'
|
|
45
|
+
);
|
|
46
|
+
}
|
package/src/queries/gql.ts
CHANGED
package/src/queries/index.ts
CHANGED
|
@@ -5,7 +5,7 @@ import Queries, {
|
|
|
5
5
|
QueryOptions,
|
|
6
6
|
} from '../queries/Queries.js';
|
|
7
7
|
import { gql } from './gql.js';
|
|
8
|
-
import QueryError from './QueryError.js';
|
|
8
|
+
import QueryError, { QueryErrorResponse } from './QueryError.js';
|
|
9
9
|
import { QueryVar, ValidIf, vars, varsIdsEqual } from './vars.js';
|
|
10
10
|
|
|
11
11
|
export {
|
|
@@ -15,6 +15,7 @@ export {
|
|
|
15
15
|
type Query,
|
|
16
16
|
isQuery,
|
|
17
17
|
QueryError,
|
|
18
|
+
type QueryErrorResponse,
|
|
18
19
|
gql,
|
|
19
20
|
vars,
|
|
20
21
|
type ValidIf,
|
|
@@ -22,14 +23,16 @@ export {
|
|
|
22
23
|
varsIdsEqual,
|
|
23
24
|
};
|
|
24
25
|
|
|
25
|
-
|
|
26
|
+
/** @hidden */
|
|
27
|
+
export type InferQueryVarType<T> = T extends QueryVar<infer U> ? U : never;
|
|
26
28
|
|
|
27
|
-
|
|
29
|
+
/** @hidden */
|
|
30
|
+
export type InferVariableTypes<T> = {
|
|
28
31
|
[K in keyof T]: InferQueryVarType<T[K]>;
|
|
29
32
|
};
|
|
30
33
|
|
|
31
34
|
/**
|
|
32
|
-
*
|
|
35
|
+
* #### Example
|
|
33
36
|
* ```ts
|
|
34
37
|
* import { vars, Caching } from 'crelte/queries';
|
|
35
38
|
*
|
package/src/queries/vars.ts
CHANGED
|
@@ -9,7 +9,7 @@ export const vars = {
|
|
|
9
9
|
* Id is almost the same as number but will also parse
|
|
10
10
|
* strings, but only allow non negative integers
|
|
11
11
|
*
|
|
12
|
-
*
|
|
12
|
+
* #### Warning
|
|
13
13
|
* Ids are not automatically safe to be cached
|
|
14
14
|
* you need to validate the response to make sure filters
|
|
15
15
|
* with this id returned something
|
|
@@ -24,7 +24,7 @@ export const vars = {
|
|
|
24
24
|
*
|
|
25
25
|
* The numbers are always unique and sorted in ascending order
|
|
26
26
|
*
|
|
27
|
-
*
|
|
27
|
+
* #### Warning
|
|
28
28
|
* Ids are not automatically safe to be cached, it is also not
|
|
29
29
|
* enough to just check if the filter returned some results.
|
|
30
30
|
* Since for example a `relatedTo` filter works like an `or` and
|
|
@@ -180,7 +180,11 @@ export class QueryVar<T = any> {
|
|
|
180
180
|
}
|
|
181
181
|
|
|
182
182
|
export function isQueryVar(v: any): v is QueryVar {
|
|
183
|
-
return
|
|
183
|
+
return (
|
|
184
|
+
typeof v === 'object' &&
|
|
185
|
+
v !== null &&
|
|
186
|
+
typeof v.__QueryVar__ === 'function'
|
|
187
|
+
);
|
|
184
188
|
}
|
|
185
189
|
|
|
186
190
|
// does not do string to number conversion
|
|
@@ -194,8 +198,8 @@ function isValidId(id: any): id is number {
|
|
|
194
198
|
* The first argument needs to come from a `vars.ids()` variable.
|
|
195
199
|
* The second argument should come from a query, where the output is trusted.
|
|
196
200
|
*
|
|
197
|
-
*
|
|
198
|
-
* ```
|
|
201
|
+
* #### Example
|
|
202
|
+
* ```js
|
|
199
203
|
* export const variables = {
|
|
200
204
|
* categories: vars.ids()
|
|
201
205
|
* };
|
|
@@ -206,15 +210,15 @@ function isValidId(id: any): id is number {
|
|
|
206
210
|
* };
|
|
207
211
|
* ```
|
|
208
212
|
*
|
|
209
|
-
*
|
|
213
|
+
* #### Note
|
|
210
214
|
* The following cases are considered equal:
|
|
211
|
-
* ```
|
|
215
|
+
* ```js
|
|
212
216
|
* varsIdsEqual(null, null);
|
|
213
217
|
* varsIdsEqual([], null);
|
|
214
218
|
* varsIdsEqual([1,2], ['2',1]);
|
|
215
219
|
* ```
|
|
216
220
|
* These are not equal:
|
|
217
|
-
* ```
|
|
221
|
+
* ```js
|
|
218
222
|
* varsIdsEqual([1], null);
|
|
219
223
|
* varsIdsEqual([2,1], [2,1]); // because the second arg gets ordered
|
|
220
224
|
* ```
|
package/src/routing/index.ts
CHANGED
|
@@ -1,22 +1,32 @@
|
|
|
1
1
|
import Router, { type UpdateRequest } from './router/Router.js';
|
|
2
|
-
import Route, {
|
|
2
|
+
import Route, {
|
|
3
|
+
type TemplateModule,
|
|
4
|
+
type RouteOptions,
|
|
5
|
+
} from './route/Route.js';
|
|
3
6
|
import Request, {
|
|
4
7
|
type RequestOptions,
|
|
5
8
|
type DelayRender,
|
|
6
9
|
} from './route/Request.js';
|
|
7
|
-
import Site from './Site.js';
|
|
8
|
-
import BaseRoute
|
|
10
|
+
import Site, { type SiteFromGraphQl } from './Site.js';
|
|
11
|
+
import BaseRoute, {
|
|
12
|
+
type BaseRouteOptions,
|
|
13
|
+
type RouteOrigin,
|
|
14
|
+
} from './route/BaseRoute.js';
|
|
9
15
|
import { shouldInterceptClick } from './utils.js';
|
|
10
16
|
|
|
11
17
|
export {
|
|
12
18
|
Router,
|
|
13
19
|
UpdateRequest,
|
|
14
20
|
BaseRoute,
|
|
21
|
+
BaseRouteOptions,
|
|
15
22
|
Route,
|
|
16
23
|
RouteOptions,
|
|
24
|
+
TemplateModule,
|
|
17
25
|
Site,
|
|
26
|
+
SiteFromGraphQl,
|
|
18
27
|
Request,
|
|
19
28
|
DelayRender,
|
|
20
29
|
RequestOptions,
|
|
21
30
|
shouldInterceptClick,
|
|
31
|
+
RouteOrigin,
|
|
22
32
|
};
|
|
@@ -22,7 +22,7 @@ export type BaseRouteOptions = {
|
|
|
22
22
|
* - `'replace'`: is set when a route is replaced via `Router.replaceState`
|
|
23
23
|
* - `'push'`: is set when a route is pushed via `Router.pushState`
|
|
24
24
|
*
|
|
25
|
-
*
|
|
25
|
+
* #### Note
|
|
26
26
|
* `replace` and `push` will not call loadData
|
|
27
27
|
*/
|
|
28
28
|
export type RouteOrigin =
|
|
@@ -45,9 +45,9 @@ export default class BaseRoute {
|
|
|
45
45
|
url: URL;
|
|
46
46
|
|
|
47
47
|
/**
|
|
48
|
-
* The
|
|
48
|
+
* The {@link Site} of the route
|
|
49
49
|
*
|
|
50
|
-
*
|
|
50
|
+
* #### Note
|
|
51
51
|
* In the `beforeRequest` event the site might not match
|
|
52
52
|
* the url and could be the default site or one that matches
|
|
53
53
|
* the users language.
|
|
@@ -59,7 +59,7 @@ export default class BaseRoute {
|
|
|
59
59
|
/**
|
|
60
60
|
* The scroll position of the current route
|
|
61
61
|
*
|
|
62
|
-
*
|
|
62
|
+
* #### Note
|
|
63
63
|
* This does not have to represent the current scroll position
|
|
64
64
|
* should more be used internally.
|
|
65
65
|
*
|
|
@@ -75,7 +75,7 @@ export default class BaseRoute {
|
|
|
75
75
|
index: number;
|
|
76
76
|
|
|
77
77
|
/**
|
|
78
|
-
* The origin of this route, See
|
|
78
|
+
* The origin of this route, See {@link RouteOrigin}
|
|
79
79
|
*/
|
|
80
80
|
origin: RouteOrigin;
|
|
81
81
|
|
|
@@ -92,7 +92,7 @@ export default class BaseRoute {
|
|
|
92
92
|
* This context is not persistant and should be considered "valid"
|
|
93
93
|
* only for the current request / route
|
|
94
94
|
*
|
|
95
|
-
*
|
|
95
|
+
* #### Note
|
|
96
96
|
* Consider using state instead. This will not be cloned in the clone
|
|
97
97
|
* call so will always be the same object
|
|
98
98
|
*/
|
|
@@ -117,8 +117,8 @@ export default class BaseRoute {
|
|
|
117
117
|
*
|
|
118
118
|
* Never ends with a slash
|
|
119
119
|
*
|
|
120
|
-
*
|
|
121
|
-
* ```
|
|
120
|
+
* #### Example
|
|
121
|
+
* ```js
|
|
122
122
|
* const site = _; // site with url https://example.com/fo
|
|
123
123
|
* const route = new Route('https://example.com/foo/bar/', site);
|
|
124
124
|
* console.log(route.uri); // '/bar'
|
|
@@ -143,8 +143,8 @@ export default class BaseRoute {
|
|
|
143
143
|
*
|
|
144
144
|
* Never ends with a slash
|
|
145
145
|
*
|
|
146
|
-
*
|
|
147
|
-
* ```
|
|
146
|
+
* #### Example
|
|
147
|
+
* ```js
|
|
148
148
|
* const site = _; // site with url https://example.com/foo
|
|
149
149
|
* const route = new Route('https://example.com/foo/bar/', null);
|
|
150
150
|
* console.log(route.baseUrl); // 'https://example.com/foo'
|
|
@@ -163,11 +163,11 @@ export default class BaseRoute {
|
|
|
163
163
|
/**
|
|
164
164
|
* Returns the search params
|
|
165
165
|
*
|
|
166
|
-
*
|
|
166
|
+
* #### Note
|
|
167
167
|
* You might also have a look at `getSearchParam` and `setSearchParam`
|
|
168
168
|
*
|
|
169
|
-
*
|
|
170
|
-
* ```
|
|
169
|
+
* #### Example
|
|
170
|
+
* ```js
|
|
171
171
|
* const route = new Route('https://example.com/foo/bar/?a=1&b=2', null);
|
|
172
172
|
* console.log(route.search.get('a')); // '1'
|
|
173
173
|
* ```
|
|
@@ -179,8 +179,8 @@ export default class BaseRoute {
|
|
|
179
179
|
/**
|
|
180
180
|
* Returns the hash of the route
|
|
181
181
|
*
|
|
182
|
-
*
|
|
183
|
-
* ```
|
|
182
|
+
* #### Example
|
|
183
|
+
* ```js
|
|
184
184
|
* const route = new Route('https://example.com/foo/bar/#hash', null);
|
|
185
185
|
* console.log(route.hash); // '#hash'
|
|
186
186
|
* ```
|
|
@@ -192,8 +192,8 @@ export default class BaseRoute {
|
|
|
192
192
|
/**
|
|
193
193
|
* Set the hash of the route
|
|
194
194
|
*
|
|
195
|
-
*
|
|
196
|
-
* ```
|
|
195
|
+
* #### Example
|
|
196
|
+
* ```js
|
|
197
197
|
* const route = new Route('https://example.com/foo/bar/', null);
|
|
198
198
|
* route.hash = '#hash';
|
|
199
199
|
* console.log(route.url.href); // 'https://example.com/foo/bar/#hash'
|
|
@@ -213,8 +213,8 @@ export default class BaseRoute {
|
|
|
213
213
|
/**
|
|
214
214
|
* Gets the search param
|
|
215
215
|
*
|
|
216
|
-
*
|
|
217
|
-
* ```
|
|
216
|
+
* #### Example
|
|
217
|
+
* ```js
|
|
218
218
|
* const route = new Route('https://example.com/foo/bar/?a=1&b=2', null);
|
|
219
219
|
* console.log(route.getSearchParam('a')); // '1'
|
|
220
220
|
* ```
|
|
@@ -227,8 +227,8 @@ export default class BaseRoute {
|
|
|
227
227
|
* Sets the search param or removes it if the value is null, undefined or an
|
|
228
228
|
* empty string
|
|
229
229
|
*
|
|
230
|
-
*
|
|
231
|
-
* ```
|
|
230
|
+
* #### Example
|
|
231
|
+
* ```js
|
|
232
232
|
* const route = new Route('https://example.com/foo/bar/?a=1&b=2', null);
|
|
233
233
|
* route.setSearchParam('a', '3');
|
|
234
234
|
* console.log(route.url.href); // 'https://example.com/foo/bar/?a=3&b=2'
|
|
@@ -261,7 +261,7 @@ export default class BaseRoute {
|
|
|
261
261
|
* Sets a state value.
|
|
262
262
|
* If the value is null or undefined, the key will be removed.
|
|
263
263
|
*
|
|
264
|
-
*
|
|
264
|
+
* #### When to use state
|
|
265
265
|
* State is used to store additional information that persists across route changes.
|
|
266
266
|
* The State is only available in the client code since it is stored using window.history.
|
|
267
267
|
*
|
|
@@ -286,7 +286,7 @@ export default class BaseRoute {
|
|
|
286
286
|
* Sets a context value.
|
|
287
287
|
* If the value is null or undefined, the key will be removed.
|
|
288
288
|
*
|
|
289
|
-
*
|
|
289
|
+
* #### When to use context
|
|
290
290
|
* Context is used to pass data to onRoute and onRequest handlers or exchange data between loadData calls.
|
|
291
291
|
* This context is not persistent and should be considered valid only for the current request/route.
|
|
292
292
|
* The context is not cloned in the clone call and will be the same object.
|
|
@@ -324,7 +324,7 @@ export default class BaseRoute {
|
|
|
324
324
|
* This checks the url but search params do not have to be in the
|
|
325
325
|
* same order
|
|
326
326
|
*
|
|
327
|
-
*
|
|
327
|
+
* #### Note
|
|
328
328
|
* This only check the url, not site or anything else.
|
|
329
329
|
*/
|
|
330
330
|
eq(route: BaseRoute | null) {
|
|
@@ -380,7 +380,7 @@ export default class BaseRoute {
|
|
|
380
380
|
/**
|
|
381
381
|
* Create a copy of the Route
|
|
382
382
|
*
|
|
383
|
-
*
|
|
383
|
+
* #### Note
|
|
384
384
|
* This does not make a copy of the template or context
|
|
385
385
|
*/
|
|
386
386
|
clone() {
|
|
@@ -107,12 +107,12 @@ export default class Request extends BaseRoute {
|
|
|
107
107
|
* With delayRender you can make sure that the render waits
|
|
108
108
|
* until you are ready. This is useful for building page transitions.
|
|
109
109
|
*
|
|
110
|
-
*
|
|
110
|
+
* #### Important
|
|
111
111
|
* If you call delayRender you need to call `ready/remove` or the render
|
|
112
112
|
* will never happen
|
|
113
113
|
*
|
|
114
|
-
*
|
|
115
|
-
* ```
|
|
114
|
+
* #### Example
|
|
115
|
+
* ```js
|
|
116
116
|
* import { onRequest } from 'crelte';
|
|
117
117
|
* import { animate } from 'motion';
|
|
118
118
|
*
|
|
@@ -175,7 +175,7 @@ export default class Request extends BaseRoute {
|
|
|
175
175
|
/**
|
|
176
176
|
* Create a Route from the Request
|
|
177
177
|
*
|
|
178
|
-
*
|
|
178
|
+
* #### Throws
|
|
179
179
|
* if the entry, template or loadedData is missing
|
|
180
180
|
* or the request was cancelled
|
|
181
181
|
*/
|
|
@@ -48,7 +48,7 @@ export default class Route extends BaseRoute {
|
|
|
48
48
|
/**
|
|
49
49
|
* Create a new Route
|
|
50
50
|
*
|
|
51
|
-
*
|
|
51
|
+
* #### Note
|
|
52
52
|
* This should only be created by crelte
|
|
53
53
|
*/
|
|
54
54
|
constructor(
|
|
@@ -70,7 +70,7 @@ export default class Route extends BaseRoute {
|
|
|
70
70
|
/**
|
|
71
71
|
* Create a copy of the EntryRoute
|
|
72
72
|
*
|
|
73
|
-
*
|
|
73
|
+
* #### Note
|
|
74
74
|
* This does not make a copy of the entry, template or loadedData.
|
|
75
75
|
*/
|
|
76
76
|
clone() {
|
|
@@ -25,7 +25,7 @@ export default class BaseRouter {
|
|
|
25
25
|
/**
|
|
26
26
|
* The current route
|
|
27
27
|
*
|
|
28
|
-
*
|
|
28
|
+
* #### Note
|
|
29
29
|
* Will always contain a route except in the first loadData call
|
|
30
30
|
*/
|
|
31
31
|
route: Writable<Route | null>;
|
|
@@ -34,7 +34,7 @@ export default class BaseRouter {
|
|
|
34
34
|
/**
|
|
35
35
|
* The current site
|
|
36
36
|
*
|
|
37
|
-
*
|
|
37
|
+
* #### Note
|
|
38
38
|
* Will always contain a site except in the first loadData call
|
|
39
39
|
*/
|
|
40
40
|
site: Writable<Site | null>;
|
|
@@ -79,7 +79,7 @@ export default class BaseRouter {
|
|
|
79
79
|
onRender: (
|
|
80
80
|
cr: CrelteRequest,
|
|
81
81
|
/**
|
|
82
|
-
*
|
|
82
|
+
* #### Throws
|
|
83
83
|
* if the route is missing entry, template or loadedData
|
|
84
84
|
*/
|
|
85
85
|
readyForRoute: () => Route,
|
|
@@ -252,7 +252,7 @@ export default class BaseRouter {
|
|
|
252
252
|
}
|
|
253
253
|
|
|
254
254
|
/**
|
|
255
|
-
*
|
|
255
|
+
* #### Throws
|
|
256
256
|
* If the request completed but had an error
|
|
257
257
|
*/
|
|
258
258
|
async handleRequest(
|
|
@@ -9,26 +9,29 @@ import { Entry } from '../../loadData/index.js';
|
|
|
9
9
|
*
|
|
10
10
|
* If you return `false` the request will be aborted
|
|
11
11
|
*
|
|
12
|
-
*
|
|
13
|
-
* ```
|
|
12
|
+
* #### Example
|
|
13
|
+
* ```js
|
|
14
14
|
* router.replace(req => (req.hash = ''));
|
|
15
15
|
* ```
|
|
16
16
|
*/
|
|
17
17
|
export type UpdateRequest = (req: Request) => boolean | null | undefined | void;
|
|
18
18
|
|
|
19
|
+
/**
|
|
20
|
+
* The Crelte Router
|
|
21
|
+
*/
|
|
19
22
|
export default class Router {
|
|
20
23
|
private inner: BaseRouter;
|
|
21
24
|
private _request: Request | null;
|
|
22
25
|
|
|
23
|
-
constructor(inner:
|
|
24
|
-
this.inner = inner;
|
|
26
|
+
constructor(inner: any) {
|
|
27
|
+
this.inner = inner as BaseRouter;
|
|
25
28
|
this._request = null;
|
|
26
29
|
}
|
|
27
30
|
|
|
28
31
|
/**
|
|
29
32
|
* returns a store with the current route
|
|
30
33
|
*
|
|
31
|
-
*
|
|
34
|
+
* #### Note
|
|
32
35
|
* Will always contain a route except in the first loadData call this
|
|
33
36
|
* is intentional since you will get the wrong route in a loadData call.
|
|
34
37
|
* In a loadData you should always use the `CrelteRequest` provided
|
|
@@ -41,10 +44,10 @@ export default class Router {
|
|
|
41
44
|
/**
|
|
42
45
|
* returns a store with the current site
|
|
43
46
|
*
|
|
44
|
-
*
|
|
47
|
+
* #### Note
|
|
45
48
|
* Will always contain a site except in the first loadData call this
|
|
46
49
|
* is intentional since you might get the wrong site if a site switch
|
|
47
|
-
* is happening and you call this in loadData. If possible use the CrelteRequest
|
|
50
|
+
* is happening and you call this in loadData. If possible use the {@link CrelteRequest#site}
|
|
48
51
|
* provided in each loadData call.
|
|
49
52
|
*
|
|
50
53
|
* Else use `router.site.get() ?? router.req.site`
|
|
@@ -56,7 +59,7 @@ export default class Router {
|
|
|
56
59
|
/**
|
|
57
60
|
* returns a store with the current entry
|
|
58
61
|
*
|
|
59
|
-
*
|
|
62
|
+
* #### Note
|
|
60
63
|
* Will always contain an entry except in the first loadData call this
|
|
61
64
|
* is intentional since you might get the wrong entry if a request is happening
|
|
62
65
|
* and you call this in loadData. If possible use the CrelteRequest
|
|
@@ -69,8 +72,8 @@ export default class Router {
|
|
|
69
72
|
/**
|
|
70
73
|
* returns the latest request in progress otherwise returns null.
|
|
71
74
|
*
|
|
72
|
-
*
|
|
73
|
-
* If at all possible prefer using the
|
|
75
|
+
* #### Important !!
|
|
76
|
+
* If at all possible prefer using the {@link CrelteRequest} provided in each
|
|
74
77
|
* loadData call. For example in a preload request this will return null.
|
|
75
78
|
* Or a user has clicked multiple times on different links you might get
|
|
76
79
|
* the url of the newer request.
|
|
@@ -121,7 +124,7 @@ export default class Router {
|
|
|
121
124
|
}
|
|
122
125
|
|
|
123
126
|
/**
|
|
124
|
-
* Returns a site which is
|
|
127
|
+
* Returns a site which is preferred based on the users language
|
|
125
128
|
*
|
|
126
129
|
* Returns null if no site could be determined
|
|
127
130
|
*/
|
|
@@ -136,11 +139,11 @@ export default class Router {
|
|
|
136
139
|
* the url needs to start with http or with a / which will be considered as
|
|
137
140
|
* the site baseUrl
|
|
138
141
|
*
|
|
139
|
-
*
|
|
142
|
+
* #### Note
|
|
140
143
|
* The origin will always be set to 'manual'
|
|
141
144
|
*
|
|
142
|
-
*
|
|
143
|
-
* ```
|
|
145
|
+
* #### Example
|
|
146
|
+
* ```js
|
|
144
147
|
* import { getRouter } from 'crelte';
|
|
145
148
|
*
|
|
146
149
|
* const router = getRouter();
|
|
@@ -181,13 +184,13 @@ export default class Router {
|
|
|
181
184
|
* You can use this when using pagination for example change the route object
|
|
182
185
|
* (search argument) and then call push
|
|
183
186
|
*
|
|
184
|
-
*
|
|
187
|
+
* #### Note
|
|
185
188
|
* This will always set the origin to 'push'
|
|
186
189
|
* And will clear the scrollY value if you dont provide a new one via the `opts`
|
|
187
190
|
* This will disableLoadData by default if you dont provide an override via the `opts`
|
|
188
191
|
*
|
|
189
|
-
*
|
|
190
|
-
* ```
|
|
192
|
+
* #### Example using the update function
|
|
193
|
+
* ```js
|
|
191
194
|
* import { getRouter } from 'crelte';
|
|
192
195
|
*
|
|
193
196
|
* const router = getRouter();
|
|
@@ -196,8 +199,8 @@ export default class Router {
|
|
|
196
199
|
* router.push(req => req.setSearchParam('page', page || null));
|
|
197
200
|
* ```
|
|
198
201
|
*
|
|
199
|
-
*
|
|
200
|
-
* ```
|
|
202
|
+
* #### Example using the route object
|
|
203
|
+
* ```js
|
|
201
204
|
* import { getRouter } from 'crelte';
|
|
202
205
|
*
|
|
203
206
|
* const router = getRouter();
|
|
@@ -244,13 +247,13 @@ export default class Router {
|
|
|
244
247
|
*
|
|
245
248
|
* You can use this when using some filters for example a search filter
|
|
246
249
|
*
|
|
247
|
-
*
|
|
250
|
+
* #### Note
|
|
248
251
|
* This will always set the origin to 'replace'
|
|
249
252
|
* And will clear the scrollY value if you don't provide a new one via the `opts`
|
|
250
253
|
* This will disableLoadData by default if you don't provide an override via the `opts`
|
|
251
254
|
*
|
|
252
|
-
*
|
|
253
|
-
* ```
|
|
255
|
+
* #### Example using the update function
|
|
256
|
+
* ```js
|
|
254
257
|
* import { getRouter } from 'crelte';
|
|
255
258
|
*
|
|
256
259
|
* const router = getRouter();
|
|
@@ -259,8 +262,8 @@ export default class Router {
|
|
|
259
262
|
* router.replace(req => req.setSearchParam('search', search));
|
|
260
263
|
* ```
|
|
261
264
|
*
|
|
262
|
-
*
|
|
263
|
-
* ```
|
|
265
|
+
* #### Example using the route object
|
|
266
|
+
* ```js
|
|
264
267
|
* import { getRouter } from 'crelte';
|
|
265
268
|
*
|
|
266
269
|
* const router = getRouter();
|
|
@@ -355,7 +358,7 @@ export default class Router {
|
|
|
355
358
|
* Resolve a url or Route and convert it to a Request
|
|
356
359
|
*
|
|
357
360
|
* @param target
|
|
358
|
-
* @param opts
|
|
361
|
+
* @param opts any option present will override the value in target
|
|
359
362
|
* @return Returns null if the url does not match our host (the protocol get's ignored)
|
|
360
363
|
*/
|
|
361
364
|
targetToRequest(
|