@contello/store 1.0.0 → 1.0.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.js CHANGED
@@ -1,19 +1,16 @@
1
1
  // src/store.ts
2
- import { ContelloSdkClient } from "@contello/sdk-client";
2
+ import { createContelloClient } from "@contello/client";
3
3
 
4
4
  // src/assets.ts
5
5
  import { ProjectedLazyMap } from "projected";
6
6
  import { Subject } from "rxjs";
7
7
 
8
- // graphql/assets.gql
9
- var assets_default = "fragment StoreFile on ContelloFile {\n uid\n mimeType\n metadata {\n ... on ContelloImageMetadata {\n width\n height\n }\n ... on ContelloVideoMetadata {\n width\n height\n }\n }\n}\n\nfragment StoreAsset on ContelloAsset {\n id\n original {\n ...StoreFile\n }\n preview {\n ...StoreFile\n }\n optimized {\n ...StoreFile\n }\n}\n\n\nquery StoreGetAssets($filter: ContelloAssetsFilterInput!) {\n contelloAssets(filter: $filter) {\n ...StoreAsset\n }\n}\n";
10
-
11
8
  // src/diagnostics.ts
12
9
  import dc from "diagnostics_channel";
13
10
  var channels = {
14
- start: "contello:store.start",
15
- end: "contello:store.end",
16
- error: "contello:store.error"
11
+ start: "@contello/store:start",
12
+ end: "@contello/store:end",
13
+ error: "@contello/store:error"
17
14
  };
18
15
  var onStart = dc.channel(channels.start);
19
16
  var onEnd = dc.channel(channels.end);
@@ -50,6 +47,168 @@ function wrap(name, fn) {
50
47
  return result;
51
48
  }
52
49
 
50
+ // src/generated/graphql.ts
51
+ var storeGetAssetsDocument = `fragment StoreFile on ContelloFile {
52
+ uid
53
+ mimeType
54
+ metadata {
55
+ ... on ContelloImageMetadata {
56
+ width
57
+ height
58
+ }
59
+ ... on ContelloVideoMetadata {
60
+ width
61
+ height
62
+ }
63
+ }
64
+ }
65
+ fragment StoreAsset on ContelloAsset {
66
+ id
67
+ original {
68
+ ...StoreFile
69
+ }
70
+ preview {
71
+ ...StoreFile
72
+ }
73
+ optimized {
74
+ ...StoreFile
75
+ }
76
+ }
77
+ query StoreGetAssets($filter: ContelloAssetsFilterInput!) {
78
+ contelloAssets(filter: $filter) {
79
+ ...StoreAsset
80
+ }
81
+ }`;
82
+ var storeGetI18nMessagesDocument = `subscription StoreGetI18nMessages($collection: String!) {
83
+ contelloI18nMessagesBatch(collectionReferenceName: $collection) {
84
+ id
85
+ token
86
+ translations {
87
+ language
88
+ value
89
+ }
90
+ }
91
+ }`;
92
+ var storeRegisterI18nMessagesDocument = `mutation StoreRegisterI18nMessages($collection: String!, $messages: [ContelloI18nMessageInput!]!) {
93
+ registerContelloI18nMessages(
94
+ collectionReferenceName: $collection
95
+ messages: $messages
96
+ deprecateUnmentioned: true
97
+ ) {
98
+ ids
99
+ }
100
+ }`;
101
+ var storeGetRoutesDocument = `fragment StoreRoute on ContelloRoute {
102
+ id
103
+ path
104
+ target {
105
+ __typename
106
+ ... on ContelloRouteTargetEntity {
107
+ entity {
108
+ __typename
109
+ ... on ContelloEntityBase {
110
+ id
111
+ }
112
+ }
113
+ }
114
+ ... on ContelloRouteTargetAsset {
115
+ asset {
116
+ id
117
+ original {
118
+ uid
119
+ mimeType
120
+ }
121
+ }
122
+ contentDisposition
123
+ }
124
+ ... on ContelloRouteTargetRedirect {
125
+ location
126
+ responseCode
127
+ }
128
+ ... on ContelloRouteTargetText {
129
+ content
130
+ mimeType
131
+ }
132
+ }
133
+ customHeaders {
134
+ name
135
+ value
136
+ }
137
+ }
138
+ query StoreGetRoutes($request: ContelloRoutesRequestInput!) {
139
+ contelloRoutes(request: $request) {
140
+ ...StoreRoute
141
+ }
142
+ }`;
143
+ var storeWatchUpdatesDocument = `fragment StoreRoute on ContelloRoute {
144
+ id
145
+ path
146
+ target {
147
+ __typename
148
+ ... on ContelloRouteTargetEntity {
149
+ entity {
150
+ __typename
151
+ ... on ContelloEntityBase {
152
+ id
153
+ }
154
+ }
155
+ }
156
+ ... on ContelloRouteTargetAsset {
157
+ asset {
158
+ id
159
+ original {
160
+ uid
161
+ mimeType
162
+ }
163
+ }
164
+ contentDisposition
165
+ }
166
+ ... on ContelloRouteTargetRedirect {
167
+ location
168
+ responseCode
169
+ }
170
+ ... on ContelloRouteTargetText {
171
+ content
172
+ mimeType
173
+ }
174
+ }
175
+ customHeaders {
176
+ name
177
+ value
178
+ }
179
+ }
180
+ subscription StoreWatchUpdates {
181
+ contelloUpdatesBatch {
182
+ events {
183
+ target {
184
+ __typename
185
+ ... on ContelloEntityBase {
186
+ id
187
+ }
188
+ ... on ContelloRoute {
189
+ ...StoreRoute
190
+ }
191
+ ... on ContelloAsset {
192
+ id
193
+ }
194
+ ... on ContelloI18nMessage {
195
+ id
196
+ token
197
+ }
198
+ }
199
+ prev {
200
+ __typename
201
+ ... on ContelloRoute {
202
+ ...StoreRoute
203
+ }
204
+ }
205
+ mutation {
206
+ type
207
+ }
208
+ }
209
+ }
210
+ }`;
211
+
53
212
  // src/lru.ts
54
213
  import { LRUCache } from "lru-cache";
55
214
  function createLruCache(options) {
@@ -79,18 +238,18 @@ function mapFile(raw) {
79
238
  return {
80
239
  uid: raw.uid,
81
240
  mimeType: raw.mimeType,
82
- metadata: raw.metadata ? { width: raw.metadata.width, height: raw.metadata.height } : null
241
+ metadata: raw.metadata ? { width: raw.metadata.width, height: raw.metadata.height } : void 0
83
242
  };
84
243
  }
85
244
  function mapAsset(raw) {
86
245
  return {
87
246
  id: raw.id,
88
247
  original: mapFile(raw.original),
89
- preview: raw.preview ? mapFile(raw.preview) : null,
248
+ preview: raw.preview ? mapFile(raw.preview) : void 0,
90
249
  optimized: raw.optimized.map(mapFile)
91
250
  };
92
251
  }
93
- function createAssetsCollection(def, client, baseUrl, updates$) {
252
+ function createAssetsCollection(def, client, updates$) {
94
253
  const _def = {
95
254
  cache: {
96
255
  max: def?.cache?.max ?? 1e3,
@@ -101,7 +260,7 @@ function createAssetsCollection(def, client, baseUrl, updates$) {
101
260
  key: (asset) => asset.id,
102
261
  values: (ids) => wrap(
103
262
  "assets",
104
- () => client.execute(assets_default, { filter: { ids } }).then(
263
+ () => client.execute(storeGetAssetsDocument, { filter: { ids } }).then(
105
264
  (data) => (data.contelloAssets ?? []).reduce((acc, raw) => {
106
265
  if (raw) {
107
266
  acc.push(mapAsset(raw));
@@ -126,8 +285,11 @@ function createAssetsCollection(def, client, baseUrl, updates$) {
126
285
  get(idOrIds) {
127
286
  return projected.get(idOrIds);
128
287
  },
288
+ upload(data, meta, options) {
289
+ return client.upload(data, meta, options);
290
+ },
129
291
  download(fileId) {
130
- return fetch(`${baseUrl}/api/v1/assets/files/${fileId}`);
292
+ return client.download(fileId);
131
293
  }
132
294
  };
133
295
  }
@@ -147,9 +309,9 @@ function createRouteTargetKey(typename, id) {
147
309
  return `routeTarget\0${typename}\0${id}`;
148
310
  }
149
311
  var DependencyCollector = class {
150
- constructor(model, entityTypes) {
312
+ constructor(model, resolver) {
151
313
  this.model = model;
152
- this.entityTypes = entityTypes;
314
+ this.resolver = resolver;
153
315
  }
154
316
  /** dep key -> item keys that declared a dependency on it */
155
317
  reverseIndex = /* @__PURE__ */ new Map();
@@ -175,19 +337,20 @@ var DependencyCollector = class {
175
337
  case "ContelloRoute":
176
338
  deps.push(createDependencyKey("route", obj.id));
177
339
  break;
178
- default:
179
- if (!obj.__typename.endsWith("Entity")) {
340
+ default: {
341
+ const model = this.resolver.getModel(obj.__typename);
342
+ if (!model) {
180
343
  throw new Error(
181
- `track(): unexpected __typename "${obj.__typename}" \u2014 expected ContelloAsset, ContelloRoute, or *Entity`
344
+ `track(): unexpected __typename "${obj.__typename}" \u2014 expected ContelloAsset, ContelloRoute, or a known entity model`
182
345
  );
183
346
  }
184
- this.warnIfUnknownEntityType(obj.__typename);
185
- deps.push(createEntityDependencyKey(obj.__typename, obj.id));
347
+ deps.push(createEntityDependencyKey(model, obj.id));
348
+ }
186
349
  }
187
350
  },
188
- trackEntity: (typename, id) => {
189
- this.warnIfUnknownEntityType(typename);
190
- deps.push(createEntityDependencyKey(typename, id));
351
+ trackEntity: (model, id) => {
352
+ this.warnIfUnknownModel(model);
353
+ deps.push(createEntityDependencyKey(model, id));
191
354
  },
192
355
  trackAsset: (id) => deps.push(createDependencyKey("asset", id)),
193
356
  trackRoute: (id) => deps.push(createDependencyKey("route", id))
@@ -222,10 +385,10 @@ var DependencyCollector = class {
222
385
  let affected = this.reverseIndex.get(createDependencyKey(event.target, event.id));
223
386
  if (event.target === "route") {
224
387
  if ("after" in event && event.after.type === "entity") {
225
- affected = this.mergeRouteTargetKeys(affected, event.after.entityType, event.after.entityId);
388
+ affected = this.mergeRouteTargetKeys(affected, event.after.model, event.after.entityId);
226
389
  }
227
390
  if ("before" in event && event.before.type === "entity") {
228
- affected = this.mergeRouteTargetKeys(affected, event.before.entityType, event.before.entityId);
391
+ affected = this.mergeRouteTargetKeys(affected, event.before.model, event.before.entityId);
229
392
  }
230
393
  }
231
394
  return affected ?? /* @__PURE__ */ new Set();
@@ -257,10 +420,10 @@ var DependencyCollector = class {
257
420
  }
258
421
  }
259
422
  }
260
- warnIfUnknownEntityType(typename) {
261
- if (this.entityTypes && !this.entityTypes.has(typename)) {
423
+ warnIfUnknownModel(model) {
424
+ if (!this.resolver.hasModel(model)) {
262
425
  console.warn(
263
- `[contello/store] tracking entity type "${typename}" which is not in entityTypes \u2014 updates for this type will be ignored`
426
+ `[contello/store] tracking model "${model}" which is not in models \u2014 updates for this model will be ignored`
264
427
  );
265
428
  }
266
429
  }
@@ -339,7 +502,7 @@ function collect(observable) {
339
502
  }
340
503
 
341
504
  // src/collection.ts
342
- function createCollection(def, sdk, updates$, entityTypes) {
505
+ function createCollection(def, client, updates$, resolver) {
343
506
  const _def = {
344
507
  name: def.name ?? def.model,
345
508
  model: def.model,
@@ -348,14 +511,14 @@ function createCollection(def, sdk, updates$, entityTypes) {
348
511
  eviction: def.cache?.eviction ?? "refresh"
349
512
  }
350
513
  };
351
- const dependencyCollector = new DependencyCollector(_def.model, entityTypes);
514
+ const dependencyCollector = new DependencyCollector(_def.model, resolver);
352
515
  let timer;
353
516
  const projected = new ProjectedMap({
354
517
  key: (item) => item.id,
355
518
  values: () => wrap(
356
519
  `collection:${_def.name}`,
357
520
  () => maybeThen(
358
- resolveFetchable(def.fetch(sdk)),
521
+ resolveFetchable(def.fetch(client)),
359
522
  (rawItems) => (
360
523
  // maybeAll one day?
361
524
  Promise.all(
@@ -424,8 +587,8 @@ function createCollection(def, sdk, updates$, entityTypes) {
424
587
  };
425
588
  return instance;
426
589
  }
427
- function createCollectionSync(def, sdk, updates$, entityTypes) {
428
- const base = createCollection(def, sdk, updates$, entityTypes);
590
+ function createCollectionSync(def, client, updates$, resolver) {
591
+ const base = createCollection(def, client, updates$, resolver);
429
592
  function assertSync(value, method) {
430
593
  if (value instanceof Promise) {
431
594
  throw new Error(`collection "${def.name}".${method}() is not initialized yet \u2014 call collection.load() first`);
@@ -446,22 +609,14 @@ function createCollectionSync(def, sdk, updates$, entityTypes) {
446
609
  // src/i18n.ts
447
610
  import { ProjectedMap as ProjectedMap2, maybeThen as maybeThen2 } from "projected";
448
611
  import { Subject as Subject3, map } from "rxjs";
449
-
450
- // graphql/i18n-messages.gql
451
- var i18n_messages_default = "subscription StoreGetI18nMessages($collection: String!) {\n contelloI18nMessagesBatch(collectionReferenceName: $collection) {\n id\n token\n translations {\n language\n value\n }\n }\n}\n";
452
-
453
- // graphql/i18n-register.gql
454
- var i18n_register_default = "mutation StoreRegisterI18nMessages($collection: String!, $messages: [ContelloI18nMessageInput!]!) {\n registerContelloI18nMessages(\n collectionReferenceName: $collection\n messages: $messages\n deprecateUnmentioned: true\n ) {\n ids\n }\n}\n";
455
-
456
- // src/i18n.ts
457
612
  function toGqlMessageInput(msg) {
458
613
  return {
459
614
  token: msg.token,
460
- example: msg.example ?? null,
461
- description: msg.description ?? null,
462
- variables: msg.variables?.map((v) => ({ name: v.name, description: v.description, example: v.example })) ?? null,
463
- translations: msg.translations?.map((t) => ({ language: t.language, message: t.message })) ?? null,
464
- initialTranslations: msg.initialTranslations?.map((t) => ({ language: t.language, message: t.message })) ?? null
615
+ example: msg.example,
616
+ description: msg.description,
617
+ variables: msg.variables?.map((v) => ({ name: v.name, description: v.description, example: v.example })),
618
+ translations: msg.translations?.map((t) => ({ language: t.language, message: t.message })),
619
+ initialTranslations: msg.initialTranslations?.map((t) => ({ language: t.language, message: t.message }))
465
620
  };
466
621
  }
467
622
  function createI18nMessagesCollection(def, client, updates$) {
@@ -470,7 +625,7 @@ function createI18nMessagesCollection(def, client, updates$) {
470
625
  values: () => wrap(
471
626
  `i18n:${def.collection}`,
472
627
  () => collect(
473
- client.subscribe(i18n_messages_default, { collection: def.collection }).pipe(map((data) => data.contelloI18nMessagesBatch))
628
+ client.subscribe(storeGetI18nMessagesDocument, { collection: def.collection }).pipe(map((data) => data.contelloI18nMessagesBatch))
474
629
  ).then(
475
630
  (msgs) => msgs.map((msg) => ({
476
631
  id: msg.id,
@@ -508,7 +663,7 @@ function createI18nMessagesCollection(def, client, updates$) {
508
663
  register(messages) {
509
664
  return wrap(
510
665
  `i18n-register:${def.collection}`,
511
- () => client.execute(i18n_register_default, {
666
+ () => client.execute(storeRegisterI18nMessagesDocument, {
512
667
  collection: def.collection,
513
668
  messages: messages.map(toGqlMessageInput)
514
669
  }).then(() => {
@@ -521,7 +676,7 @@ function createI18nMessagesCollection(def, client, updates$) {
521
676
  // src/lazy-collection.ts
522
677
  import { ProjectedLazyMap as ProjectedLazyMap2, maybeThen as maybeThen3 } from "projected";
523
678
  import { Subject as Subject4 } from "rxjs";
524
- function createLazyCollection(def, sdk, updates$, entityTypes) {
679
+ function createLazyCollection(def, client, updates$, resolver) {
525
680
  const _def = {
526
681
  name: def.name ?? def.model,
527
682
  model: def.model,
@@ -530,7 +685,7 @@ function createLazyCollection(def, sdk, updates$, entityTypes) {
530
685
  ttl: def.cache?.ttl
531
686
  }
532
687
  };
533
- const dependencyCollector = new DependencyCollector(_def.model, entityTypes);
688
+ const dependencyCollector = new DependencyCollector(_def.model, resolver);
534
689
  const cache = createLruCache({
535
690
  max: _def.cache.max,
536
691
  ttl: _def.cache.ttl,
@@ -541,7 +696,7 @@ function createLazyCollection(def, sdk, updates$, entityTypes) {
541
696
  values: (keys) => wrap(
542
697
  `lazy-collection:${_def.name}`,
543
698
  () => maybeThen3(
544
- resolveFetchable(def.fetch(keys, sdk)),
699
+ resolveFetchable(def.fetch(keys, client)),
545
700
  (rawItems) => Promise.all(
546
701
  rawItems.map(
547
702
  (item) => dependencyCollector.createContext(
@@ -590,30 +745,50 @@ function createLazyCollection(def, sdk, updates$, entityTypes) {
590
745
  };
591
746
  }
592
747
 
593
- // graphql/ping.gql
594
- var ping_default = "query StorePing {\n contelloPing {\n response\n }\n}\n";
595
-
596
- // src/ping.ts
597
- function createPing(client) {
598
- return () => wrap(
599
- "store:ping",
600
- () => client.execute(ping_default).then((data) => {
601
- if (data.contelloPing.response !== "pong") {
602
- throw new Error(`unexpected ping response: ${data.contelloPing.response}`);
748
+ // src/model-resolver.ts
749
+ var ModelResolver = class {
750
+ _typeNames;
751
+ _toModel;
752
+ _toTypeName;
753
+ constructor(models) {
754
+ const toModel = /* @__PURE__ */ new Map();
755
+ const toTypeName = /* @__PURE__ */ new Map();
756
+ let typeNames;
757
+ if (models) {
758
+ typeNames = /* @__PURE__ */ new Set();
759
+ for (const [model, typeName] of Object.entries(models)) {
760
+ toModel.set(typeName, model);
761
+ toTypeName.set(model, typeName);
762
+ typeNames.add(typeName);
603
763
  }
604
- })
605
- );
606
- }
764
+ }
765
+ this._typeNames = typeNames;
766
+ this._toModel = toModel;
767
+ this._toTypeName = toTypeName;
768
+ }
769
+ hasTypeName(typeName) {
770
+ return !this._typeNames || this._typeNames.has(typeName);
771
+ }
772
+ getModel(typeName) {
773
+ return this._toModel.get(typeName);
774
+ }
775
+ getTypeName(model) {
776
+ return this._toTypeName.get(model) ?? model;
777
+ }
778
+ hasModel(model) {
779
+ return !this._toTypeName.size || this._toTypeName.has(model);
780
+ }
781
+ resolveModel(typeName) {
782
+ return this._toModel.get(typeName) ?? typeName;
783
+ }
784
+ };
607
785
 
608
786
  // src/routes.ts
609
787
  import { ProjectedLazyMap as ProjectedLazyMap3 } from "projected";
610
788
  import { Subject as Subject5 } from "rxjs";
611
789
 
612
- // graphql/routes.gql
613
- var routes_default = "fragment StoreRoute on ContelloRoute {\n id\n path\n target {\n __typename\n ... on ContelloRouteTargetEntity {\n entity {\n __typename\n ... on ContelloEntityBase {\n id\n }\n }\n }\n ... on ContelloRouteTargetAsset {\n asset {\n id\n original {\n uid\n mimeType\n }\n }\n contentDisposition\n }\n ... on ContelloRouteTargetRedirect {\n location\n responseCode\n }\n ... on ContelloRouteTargetText {\n content\n mimeType\n }\n }\n customHeaders {\n name\n value\n }\n}\n\n\nquery StoreGetRoutes($request: ContelloRoutesRequestInput!) {\n contelloRoutes(request: $request) {\n ...StoreRoute\n }\n}\n";
614
-
615
790
  // src/routes-mapping.ts
616
- function mapRoute(raw) {
791
+ function mapRoute(raw, resolver) {
617
792
  const base = {
618
793
  id: raw.id,
619
794
  path: raw.path,
@@ -647,10 +822,12 @@ function mapRoute(raw) {
647
822
  if (!raw.target.entity) {
648
823
  return void 0;
649
824
  }
825
+ const entityType = raw.target.entity.__typename;
650
826
  return {
651
827
  ...base,
652
828
  type: "entity",
653
- entityType: raw.target.entity.__typename,
829
+ model: resolver.resolveModel(entityType),
830
+ entityType,
654
831
  entityId: raw.target.entity.id
655
832
  };
656
833
  }
@@ -663,12 +840,12 @@ function mapRoute(raw) {
663
840
  var ID_PREFIX = "1\0";
664
841
  var PATH_PREFIX = "2\0";
665
842
  var CACHE_KEY = /* @__PURE__ */ Symbol("cacheKey");
666
- function collectRoutes(data, keyFn) {
843
+ function collectRoutes(data, keyFn, resolver) {
667
844
  return (data.contelloRoutes ?? []).reduce((acc, raw) => {
668
845
  if (!raw) {
669
846
  return acc;
670
847
  }
671
- const mapped = mapRoute(raw);
848
+ const mapped = mapRoute(raw, resolver);
672
849
  if (mapped) {
673
850
  mapped[CACHE_KEY] = keyFn(mapped);
674
851
  acc.push(mapped);
@@ -716,7 +893,7 @@ function createRoutesCache(max, ttl) {
716
893
  }
717
894
  };
718
895
  }
719
- function createRoutesCollection(def, client, updates$) {
896
+ function createRoutesCollection(def, client, updates$, resolver) {
720
897
  const _def = {
721
898
  cache: {
722
899
  max: def?.cache?.max ?? 1e3,
@@ -738,8 +915,8 @@ function createRoutesCollection(def, client, updates$) {
738
915
  }
739
916
  }
740
917
  return Promise.all([
741
- ids.length > 0 ? client.execute(routes_default, { request: { ids } }).then((data) => collectRoutes(data, (r) => ID_PREFIX + r.id)) : Promise.resolve([]),
742
- paths.length > 0 ? client.execute(routes_default, { request: { paths } }).then((data) => collectRoutes(data, (r) => PATH_PREFIX + r.path)) : Promise.resolve([])
918
+ ids.length > 0 ? client.execute(storeGetRoutesDocument, { request: { ids } }).then((data) => collectRoutes(data, (r) => ID_PREFIX + r.id, resolver)) : Promise.resolve([]),
919
+ paths.length > 0 ? client.execute(storeGetRoutesDocument, { request: { paths } }).then((data) => collectRoutes(data, (r) => PATH_PREFIX + r.path, resolver)) : Promise.resolve([])
743
920
  ]).then(([byIds, byPaths]) => [...byIds, ...byPaths]);
744
921
  }),
745
922
  cache,
@@ -784,7 +961,7 @@ function createRoutesCollection(def, client, updates$) {
784
961
  // src/singleton.ts
785
962
  import { ProjectedValue, maybeThen as maybeThen4 } from "projected";
786
963
  import { Subject as Subject6 } from "rxjs";
787
- function createSingleton(def, sdk, updates$, entityTypes) {
964
+ function createSingleton(def, client, updates$, resolver) {
788
965
  const _def = {
789
966
  name: def.name ?? def.model,
790
967
  model: def.model,
@@ -793,14 +970,14 @@ function createSingleton(def, sdk, updates$, entityTypes) {
793
970
  eviction: def.cache?.eviction ?? "refresh"
794
971
  }
795
972
  };
796
- const dependencyCollector = new DependencyCollector(_def.model, entityTypes);
973
+ const dependencyCollector = new DependencyCollector(_def.model, resolver);
797
974
  const itemKey = `singleton:${_def.name}`;
798
975
  let timer;
799
976
  const projected = new ProjectedValue({
800
977
  value: () => wrap(
801
978
  `singleton:${_def.name}`,
802
979
  () => maybeThen4(
803
- def.fetch(sdk),
980
+ def.fetch(client),
804
981
  (raw) => dependencyCollector.createContext(
805
982
  (ref, register) => maybeThen4(def.map(raw, ref), (mapped) => {
806
983
  register(itemKey);
@@ -852,8 +1029,8 @@ function createSingleton(def, sdk, updates$, entityTypes) {
852
1029
  }
853
1030
  };
854
1031
  }
855
- function createSingletonSync(def, sdk, updates$, entityTypes) {
856
- const base = createSingleton(def, sdk, updates$, entityTypes);
1032
+ function createSingletonSync(def, client, updates$, resolver) {
1033
+ const base = createSingleton(def, client, updates$, resolver);
857
1034
  return {
858
1035
  ...base,
859
1036
  get() {
@@ -868,11 +1045,6 @@ function createSingletonSync(def, sdk, updates$, entityTypes) {
868
1045
 
869
1046
  // src/watcher.ts
870
1047
  import { Subject as Subject7, filter, map as map2, share } from "rxjs";
871
-
872
- // graphql/updates.gql
873
- var updates_default = "fragment StoreRoute on ContelloRoute {\n id\n path\n target {\n __typename\n ... on ContelloRouteTargetEntity {\n entity {\n __typename\n ... on ContelloEntityBase {\n id\n }\n }\n }\n ... on ContelloRouteTargetAsset {\n asset {\n id\n original {\n uid\n mimeType\n }\n }\n contentDisposition\n }\n ... on ContelloRouteTargetRedirect {\n location\n responseCode\n }\n ... on ContelloRouteTargetText {\n content\n mimeType\n }\n }\n customHeaders {\n name\n value\n }\n}\n\n\nsubscription StoreWatchUpdates {\n contelloUpdatesBatch {\n events {\n target {\n __typename\n ... on ContelloEntityBase {\n id\n }\n ... on ContelloRoute {\n ...StoreRoute\n }\n ... on ContelloAsset {\n id\n }\n ... on ContelloI18nMessage {\n id\n token\n }\n }\n prev {\n __typename\n ... on ContelloRoute {\n ...StoreRoute\n }\n }\n mutation {\n type\n }\n }\n }\n}\n";
874
-
875
- // src/watcher.ts
876
1048
  function addRouteByModel(map3, entityType, event) {
877
1049
  let list = map3.get(entityType);
878
1050
  if (!list) {
@@ -892,10 +1064,10 @@ function createUpdateBatch(events) {
892
1064
  case "route":
893
1065
  route.push(event);
894
1066
  if ("after" in event && event.after.type === "entity") {
895
- addRouteByModel(routeByEntityModel, event.after.entityType, event);
1067
+ addRouteByModel(routeByEntityModel, event.after.model, event);
896
1068
  }
897
1069
  if ("before" in event && event.before.type === "entity") {
898
- addRouteByModel(routeByEntityModel, event.before.entityType, event);
1070
+ addRouteByModel(routeByEntityModel, event.before.model, event);
899
1071
  }
900
1072
  break;
901
1073
  case "asset":
@@ -917,23 +1089,33 @@ function createUpdateBatch(events) {
917
1089
  }
918
1090
  return { events, route, asset, i18nMessage, entity, routeByEntityModel };
919
1091
  }
920
- function mapEvent(raw, entityTypes) {
1092
+ function castMutationType(type) {
1093
+ switch (type) {
1094
+ case "CREATE":
1095
+ return "create";
1096
+ case "UPDATE":
1097
+ return "update";
1098
+ case "DELETE":
1099
+ return "delete";
1100
+ }
1101
+ }
1102
+ function mapEvent(raw, resolver) {
1103
+ const mutation = castMutationType(raw.mutation.type);
921
1104
  const target = raw.target;
922
- const id = "id" in target ? target.id : "";
923
- const mutation = raw.mutation.type.toLowerCase();
924
- switch (target.__typename) {
1105
+ const { id, __typename } = target;
1106
+ switch (__typename) {
925
1107
  case "ContelloRoute": {
926
1108
  if (mutation === "delete") {
927
1109
  return { id, mutation: "delete", target: "route" };
928
1110
  }
929
- const after = mapRoute(target);
1111
+ const after = mapRoute(target, resolver);
930
1112
  if (!after) {
931
1113
  return void 0;
932
1114
  }
933
1115
  if (mutation === "create") {
934
1116
  return { id, mutation, target: "route", after };
935
1117
  }
936
- const before = raw.prev?.__typename === "ContelloRoute" ? mapRoute(raw.prev) ?? void 0 : void 0;
1118
+ const before = raw.prev?.__typename === "ContelloRoute" ? mapRoute(raw.prev, resolver) ?? void 0 : void 0;
937
1119
  if (!before) {
938
1120
  return void 0;
939
1121
  }
@@ -943,14 +1125,15 @@ function mapEvent(raw, entityTypes) {
943
1125
  return { id, mutation, target: "asset" };
944
1126
  case "ContelloI18nMessage":
945
1127
  return { id, mutation, target: "i18nMessage", token: target.token };
946
- default:
947
- if (!target.__typename || entityTypes && !entityTypes.has(target.__typename)) {
1128
+ default: {
1129
+ if (!__typename || !resolver.hasTypeName(__typename)) {
948
1130
  return void 0;
949
1131
  }
950
- return { id, mutation, target: "entity", model: target.__typename };
1132
+ return { id, mutation, target: "entity", model: resolver.resolveModel(__typename) };
1133
+ }
951
1134
  }
952
1135
  }
953
- function createInternalWatcher(client, entityTypes) {
1136
+ function createInternalWatcher(client, resolver) {
954
1137
  const subject = new Subject7();
955
1138
  let subscription;
956
1139
  const updates$ = subject.asObservable().pipe(share());
@@ -960,10 +1143,10 @@ function createInternalWatcher(client, entityTypes) {
960
1143
  if (subscription) {
961
1144
  return;
962
1145
  }
963
- const source$ = client.subscribe(updates_default).pipe(
1146
+ const source$ = client.subscribe(storeWatchUpdatesDocument).pipe(
964
1147
  keepalive(),
965
1148
  map2(
966
- (data) => (data.contelloUpdatesBatch?.events ?? []).map((e) => mapEvent(e, entityTypes)).filter((e) => e !== void 0)
1149
+ (data) => (data.contelloUpdatesBatch?.events ?? []).map((e) => mapEvent(e, resolver)).filter((e) => e !== void 0)
967
1150
  ),
968
1151
  filter((events) => events.length > 0),
969
1152
  map2(createUpdateBatch)
@@ -983,67 +1166,60 @@ function createInternalWatcher(client, entityTypes) {
983
1166
  }
984
1167
 
985
1168
  // src/store.ts
986
- function isClientConfig(client) {
987
- return "getSdk" in client;
988
- }
989
1169
  var Store = class {
990
- _sdkClient;
991
- _url;
992
- _entityTypes;
1170
+ _client;
1171
+ _resolver;
993
1172
  _watcher;
994
1173
  updates$;
995
1174
  constructor(options) {
996
- const { url, project, token, client: clientOption } = options;
997
- if (isClientConfig(clientOption)) {
998
- this._sdkClient = new ContelloSdkClient(clientOption.getSdk, {
999
- url,
1000
- project,
1001
- token,
1002
- pooling: clientOption.pooling
1003
- });
1004
- } else {
1005
- this._sdkClient = clientOption;
1006
- }
1007
- this._url = url;
1008
- this._entityTypes = options.entityTypes ? new Set(options.entityTypes) : void 0;
1009
- this._watcher = createInternalWatcher(this._sdkClient, this._entityTypes);
1175
+ const { url, project, token, operations } = options;
1176
+ this._client = createContelloClient({
1177
+ url,
1178
+ project,
1179
+ token,
1180
+ operations,
1181
+ connections: options.connections,
1182
+ onConnected: options.onConnected,
1183
+ onReconnecting: options.onReconnecting,
1184
+ onError: options.onError,
1185
+ connectionEvents: options.connectionEvents
1186
+ });
1187
+ this._resolver = new ModelResolver(options.models);
1188
+ this._watcher = createInternalWatcher(this._client, this._resolver);
1010
1189
  this.updates$ = this._watcher.updates$;
1011
- this.ping = createPing(this._sdkClient);
1012
- }
1013
- get sdk() {
1014
- return this._sdkClient.sdk;
1190
+ this.ping = () => this._client.ping();
1015
1191
  }
1016
1192
  async init() {
1017
- await wrap("store:init", () => this._sdkClient.connect());
1193
+ await wrap("store:init", () => this._client.init());
1018
1194
  this._watcher.start();
1019
1195
  }
1020
1196
  async destroy() {
1021
1197
  this._watcher.stop();
1022
- await wrap("store:destroy", () => this._sdkClient.disconnect());
1198
+ await wrap("store:destroy", () => this._client.destroy());
1023
1199
  }
1024
1200
  defineSingleton(def) {
1025
- return createSingleton(def, this.sdk, this._watcher.updates$, this._entityTypes);
1201
+ return createSingleton(def, this._client, this._watcher.updates$, this._resolver);
1026
1202
  }
1027
1203
  defineSingletonSync(def) {
1028
- return createSingletonSync(def, this.sdk, this._watcher.updates$, this._entityTypes);
1204
+ return createSingletonSync(def, this._client, this._watcher.updates$, this._resolver);
1029
1205
  }
1030
1206
  defineCollection(def) {
1031
- return createCollection(def, this.sdk, this._watcher.updates$, this._entityTypes);
1207
+ return createCollection(def, this._client, this._watcher.updates$, this._resolver);
1032
1208
  }
1033
1209
  defineCollectionSync(def) {
1034
- return createCollectionSync(def, this.sdk, this._watcher.updates$, this._entityTypes);
1210
+ return createCollectionSync(def, this._client, this._watcher.updates$, this._resolver);
1035
1211
  }
1036
1212
  defineLazyCollection(def) {
1037
- return createLazyCollection(def, this.sdk, this._watcher.updates$, this._entityTypes);
1213
+ return createLazyCollection(def, this._client, this._watcher.updates$, this._resolver);
1038
1214
  }
1039
1215
  defineAssets(options) {
1040
- return createAssetsCollection(options, this._sdkClient, this._url, this._watcher.updates$);
1216
+ return createAssetsCollection(options, this._client, this._watcher.updates$);
1041
1217
  }
1042
1218
  defineRoutes(options) {
1043
- return createRoutesCollection(options, this._sdkClient, this._watcher.updates$);
1219
+ return createRoutesCollection(options, this._client, this._watcher.updates$, this._resolver);
1044
1220
  }
1045
1221
  defineI18nMessages(def) {
1046
- return createI18nMessagesCollection(def, this._sdkClient, this._watcher.updates$);
1222
+ return createI18nMessagesCollection(def, this._client, this._watcher.updates$);
1047
1223
  }
1048
1224
  ping;
1049
1225
  };