crelte 0.2.2 → 0.3.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.
Files changed (55) hide show
  1. package/dist/Crelte.d.ts +61 -9
  2. package/dist/Crelte.d.ts.map +1 -1
  3. package/dist/Crelte.js +42 -10
  4. package/dist/CrelteRequest.d.ts +3 -11
  5. package/dist/CrelteRequest.d.ts.map +1 -1
  6. package/dist/CrelteRequest.js +9 -19
  7. package/dist/graphql/GraphQl.d.ts +7 -0
  8. package/dist/graphql/GraphQl.d.ts.map +1 -1
  9. package/dist/graphql/GraphQl.js +16 -3
  10. package/dist/index.d.ts +10 -4
  11. package/dist/index.d.ts.map +1 -1
  12. package/dist/index.js +13 -1
  13. package/dist/init/client.d.ts +0 -19
  14. package/dist/init/client.d.ts.map +1 -1
  15. package/dist/init/client.js +9 -12
  16. package/dist/init/server.d.ts +0 -4
  17. package/dist/init/server.d.ts.map +1 -1
  18. package/dist/init/server.js +2 -5
  19. package/dist/init/shared.d.ts.map +1 -1
  20. package/dist/init/shared.js +8 -8
  21. package/dist/loadData/Globals.d.ts +15 -31
  22. package/dist/loadData/Globals.d.ts.map +1 -1
  23. package/dist/loadData/Globals.js +65 -72
  24. package/dist/routing/InnerRouter.d.ts +1 -10
  25. package/dist/routing/InnerRouter.d.ts.map +1 -1
  26. package/dist/routing/InnerRouter.js +28 -23
  27. package/dist/routing/Request.d.ts +2 -0
  28. package/dist/routing/Request.d.ts.map +1 -1
  29. package/dist/routing/Request.js +9 -0
  30. package/dist/routing/Route.d.ts +56 -1
  31. package/dist/routing/Route.d.ts.map +1 -1
  32. package/dist/routing/Route.js +85 -2
  33. package/dist/routing/Router.d.ts +29 -4
  34. package/dist/routing/Router.d.ts.map +1 -1
  35. package/dist/routing/Router.js +39 -12
  36. package/dist/ssr/SsrCache.d.ts.map +1 -1
  37. package/dist/ssr/SsrCache.js +6 -1
  38. package/dist/utils.d.ts +2 -0
  39. package/dist/utils.d.ts.map +1 -0
  40. package/dist/utils.js +8 -0
  41. package/package.json +2 -2
  42. package/src/Crelte.ts +95 -13
  43. package/src/CrelteRequest.ts +14 -27
  44. package/src/graphql/GraphQl.ts +25 -6
  45. package/src/index.ts +19 -8
  46. package/src/init/client.ts +9 -40
  47. package/src/init/server.ts +2 -13
  48. package/src/init/shared.ts +8 -9
  49. package/src/loadData/Globals.ts +76 -93
  50. package/src/routing/InnerRouter.ts +38 -32
  51. package/src/routing/Request.ts +11 -0
  52. package/src/routing/Route.ts +93 -2
  53. package/src/routing/Router.ts +51 -20
  54. package/src/ssr/SsrCache.ts +6 -1
  55. package/src/utils.ts +10 -0
@@ -67,13 +67,19 @@ type ServerInited = {
67
67
  export default class Router {
68
68
  /**
69
69
  * The current route
70
+ *
71
+ * ## Note
72
+ * Will always contain a route expect in the first loadData call
70
73
  */
71
- private _route: Writable<Route>;
74
+ private _route: Writable<Route | null>;
72
75
 
73
76
  /**
74
77
  * The current site
78
+ *
79
+ * ## Note
80
+ * Will always contain a site expect in the first loadData call
75
81
  */
76
- private _site: Writable<Site>;
82
+ private _site: Writable<Site | null>;
77
83
 
78
84
  // the next request, just here to destroy it
79
85
  private _request: Request | null;
@@ -117,9 +123,9 @@ export default class Router {
117
123
  this._onRequest = new Listeners();
118
124
 
119
125
  this._internal = {
120
- onLoaded: () => { },
121
- onNothingLoaded: () => { },
122
- onLoad: () => { },
126
+ onLoaded: () => {},
127
+ onNothingLoaded: () => {},
128
+ onLoad: () => {},
123
129
  domReady: req => this.inner.domReady(req),
124
130
  initClient: () => this._initClient(),
125
131
  initServer: (url, acceptLang) => this._initServer(url, acceptLang),
@@ -139,15 +145,25 @@ export default class Router {
139
145
 
140
146
  /**
141
147
  * returns a store with the current route
148
+ *
149
+ * ## Note
150
+ * Will always contain a route expect in the first loadData call
151
+ *
152
+ * Consider to use CrelteRequest instead
142
153
  */
143
- get route(): Readable<Route> {
154
+ get route(): Readable<Route | null> {
144
155
  return this._route.readclone();
145
156
  }
146
157
 
147
158
  /**
148
159
  * returns a store with the current site
160
+ *
161
+ * ## Note
162
+ * Will always contain a site expect in the first loadData call
163
+ *
164
+ * Consider to use CrelteRequest instead
149
165
  */
150
- get site(): Readable<Site> {
166
+ get site(): Readable<Site | null> {
151
167
  return this._site.readonly();
152
168
  }
153
169
 
@@ -205,7 +221,7 @@ export default class Router {
205
221
  * This pushes the new route without triggering a new pageload
206
222
  *
207
223
  * You can use this when using pagination for example change the route object
208
- * (search argument) and then call pushState
224
+ * (search argument) and then call push
209
225
  *
210
226
  * ## Note
211
227
  * This will always set the origin to 'push'
@@ -221,7 +237,7 @@ export default class Router {
221
237
  * const page = 1;
222
238
  * const route = router.route.get();
223
239
  * route.setSearchParam('page', page > 0 ? page : null);
224
- * router.pushState(route);
240
+ * router.push(route);
225
241
  * ```
226
242
  */
227
243
  push(route: Route | Request, opts: RequestOptions = {}) {
@@ -234,14 +250,13 @@ export default class Router {
234
250
  disableLoadData: opts.disableLoadData ?? true,
235
251
  });
236
252
  this.inner.push(req);
237
- this.destroyRequest();
238
- this.setNewRoute(route);
239
253
  }
240
254
 
241
255
  /**
242
256
  * @deprecated use push instead
243
257
  */
244
258
  pushState(route: Route | Request) {
259
+ console.warn('pushState is deprecated, use push instead');
245
260
  this.push(route);
246
261
  }
247
262
 
@@ -276,14 +291,13 @@ export default class Router {
276
291
  disableLoadData: opts.disableLoadData ?? true,
277
292
  });
278
293
  this.inner.replace(req);
279
- this.destroyRequest();
280
- this.setNewRoute(req);
281
294
  }
282
295
 
283
296
  /**
284
297
  * @deprecated use replace instead
285
298
  */
286
299
  replaceState(route: Route | Request) {
300
+ console.warn('replaceState is deprecated, use replace instead');
287
301
  this.replace(route);
288
302
  }
289
303
 
@@ -313,11 +327,13 @@ export default class Router {
313
327
  *
314
328
  * This will trigger every time a new route is set
315
329
  * and is equivalent to router.route.subscribe(fn)
330
+ * expect that it will not trigger instantly
316
331
  *
317
332
  * @returns a function to remove the listener
318
333
  */
319
334
  onRoute(fn: (route: Route) => void): () => void {
320
- return this.route.subscribe(fn);
335
+ let first = true;
336
+ return this.route.subscribe(r => (first ? (first = false) : fn(r!)));
321
337
  }
322
338
 
323
339
  /**
@@ -331,6 +347,20 @@ export default class Router {
331
347
  return this._onRequest.add(fn);
332
348
  }
333
349
 
350
+ /**
351
+ * Resolve a url or Route and convert it to a Request
352
+ *
353
+ * @param target
354
+ * @param opts, any option present will override the value in target
355
+ * @return Returns null if the url does not match our host (the protocol get's ignored)
356
+ */
357
+ targetToRequest(
358
+ target: string | URL | Route | Request,
359
+ opts: RequestOptions = {},
360
+ ): Request {
361
+ return this.inner.targetToRequest(target, opts);
362
+ }
363
+
334
364
  private setNewRoute(route: Route) {
335
365
  this._route.setSilent(route);
336
366
  const siteChanged = this.site.get()?.id !== route.site.id;
@@ -356,7 +386,7 @@ export default class Router {
356
386
  const prom: Promise<ServerInited> = new Promise(resolve => {
357
387
  this._internal.onLoaded = (success, req, ready) => {
358
388
  const props = ready();
359
- this._internal.onLoaded = () => { };
389
+ this._internal.onLoaded = () => {};
360
390
 
361
391
  resolve({
362
392
  success,
@@ -367,12 +397,12 @@ export default class Router {
367
397
  };
368
398
  });
369
399
 
370
- const route = this.inner.targetToRequest(url);
371
- route.origin = 'init';
400
+ const req = this.inner.targetToRequest(url);
401
+ req.origin = 'init';
372
402
 
373
403
  // let's see if the url matches any route and site
374
404
  // if not let's redirect to the site which matches the acceptLang
375
- if (!route.siteMatches()) {
405
+ if (!req.siteMatches()) {
376
406
  const site = this.inner.siteByAcceptLang(acceptLang);
377
407
 
378
408
  return {
@@ -383,14 +413,15 @@ export default class Router {
383
413
  };
384
414
  }
385
415
 
386
- this.inner.setRoute(route);
416
+ this.inner.route = req.toRoute();
417
+ this.inner.onRoute(req, () => {});
387
418
 
388
419
  const resp = await prom;
389
420
 
390
421
  const hist = this.inner.history as ServerHistory;
391
422
  if (hist.url || hist.req) {
392
423
  const nReq = this.inner.targetToRequest(hist.req ?? hist.url!);
393
- if (!route.eq(nReq)) {
424
+ if (!req.eq(nReq)) {
394
425
  return {
395
426
  success: true,
396
427
  redirect: true,
@@ -1,7 +1,12 @@
1
1
  export async function calcKey(data: any) {
2
+ const json = JSON.stringify(data);
3
+ // this should only happen in an unsecure context
4
+ // specifically in the craft preview locally
5
+ if (!crypto?.subtle) return json;
6
+
2
7
  // Convert the string data to an ArrayBuffer
3
8
  const encoder = new TextEncoder();
4
- const dataBuffer = encoder.encode(JSON.stringify(data));
9
+ const dataBuffer = encoder.encode(json);
5
10
 
6
11
  // Use the Web Crypto API to hash the data with SHA-1
7
12
  const hashBuffer = await crypto.subtle.digest('SHA-1', dataBuffer);
package/src/utils.ts ADDED
@@ -0,0 +1,10 @@
1
+ // This are internal utils. Consider adding them to crelte-std instead
2
+
3
+ // this tries to do a structuredClone and else just uses JSON
4
+ export function objClone(obj: any): any {
5
+ if (typeof structuredClone === 'function') {
6
+ return structuredClone(obj);
7
+ }
8
+
9
+ return JSON.parse(JSON.stringify(obj));
10
+ }