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
@@ -3,6 +3,8 @@ export type RouteOptions = {
3
3
  scrollY?: number;
4
4
  index?: number;
5
5
  origin?: RouteOrigin;
6
+ state?: Record<string, any>;
7
+ context?: Record<string, any>;
6
8
  };
7
9
  /**
8
10
  * RouteOrigin represents the origin of a route.
@@ -59,6 +61,23 @@ export default class Route {
59
61
  * The origin of this route, See [[RouteOrigin]]
60
62
  */
61
63
  origin: RouteOrigin;
64
+ /**
65
+ * @hidden
66
+ * State data that can be used to store additional information
67
+ */
68
+ _state: Record<string, any>;
69
+ /**
70
+ * @hidden
71
+ * Any data that should be passed to onRoute and onRequest handlers
72
+ * or exchanged between loadData's
73
+ * This context is not persistant and should be considered "valid"
74
+ * only for the current request / route
75
+ *
76
+ * ## Note
77
+ * Consider using state instead. This will not be cloned in the clone
78
+ * call so will always be the same object
79
+ */
80
+ _context: Record<string, any>;
62
81
  /**
63
82
  * Creates a new Route
64
83
  */
@@ -135,7 +154,8 @@ export default class Route {
135
154
  */
136
155
  getSearchParam(key: string): string | null;
137
156
  /**
138
- * Sets the search param or removes it if the value is null or undefined
157
+ * Sets the search param or removes it if the value is null, undefined or an
158
+ * empty string
139
159
  *
140
160
  * ## Example
141
161
  * ```
@@ -148,6 +168,38 @@ export default class Route {
148
168
  * ```
149
169
  */
150
170
  setSearchParam(key: string, value?: string | number | null): void;
171
+ /**
172
+ * Returns a state value if it exists.
173
+ */
174
+ getState<T = any>(key: string): T | null;
175
+ /**
176
+ * Sets a state value.
177
+ * If the value is null or undefined, the key will be removed.
178
+ *
179
+ * ## When to use state
180
+ * State is used to store additional information that persists across route changes.
181
+ * The State is only available in the client code since it is stored using window.history.
182
+ *
183
+ * Consider using setSearchParam instead to enable server side rendering.
184
+ */
185
+ setState<T>(key: string, value: T | null | undefined): void;
186
+ /**
187
+ * Returns a context value if it exists.
188
+ */
189
+ getContext<T = any>(key: string): T | null;
190
+ /**
191
+ * Sets a context value.
192
+ * If the value is null or undefined, the key will be removed.
193
+ *
194
+ * ## When to use context
195
+ * Context is used to pass data to onRoute and onRequest handlers or exchange data between loadData calls.
196
+ * This context is not persistent and should be considered valid only for the current request/route.
197
+ * The context is not cloned in the clone call and will be the same object.
198
+ */
199
+ setContext<T>(key: string, value: T | null | undefined): void;
200
+ /**
201
+ * Returns true if the route is in live preview mode
202
+ */
151
203
  inLivePreview(): boolean;
152
204
  /**
153
205
  * Returns if the site matches the url
@@ -158,6 +210,9 @@ export default class Route {
158
210
  *
159
211
  * This checks all properties of the url but search params do not have to be
160
212
  * in the same order
213
+ *
214
+ * ## Note
215
+ * This does not check the state or context
161
216
  */
162
217
  eq(route: Route | null): boolean | null;
163
218
  /**
@@ -1 +1 @@
1
- {"version":3,"file":"Route.d.ts","sourceRoot":"","sources":["../../../../src/routing/Route.ts"],"names":[],"mappings":"AAAA,OAAO,IAAI,MAAM,WAAW,CAAC;AAG7B,MAAM,MAAM,YAAY,GAAG;IAC1B,OAAO,CAAC,EAAE,MAAM,CAAC;IACjB,KAAK,CAAC,EAAE,MAAM,CAAC;IACf,MAAM,CAAC,EAAE,WAAW,CAAC;CACrB,CAAC;AAEF;;;;;;;;;;;;;GAaG;AACH,MAAM,MAAM,WAAW,GACpB,MAAM,GACN,QAAQ,GACR,OAAO,GACP,KAAK,GACL,SAAS,GACT,MAAM,CAAC;AAEV;;;GAGG;AACH,MAAM,CAAC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4 @@
1
+ import { objClone } from '../utils.js';
1
2
  import { trimSlashEnd } from './utils.js';
2
3
  /**
3
4
  * A Route contains information about the current page for example the url and
@@ -39,6 +40,23 @@ export default class Route {
39
40
  * The origin of this route, See [[RouteOrigin]]
40
41
  */
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;
42
60
  /**
43
61
  * Creates a new Route
44
62
  */
@@ -48,6 +66,8 @@ export default class Route {
48
66
  this.scrollY = opts.scrollY ?? null;
49
67
  this.index = opts.index ?? 0;
50
68
  this.origin = opts.origin ?? 'manual';
69
+ this._state = opts.state ?? {};
70
+ this._context = opts.context ?? {};
51
71
  }
52
72
  /**
53
73
  * Returns the uri of the route
@@ -138,7 +158,8 @@ export default class Route {
138
158
  return this.search.get(key);
139
159
  }
140
160
  /**
141
- * Sets the search param or removes it if the value is null or undefined
161
+ * Sets the search param or removes it if the value is null, undefined or an
162
+ * empty string
142
163
  *
143
164
  * ## Example
144
165
  * ```
@@ -151,13 +172,66 @@ export default class Route {
151
172
  * ```
152
173
  */
153
174
  setSearchParam(key, value) {
154
- if (typeof value !== 'undefined' && value !== null) {
175
+ const deleteValue = typeof value === 'undefined' ||
176
+ value === null ||
177
+ (typeof value === 'string' && value === '');
178
+ if (!deleteValue) {
155
179
  this.search.set(key, value);
156
180
  }
157
181
  else {
158
182
  this.search.delete(key);
159
183
  }
160
184
  }
185
+ /**
186
+ * Returns a state value if it exists.
187
+ */
188
+ getState(key) {
189
+ return this._state[key] ?? null;
190
+ }
191
+ /**
192
+ * Sets a state value.
193
+ * If the value is null or undefined, the key will be removed.
194
+ *
195
+ * ## When to use state
196
+ * State is used to store additional information that persists across route changes.
197
+ * The State is only available in the client code since it is stored using window.history.
198
+ *
199
+ * Consider using setSearchParam instead to enable server side rendering.
200
+ */
201
+ setState(key, value) {
202
+ if (typeof value === 'undefined' || value === null) {
203
+ delete this._state[key];
204
+ }
205
+ else {
206
+ this._state[key] = value;
207
+ }
208
+ }
209
+ /**
210
+ * Returns a context value if it exists.
211
+ */
212
+ getContext(key) {
213
+ return this._context[key] ?? null;
214
+ }
215
+ /**
216
+ * Sets a context value.
217
+ * If the value is null or undefined, the key will be removed.
218
+ *
219
+ * ## When to use context
220
+ * Context is used to pass data to onRoute and onRequest handlers or exchange data between loadData calls.
221
+ * This context is not persistent and should be considered valid only for the current request/route.
222
+ * The context is not cloned in the clone call and will be the same object.
223
+ */
224
+ setContext(key, value) {
225
+ if (typeof value === 'undefined' || value === null) {
226
+ delete this._context[key];
227
+ }
228
+ else {
229
+ this._context[key] = value;
230
+ }
231
+ }
232
+ /**
233
+ * Returns true if the route is in live preview mode
234
+ */
161
235
  inLivePreview() {
162
236
  return !!this.search.get('x-craft-live-preview');
163
237
  }
@@ -179,6 +253,9 @@ export default class Route {
179
253
  *
180
254
  * This checks all properties of the url but search params do not have to be
181
255
  * in the same order
256
+ *
257
+ * ## Note
258
+ * This does not check the state or context
182
259
  */
183
260
  eq(route) {
184
261
  return (route &&
@@ -227,6 +304,8 @@ export default class Route {
227
304
  scrollY: this.scrollY ?? undefined,
228
305
  index: this.index,
229
306
  origin: this.origin,
307
+ state: objClone(this._state),
308
+ context: this._context,
230
309
  });
231
310
  }
232
311
  /** @hidden */
@@ -235,6 +314,9 @@ export default class Route {
235
314
  this.scrollY = state.route.scrollY;
236
315
  if (typeof state?.route?.index === 'number')
237
316
  this.index = state.route.index;
317
+ if (typeof state?.state === 'object' && state.state !== null) {
318
+ this._state = state.state;
319
+ }
238
320
  }
239
321
  /** @hidden */
240
322
  _toState() {
@@ -243,6 +325,7 @@ export default class Route {
243
325
  scrollY: this.scrollY,
244
326
  index: this.index,
245
327
  },
328
+ state: this._state,
246
329
  };
247
330
  }
248
331
  }
@@ -27,10 +27,16 @@ type ServerInited = {
27
27
  export default class Router {
28
28
  /**
29
29
  * The current route
30
+ *
31
+ * ## Note
32
+ * Will always contain a route expect in the first loadData call
30
33
  */
31
34
  private _route;
32
35
  /**
33
36
  * The current site
37
+ *
38
+ * ## Note
39
+ * Will always contain a site expect in the first loadData call
34
40
  */
35
41
  private _site;
36
42
  private _request;
@@ -51,12 +57,22 @@ export default class Router {
51
57
  constructor(sites: SiteFromGraphQl[], opts?: RouterOptions);
52
58
  /**
53
59
  * returns a store with the current route
60
+ *
61
+ * ## Note
62
+ * Will always contain a route expect in the first loadData call
63
+ *
64
+ * Consider to use CrelteRequest instead
54
65
  */
55
- get route(): Readable<Route>;
66
+ get route(): Readable<Route | null>;
56
67
  /**
57
68
  * returns a store with the current site
69
+ *
70
+ * ## Note
71
+ * Will always contain a site expect in the first loadData call
72
+ *
73
+ * Consider to use CrelteRequest instead
58
74
  */
59
- get site(): Readable<Site>;
75
+ get site(): Readable<Site | null>;
60
76
  /**
61
77
  * The sites which are available
62
78
  */
@@ -95,7 +111,7 @@ export default class Router {
95
111
  * This pushes the new route without triggering a new pageload
96
112
  *
97
113
  * You can use this when using pagination for example change the route object
98
- * (search argument) and then call pushState
114
+ * (search argument) and then call push
99
115
  *
100
116
  * ## Note
101
117
  * This will always set the origin to 'push'
@@ -111,7 +127,7 @@ export default class Router {
111
127
  * const page = 1;
112
128
  * const route = router.route.get();
113
129
  * route.setSearchParam('page', page > 0 ? page : null);
114
- * router.pushState(route);
130
+ * router.push(route);
115
131
  * ```
116
132
  */
117
133
  push(route: Route | Request, opts?: RequestOptions): void;
@@ -163,6 +179,7 @@ export default class Router {
163
179
  *
164
180
  * This will trigger every time a new route is set
165
181
  * and is equivalent to router.route.subscribe(fn)
182
+ * expect that it will not trigger instantly
166
183
  *
167
184
  * @returns a function to remove the listener
168
185
  */
@@ -175,6 +192,14 @@ export default class Router {
175
192
  * @returns a function to remove the listener
176
193
  */
177
194
  onRequest(fn: (req: Request) => void): () => void;
195
+ /**
196
+ * Resolve a url or Route and convert it to a Request
197
+ *
198
+ * @param target
199
+ * @param opts, any option present will override the value in target
200
+ * @return Returns null if the url does not match our host (the protocol get's ignored)
201
+ */
202
+ targetToRequest(target: string | URL | Route | Request, opts?: RequestOptions): Request;
178
203
  private setNewRoute;
179
204
  private _initClient;
180
205
  private _initServer;
@@ -1 +1 @@
1
- {"version":3,"file":"Router.d.ts","sourceRoot":"","sources":["../../../../src/routing/Router.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,MAAM,YAAY,CAAC;AAC/B,OAAO,IAAI,EAAE,EAAE,eAAe,EAAE,MAAM,WAAW,CAAC;AAElD,OAAmB,EAAE,MAAM,EAAgB,MAAM,iBAAiB,CAAC;AAEnE,OAAO,EAAE,QAAQ,EAAY,MAAM,mBAAmB,CAAC;AAEvD,OAAO,OAAO,EAAE,EAAE,cAAc,EAAE,MAAM,cAAc,CAAC;AAEvD,MAAM,MAAM,aAAa,GAAG;IAC3B,kBAAkB,CAAC,EAAE,OAAO,CAAC;IAC7B,WAAW,CAAC,EAAE,OAAO,CAAC;CACtB,CAAC;AAWF;;GAEG;AACH,KAAK,QAAQ,GAAG;IACf,QAAQ,EAAE,CACT,OAAO,EAAE,OAAO,EAChB,GAAG,EAAE,OAAO,EAKZ,KAAK,EAAE,MAAM,GAAG,KACZ,IAAI,CAAC;IAIV,eAAe,EAAE,CAChB,GAAG,EAAE,OAAO,EAKZ,KAAK,EAAE,MAAM,IAAI,KACb,IAAI,CAAC;IAEV,MAAM,EAAE,MAAM,CAAC;IAEf,QAAQ,EAAE,CAAC,GAAG,EAAE,OAAO,KAAK,IAAI,CAAC;IAEjC,UAAU,EAAE,MAAM,IAAI,CAAC;IAEvB,UAAU,EAAE,CAAC,GAAG,EAAE,MAAM,EAAE,UAAU,CAAC,EAAE,MAAM,KAAK,OAAO,CAAC,YAAY,CAAC,CAAC;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OtB,OAAO,CAAC,UAAU;YAIJ,SAAS;YAsBT,gBAAgB;IAkB9B,OAAO,CAAC,WAAW;CAKnB"}
@@ -11,10 +11,16 @@ const defaultRouterOpts = {
11
11
  export default class Router {
12
12
  /**
13
13
  * The current route
14
+ *
15
+ * ## Note
16
+ * Will always contain a route expect in the first loadData call
14
17
  */
15
18
  _route;
16
19
  /**
17
20
  * The current site
21
+ *
22
+ * ## Note
23
+ * Will always contain a site expect in the first loadData call
18
24
  */
19
25
  _site;
20
26
  // the next request, just here to destroy it
@@ -64,12 +70,22 @@ export default class Router {
64
70
  }
65
71
  /**
66
72
  * returns a store with the current route
73
+ *
74
+ * ## Note
75
+ * Will always contain a route expect in the first loadData call
76
+ *
77
+ * Consider to use CrelteRequest instead
67
78
  */
68
79
  get route() {
69
80
  return this._route.readclone();
70
81
  }
71
82
  /**
72
83
  * returns a store with the current site
84
+ *
85
+ * ## Note
86
+ * Will always contain a site expect in the first loadData call
87
+ *
88
+ * Consider to use CrelteRequest instead
73
89
  */
74
90
  get site() {
75
91
  return this._site.readonly();
@@ -124,7 +140,7 @@ export default class Router {
124
140
  * This pushes the new route without triggering a new pageload
125
141
  *
126
142
  * You can use this when using pagination for example change the route object
127
- * (search argument) and then call pushState
143
+ * (search argument) and then call push
128
144
  *
129
145
  * ## Note
130
146
  * This will always set the origin to 'push'
@@ -140,7 +156,7 @@ export default class Router {
140
156
  * const page = 1;
141
157
  * const route = router.route.get();
142
158
  * route.setSearchParam('page', page > 0 ? page : null);
143
- * router.pushState(route);
159
+ * router.push(route);
144
160
  * ```
145
161
  */
146
162
  push(route, opts = {}) {
@@ -153,13 +169,12 @@ export default class Router {
153
169
  disableLoadData: opts.disableLoadData ?? true,
154
170
  });
155
171
  this.inner.push(req);
156
- this.destroyRequest();
157
- this.setNewRoute(route);
158
172
  }
159
173
  /**
160
174
  * @deprecated use push instead
161
175
  */
162
176
  pushState(route) {
177
+ console.warn('pushState is deprecated, use push instead');
163
178
  this.push(route);
164
179
  }
165
180
  /**
@@ -193,13 +208,12 @@ export default class Router {
193
208
  disableLoadData: opts.disableLoadData ?? true,
194
209
  });
195
210
  this.inner.replace(req);
196
- this.destroyRequest();
197
- this.setNewRoute(req);
198
211
  }
199
212
  /**
200
213
  * @deprecated use replace instead
201
214
  */
202
215
  replaceState(route) {
216
+ console.warn('replaceState is deprecated, use replace instead');
203
217
  this.replace(route);
204
218
  }
205
219
  /**
@@ -225,11 +239,13 @@ export default class Router {
225
239
  *
226
240
  * This will trigger every time a new route is set
227
241
  * and is equivalent to router.route.subscribe(fn)
242
+ * expect that it will not trigger instantly
228
243
  *
229
244
  * @returns a function to remove the listener
230
245
  */
231
246
  onRoute(fn) {
232
- return this.route.subscribe(fn);
247
+ let first = true;
248
+ return this.route.subscribe(r => (first ? (first = false) : fn(r)));
233
249
  }
234
250
  /**
235
251
  * Add a listener for the onRequest event
@@ -241,6 +257,16 @@ export default class Router {
241
257
  onRequest(fn) {
242
258
  return this._onRequest.add(fn);
243
259
  }
260
+ /**
261
+ * Resolve a url or Route and convert it to a Request
262
+ *
263
+ * @param target
264
+ * @param opts, any option present will override the value in target
265
+ * @return Returns null if the url does not match our host (the protocol get's ignored)
266
+ */
267
+ targetToRequest(target, opts = {}) {
268
+ return this.inner.targetToRequest(target, opts);
269
+ }
244
270
  setNewRoute(route) {
245
271
  this._route.setSilent(route);
246
272
  const siteChanged = this.site.get()?.id !== route.site.id;
@@ -269,11 +295,11 @@ export default class Router {
269
295
  });
270
296
  };
271
297
  });
272
- const route = this.inner.targetToRequest(url);
273
- route.origin = 'init';
298
+ const req = this.inner.targetToRequest(url);
299
+ req.origin = 'init';
274
300
  // let's see if the url matches any route and site
275
301
  // if not let's redirect to the site which matches the acceptLang
276
- if (!route.siteMatches()) {
302
+ if (!req.siteMatches()) {
277
303
  const site = this.inner.siteByAcceptLang(acceptLang);
278
304
  return {
279
305
  success: true,
@@ -282,12 +308,13 @@ export default class Router {
282
308
  props: {},
283
309
  };
284
310
  }
285
- this.inner.setRoute(route);
311
+ this.inner.route = req.toRoute();
312
+ this.inner.onRoute(req, () => { });
286
313
  const resp = await prom;
287
314
  const hist = this.inner.history;
288
315
  if (hist.url || hist.req) {
289
316
  const nReq = this.inner.targetToRequest(hist.req ?? hist.url);
290
- if (!route.eq(nReq)) {
317
+ if (!req.eq(nReq)) {
291
318
  return {
292
319
  success: true,
293
320
  redirect: true,
@@ -1 +1 @@
1
- {"version":3,"file":"SsrCache.d.ts","sourceRoot":"","sources":["../../../../src/ssr/SsrCache.ts"],"names":[],"mappings":"AAAA,wBAAsB,OAAO,CAAC,IAAI,EAAE,GAAG,mBAetC;AAED;;;;;GAKG;AACH,MAAM,CAAC,OAAO,OAAO,QAAQ;IAC5B,OAAO,CAAC,KAAK,CAAsB;;IAYnC;;OAEG;IACG,IAAI,CAAC,CAAC,EAAE,GAAG,EAAE,MAAM,EAAE,EAAE,EAAE,MAAM,OAAO,CAAC,CAAC,CAAC;IAO/C;;OAEG;IACH,GAAG,CAAC,CAAC,EAAE,GAAG,EAAE,MAAM,GAAG,CAAC,GAAG,IAAI;IAI7B;;OAEG;IACH,GAAG,CAAC,CAAC,EAAE,GAAG,EAAE,MAAM,EAAE,GAAG,EAAE,CAAC,GAAG,CAAC;IAI9B,cAAc;IACd,KAAK;IAIL,OAAO,CAAC,YAAY;IAIpB,cAAc;IACd,aAAa,IAAI,MAAM;CAGvB"}
1
+ {"version":3,"file":"SsrCache.d.ts","sourceRoot":"","sources":["../../../../src/ssr/SsrCache.ts"],"names":[],"mappings":"AAAA,wBAAsB,OAAO,CAAC,IAAI,EAAE,GAAG,mBAoBtC;AAED;;;;;GAKG;AACH,MAAM,CAAC,OAAO,OAAO,QAAQ;IAC5B,OAAO,CAAC,KAAK,CAAsB;;IAYnC;;OAEG;IACG,IAAI,CAAC,CAAC,EAAE,GAAG,EAAE,MAAM,EAAE,EAAE,EAAE,MAAM,OAAO,CAAC,CAAC,CAAC;IAO/C;;OAEG;IACH,GAAG,CAAC,CAAC,EAAE,GAAG,EAAE,MAAM,GAAG,CAAC,GAAG,IAAI;IAI7B;;OAEG;IACH,GAAG,CAAC,CAAC,EAAE,GAAG,EAAE,MAAM,EAAE,GAAG,EAAE,CAAC,GAAG,CAAC;IAI9B,cAAc;IACd,KAAK;IAIL,OAAO,CAAC,YAAY;IAIpB,cAAc;IACd,aAAa,IAAI,MAAM;CAGvB"}
@@ -1,7 +1,12 @@
1
1
  export async function calcKey(data) {
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)
6
+ return json;
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
  // Use the Web Crypto API to hash the data with SHA-1
6
11
  const hashBuffer = await crypto.subtle.digest('SHA-1', dataBuffer);
7
12
  // Convert the ArrayBuffer to a hex string
@@ -0,0 +1,2 @@
1
+ export declare function objClone(obj: any): any;
2
+ //# sourceMappingURL=utils.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"utils.d.ts","sourceRoot":"","sources":["../../../src/utils.ts"],"names":[],"mappings":"AAGA,wBAAgB,QAAQ,CAAC,GAAG,EAAE,GAAG,GAAG,GAAG,CAMtC"}
package/dist/utils.js ADDED
@@ -0,0 +1,8 @@
1
+ // This are internal utils. Consider adding them to crelte-std instead
2
+ // this tries to do a structuredClone and else just uses JSON
3
+ export function objClone(obj) {
4
+ if (typeof structuredClone === 'function') {
5
+ return structuredClone(obj);
6
+ }
7
+ return JSON.parse(JSON.stringify(obj));
8
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "crelte",
3
- "version": "0.2.2",
3
+ "version": "0.3.1",
4
4
  "author": "Crelte <support@crelte.com>",
5
5
  "type": "module",
6
6
  "scripts": {
@@ -70,7 +70,7 @@
70
70
  }
71
71
  },
72
72
  "dependencies": {
73
- "crelte-std": "^0.1.0",
73
+ "crelte-std": "^0.1.1",
74
74
  "svelte": "^4.2.12"
75
75
  },
76
76
  "devDependencies": {