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