@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.cjs +307 -131
- package/dist/index.d.cts +51 -53
- package/dist/index.d.ts +51 -53
- package/dist/index.js +307 -131
- package/package.json +5 -11
package/dist/index.cjs
CHANGED
|
@@ -38,21 +38,18 @@ __export(index_exports, {
|
|
|
38
38
|
module.exports = __toCommonJS(index_exports);
|
|
39
39
|
|
|
40
40
|
// src/store.ts
|
|
41
|
-
var
|
|
41
|
+
var import_client = require("@contello/client");
|
|
42
42
|
|
|
43
43
|
// src/assets.ts
|
|
44
44
|
var import_projected = require("projected");
|
|
45
45
|
var import_rxjs = require("rxjs");
|
|
46
46
|
|
|
47
|
-
// graphql/assets.gql
|
|
48
|
-
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";
|
|
49
|
-
|
|
50
47
|
// src/diagnostics.ts
|
|
51
48
|
var import_node_diagnostics_channel = __toESM(require("diagnostics_channel"), 1);
|
|
52
49
|
var channels = {
|
|
53
|
-
start: "contello:
|
|
54
|
-
end: "contello:
|
|
55
|
-
error: "contello:
|
|
50
|
+
start: "@contello/store:start",
|
|
51
|
+
end: "@contello/store:end",
|
|
52
|
+
error: "@contello/store:error"
|
|
56
53
|
};
|
|
57
54
|
var onStart = import_node_diagnostics_channel.default.channel(channels.start);
|
|
58
55
|
var onEnd = import_node_diagnostics_channel.default.channel(channels.end);
|
|
@@ -89,6 +86,168 @@ function wrap(name, fn) {
|
|
|
89
86
|
return result;
|
|
90
87
|
}
|
|
91
88
|
|
|
89
|
+
// src/generated/graphql.ts
|
|
90
|
+
var storeGetAssetsDocument = `fragment StoreFile on ContelloFile {
|
|
91
|
+
uid
|
|
92
|
+
mimeType
|
|
93
|
+
metadata {
|
|
94
|
+
... on ContelloImageMetadata {
|
|
95
|
+
width
|
|
96
|
+
height
|
|
97
|
+
}
|
|
98
|
+
... on ContelloVideoMetadata {
|
|
99
|
+
width
|
|
100
|
+
height
|
|
101
|
+
}
|
|
102
|
+
}
|
|
103
|
+
}
|
|
104
|
+
fragment StoreAsset on ContelloAsset {
|
|
105
|
+
id
|
|
106
|
+
original {
|
|
107
|
+
...StoreFile
|
|
108
|
+
}
|
|
109
|
+
preview {
|
|
110
|
+
...StoreFile
|
|
111
|
+
}
|
|
112
|
+
optimized {
|
|
113
|
+
...StoreFile
|
|
114
|
+
}
|
|
115
|
+
}
|
|
116
|
+
query StoreGetAssets($filter: ContelloAssetsFilterInput!) {
|
|
117
|
+
contelloAssets(filter: $filter) {
|
|
118
|
+
...StoreAsset
|
|
119
|
+
}
|
|
120
|
+
}`;
|
|
121
|
+
var storeGetI18nMessagesDocument = `subscription StoreGetI18nMessages($collection: String!) {
|
|
122
|
+
contelloI18nMessagesBatch(collectionReferenceName: $collection) {
|
|
123
|
+
id
|
|
124
|
+
token
|
|
125
|
+
translations {
|
|
126
|
+
language
|
|
127
|
+
value
|
|
128
|
+
}
|
|
129
|
+
}
|
|
130
|
+
}`;
|
|
131
|
+
var storeRegisterI18nMessagesDocument = `mutation StoreRegisterI18nMessages($collection: String!, $messages: [ContelloI18nMessageInput!]!) {
|
|
132
|
+
registerContelloI18nMessages(
|
|
133
|
+
collectionReferenceName: $collection
|
|
134
|
+
messages: $messages
|
|
135
|
+
deprecateUnmentioned: true
|
|
136
|
+
) {
|
|
137
|
+
ids
|
|
138
|
+
}
|
|
139
|
+
}`;
|
|
140
|
+
var storeGetRoutesDocument = `fragment StoreRoute on ContelloRoute {
|
|
141
|
+
id
|
|
142
|
+
path
|
|
143
|
+
target {
|
|
144
|
+
__typename
|
|
145
|
+
... on ContelloRouteTargetEntity {
|
|
146
|
+
entity {
|
|
147
|
+
__typename
|
|
148
|
+
... on ContelloEntityBase {
|
|
149
|
+
id
|
|
150
|
+
}
|
|
151
|
+
}
|
|
152
|
+
}
|
|
153
|
+
... on ContelloRouteTargetAsset {
|
|
154
|
+
asset {
|
|
155
|
+
id
|
|
156
|
+
original {
|
|
157
|
+
uid
|
|
158
|
+
mimeType
|
|
159
|
+
}
|
|
160
|
+
}
|
|
161
|
+
contentDisposition
|
|
162
|
+
}
|
|
163
|
+
... on ContelloRouteTargetRedirect {
|
|
164
|
+
location
|
|
165
|
+
responseCode
|
|
166
|
+
}
|
|
167
|
+
... on ContelloRouteTargetText {
|
|
168
|
+
content
|
|
169
|
+
mimeType
|
|
170
|
+
}
|
|
171
|
+
}
|
|
172
|
+
customHeaders {
|
|
173
|
+
name
|
|
174
|
+
value
|
|
175
|
+
}
|
|
176
|
+
}
|
|
177
|
+
query StoreGetRoutes($request: ContelloRoutesRequestInput!) {
|
|
178
|
+
contelloRoutes(request: $request) {
|
|
179
|
+
...StoreRoute
|
|
180
|
+
}
|
|
181
|
+
}`;
|
|
182
|
+
var storeWatchUpdatesDocument = `fragment StoreRoute on ContelloRoute {
|
|
183
|
+
id
|
|
184
|
+
path
|
|
185
|
+
target {
|
|
186
|
+
__typename
|
|
187
|
+
... on ContelloRouteTargetEntity {
|
|
188
|
+
entity {
|
|
189
|
+
__typename
|
|
190
|
+
... on ContelloEntityBase {
|
|
191
|
+
id
|
|
192
|
+
}
|
|
193
|
+
}
|
|
194
|
+
}
|
|
195
|
+
... on ContelloRouteTargetAsset {
|
|
196
|
+
asset {
|
|
197
|
+
id
|
|
198
|
+
original {
|
|
199
|
+
uid
|
|
200
|
+
mimeType
|
|
201
|
+
}
|
|
202
|
+
}
|
|
203
|
+
contentDisposition
|
|
204
|
+
}
|
|
205
|
+
... on ContelloRouteTargetRedirect {
|
|
206
|
+
location
|
|
207
|
+
responseCode
|
|
208
|
+
}
|
|
209
|
+
... on ContelloRouteTargetText {
|
|
210
|
+
content
|
|
211
|
+
mimeType
|
|
212
|
+
}
|
|
213
|
+
}
|
|
214
|
+
customHeaders {
|
|
215
|
+
name
|
|
216
|
+
value
|
|
217
|
+
}
|
|
218
|
+
}
|
|
219
|
+
subscription StoreWatchUpdates {
|
|
220
|
+
contelloUpdatesBatch {
|
|
221
|
+
events {
|
|
222
|
+
target {
|
|
223
|
+
__typename
|
|
224
|
+
... on ContelloEntityBase {
|
|
225
|
+
id
|
|
226
|
+
}
|
|
227
|
+
... on ContelloRoute {
|
|
228
|
+
...StoreRoute
|
|
229
|
+
}
|
|
230
|
+
... on ContelloAsset {
|
|
231
|
+
id
|
|
232
|
+
}
|
|
233
|
+
... on ContelloI18nMessage {
|
|
234
|
+
id
|
|
235
|
+
token
|
|
236
|
+
}
|
|
237
|
+
}
|
|
238
|
+
prev {
|
|
239
|
+
__typename
|
|
240
|
+
... on ContelloRoute {
|
|
241
|
+
...StoreRoute
|
|
242
|
+
}
|
|
243
|
+
}
|
|
244
|
+
mutation {
|
|
245
|
+
type
|
|
246
|
+
}
|
|
247
|
+
}
|
|
248
|
+
}
|
|
249
|
+
}`;
|
|
250
|
+
|
|
92
251
|
// src/lru.ts
|
|
93
252
|
var import_lru_cache = require("lru-cache");
|
|
94
253
|
function createLruCache(options) {
|
|
@@ -118,18 +277,18 @@ function mapFile(raw) {
|
|
|
118
277
|
return {
|
|
119
278
|
uid: raw.uid,
|
|
120
279
|
mimeType: raw.mimeType,
|
|
121
|
-
metadata: raw.metadata ? { width: raw.metadata.width, height: raw.metadata.height } :
|
|
280
|
+
metadata: raw.metadata ? { width: raw.metadata.width, height: raw.metadata.height } : void 0
|
|
122
281
|
};
|
|
123
282
|
}
|
|
124
283
|
function mapAsset(raw) {
|
|
125
284
|
return {
|
|
126
285
|
id: raw.id,
|
|
127
286
|
original: mapFile(raw.original),
|
|
128
|
-
preview: raw.preview ? mapFile(raw.preview) :
|
|
287
|
+
preview: raw.preview ? mapFile(raw.preview) : void 0,
|
|
129
288
|
optimized: raw.optimized.map(mapFile)
|
|
130
289
|
};
|
|
131
290
|
}
|
|
132
|
-
function createAssetsCollection(def, client,
|
|
291
|
+
function createAssetsCollection(def, client, updates$) {
|
|
133
292
|
const _def = {
|
|
134
293
|
cache: {
|
|
135
294
|
max: def?.cache?.max ?? 1e3,
|
|
@@ -140,7 +299,7 @@ function createAssetsCollection(def, client, baseUrl, updates$) {
|
|
|
140
299
|
key: (asset) => asset.id,
|
|
141
300
|
values: (ids) => wrap(
|
|
142
301
|
"assets",
|
|
143
|
-
() => client.execute(
|
|
302
|
+
() => client.execute(storeGetAssetsDocument, { filter: { ids } }).then(
|
|
144
303
|
(data) => (data.contelloAssets ?? []).reduce((acc, raw) => {
|
|
145
304
|
if (raw) {
|
|
146
305
|
acc.push(mapAsset(raw));
|
|
@@ -165,8 +324,11 @@ function createAssetsCollection(def, client, baseUrl, updates$) {
|
|
|
165
324
|
get(idOrIds) {
|
|
166
325
|
return projected.get(idOrIds);
|
|
167
326
|
},
|
|
327
|
+
upload(data, meta, options) {
|
|
328
|
+
return client.upload(data, meta, options);
|
|
329
|
+
},
|
|
168
330
|
download(fileId) {
|
|
169
|
-
return
|
|
331
|
+
return client.download(fileId);
|
|
170
332
|
}
|
|
171
333
|
};
|
|
172
334
|
}
|
|
@@ -186,9 +348,9 @@ function createRouteTargetKey(typename, id) {
|
|
|
186
348
|
return `routeTarget\0${typename}\0${id}`;
|
|
187
349
|
}
|
|
188
350
|
var DependencyCollector = class {
|
|
189
|
-
constructor(model,
|
|
351
|
+
constructor(model, resolver) {
|
|
190
352
|
this.model = model;
|
|
191
|
-
this.
|
|
353
|
+
this.resolver = resolver;
|
|
192
354
|
}
|
|
193
355
|
/** dep key -> item keys that declared a dependency on it */
|
|
194
356
|
reverseIndex = /* @__PURE__ */ new Map();
|
|
@@ -214,19 +376,20 @@ var DependencyCollector = class {
|
|
|
214
376
|
case "ContelloRoute":
|
|
215
377
|
deps.push(createDependencyKey("route", obj.id));
|
|
216
378
|
break;
|
|
217
|
-
default:
|
|
218
|
-
|
|
379
|
+
default: {
|
|
380
|
+
const model = this.resolver.getModel(obj.__typename);
|
|
381
|
+
if (!model) {
|
|
219
382
|
throw new Error(
|
|
220
|
-
`track(): unexpected __typename "${obj.__typename}" \u2014 expected ContelloAsset, ContelloRoute, or
|
|
383
|
+
`track(): unexpected __typename "${obj.__typename}" \u2014 expected ContelloAsset, ContelloRoute, or a known entity model`
|
|
221
384
|
);
|
|
222
385
|
}
|
|
223
|
-
|
|
224
|
-
|
|
386
|
+
deps.push(createEntityDependencyKey(model, obj.id));
|
|
387
|
+
}
|
|
225
388
|
}
|
|
226
389
|
},
|
|
227
|
-
trackEntity: (
|
|
228
|
-
this.
|
|
229
|
-
deps.push(createEntityDependencyKey(
|
|
390
|
+
trackEntity: (model, id) => {
|
|
391
|
+
this.warnIfUnknownModel(model);
|
|
392
|
+
deps.push(createEntityDependencyKey(model, id));
|
|
230
393
|
},
|
|
231
394
|
trackAsset: (id) => deps.push(createDependencyKey("asset", id)),
|
|
232
395
|
trackRoute: (id) => deps.push(createDependencyKey("route", id))
|
|
@@ -261,10 +424,10 @@ var DependencyCollector = class {
|
|
|
261
424
|
let affected = this.reverseIndex.get(createDependencyKey(event.target, event.id));
|
|
262
425
|
if (event.target === "route") {
|
|
263
426
|
if ("after" in event && event.after.type === "entity") {
|
|
264
|
-
affected = this.mergeRouteTargetKeys(affected, event.after.
|
|
427
|
+
affected = this.mergeRouteTargetKeys(affected, event.after.model, event.after.entityId);
|
|
265
428
|
}
|
|
266
429
|
if ("before" in event && event.before.type === "entity") {
|
|
267
|
-
affected = this.mergeRouteTargetKeys(affected, event.before.
|
|
430
|
+
affected = this.mergeRouteTargetKeys(affected, event.before.model, event.before.entityId);
|
|
268
431
|
}
|
|
269
432
|
}
|
|
270
433
|
return affected ?? /* @__PURE__ */ new Set();
|
|
@@ -296,10 +459,10 @@ var DependencyCollector = class {
|
|
|
296
459
|
}
|
|
297
460
|
}
|
|
298
461
|
}
|
|
299
|
-
|
|
300
|
-
if (
|
|
462
|
+
warnIfUnknownModel(model) {
|
|
463
|
+
if (!this.resolver.hasModel(model)) {
|
|
301
464
|
console.warn(
|
|
302
|
-
`[contello/store] tracking
|
|
465
|
+
`[contello/store] tracking model "${model}" which is not in models \u2014 updates for this model will be ignored`
|
|
303
466
|
);
|
|
304
467
|
}
|
|
305
468
|
}
|
|
@@ -378,7 +541,7 @@ function collect(observable) {
|
|
|
378
541
|
}
|
|
379
542
|
|
|
380
543
|
// src/collection.ts
|
|
381
|
-
function createCollection(def,
|
|
544
|
+
function createCollection(def, client, updates$, resolver) {
|
|
382
545
|
const _def = {
|
|
383
546
|
name: def.name ?? def.model,
|
|
384
547
|
model: def.model,
|
|
@@ -387,14 +550,14 @@ function createCollection(def, sdk, updates$, entityTypes) {
|
|
|
387
550
|
eviction: def.cache?.eviction ?? "refresh"
|
|
388
551
|
}
|
|
389
552
|
};
|
|
390
|
-
const dependencyCollector = new DependencyCollector(_def.model,
|
|
553
|
+
const dependencyCollector = new DependencyCollector(_def.model, resolver);
|
|
391
554
|
let timer;
|
|
392
555
|
const projected = new import_projected2.ProjectedMap({
|
|
393
556
|
key: (item) => item.id,
|
|
394
557
|
values: () => wrap(
|
|
395
558
|
`collection:${_def.name}`,
|
|
396
559
|
() => (0, import_projected2.maybeThen)(
|
|
397
|
-
resolveFetchable(def.fetch(
|
|
560
|
+
resolveFetchable(def.fetch(client)),
|
|
398
561
|
(rawItems) => (
|
|
399
562
|
// maybeAll one day?
|
|
400
563
|
Promise.all(
|
|
@@ -463,8 +626,8 @@ function createCollection(def, sdk, updates$, entityTypes) {
|
|
|
463
626
|
};
|
|
464
627
|
return instance;
|
|
465
628
|
}
|
|
466
|
-
function createCollectionSync(def,
|
|
467
|
-
const base = createCollection(def,
|
|
629
|
+
function createCollectionSync(def, client, updates$, resolver) {
|
|
630
|
+
const base = createCollection(def, client, updates$, resolver);
|
|
468
631
|
function assertSync(value, method) {
|
|
469
632
|
if (value instanceof Promise) {
|
|
470
633
|
throw new Error(`collection "${def.name}".${method}() is not initialized yet \u2014 call collection.load() first`);
|
|
@@ -485,22 +648,14 @@ function createCollectionSync(def, sdk, updates$, entityTypes) {
|
|
|
485
648
|
// src/i18n.ts
|
|
486
649
|
var import_projected3 = require("projected");
|
|
487
650
|
var import_rxjs4 = require("rxjs");
|
|
488
|
-
|
|
489
|
-
// graphql/i18n-messages.gql
|
|
490
|
-
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";
|
|
491
|
-
|
|
492
|
-
// graphql/i18n-register.gql
|
|
493
|
-
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";
|
|
494
|
-
|
|
495
|
-
// src/i18n.ts
|
|
496
651
|
function toGqlMessageInput(msg) {
|
|
497
652
|
return {
|
|
498
653
|
token: msg.token,
|
|
499
|
-
example: msg.example
|
|
500
|
-
description: msg.description
|
|
501
|
-
variables: msg.variables?.map((v) => ({ name: v.name, description: v.description, example: v.example }))
|
|
502
|
-
translations: msg.translations?.map((t) => ({ language: t.language, message: t.message }))
|
|
503
|
-
initialTranslations: msg.initialTranslations?.map((t) => ({ language: t.language, message: t.message }))
|
|
654
|
+
example: msg.example,
|
|
655
|
+
description: msg.description,
|
|
656
|
+
variables: msg.variables?.map((v) => ({ name: v.name, description: v.description, example: v.example })),
|
|
657
|
+
translations: msg.translations?.map((t) => ({ language: t.language, message: t.message })),
|
|
658
|
+
initialTranslations: msg.initialTranslations?.map((t) => ({ language: t.language, message: t.message }))
|
|
504
659
|
};
|
|
505
660
|
}
|
|
506
661
|
function createI18nMessagesCollection(def, client, updates$) {
|
|
@@ -509,7 +664,7 @@ function createI18nMessagesCollection(def, client, updates$) {
|
|
|
509
664
|
values: () => wrap(
|
|
510
665
|
`i18n:${def.collection}`,
|
|
511
666
|
() => collect(
|
|
512
|
-
client.subscribe(
|
|
667
|
+
client.subscribe(storeGetI18nMessagesDocument, { collection: def.collection }).pipe((0, import_rxjs4.map)((data) => data.contelloI18nMessagesBatch))
|
|
513
668
|
).then(
|
|
514
669
|
(msgs) => msgs.map((msg) => ({
|
|
515
670
|
id: msg.id,
|
|
@@ -547,7 +702,7 @@ function createI18nMessagesCollection(def, client, updates$) {
|
|
|
547
702
|
register(messages) {
|
|
548
703
|
return wrap(
|
|
549
704
|
`i18n-register:${def.collection}`,
|
|
550
|
-
() => client.execute(
|
|
705
|
+
() => client.execute(storeRegisterI18nMessagesDocument, {
|
|
551
706
|
collection: def.collection,
|
|
552
707
|
messages: messages.map(toGqlMessageInput)
|
|
553
708
|
}).then(() => {
|
|
@@ -560,7 +715,7 @@ function createI18nMessagesCollection(def, client, updates$) {
|
|
|
560
715
|
// src/lazy-collection.ts
|
|
561
716
|
var import_projected4 = require("projected");
|
|
562
717
|
var import_rxjs5 = require("rxjs");
|
|
563
|
-
function createLazyCollection(def,
|
|
718
|
+
function createLazyCollection(def, client, updates$, resolver) {
|
|
564
719
|
const _def = {
|
|
565
720
|
name: def.name ?? def.model,
|
|
566
721
|
model: def.model,
|
|
@@ -569,7 +724,7 @@ function createLazyCollection(def, sdk, updates$, entityTypes) {
|
|
|
569
724
|
ttl: def.cache?.ttl
|
|
570
725
|
}
|
|
571
726
|
};
|
|
572
|
-
const dependencyCollector = new DependencyCollector(_def.model,
|
|
727
|
+
const dependencyCollector = new DependencyCollector(_def.model, resolver);
|
|
573
728
|
const cache = createLruCache({
|
|
574
729
|
max: _def.cache.max,
|
|
575
730
|
ttl: _def.cache.ttl,
|
|
@@ -580,7 +735,7 @@ function createLazyCollection(def, sdk, updates$, entityTypes) {
|
|
|
580
735
|
values: (keys) => wrap(
|
|
581
736
|
`lazy-collection:${_def.name}`,
|
|
582
737
|
() => (0, import_projected4.maybeThen)(
|
|
583
|
-
resolveFetchable(def.fetch(keys,
|
|
738
|
+
resolveFetchable(def.fetch(keys, client)),
|
|
584
739
|
(rawItems) => Promise.all(
|
|
585
740
|
rawItems.map(
|
|
586
741
|
(item) => dependencyCollector.createContext(
|
|
@@ -629,30 +784,50 @@ function createLazyCollection(def, sdk, updates$, entityTypes) {
|
|
|
629
784
|
};
|
|
630
785
|
}
|
|
631
786
|
|
|
632
|
-
//
|
|
633
|
-
var
|
|
634
|
-
|
|
635
|
-
|
|
636
|
-
|
|
637
|
-
|
|
638
|
-
|
|
639
|
-
|
|
640
|
-
|
|
641
|
-
|
|
787
|
+
// src/model-resolver.ts
|
|
788
|
+
var ModelResolver = class {
|
|
789
|
+
_typeNames;
|
|
790
|
+
_toModel;
|
|
791
|
+
_toTypeName;
|
|
792
|
+
constructor(models) {
|
|
793
|
+
const toModel = /* @__PURE__ */ new Map();
|
|
794
|
+
const toTypeName = /* @__PURE__ */ new Map();
|
|
795
|
+
let typeNames;
|
|
796
|
+
if (models) {
|
|
797
|
+
typeNames = /* @__PURE__ */ new Set();
|
|
798
|
+
for (const [model, typeName] of Object.entries(models)) {
|
|
799
|
+
toModel.set(typeName, model);
|
|
800
|
+
toTypeName.set(model, typeName);
|
|
801
|
+
typeNames.add(typeName);
|
|
642
802
|
}
|
|
643
|
-
}
|
|
644
|
-
|
|
645
|
-
|
|
803
|
+
}
|
|
804
|
+
this._typeNames = typeNames;
|
|
805
|
+
this._toModel = toModel;
|
|
806
|
+
this._toTypeName = toTypeName;
|
|
807
|
+
}
|
|
808
|
+
hasTypeName(typeName) {
|
|
809
|
+
return !this._typeNames || this._typeNames.has(typeName);
|
|
810
|
+
}
|
|
811
|
+
getModel(typeName) {
|
|
812
|
+
return this._toModel.get(typeName);
|
|
813
|
+
}
|
|
814
|
+
getTypeName(model) {
|
|
815
|
+
return this._toTypeName.get(model) ?? model;
|
|
816
|
+
}
|
|
817
|
+
hasModel(model) {
|
|
818
|
+
return !this._toTypeName.size || this._toTypeName.has(model);
|
|
819
|
+
}
|
|
820
|
+
resolveModel(typeName) {
|
|
821
|
+
return this._toModel.get(typeName) ?? typeName;
|
|
822
|
+
}
|
|
823
|
+
};
|
|
646
824
|
|
|
647
825
|
// src/routes.ts
|
|
648
826
|
var import_projected5 = require("projected");
|
|
649
827
|
var import_rxjs6 = require("rxjs");
|
|
650
828
|
|
|
651
|
-
// graphql/routes.gql
|
|
652
|
-
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";
|
|
653
|
-
|
|
654
829
|
// src/routes-mapping.ts
|
|
655
|
-
function mapRoute(raw) {
|
|
830
|
+
function mapRoute(raw, resolver) {
|
|
656
831
|
const base = {
|
|
657
832
|
id: raw.id,
|
|
658
833
|
path: raw.path,
|
|
@@ -686,10 +861,12 @@ function mapRoute(raw) {
|
|
|
686
861
|
if (!raw.target.entity) {
|
|
687
862
|
return void 0;
|
|
688
863
|
}
|
|
864
|
+
const entityType = raw.target.entity.__typename;
|
|
689
865
|
return {
|
|
690
866
|
...base,
|
|
691
867
|
type: "entity",
|
|
692
|
-
|
|
868
|
+
model: resolver.resolveModel(entityType),
|
|
869
|
+
entityType,
|
|
693
870
|
entityId: raw.target.entity.id
|
|
694
871
|
};
|
|
695
872
|
}
|
|
@@ -702,12 +879,12 @@ function mapRoute(raw) {
|
|
|
702
879
|
var ID_PREFIX = "1\0";
|
|
703
880
|
var PATH_PREFIX = "2\0";
|
|
704
881
|
var CACHE_KEY = /* @__PURE__ */ Symbol("cacheKey");
|
|
705
|
-
function collectRoutes(data, keyFn) {
|
|
882
|
+
function collectRoutes(data, keyFn, resolver) {
|
|
706
883
|
return (data.contelloRoutes ?? []).reduce((acc, raw) => {
|
|
707
884
|
if (!raw) {
|
|
708
885
|
return acc;
|
|
709
886
|
}
|
|
710
|
-
const mapped = mapRoute(raw);
|
|
887
|
+
const mapped = mapRoute(raw, resolver);
|
|
711
888
|
if (mapped) {
|
|
712
889
|
mapped[CACHE_KEY] = keyFn(mapped);
|
|
713
890
|
acc.push(mapped);
|
|
@@ -755,7 +932,7 @@ function createRoutesCache(max, ttl) {
|
|
|
755
932
|
}
|
|
756
933
|
};
|
|
757
934
|
}
|
|
758
|
-
function createRoutesCollection(def, client, updates
|
|
935
|
+
function createRoutesCollection(def, client, updates$, resolver) {
|
|
759
936
|
const _def = {
|
|
760
937
|
cache: {
|
|
761
938
|
max: def?.cache?.max ?? 1e3,
|
|
@@ -777,8 +954,8 @@ function createRoutesCollection(def, client, updates$) {
|
|
|
777
954
|
}
|
|
778
955
|
}
|
|
779
956
|
return Promise.all([
|
|
780
|
-
ids.length > 0 ? client.execute(
|
|
781
|
-
paths.length > 0 ? client.execute(
|
|
957
|
+
ids.length > 0 ? client.execute(storeGetRoutesDocument, { request: { ids } }).then((data) => collectRoutes(data, (r) => ID_PREFIX + r.id, resolver)) : Promise.resolve([]),
|
|
958
|
+
paths.length > 0 ? client.execute(storeGetRoutesDocument, { request: { paths } }).then((data) => collectRoutes(data, (r) => PATH_PREFIX + r.path, resolver)) : Promise.resolve([])
|
|
782
959
|
]).then(([byIds, byPaths]) => [...byIds, ...byPaths]);
|
|
783
960
|
}),
|
|
784
961
|
cache,
|
|
@@ -823,7 +1000,7 @@ function createRoutesCollection(def, client, updates$) {
|
|
|
823
1000
|
// src/singleton.ts
|
|
824
1001
|
var import_projected6 = require("projected");
|
|
825
1002
|
var import_rxjs7 = require("rxjs");
|
|
826
|
-
function createSingleton(def,
|
|
1003
|
+
function createSingleton(def, client, updates$, resolver) {
|
|
827
1004
|
const _def = {
|
|
828
1005
|
name: def.name ?? def.model,
|
|
829
1006
|
model: def.model,
|
|
@@ -832,14 +1009,14 @@ function createSingleton(def, sdk, updates$, entityTypes) {
|
|
|
832
1009
|
eviction: def.cache?.eviction ?? "refresh"
|
|
833
1010
|
}
|
|
834
1011
|
};
|
|
835
|
-
const dependencyCollector = new DependencyCollector(_def.model,
|
|
1012
|
+
const dependencyCollector = new DependencyCollector(_def.model, resolver);
|
|
836
1013
|
const itemKey = `singleton:${_def.name}`;
|
|
837
1014
|
let timer;
|
|
838
1015
|
const projected = new import_projected6.ProjectedValue({
|
|
839
1016
|
value: () => wrap(
|
|
840
1017
|
`singleton:${_def.name}`,
|
|
841
1018
|
() => (0, import_projected6.maybeThen)(
|
|
842
|
-
def.fetch(
|
|
1019
|
+
def.fetch(client),
|
|
843
1020
|
(raw) => dependencyCollector.createContext(
|
|
844
1021
|
(ref, register) => (0, import_projected6.maybeThen)(def.map(raw, ref), (mapped) => {
|
|
845
1022
|
register(itemKey);
|
|
@@ -891,8 +1068,8 @@ function createSingleton(def, sdk, updates$, entityTypes) {
|
|
|
891
1068
|
}
|
|
892
1069
|
};
|
|
893
1070
|
}
|
|
894
|
-
function createSingletonSync(def,
|
|
895
|
-
const base = createSingleton(def,
|
|
1071
|
+
function createSingletonSync(def, client, updates$, resolver) {
|
|
1072
|
+
const base = createSingleton(def, client, updates$, resolver);
|
|
896
1073
|
return {
|
|
897
1074
|
...base,
|
|
898
1075
|
get() {
|
|
@@ -907,11 +1084,6 @@ function createSingletonSync(def, sdk, updates$, entityTypes) {
|
|
|
907
1084
|
|
|
908
1085
|
// src/watcher.ts
|
|
909
1086
|
var import_rxjs8 = require("rxjs");
|
|
910
|
-
|
|
911
|
-
// graphql/updates.gql
|
|
912
|
-
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";
|
|
913
|
-
|
|
914
|
-
// src/watcher.ts
|
|
915
1087
|
function addRouteByModel(map3, entityType, event) {
|
|
916
1088
|
let list = map3.get(entityType);
|
|
917
1089
|
if (!list) {
|
|
@@ -931,10 +1103,10 @@ function createUpdateBatch(events) {
|
|
|
931
1103
|
case "route":
|
|
932
1104
|
route.push(event);
|
|
933
1105
|
if ("after" in event && event.after.type === "entity") {
|
|
934
|
-
addRouteByModel(routeByEntityModel, event.after.
|
|
1106
|
+
addRouteByModel(routeByEntityModel, event.after.model, event);
|
|
935
1107
|
}
|
|
936
1108
|
if ("before" in event && event.before.type === "entity") {
|
|
937
|
-
addRouteByModel(routeByEntityModel, event.before.
|
|
1109
|
+
addRouteByModel(routeByEntityModel, event.before.model, event);
|
|
938
1110
|
}
|
|
939
1111
|
break;
|
|
940
1112
|
case "asset":
|
|
@@ -956,23 +1128,33 @@ function createUpdateBatch(events) {
|
|
|
956
1128
|
}
|
|
957
1129
|
return { events, route, asset, i18nMessage, entity, routeByEntityModel };
|
|
958
1130
|
}
|
|
959
|
-
function
|
|
1131
|
+
function castMutationType(type) {
|
|
1132
|
+
switch (type) {
|
|
1133
|
+
case "CREATE":
|
|
1134
|
+
return "create";
|
|
1135
|
+
case "UPDATE":
|
|
1136
|
+
return "update";
|
|
1137
|
+
case "DELETE":
|
|
1138
|
+
return "delete";
|
|
1139
|
+
}
|
|
1140
|
+
}
|
|
1141
|
+
function mapEvent(raw, resolver) {
|
|
1142
|
+
const mutation = castMutationType(raw.mutation.type);
|
|
960
1143
|
const target = raw.target;
|
|
961
|
-
const
|
|
962
|
-
|
|
963
|
-
switch (target.__typename) {
|
|
1144
|
+
const { id, __typename } = target;
|
|
1145
|
+
switch (__typename) {
|
|
964
1146
|
case "ContelloRoute": {
|
|
965
1147
|
if (mutation === "delete") {
|
|
966
1148
|
return { id, mutation: "delete", target: "route" };
|
|
967
1149
|
}
|
|
968
|
-
const after = mapRoute(target);
|
|
1150
|
+
const after = mapRoute(target, resolver);
|
|
969
1151
|
if (!after) {
|
|
970
1152
|
return void 0;
|
|
971
1153
|
}
|
|
972
1154
|
if (mutation === "create") {
|
|
973
1155
|
return { id, mutation, target: "route", after };
|
|
974
1156
|
}
|
|
975
|
-
const before = raw.prev?.__typename === "ContelloRoute" ? mapRoute(raw.prev) ?? void 0 : void 0;
|
|
1157
|
+
const before = raw.prev?.__typename === "ContelloRoute" ? mapRoute(raw.prev, resolver) ?? void 0 : void 0;
|
|
976
1158
|
if (!before) {
|
|
977
1159
|
return void 0;
|
|
978
1160
|
}
|
|
@@ -982,14 +1164,15 @@ function mapEvent(raw, entityTypes) {
|
|
|
982
1164
|
return { id, mutation, target: "asset" };
|
|
983
1165
|
case "ContelloI18nMessage":
|
|
984
1166
|
return { id, mutation, target: "i18nMessage", token: target.token };
|
|
985
|
-
default:
|
|
986
|
-
if (!
|
|
1167
|
+
default: {
|
|
1168
|
+
if (!__typename || !resolver.hasTypeName(__typename)) {
|
|
987
1169
|
return void 0;
|
|
988
1170
|
}
|
|
989
|
-
return { id, mutation, target: "entity", model:
|
|
1171
|
+
return { id, mutation, target: "entity", model: resolver.resolveModel(__typename) };
|
|
1172
|
+
}
|
|
990
1173
|
}
|
|
991
1174
|
}
|
|
992
|
-
function createInternalWatcher(client,
|
|
1175
|
+
function createInternalWatcher(client, resolver) {
|
|
993
1176
|
const subject = new import_rxjs8.Subject();
|
|
994
1177
|
let subscription;
|
|
995
1178
|
const updates$ = subject.asObservable().pipe((0, import_rxjs8.share)());
|
|
@@ -999,10 +1182,10 @@ function createInternalWatcher(client, entityTypes) {
|
|
|
999
1182
|
if (subscription) {
|
|
1000
1183
|
return;
|
|
1001
1184
|
}
|
|
1002
|
-
const source$ = client.subscribe(
|
|
1185
|
+
const source$ = client.subscribe(storeWatchUpdatesDocument).pipe(
|
|
1003
1186
|
keepalive(),
|
|
1004
1187
|
(0, import_rxjs8.map)(
|
|
1005
|
-
(data) => (data.contelloUpdatesBatch?.events ?? []).map((e) => mapEvent(e,
|
|
1188
|
+
(data) => (data.contelloUpdatesBatch?.events ?? []).map((e) => mapEvent(e, resolver)).filter((e) => e !== void 0)
|
|
1006
1189
|
),
|
|
1007
1190
|
(0, import_rxjs8.filter)((events) => events.length > 0),
|
|
1008
1191
|
(0, import_rxjs8.map)(createUpdateBatch)
|
|
@@ -1022,67 +1205,60 @@ function createInternalWatcher(client, entityTypes) {
|
|
|
1022
1205
|
}
|
|
1023
1206
|
|
|
1024
1207
|
// src/store.ts
|
|
1025
|
-
function isClientConfig(client) {
|
|
1026
|
-
return "getSdk" in client;
|
|
1027
|
-
}
|
|
1028
1208
|
var Store = class {
|
|
1029
|
-
|
|
1030
|
-
|
|
1031
|
-
_entityTypes;
|
|
1209
|
+
_client;
|
|
1210
|
+
_resolver;
|
|
1032
1211
|
_watcher;
|
|
1033
1212
|
updates$;
|
|
1034
1213
|
constructor(options) {
|
|
1035
|
-
const { url, project, token,
|
|
1036
|
-
|
|
1037
|
-
|
|
1038
|
-
|
|
1039
|
-
|
|
1040
|
-
|
|
1041
|
-
|
|
1042
|
-
|
|
1043
|
-
|
|
1044
|
-
|
|
1045
|
-
|
|
1046
|
-
|
|
1047
|
-
this.
|
|
1048
|
-
this._watcher = createInternalWatcher(this.
|
|
1214
|
+
const { url, project, token, operations } = options;
|
|
1215
|
+
this._client = (0, import_client.createContelloClient)({
|
|
1216
|
+
url,
|
|
1217
|
+
project,
|
|
1218
|
+
token,
|
|
1219
|
+
operations,
|
|
1220
|
+
connections: options.connections,
|
|
1221
|
+
onConnected: options.onConnected,
|
|
1222
|
+
onReconnecting: options.onReconnecting,
|
|
1223
|
+
onError: options.onError,
|
|
1224
|
+
connectionEvents: options.connectionEvents
|
|
1225
|
+
});
|
|
1226
|
+
this._resolver = new ModelResolver(options.models);
|
|
1227
|
+
this._watcher = createInternalWatcher(this._client, this._resolver);
|
|
1049
1228
|
this.updates$ = this._watcher.updates$;
|
|
1050
|
-
this.ping =
|
|
1051
|
-
}
|
|
1052
|
-
get sdk() {
|
|
1053
|
-
return this._sdkClient.sdk;
|
|
1229
|
+
this.ping = () => this._client.ping();
|
|
1054
1230
|
}
|
|
1055
1231
|
async init() {
|
|
1056
|
-
await wrap("store:init", () => this.
|
|
1232
|
+
await wrap("store:init", () => this._client.init());
|
|
1057
1233
|
this._watcher.start();
|
|
1058
1234
|
}
|
|
1059
1235
|
async destroy() {
|
|
1060
1236
|
this._watcher.stop();
|
|
1061
|
-
await wrap("store:destroy", () => this.
|
|
1237
|
+
await wrap("store:destroy", () => this._client.destroy());
|
|
1062
1238
|
}
|
|
1063
1239
|
defineSingleton(def) {
|
|
1064
|
-
return createSingleton(def, this.
|
|
1240
|
+
return createSingleton(def, this._client, this._watcher.updates$, this._resolver);
|
|
1065
1241
|
}
|
|
1066
1242
|
defineSingletonSync(def) {
|
|
1067
|
-
return createSingletonSync(def, this.
|
|
1243
|
+
return createSingletonSync(def, this._client, this._watcher.updates$, this._resolver);
|
|
1068
1244
|
}
|
|
1069
1245
|
defineCollection(def) {
|
|
1070
|
-
return createCollection(def, this.
|
|
1246
|
+
return createCollection(def, this._client, this._watcher.updates$, this._resolver);
|
|
1071
1247
|
}
|
|
1072
1248
|
defineCollectionSync(def) {
|
|
1073
|
-
return createCollectionSync(def, this.
|
|
1249
|
+
return createCollectionSync(def, this._client, this._watcher.updates$, this._resolver);
|
|
1074
1250
|
}
|
|
1075
1251
|
defineLazyCollection(def) {
|
|
1076
|
-
return createLazyCollection(def, this.
|
|
1252
|
+
return createLazyCollection(def, this._client, this._watcher.updates$, this._resolver);
|
|
1077
1253
|
}
|
|
1078
1254
|
defineAssets(options) {
|
|
1079
|
-
return createAssetsCollection(options, this.
|
|
1255
|
+
return createAssetsCollection(options, this._client, this._watcher.updates$);
|
|
1080
1256
|
}
|
|
1081
1257
|
defineRoutes(options) {
|
|
1082
|
-
return createRoutesCollection(options, this.
|
|
1258
|
+
return createRoutesCollection(options, this._client, this._watcher.updates$, this._resolver);
|
|
1083
1259
|
}
|
|
1084
1260
|
defineI18nMessages(def) {
|
|
1085
|
-
return createI18nMessagesCollection(def, this.
|
|
1261
|
+
return createI18nMessagesCollection(def, this._client, this._watcher.updates$);
|
|
1086
1262
|
}
|
|
1087
1263
|
ping;
|
|
1088
1264
|
};
|