@contello/store 2.1.0 → 2.1.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.cjs CHANGED
@@ -248,6 +248,7 @@ var schema = { operations, sources, models };
248
248
  // src/utils.ts
249
249
  var import_client = require("@contello/client");
250
250
  var DEFAULT_TTL_MS = 3 * 60 * 60 * 1e3;
251
+ var DEFAULT_LRU_MAX = 1e3;
251
252
  function resolveTtl(ttl) {
252
253
  if (ttl === void 0) return DEFAULT_TTL_MS;
253
254
  if (ttl === false || ttl <= 0) return void 0;
@@ -348,7 +349,7 @@ function mapAsset(raw) {
348
349
  id: raw.id,
349
350
  original: mapFile(raw.original),
350
351
  preview: raw.preview ? mapFile(raw.preview) : void 0,
351
- optimized: raw.optimized.map(mapFile)
352
+ optimized: raw.optimized.map((file) => mapFile(file))
352
353
  };
353
354
  }
354
355
  function createAssetsCollection(options, client, updates$, refreshByTtl) {
@@ -368,7 +369,7 @@ function createAssetsCollection(options, client, updates$, refreshByTtl) {
368
369
  () => (0, import_client2.collectAsync)(
369
370
  (0, import_client2.mapAsync)(client.subscribe(assetsSourceDoc, { ids }), (data) => data.source)
370
371
  ).then((rawItems) => {
371
- const items = rawItems.map(mapAsset);
372
+ const items = rawItems.map((item) => mapAsset(item));
372
373
  if (ids === void 0 && !loaded) {
373
374
  loaded = true;
374
375
  ttl.mark();
@@ -461,7 +462,7 @@ function createAssetsSyncCollection(options, client, updates$, refreshByTtl) {
461
462
  const { instance: base, destroy } = createAssetsCollection(options, client, updates$, refreshByTtl);
462
463
  function assertSync(value, method) {
463
464
  if (value instanceof Promise) {
464
- throw new Error(`assets.${method}() is not initialized yet \u2014 call assets.load() first`);
465
+ throw new TypeError(`assets.${method}() is not initialized yet \u2014 call assets.load() first`);
465
466
  }
466
467
  return value;
467
468
  }
@@ -513,12 +514,14 @@ var DependencyCollector = class {
513
514
  throw new Error(`track(): object with __typename "${obj.__typename}" has no "id" field`);
514
515
  }
515
516
  switch (obj.__typename) {
516
- case "ContelloAsset":
517
+ case "ContelloAsset": {
517
518
  deps.push(createDependencyKey("asset", obj.id));
518
519
  break;
519
- case "ContelloRoute":
520
+ }
521
+ case "ContelloRoute": {
520
522
  deps.push(createDependencyKey("route", obj.id));
521
523
  break;
524
+ }
522
525
  default: {
523
526
  const model = this.resolver.getModel(obj.__typename);
524
527
  if (!model) {
@@ -664,11 +667,13 @@ function createCollection(source, options, client, updates$, resolver, refreshBy
664
667
  fetchCollection(source, client, ids),
665
668
  (rawItems) => Promise.all(
666
669
  rawItems.map(
667
- (item) => dependencyCollector.createContext(
668
- (ref, register) => (0, import_projected2.maybeThen)(mapFn(item, ref), (mapped) => {
669
- register(mapped.id);
670
- return mapped;
671
- })
670
+ (item) => Promise.resolve(
671
+ dependencyCollector.createContext(
672
+ (ref, register) => (0, import_projected2.maybeThen)(mapFn(item, ref), (mapped) => {
673
+ register(mapped.id);
674
+ return mapped;
675
+ })
676
+ )
672
677
  )
673
678
  )
674
679
  ).then((items) => {
@@ -796,7 +801,7 @@ function createCollectionSync(source, options, client, updates$, resolver, refre
796
801
  );
797
802
  function assertSync(value, method) {
798
803
  if (value instanceof Promise) {
799
- throw new Error(`collection "${base.name}".${method}() is not initialized yet \u2014 call collection.load() first`);
804
+ throw new TypeError(`collection "${base.name}".${method}() is not initialized yet \u2014 call collection.load() first`);
800
805
  }
801
806
  return value;
802
807
  }
@@ -935,7 +940,7 @@ function createI18nMessagesCollection(def, client, updates$, refreshByTtl) {
935
940
  `i18n-register:${def.collection}`,
936
941
  () => client.execute(storeRegisterI18nMessagesDocument, {
937
942
  collection: def.collection,
938
- messages: messages.map(toGqlMessageInput)
943
+ messages: messages.map((message) => toGqlMessageInput(message))
939
944
  }).then(() => {
940
945
  })
941
946
  );
@@ -993,13 +998,13 @@ function mapAsset2(raw) {
993
998
  id: raw.id,
994
999
  original: mapFile2(raw.original),
995
1000
  preview: raw.preview ? mapFile2(raw.preview) : void 0,
996
- optimized: raw.optimized.map(mapFile2)
1001
+ optimized: raw.optimized.map((file) => mapFile2(file))
997
1002
  };
998
1003
  }
999
1004
  function createLazyAssetsCollection(def, client, updates$) {
1000
1005
  const _def = {
1001
1006
  cache: {
1002
- max: def?.cache?.max ?? 1e3,
1007
+ max: def?.cache?.max ?? DEFAULT_LRU_MAX,
1003
1008
  ttl: resolveTtl(def?.cache?.ttl)
1004
1009
  }
1005
1010
  };
@@ -1011,7 +1016,7 @@ function createLazyAssetsCollection(def, client, updates$) {
1011
1016
  "assets",
1012
1017
  () => (0, import_client5.collectAsync)(
1013
1018
  (0, import_client5.mapAsync)(client.subscribe(assetsSourceDoc, { ids }), (data) => data.source)
1014
- ).then((rawItems) => rawItems.map(mapAsset2))
1019
+ ).then((rawItems) => rawItems.map((item) => mapAsset2(item)))
1015
1020
  ),
1016
1021
  cache
1017
1022
  });
@@ -1085,7 +1090,7 @@ function createLazyCollection(source, options, client, updates$, resolver) {
1085
1090
  name: opts.name ?? source.__model,
1086
1091
  model: source.__model,
1087
1092
  cache: {
1088
- max: opts.cache?.max ?? 1e3,
1093
+ max: opts.cache?.max ?? DEFAULT_LRU_MAX,
1089
1094
  ttl: resolveTtl(opts.cache?.ttl)
1090
1095
  }
1091
1096
  };
@@ -1110,11 +1115,13 @@ function createLazyCollection(source, options, client, updates$, resolver) {
1110
1115
  ),
1111
1116
  (rawItems) => Promise.all(
1112
1117
  rawItems.map(
1113
- (item) => dependencyCollector.createContext(
1114
- (ref, register) => (0, import_projected5.maybeThen)(mapFn(item, ref), (mapped) => {
1115
- register(mapped.id);
1116
- return mapped;
1117
- })
1118
+ (item) => Promise.resolve(
1119
+ dependencyCollector.createContext(
1120
+ (ref, register) => (0, import_projected5.maybeThen)(mapFn(item, ref), (mapped) => {
1121
+ register(mapped.id);
1122
+ return mapped;
1123
+ })
1124
+ )
1118
1125
  )
1119
1126
  )
1120
1127
  )
@@ -1200,21 +1207,23 @@ function mapRoute(raw, resolver) {
1200
1207
  customHeaders: raw.customHeaders.map((h) => ({ name: h.name, value: h.value }))
1201
1208
  };
1202
1209
  switch (raw.target.__typename) {
1203
- case "ContelloRouteTargetRedirect":
1210
+ case "ContelloRouteTargetRedirect": {
1204
1211
  return {
1205
1212
  ...base,
1206
1213
  type: "redirect",
1207
1214
  location: raw.target.location,
1208
1215
  status: raw.target.responseCode
1209
1216
  };
1210
- case "ContelloRouteTargetText":
1217
+ }
1218
+ case "ContelloRouteTargetText": {
1211
1219
  return {
1212
1220
  ...base,
1213
1221
  type: "text",
1214
1222
  content: raw.target.content,
1215
1223
  mimeType: raw.target.mimeType
1216
1224
  };
1217
- case "ContelloRouteTargetAsset":
1225
+ }
1226
+ case "ContelloRouteTargetAsset": {
1218
1227
  return {
1219
1228
  ...base,
1220
1229
  type: "asset",
@@ -1223,6 +1232,7 @@ function mapRoute(raw, resolver) {
1223
1232
  contentDisposition: raw.target.contentDisposition === "INLINE" ? "inline" : "attachment",
1224
1233
  mimeType: raw.target.asset.original.mimeType
1225
1234
  };
1235
+ }
1226
1236
  case "ContelloRouteTargetEntity": {
1227
1237
  if (!raw.target.entity) {
1228
1238
  return void 0;
@@ -1236,8 +1246,9 @@ function mapRoute(raw, resolver) {
1236
1246
  entityId: raw.target.entity.id
1237
1247
  };
1238
1248
  }
1239
- default:
1249
+ default: {
1240
1250
  return void 0;
1251
+ }
1241
1252
  }
1242
1253
  }
1243
1254
 
@@ -1299,7 +1310,7 @@ function createRoutesCache(max, ttl) {
1299
1310
  function createLazyRoutesCollection(def, client, updates$, resolver) {
1300
1311
  const _def = {
1301
1312
  cache: {
1302
- max: def?.cache?.max ?? 1e3,
1313
+ max: def?.cache?.max ?? DEFAULT_LRU_MAX,
1303
1314
  ttl: resolveTtl(def?.cache?.ttl)
1304
1315
  }
1305
1316
  };
@@ -1432,7 +1443,7 @@ var ModelResolver = class {
1432
1443
  return this._toTypeName.get(model) ?? model;
1433
1444
  }
1434
1445
  hasModel(model) {
1435
- return !this._toTypeName.size || this._toTypeName.has(model);
1446
+ return this._toTypeName.size === 0 || this._toTypeName.has(model);
1436
1447
  }
1437
1448
  resolveModel(typeName) {
1438
1449
  return this._toModel.get(typeName) ?? typeName;
@@ -1588,7 +1599,8 @@ function createRoutesCollection(options, client, updates$, resolver, refreshByTt
1588
1599
  if (loaded) {
1589
1600
  return void 0;
1590
1601
  }
1591
- return (0, import_projected7.maybeThen)(projected.getAllAsMap(), () => void 0);
1602
+ return (0, import_projected7.maybeThen)(projected.getAllAsMap(), () => {
1603
+ });
1592
1604
  }
1593
1605
  const instance = {
1594
1606
  refresh$: channel.stream$,
@@ -1598,10 +1610,10 @@ function createRoutesCollection(options, client, updates$, resolver, refreshByTt
1598
1610
  }
1599
1611
  },
1600
1612
  get(idOrIds) {
1601
- return Array.isArray(idOrIds) ? getByIds(idOrIds) : getByIdSingle(idOrIds);
1613
+ return typeof idOrIds === "string" ? getByIdSingle(idOrIds) : getByIds(idOrIds);
1602
1614
  },
1603
1615
  getByPath(pathOrPaths) {
1604
- return Array.isArray(pathOrPaths) ? lookupByPaths(pathOrPaths) : lookupByPathSingle(pathOrPaths);
1616
+ return typeof pathOrPaths === "string" ? lookupByPathSingle(pathOrPaths) : lookupByPaths(pathOrPaths);
1605
1617
  },
1606
1618
  refresh() {
1607
1619
  runFullRefresh("on-demand");
@@ -1620,7 +1632,7 @@ function createRoutesSyncCollection(options, client, updates$, resolver, refresh
1620
1632
  const { instance: base, destroy } = createRoutesCollection(options, client, updates$, resolver, refreshByTtl);
1621
1633
  function assertSync(value, method) {
1622
1634
  if (value instanceof Promise) {
1623
- throw new Error(`routes.${method}() is not initialized yet \u2014 call routes.load() first`);
1635
+ throw new TypeError(`routes.${method}() is not initialized yet \u2014 call routes.load() first`);
1624
1636
  }
1625
1637
  return value;
1626
1638
  }
@@ -1755,7 +1767,7 @@ function createSingletonSync(source, options, client, updates$, resolver, refres
1755
1767
  get() {
1756
1768
  const result = base.get();
1757
1769
  if (result instanceof Promise) {
1758
- throw new Error(`singleton "${base.name}" is not initialized yet \u2014 call singleton.load() first`);
1770
+ throw new TypeError(`singleton "${base.name}" is not initialized yet \u2014 call singleton.load() first`);
1759
1771
  }
1760
1772
  return result;
1761
1773
  }
@@ -1782,7 +1794,7 @@ function createUpdateBatch(events) {
1782
1794
  const routeByEntityModel = /* @__PURE__ */ new Map();
1783
1795
  for (const event of events) {
1784
1796
  switch (event.target) {
1785
- case "route":
1797
+ case "route": {
1786
1798
  route.push(event);
1787
1799
  if ("after" in event && event.after.type === "entity") {
1788
1800
  addRouteByModel(routeByEntityModel, event.after.model, event);
@@ -1791,12 +1803,15 @@ function createUpdateBatch(events) {
1791
1803
  addRouteByModel(routeByEntityModel, event.before.model, event);
1792
1804
  }
1793
1805
  break;
1794
- case "asset":
1806
+ }
1807
+ case "asset": {
1795
1808
  asset.push(event);
1796
1809
  break;
1797
- case "i18nMessage":
1810
+ }
1811
+ case "i18nMessage": {
1798
1812
  i18nMessage.push(event);
1799
1813
  break;
1814
+ }
1800
1815
  case "entity": {
1801
1816
  let list = entity.get(event.model);
1802
1817
  if (!list) {
@@ -1812,12 +1827,15 @@ function createUpdateBatch(events) {
1812
1827
  }
1813
1828
  function castMutationType(type) {
1814
1829
  switch (type) {
1815
- case "CREATE":
1830
+ case "CREATE": {
1816
1831
  return "create";
1817
- case "UPDATE":
1832
+ }
1833
+ case "UPDATE": {
1818
1834
  return "update";
1819
- case "DELETE":
1835
+ }
1836
+ case "DELETE": {
1820
1837
  return "delete";
1838
+ }
1821
1839
  }
1822
1840
  }
1823
1841
  function mapEvent(raw, resolver) {
@@ -1842,10 +1860,12 @@ function mapEvent(raw, resolver) {
1842
1860
  }
1843
1861
  return { id, mutation, target: "route", after, before };
1844
1862
  }
1845
- case "ContelloAsset":
1863
+ case "ContelloAsset": {
1846
1864
  return { id, mutation, target: "asset" };
1847
- case "ContelloI18nMessage":
1865
+ }
1866
+ case "ContelloI18nMessage": {
1848
1867
  return { id, mutation, target: "i18nMessage", token: target.token };
1868
+ }
1849
1869
  default: {
1850
1870
  if (!__typename || !resolver.hasTypeName(__typename)) {
1851
1871
  return void 0;
package/dist/index.d.cts CHANGED
@@ -114,7 +114,8 @@ type SourceAt<TSchema, TKey> = TSchema extends {
114
114
  } ? TKey extends keyof TSources ? TSources[TKey] : never : never;
115
115
  /**
116
116
  * For `define*(arg, options?)` — accepts either a string key (looked up in `schema.sources`) or a SourceDef directly.
117
- * `ResolveSource` projects the argument to the underlying SourceDef so `ExtractSourceResult` can extract the fragment type.
117
+ * `ResolveSource` projects the argument to the underlying SourceDef so `ExtractSourceResult`
118
+ * can extract the fragment type.
118
119
  */
119
120
  type ResolveSource<TSchema, T> = T extends string ? SourceAt<TSchema, T> : T extends SourceDef<any, any, any> ? T : never;
120
121
  type CreateStoreOptions<TSchema extends Schema | undefined = undefined> = {
@@ -161,7 +162,10 @@ type CacheOptions = {
161
162
  */
162
163
  eviction?: 'refresh' | 'clear' | undefined;
163
164
  };
164
- /** Cache options for sync collections and singletons. `eviction` is omitted — clearing the cache would break the sync guarantee. */
165
+ /**
166
+ * Cache options for sync collections and singletons. `eviction` is omitted — clearing
167
+ * the cache would break the sync guarantee.
168
+ */
165
169
  type SyncCacheOptions = {
166
170
  /**
167
171
  * Periodic full refresh — guards against stale data from missed update events.
@@ -235,7 +239,7 @@ type Collection<T> = {
235
239
  readonly refresh$: AsyncIterable<RefreshEvent>;
236
240
  load(): Promise<void>;
237
241
  get(id: string): MaybePromise<ReadonlyDeep<T> | undefined>;
238
- get(ids: string[]): MaybePromise<ReadonlyArray<ReadonlyDeep<T>>>;
242
+ get(ids: readonly string[]): MaybePromise<ReadonlyArray<ReadonlyDeep<T>>>;
239
243
  getAll(): MaybePromise<ReadonlyArray<ReadonlyDeep<T>>>;
240
244
  refresh(): void;
241
245
  };
@@ -244,7 +248,7 @@ type CollectionSync<T> = {
244
248
  readonly refresh$: AsyncIterable<RefreshEvent>;
245
249
  load(): Promise<void>;
246
250
  get(id: string): ReadonlyDeep<T> | undefined;
247
- get(ids: string[]): ReadonlyArray<ReadonlyDeep<T>>;
251
+ get(ids: readonly string[]): ReadonlyArray<ReadonlyDeep<T>>;
248
252
  getAll(): ReadonlyArray<ReadonlyDeep<T>>;
249
253
  refresh(): void;
250
254
  };
@@ -260,7 +264,7 @@ type LazyCollection<T> = {
260
264
  readonly name: string;
261
265
  readonly refresh$: AsyncIterable<RefreshEvent>;
262
266
  get(id: string): MaybePromise<ReadonlyDeep<T> | undefined>;
263
- get(ids: string[]): MaybePromise<ReadonlyArray<ReadonlyDeep<T>>>;
267
+ get(ids: readonly string[]): MaybePromise<ReadonlyArray<ReadonlyDeep<T>>>;
264
268
  refresh(): void;
265
269
  clear(): void;
266
270
  };
@@ -287,7 +291,7 @@ type LazyAssetsOptions = {
287
291
  type LazyAssets = {
288
292
  readonly refresh$: AsyncIterable<RefreshEvent>;
289
293
  get(id: string): Promise<ReadonlyDeep<StoreAsset> | undefined>;
290
- get(ids: string[]): Promise<ReadonlyDeep<StoreAsset[]>>;
294
+ get(ids: readonly string[]): Promise<ReadonlyDeep<StoreAsset[]>>;
291
295
  upload(data: UploadData, meta: UploadMetadata, options?: UploadOptions | undefined): Promise<string>;
292
296
  download(fileId: string): Promise<DownloadResult>;
293
297
  proxyHls(path: string, signal?: AbortSignal | undefined): Promise<ProxyResult>;
@@ -309,7 +313,7 @@ type Assets = {
309
313
  readonly refresh$: AsyncIterable<RefreshEvent>;
310
314
  load(): Promise<void>;
311
315
  get(id: string): MaybePromise<ReadonlyDeep<StoreAsset> | undefined>;
312
- get(ids: string[]): MaybePromise<ReadonlyArray<ReadonlyDeep<StoreAsset>>>;
316
+ get(ids: readonly string[]): MaybePromise<ReadonlyArray<ReadonlyDeep<StoreAsset>>>;
313
317
  upload(data: UploadData, meta: UploadMetadata, options?: UploadOptions | undefined): Promise<string>;
314
318
  download(fileId: string): Promise<DownloadResult>;
315
319
  proxyHls(path: string, signal?: AbortSignal | undefined): Promise<ProxyResult>;
@@ -319,7 +323,7 @@ type AssetsSync = {
319
323
  readonly refresh$: AsyncIterable<RefreshEvent>;
320
324
  load(): Promise<void>;
321
325
  get(id: string): ReadonlyDeep<StoreAsset> | undefined;
322
- get(ids: string[]): ReadonlyArray<ReadonlyDeep<StoreAsset>>;
326
+ get(ids: readonly string[]): ReadonlyArray<ReadonlyDeep<StoreAsset>>;
323
327
  upload(data: UploadData, meta: UploadMetadata, options?: UploadOptions | undefined): Promise<string>;
324
328
  download(fileId: string): Promise<DownloadResult>;
325
329
  proxyHls(path: string, signal?: AbortSignal | undefined): Promise<ProxyResult>;
@@ -362,7 +366,7 @@ type I18nMessage = {
362
366
  type I18nMessages = {
363
367
  readonly refresh$: AsyncIterable<RefreshEvent>;
364
368
  get(id: string): MaybePromise<ReadonlyDeep<I18nMessage> | undefined>;
365
- get(ids: string[]): MaybePromise<ReadonlyDeep<I18nMessage[]>>;
369
+ get(ids: readonly string[]): MaybePromise<ReadonlyDeep<I18nMessage[]>>;
366
370
  getAll(): MaybePromise<ReadonlyDeep<I18nMessage[]>>;
367
371
  register(messages: I18nMessageRegistrationDefinition[]): Promise<void>;
368
372
  refresh(): void;
@@ -377,9 +381,9 @@ type LazyRoutesOptions = {
377
381
  type LazyRoutes = {
378
382
  readonly refresh$: AsyncIterable<RefreshEvent>;
379
383
  get(id: string): Promise<ReadonlyDeep<StoreRoute> | undefined>;
380
- get(ids: string[]): Promise<ReadonlyDeep<StoreRoute[]>>;
384
+ get(ids: readonly string[]): Promise<ReadonlyDeep<StoreRoute[]>>;
381
385
  getByPath(path: string): Promise<ReadonlyDeep<StoreRoute> | undefined>;
382
- getByPath(paths: string[]): Promise<ReadonlyDeep<StoreRoute[]>>;
386
+ getByPath(paths: readonly string[]): Promise<ReadonlyDeep<StoreRoute[]>>;
383
387
  refresh(): void;
384
388
  clear(): void;
385
389
  };
@@ -398,18 +402,18 @@ type Routes = {
398
402
  readonly refresh$: AsyncIterable<RefreshEvent>;
399
403
  load(): Promise<void>;
400
404
  get(id: string): MaybePromise<ReadonlyDeep<StoreRoute> | undefined>;
401
- get(ids: string[]): MaybePromise<ReadonlyArray<ReadonlyDeep<StoreRoute>>>;
405
+ get(ids: readonly string[]): MaybePromise<ReadonlyArray<ReadonlyDeep<StoreRoute>>>;
402
406
  getByPath(path: string): MaybePromise<ReadonlyDeep<StoreRoute> | undefined>;
403
- getByPath(paths: string[]): MaybePromise<ReadonlyArray<ReadonlyDeep<StoreRoute>>>;
407
+ getByPath(paths: readonly string[]): MaybePromise<ReadonlyArray<ReadonlyDeep<StoreRoute>>>;
404
408
  refresh(): void;
405
409
  };
406
410
  type RoutesSync = {
407
411
  readonly refresh$: AsyncIterable<RefreshEvent>;
408
412
  load(): Promise<void>;
409
413
  get(id: string): ReadonlyDeep<StoreRoute> | undefined;
410
- get(ids: string[]): ReadonlyArray<ReadonlyDeep<StoreRoute>>;
414
+ get(ids: readonly string[]): ReadonlyArray<ReadonlyDeep<StoreRoute>>;
411
415
  getByPath(path: string): ReadonlyDeep<StoreRoute> | undefined;
412
- getByPath(paths: string[]): ReadonlyArray<ReadonlyDeep<StoreRoute>>;
416
+ getByPath(paths: readonly string[]): ReadonlyArray<ReadonlyDeep<StoreRoute>>;
413
417
  refresh(): void;
414
418
  };
415
419
 
package/dist/index.d.ts CHANGED
@@ -114,7 +114,8 @@ type SourceAt<TSchema, TKey> = TSchema extends {
114
114
  } ? TKey extends keyof TSources ? TSources[TKey] : never : never;
115
115
  /**
116
116
  * For `define*(arg, options?)` — accepts either a string key (looked up in `schema.sources`) or a SourceDef directly.
117
- * `ResolveSource` projects the argument to the underlying SourceDef so `ExtractSourceResult` can extract the fragment type.
117
+ * `ResolveSource` projects the argument to the underlying SourceDef so `ExtractSourceResult`
118
+ * can extract the fragment type.
118
119
  */
119
120
  type ResolveSource<TSchema, T> = T extends string ? SourceAt<TSchema, T> : T extends SourceDef<any, any, any> ? T : never;
120
121
  type CreateStoreOptions<TSchema extends Schema | undefined = undefined> = {
@@ -161,7 +162,10 @@ type CacheOptions = {
161
162
  */
162
163
  eviction?: 'refresh' | 'clear' | undefined;
163
164
  };
164
- /** Cache options for sync collections and singletons. `eviction` is omitted — clearing the cache would break the sync guarantee. */
165
+ /**
166
+ * Cache options for sync collections and singletons. `eviction` is omitted — clearing
167
+ * the cache would break the sync guarantee.
168
+ */
165
169
  type SyncCacheOptions = {
166
170
  /**
167
171
  * Periodic full refresh — guards against stale data from missed update events.
@@ -235,7 +239,7 @@ type Collection<T> = {
235
239
  readonly refresh$: AsyncIterable<RefreshEvent>;
236
240
  load(): Promise<void>;
237
241
  get(id: string): MaybePromise<ReadonlyDeep<T> | undefined>;
238
- get(ids: string[]): MaybePromise<ReadonlyArray<ReadonlyDeep<T>>>;
242
+ get(ids: readonly string[]): MaybePromise<ReadonlyArray<ReadonlyDeep<T>>>;
239
243
  getAll(): MaybePromise<ReadonlyArray<ReadonlyDeep<T>>>;
240
244
  refresh(): void;
241
245
  };
@@ -244,7 +248,7 @@ type CollectionSync<T> = {
244
248
  readonly refresh$: AsyncIterable<RefreshEvent>;
245
249
  load(): Promise<void>;
246
250
  get(id: string): ReadonlyDeep<T> | undefined;
247
- get(ids: string[]): ReadonlyArray<ReadonlyDeep<T>>;
251
+ get(ids: readonly string[]): ReadonlyArray<ReadonlyDeep<T>>;
248
252
  getAll(): ReadonlyArray<ReadonlyDeep<T>>;
249
253
  refresh(): void;
250
254
  };
@@ -260,7 +264,7 @@ type LazyCollection<T> = {
260
264
  readonly name: string;
261
265
  readonly refresh$: AsyncIterable<RefreshEvent>;
262
266
  get(id: string): MaybePromise<ReadonlyDeep<T> | undefined>;
263
- get(ids: string[]): MaybePromise<ReadonlyArray<ReadonlyDeep<T>>>;
267
+ get(ids: readonly string[]): MaybePromise<ReadonlyArray<ReadonlyDeep<T>>>;
264
268
  refresh(): void;
265
269
  clear(): void;
266
270
  };
@@ -287,7 +291,7 @@ type LazyAssetsOptions = {
287
291
  type LazyAssets = {
288
292
  readonly refresh$: AsyncIterable<RefreshEvent>;
289
293
  get(id: string): Promise<ReadonlyDeep<StoreAsset> | undefined>;
290
- get(ids: string[]): Promise<ReadonlyDeep<StoreAsset[]>>;
294
+ get(ids: readonly string[]): Promise<ReadonlyDeep<StoreAsset[]>>;
291
295
  upload(data: UploadData, meta: UploadMetadata, options?: UploadOptions | undefined): Promise<string>;
292
296
  download(fileId: string): Promise<DownloadResult>;
293
297
  proxyHls(path: string, signal?: AbortSignal | undefined): Promise<ProxyResult>;
@@ -309,7 +313,7 @@ type Assets = {
309
313
  readonly refresh$: AsyncIterable<RefreshEvent>;
310
314
  load(): Promise<void>;
311
315
  get(id: string): MaybePromise<ReadonlyDeep<StoreAsset> | undefined>;
312
- get(ids: string[]): MaybePromise<ReadonlyArray<ReadonlyDeep<StoreAsset>>>;
316
+ get(ids: readonly string[]): MaybePromise<ReadonlyArray<ReadonlyDeep<StoreAsset>>>;
313
317
  upload(data: UploadData, meta: UploadMetadata, options?: UploadOptions | undefined): Promise<string>;
314
318
  download(fileId: string): Promise<DownloadResult>;
315
319
  proxyHls(path: string, signal?: AbortSignal | undefined): Promise<ProxyResult>;
@@ -319,7 +323,7 @@ type AssetsSync = {
319
323
  readonly refresh$: AsyncIterable<RefreshEvent>;
320
324
  load(): Promise<void>;
321
325
  get(id: string): ReadonlyDeep<StoreAsset> | undefined;
322
- get(ids: string[]): ReadonlyArray<ReadonlyDeep<StoreAsset>>;
326
+ get(ids: readonly string[]): ReadonlyArray<ReadonlyDeep<StoreAsset>>;
323
327
  upload(data: UploadData, meta: UploadMetadata, options?: UploadOptions | undefined): Promise<string>;
324
328
  download(fileId: string): Promise<DownloadResult>;
325
329
  proxyHls(path: string, signal?: AbortSignal | undefined): Promise<ProxyResult>;
@@ -362,7 +366,7 @@ type I18nMessage = {
362
366
  type I18nMessages = {
363
367
  readonly refresh$: AsyncIterable<RefreshEvent>;
364
368
  get(id: string): MaybePromise<ReadonlyDeep<I18nMessage> | undefined>;
365
- get(ids: string[]): MaybePromise<ReadonlyDeep<I18nMessage[]>>;
369
+ get(ids: readonly string[]): MaybePromise<ReadonlyDeep<I18nMessage[]>>;
366
370
  getAll(): MaybePromise<ReadonlyDeep<I18nMessage[]>>;
367
371
  register(messages: I18nMessageRegistrationDefinition[]): Promise<void>;
368
372
  refresh(): void;
@@ -377,9 +381,9 @@ type LazyRoutesOptions = {
377
381
  type LazyRoutes = {
378
382
  readonly refresh$: AsyncIterable<RefreshEvent>;
379
383
  get(id: string): Promise<ReadonlyDeep<StoreRoute> | undefined>;
380
- get(ids: string[]): Promise<ReadonlyDeep<StoreRoute[]>>;
384
+ get(ids: readonly string[]): Promise<ReadonlyDeep<StoreRoute[]>>;
381
385
  getByPath(path: string): Promise<ReadonlyDeep<StoreRoute> | undefined>;
382
- getByPath(paths: string[]): Promise<ReadonlyDeep<StoreRoute[]>>;
386
+ getByPath(paths: readonly string[]): Promise<ReadonlyDeep<StoreRoute[]>>;
383
387
  refresh(): void;
384
388
  clear(): void;
385
389
  };
@@ -398,18 +402,18 @@ type Routes = {
398
402
  readonly refresh$: AsyncIterable<RefreshEvent>;
399
403
  load(): Promise<void>;
400
404
  get(id: string): MaybePromise<ReadonlyDeep<StoreRoute> | undefined>;
401
- get(ids: string[]): MaybePromise<ReadonlyArray<ReadonlyDeep<StoreRoute>>>;
405
+ get(ids: readonly string[]): MaybePromise<ReadonlyArray<ReadonlyDeep<StoreRoute>>>;
402
406
  getByPath(path: string): MaybePromise<ReadonlyDeep<StoreRoute> | undefined>;
403
- getByPath(paths: string[]): MaybePromise<ReadonlyArray<ReadonlyDeep<StoreRoute>>>;
407
+ getByPath(paths: readonly string[]): MaybePromise<ReadonlyArray<ReadonlyDeep<StoreRoute>>>;
404
408
  refresh(): void;
405
409
  };
406
410
  type RoutesSync = {
407
411
  readonly refresh$: AsyncIterable<RefreshEvent>;
408
412
  load(): Promise<void>;
409
413
  get(id: string): ReadonlyDeep<StoreRoute> | undefined;
410
- get(ids: string[]): ReadonlyArray<ReadonlyDeep<StoreRoute>>;
414
+ get(ids: readonly string[]): ReadonlyArray<ReadonlyDeep<StoreRoute>>;
411
415
  getByPath(path: string): ReadonlyDeep<StoreRoute> | undefined;
412
- getByPath(paths: string[]): ReadonlyArray<ReadonlyDeep<StoreRoute>>;
416
+ getByPath(paths: readonly string[]): ReadonlyArray<ReadonlyDeep<StoreRoute>>;
413
417
  refresh(): void;
414
418
  };
415
419
 
package/dist/index.js CHANGED
@@ -214,6 +214,7 @@ var schema = { operations, sources, models };
214
214
  // src/utils.ts
215
215
  import { createAsyncIterableSubject, runWithBackoff } from "@contello/client";
216
216
  var DEFAULT_TTL_MS = 3 * 60 * 60 * 1e3;
217
+ var DEFAULT_LRU_MAX = 1e3;
217
218
  function resolveTtl(ttl) {
218
219
  if (ttl === void 0) return DEFAULT_TTL_MS;
219
220
  if (ttl === false || ttl <= 0) return void 0;
@@ -314,7 +315,7 @@ function mapAsset(raw) {
314
315
  id: raw.id,
315
316
  original: mapFile(raw.original),
316
317
  preview: raw.preview ? mapFile(raw.preview) : void 0,
317
- optimized: raw.optimized.map(mapFile)
318
+ optimized: raw.optimized.map((file) => mapFile(file))
318
319
  };
319
320
  }
320
321
  function createAssetsCollection(options, client, updates$, refreshByTtl) {
@@ -334,7 +335,7 @@ function createAssetsCollection(options, client, updates$, refreshByTtl) {
334
335
  () => collectAsync(
335
336
  mapAsync(client.subscribe(assetsSourceDoc, { ids }), (data) => data.source)
336
337
  ).then((rawItems) => {
337
- const items = rawItems.map(mapAsset);
338
+ const items = rawItems.map((item) => mapAsset(item));
338
339
  if (ids === void 0 && !loaded) {
339
340
  loaded = true;
340
341
  ttl.mark();
@@ -427,7 +428,7 @@ function createAssetsSyncCollection(options, client, updates$, refreshByTtl) {
427
428
  const { instance: base, destroy } = createAssetsCollection(options, client, updates$, refreshByTtl);
428
429
  function assertSync(value, method) {
429
430
  if (value instanceof Promise) {
430
- throw new Error(`assets.${method}() is not initialized yet \u2014 call assets.load() first`);
431
+ throw new TypeError(`assets.${method}() is not initialized yet \u2014 call assets.load() first`);
431
432
  }
432
433
  return value;
433
434
  }
@@ -484,12 +485,14 @@ var DependencyCollector = class {
484
485
  throw new Error(`track(): object with __typename "${obj.__typename}" has no "id" field`);
485
486
  }
486
487
  switch (obj.__typename) {
487
- case "ContelloAsset":
488
+ case "ContelloAsset": {
488
489
  deps.push(createDependencyKey("asset", obj.id));
489
490
  break;
490
- case "ContelloRoute":
491
+ }
492
+ case "ContelloRoute": {
491
493
  deps.push(createDependencyKey("route", obj.id));
492
494
  break;
495
+ }
493
496
  default: {
494
497
  const model = this.resolver.getModel(obj.__typename);
495
498
  if (!model) {
@@ -635,11 +638,13 @@ function createCollection(source, options, client, updates$, resolver, refreshBy
635
638
  fetchCollection(source, client, ids),
636
639
  (rawItems) => Promise.all(
637
640
  rawItems.map(
638
- (item) => dependencyCollector.createContext(
639
- (ref, register) => maybeThen(mapFn(item, ref), (mapped) => {
640
- register(mapped.id);
641
- return mapped;
642
- })
641
+ (item) => Promise.resolve(
642
+ dependencyCollector.createContext(
643
+ (ref, register) => maybeThen(mapFn(item, ref), (mapped) => {
644
+ register(mapped.id);
645
+ return mapped;
646
+ })
647
+ )
643
648
  )
644
649
  )
645
650
  ).then((items) => {
@@ -767,7 +772,7 @@ function createCollectionSync(source, options, client, updates$, resolver, refre
767
772
  );
768
773
  function assertSync(value, method) {
769
774
  if (value instanceof Promise) {
770
- throw new Error(`collection "${base.name}".${method}() is not initialized yet \u2014 call collection.load() first`);
775
+ throw new TypeError(`collection "${base.name}".${method}() is not initialized yet \u2014 call collection.load() first`);
771
776
  }
772
777
  return value;
773
778
  }
@@ -911,7 +916,7 @@ function createI18nMessagesCollection(def, client, updates$, refreshByTtl) {
911
916
  `i18n-register:${def.collection}`,
912
917
  () => client.execute(storeRegisterI18nMessagesDocument, {
913
918
  collection: def.collection,
914
- messages: messages.map(toGqlMessageInput)
919
+ messages: messages.map((message) => toGqlMessageInput(message))
915
920
  }).then(() => {
916
921
  })
917
922
  );
@@ -973,13 +978,13 @@ function mapAsset2(raw) {
973
978
  id: raw.id,
974
979
  original: mapFile2(raw.original),
975
980
  preview: raw.preview ? mapFile2(raw.preview) : void 0,
976
- optimized: raw.optimized.map(mapFile2)
981
+ optimized: raw.optimized.map((file) => mapFile2(file))
977
982
  };
978
983
  }
979
984
  function createLazyAssetsCollection(def, client, updates$) {
980
985
  const _def = {
981
986
  cache: {
982
- max: def?.cache?.max ?? 1e3,
987
+ max: def?.cache?.max ?? DEFAULT_LRU_MAX,
983
988
  ttl: resolveTtl(def?.cache?.ttl)
984
989
  }
985
990
  };
@@ -991,7 +996,7 @@ function createLazyAssetsCollection(def, client, updates$) {
991
996
  "assets",
992
997
  () => collectAsync4(
993
998
  mapAsync4(client.subscribe(assetsSourceDoc, { ids }), (data) => data.source)
994
- ).then((rawItems) => rawItems.map(mapAsset2))
999
+ ).then((rawItems) => rawItems.map((item) => mapAsset2(item)))
995
1000
  ),
996
1001
  cache
997
1002
  });
@@ -1070,7 +1075,7 @@ function createLazyCollection(source, options, client, updates$, resolver) {
1070
1075
  name: opts.name ?? source.__model,
1071
1076
  model: source.__model,
1072
1077
  cache: {
1073
- max: opts.cache?.max ?? 1e3,
1078
+ max: opts.cache?.max ?? DEFAULT_LRU_MAX,
1074
1079
  ttl: resolveTtl(opts.cache?.ttl)
1075
1080
  }
1076
1081
  };
@@ -1095,11 +1100,13 @@ function createLazyCollection(source, options, client, updates$, resolver) {
1095
1100
  ),
1096
1101
  (rawItems) => Promise.all(
1097
1102
  rawItems.map(
1098
- (item) => dependencyCollector.createContext(
1099
- (ref, register) => maybeThen3(mapFn(item, ref), (mapped) => {
1100
- register(mapped.id);
1101
- return mapped;
1102
- })
1103
+ (item) => Promise.resolve(
1104
+ dependencyCollector.createContext(
1105
+ (ref, register) => maybeThen3(mapFn(item, ref), (mapped) => {
1106
+ register(mapped.id);
1107
+ return mapped;
1108
+ })
1109
+ )
1103
1110
  )
1104
1111
  )
1105
1112
  )
@@ -1189,21 +1196,23 @@ function mapRoute(raw, resolver) {
1189
1196
  customHeaders: raw.customHeaders.map((h) => ({ name: h.name, value: h.value }))
1190
1197
  };
1191
1198
  switch (raw.target.__typename) {
1192
- case "ContelloRouteTargetRedirect":
1199
+ case "ContelloRouteTargetRedirect": {
1193
1200
  return {
1194
1201
  ...base,
1195
1202
  type: "redirect",
1196
1203
  location: raw.target.location,
1197
1204
  status: raw.target.responseCode
1198
1205
  };
1199
- case "ContelloRouteTargetText":
1206
+ }
1207
+ case "ContelloRouteTargetText": {
1200
1208
  return {
1201
1209
  ...base,
1202
1210
  type: "text",
1203
1211
  content: raw.target.content,
1204
1212
  mimeType: raw.target.mimeType
1205
1213
  };
1206
- case "ContelloRouteTargetAsset":
1214
+ }
1215
+ case "ContelloRouteTargetAsset": {
1207
1216
  return {
1208
1217
  ...base,
1209
1218
  type: "asset",
@@ -1212,6 +1221,7 @@ function mapRoute(raw, resolver) {
1212
1221
  contentDisposition: raw.target.contentDisposition === "INLINE" ? "inline" : "attachment",
1213
1222
  mimeType: raw.target.asset.original.mimeType
1214
1223
  };
1224
+ }
1215
1225
  case "ContelloRouteTargetEntity": {
1216
1226
  if (!raw.target.entity) {
1217
1227
  return void 0;
@@ -1225,8 +1235,9 @@ function mapRoute(raw, resolver) {
1225
1235
  entityId: raw.target.entity.id
1226
1236
  };
1227
1237
  }
1228
- default:
1238
+ default: {
1229
1239
  return void 0;
1240
+ }
1230
1241
  }
1231
1242
  }
1232
1243
 
@@ -1288,7 +1299,7 @@ function createRoutesCache(max, ttl) {
1288
1299
  function createLazyRoutesCollection(def, client, updates$, resolver) {
1289
1300
  const _def = {
1290
1301
  cache: {
1291
- max: def?.cache?.max ?? 1e3,
1302
+ max: def?.cache?.max ?? DEFAULT_LRU_MAX,
1292
1303
  ttl: resolveTtl(def?.cache?.ttl)
1293
1304
  }
1294
1305
  };
@@ -1421,7 +1432,7 @@ var ModelResolver = class {
1421
1432
  return this._toTypeName.get(model) ?? model;
1422
1433
  }
1423
1434
  hasModel(model) {
1424
- return !this._toTypeName.size || this._toTypeName.has(model);
1435
+ return this._toTypeName.size === 0 || this._toTypeName.has(model);
1425
1436
  }
1426
1437
  resolveModel(typeName) {
1427
1438
  return this._toModel.get(typeName) ?? typeName;
@@ -1582,7 +1593,8 @@ function createRoutesCollection(options, client, updates$, resolver, refreshByTt
1582
1593
  if (loaded) {
1583
1594
  return void 0;
1584
1595
  }
1585
- return maybeThen4(projected.getAllAsMap(), () => void 0);
1596
+ return maybeThen4(projected.getAllAsMap(), () => {
1597
+ });
1586
1598
  }
1587
1599
  const instance = {
1588
1600
  refresh$: channel.stream$,
@@ -1592,10 +1604,10 @@ function createRoutesCollection(options, client, updates$, resolver, refreshByTt
1592
1604
  }
1593
1605
  },
1594
1606
  get(idOrIds) {
1595
- return Array.isArray(idOrIds) ? getByIds(idOrIds) : getByIdSingle(idOrIds);
1607
+ return typeof idOrIds === "string" ? getByIdSingle(idOrIds) : getByIds(idOrIds);
1596
1608
  },
1597
1609
  getByPath(pathOrPaths) {
1598
- return Array.isArray(pathOrPaths) ? lookupByPaths(pathOrPaths) : lookupByPathSingle(pathOrPaths);
1610
+ return typeof pathOrPaths === "string" ? lookupByPathSingle(pathOrPaths) : lookupByPaths(pathOrPaths);
1599
1611
  },
1600
1612
  refresh() {
1601
1613
  runFullRefresh("on-demand");
@@ -1614,7 +1626,7 @@ function createRoutesSyncCollection(options, client, updates$, resolver, refresh
1614
1626
  const { instance: base, destroy } = createRoutesCollection(options, client, updates$, resolver, refreshByTtl);
1615
1627
  function assertSync(value, method) {
1616
1628
  if (value instanceof Promise) {
1617
- throw new Error(`routes.${method}() is not initialized yet \u2014 call routes.load() first`);
1629
+ throw new TypeError(`routes.${method}() is not initialized yet \u2014 call routes.load() first`);
1618
1630
  }
1619
1631
  return value;
1620
1632
  }
@@ -1754,7 +1766,7 @@ function createSingletonSync(source, options, client, updates$, resolver, refres
1754
1766
  get() {
1755
1767
  const result = base.get();
1756
1768
  if (result instanceof Promise) {
1757
- throw new Error(`singleton "${base.name}" is not initialized yet \u2014 call singleton.load() first`);
1769
+ throw new TypeError(`singleton "${base.name}" is not initialized yet \u2014 call singleton.load() first`);
1758
1770
  }
1759
1771
  return result;
1760
1772
  }
@@ -1785,7 +1797,7 @@ function createUpdateBatch(events) {
1785
1797
  const routeByEntityModel = /* @__PURE__ */ new Map();
1786
1798
  for (const event of events) {
1787
1799
  switch (event.target) {
1788
- case "route":
1800
+ case "route": {
1789
1801
  route.push(event);
1790
1802
  if ("after" in event && event.after.type === "entity") {
1791
1803
  addRouteByModel(routeByEntityModel, event.after.model, event);
@@ -1794,12 +1806,15 @@ function createUpdateBatch(events) {
1794
1806
  addRouteByModel(routeByEntityModel, event.before.model, event);
1795
1807
  }
1796
1808
  break;
1797
- case "asset":
1809
+ }
1810
+ case "asset": {
1798
1811
  asset.push(event);
1799
1812
  break;
1800
- case "i18nMessage":
1813
+ }
1814
+ case "i18nMessage": {
1801
1815
  i18nMessage.push(event);
1802
1816
  break;
1817
+ }
1803
1818
  case "entity": {
1804
1819
  let list = entity.get(event.model);
1805
1820
  if (!list) {
@@ -1815,12 +1830,15 @@ function createUpdateBatch(events) {
1815
1830
  }
1816
1831
  function castMutationType(type) {
1817
1832
  switch (type) {
1818
- case "CREATE":
1833
+ case "CREATE": {
1819
1834
  return "create";
1820
- case "UPDATE":
1835
+ }
1836
+ case "UPDATE": {
1821
1837
  return "update";
1822
- case "DELETE":
1838
+ }
1839
+ case "DELETE": {
1823
1840
  return "delete";
1841
+ }
1824
1842
  }
1825
1843
  }
1826
1844
  function mapEvent(raw, resolver) {
@@ -1845,10 +1863,12 @@ function mapEvent(raw, resolver) {
1845
1863
  }
1846
1864
  return { id, mutation, target: "route", after, before };
1847
1865
  }
1848
- case "ContelloAsset":
1866
+ case "ContelloAsset": {
1849
1867
  return { id, mutation, target: "asset" };
1850
- case "ContelloI18nMessage":
1868
+ }
1869
+ case "ContelloI18nMessage": {
1851
1870
  return { id, mutation, target: "i18nMessage", token: target.token };
1871
+ }
1852
1872
  default: {
1853
1873
  if (!__typename || !resolver.hasTypeName(__typename)) {
1854
1874
  return void 0;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@contello/store",
3
- "version": "2.1.0",
3
+ "version": "2.1.1",
4
4
  "description": "Framework-agnostic store for Contello CMS with reactive collections and automatic dependency tracking",
5
5
  "type": "module",
6
6
  "license": "MIT",
@@ -11,16 +11,16 @@
11
11
  "directory": "packages/store"
12
12
  },
13
13
  "peerDependencies": {
14
- "@contello/client": "^2.2.0",
15
- "@contello/media": "^1.0.1"
14
+ "@contello/client": "^2.2.1",
15
+ "@contello/media": "^1.1.0"
16
16
  },
17
17
  "dependencies": {
18
18
  "lru-cache": "^11.3.5",
19
- "projected": "^3.0.1"
19
+ "projected": "^3.0.2"
20
20
  },
21
21
  "devDependencies": {
22
- "@contello/media": "^1.0.1",
23
- "@contello/client": "^2.2.0"
22
+ "@contello/client": "^2.2.1",
23
+ "@contello/media": "^1.1.0"
24
24
  },
25
25
  "files": [
26
26
  "dist",
@@ -44,7 +44,7 @@
44
44
  "scripts": {
45
45
  "build": "tsup",
46
46
  "generate": "node --env-file=../../.env ../client/dist/cli.js generate",
47
- "lint": "eslint .",
47
+ "lint": "eslint . --cache",
48
48
  "lint:fix": "eslint . --fix",
49
49
  "typecheck": "tsc --noEmit"
50
50
  }