@contello/client 1.2.2 → 2.1.0
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/cli.js +1 -1
- package/dist/{generate-PGYNYGNX.js → generate-FPYYN7UV.js} +434 -37
- package/dist/index.cjs +530 -23
- package/dist/index.d.cts +138 -10
- package/dist/index.d.ts +138 -10
- package/dist/index.js +520 -22
- package/package.json +1 -7
package/dist/index.js
CHANGED
|
@@ -1,6 +1,104 @@
|
|
|
1
1
|
// src/client.ts
|
|
2
2
|
import { createClient } from "graphql-ws";
|
|
3
|
-
|
|
3
|
+
|
|
4
|
+
// src/async-iterable-utils.ts
|
|
5
|
+
var MAX_BACKOFF_MS = 3e4;
|
|
6
|
+
var JITTER_MIN_MS = 300;
|
|
7
|
+
var JITTER_MAX_MS = 3e3;
|
|
8
|
+
async function firstAsync(iter) {
|
|
9
|
+
const it = iter[Symbol.asyncIterator]();
|
|
10
|
+
try {
|
|
11
|
+
const result = await it.next();
|
|
12
|
+
if (result.done) {
|
|
13
|
+
throw new Error("firstAsync: async iterable completed without yielding a value");
|
|
14
|
+
}
|
|
15
|
+
return result.value;
|
|
16
|
+
} finally {
|
|
17
|
+
await it.return?.();
|
|
18
|
+
}
|
|
19
|
+
}
|
|
20
|
+
async function* mapAsync(iter, fn) {
|
|
21
|
+
for await (const value of iter) {
|
|
22
|
+
yield fn(value);
|
|
23
|
+
}
|
|
24
|
+
}
|
|
25
|
+
async function* filterAsync(iter, predicate) {
|
|
26
|
+
for await (const value of iter) {
|
|
27
|
+
if (predicate(value)) {
|
|
28
|
+
yield value;
|
|
29
|
+
}
|
|
30
|
+
}
|
|
31
|
+
}
|
|
32
|
+
async function collectAsync(iter) {
|
|
33
|
+
const out = [];
|
|
34
|
+
for await (const chunk of iter) {
|
|
35
|
+
out.push(...chunk);
|
|
36
|
+
}
|
|
37
|
+
return out;
|
|
38
|
+
}
|
|
39
|
+
function exponentialBackoff(attempt) {
|
|
40
|
+
const base = Math.min(Math.pow(2, attempt) * 1e3, MAX_BACKOFF_MS);
|
|
41
|
+
const jitter = Math.floor(Math.random() * (JITTER_MAX_MS - JITTER_MIN_MS) + JITTER_MIN_MS);
|
|
42
|
+
return delay(base + jitter);
|
|
43
|
+
}
|
|
44
|
+
function delay(ms, signal) {
|
|
45
|
+
if (signal?.aborted) {
|
|
46
|
+
return Promise.reject(signal.reason);
|
|
47
|
+
}
|
|
48
|
+
return new Promise((resolve, reject) => {
|
|
49
|
+
const timer = setTimeout(() => {
|
|
50
|
+
signal?.removeEventListener("abort", onAbort);
|
|
51
|
+
resolve();
|
|
52
|
+
}, ms);
|
|
53
|
+
const onAbort = () => {
|
|
54
|
+
clearTimeout(timer);
|
|
55
|
+
reject(signal.reason);
|
|
56
|
+
};
|
|
57
|
+
signal?.addEventListener("abort", onAbort, { once: true });
|
|
58
|
+
});
|
|
59
|
+
}
|
|
60
|
+
async function runWithBackoff(fn, signal) {
|
|
61
|
+
let attempt = 0;
|
|
62
|
+
for (; ; ) {
|
|
63
|
+
if (signal?.aborted) {
|
|
64
|
+
throw signal.reason;
|
|
65
|
+
}
|
|
66
|
+
try {
|
|
67
|
+
await fn();
|
|
68
|
+
return;
|
|
69
|
+
} catch {
|
|
70
|
+
await exponentialBackoffWithSignal(attempt, signal);
|
|
71
|
+
attempt++;
|
|
72
|
+
}
|
|
73
|
+
}
|
|
74
|
+
}
|
|
75
|
+
function exponentialBackoffWithSignal(attempt, signal) {
|
|
76
|
+
const base = Math.min(Math.pow(2, attempt) * 1e3, MAX_BACKOFF_MS);
|
|
77
|
+
const jitter = Math.floor(Math.random() * (JITTER_MAX_MS - JITTER_MIN_MS) + JITTER_MIN_MS);
|
|
78
|
+
return delay(base + jitter, signal);
|
|
79
|
+
}
|
|
80
|
+
async function* asyncKeepalive(factory, signal) {
|
|
81
|
+
let attempt = 0;
|
|
82
|
+
while (!signal?.aborted) {
|
|
83
|
+
try {
|
|
84
|
+
for await (const value of factory()) {
|
|
85
|
+
if (signal?.aborted) {
|
|
86
|
+
return;
|
|
87
|
+
}
|
|
88
|
+
yield value;
|
|
89
|
+
attempt = 0;
|
|
90
|
+
}
|
|
91
|
+
attempt = 0;
|
|
92
|
+
} catch (err) {
|
|
93
|
+
if (signal?.aborted) {
|
|
94
|
+
return;
|
|
95
|
+
}
|
|
96
|
+
void err;
|
|
97
|
+
await exponentialBackoffWithSignal(attempt, signal);
|
|
98
|
+
attempt++;
|
|
99
|
+
}
|
|
100
|
+
}
|
|
101
|
+
}
|
|
4
102
|
|
|
5
103
|
// src/diagnostics.ts
|
|
6
104
|
import dc from "diagnostics_channel";
|
|
@@ -231,12 +329,92 @@ var ConnectionPool = class {
|
|
|
231
329
|
}
|
|
232
330
|
};
|
|
233
331
|
|
|
234
|
-
// src/
|
|
235
|
-
|
|
332
|
+
// src/scalars.ts
|
|
333
|
+
var LOCAL_DATE_TIME_KEYS = ["year", "month", "day", "hour", "minute", "second"];
|
|
334
|
+
var LOCAL_DATE_KEYS = ["year", "month", "day"];
|
|
335
|
+
var DATE_TIME_REGEX = /^(\d{4})-(\d{2})-(\d{2})T(\d{2}):(\d{2}):(\d{2})/;
|
|
336
|
+
var DATE_REGEX = /^(\d{4})-(\d{2})-(\d{2})/;
|
|
337
|
+
function pad2(n) {
|
|
338
|
+
return n.toString().padStart(2, "0");
|
|
339
|
+
}
|
|
340
|
+
function pad4(n) {
|
|
341
|
+
return n.toString().padStart(4, "0");
|
|
342
|
+
}
|
|
343
|
+
function hasExactKeys(value, keys) {
|
|
344
|
+
const ownKeys = Object.keys(value);
|
|
345
|
+
if (ownKeys.length !== keys.length) {
|
|
346
|
+
return false;
|
|
347
|
+
}
|
|
348
|
+
for (const key of keys) {
|
|
349
|
+
if (!Object.prototype.hasOwnProperty.call(value, key)) {
|
|
350
|
+
return false;
|
|
351
|
+
}
|
|
352
|
+
}
|
|
353
|
+
return true;
|
|
354
|
+
}
|
|
355
|
+
function allNumbers(value, keys) {
|
|
356
|
+
for (const key of keys) {
|
|
357
|
+
if (typeof value[key] !== "number") {
|
|
358
|
+
return false;
|
|
359
|
+
}
|
|
360
|
+
}
|
|
361
|
+
return true;
|
|
362
|
+
}
|
|
363
|
+
function isLocalDateTime(value) {
|
|
364
|
+
return !!value && typeof value === "object" && !Array.isArray(value) && hasExactKeys(value, LOCAL_DATE_TIME_KEYS) && allNumbers(value, LOCAL_DATE_TIME_KEYS);
|
|
365
|
+
}
|
|
366
|
+
function isLocalDate(value) {
|
|
367
|
+
return !!value && typeof value === "object" && !Array.isArray(value) && hasExactKeys(value, LOCAL_DATE_KEYS) && allNumbers(value, LOCAL_DATE_KEYS);
|
|
368
|
+
}
|
|
369
|
+
function decodeLocalDateTime(value) {
|
|
370
|
+
const match = DATE_TIME_REGEX.exec(value);
|
|
371
|
+
if (!match) {
|
|
372
|
+
throw new Error(`@contello/client: cannot decode LocalDateTime from "${value}"`);
|
|
373
|
+
}
|
|
374
|
+
return {
|
|
375
|
+
year: Number(match[1]),
|
|
376
|
+
month: Number(match[2]),
|
|
377
|
+
day: Number(match[3]),
|
|
378
|
+
hour: Number(match[4]),
|
|
379
|
+
minute: Number(match[5]),
|
|
380
|
+
second: Number(match[6])
|
|
381
|
+
};
|
|
382
|
+
}
|
|
383
|
+
function decodeLocalDate(value) {
|
|
384
|
+
const match = DATE_REGEX.exec(value);
|
|
385
|
+
if (!match) {
|
|
386
|
+
throw new Error(`@contello/client: cannot decode LocalDate from "${value}"`);
|
|
387
|
+
}
|
|
388
|
+
return {
|
|
389
|
+
year: Number(match[1]),
|
|
390
|
+
month: Number(match[2]),
|
|
391
|
+
day: Number(match[3])
|
|
392
|
+
};
|
|
393
|
+
}
|
|
394
|
+
function encodeLocalDateTime(value) {
|
|
395
|
+
return `${pad4(value.year)}-${pad2(value.month)}-${pad2(value.day)}T${pad2(value.hour)}:${pad2(value.minute)}:${pad2(value.second)}Z`;
|
|
396
|
+
}
|
|
397
|
+
function encodeLocalDate(value) {
|
|
398
|
+
return `${pad4(value.year)}-${pad2(value.month)}-${pad2(value.day)}T00:00:00Z`;
|
|
399
|
+
}
|
|
236
400
|
|
|
237
401
|
// src/transform-response.ts
|
|
238
402
|
var FLAT_PREFIX = "_flat_";
|
|
403
|
+
var LDT_PREFIX = "_ldt_";
|
|
404
|
+
var LD_PREFIX = "_ld_";
|
|
239
405
|
var MODEL_SUFFIXES = ["Entity", "Component"];
|
|
406
|
+
function decodeValue(value, decoder) {
|
|
407
|
+
if (value === null || value === void 0) {
|
|
408
|
+
return value;
|
|
409
|
+
}
|
|
410
|
+
if (Array.isArray(value)) {
|
|
411
|
+
return value.map((item) => decodeValue(item, decoder));
|
|
412
|
+
}
|
|
413
|
+
if (typeof value === "string") {
|
|
414
|
+
return decoder(value);
|
|
415
|
+
}
|
|
416
|
+
return value;
|
|
417
|
+
}
|
|
240
418
|
function deriveModel(typename) {
|
|
241
419
|
for (const suffix of MODEL_SUFFIXES) {
|
|
242
420
|
if (typename.endsWith(suffix)) {
|
|
@@ -281,6 +459,17 @@ function transformResponse(data) {
|
|
|
281
459
|
obj[fieldKey] = resolveRefs(refArray, flatMap);
|
|
282
460
|
delete obj[flatKey];
|
|
283
461
|
}
|
|
462
|
+
for (const key of Object.keys(obj)) {
|
|
463
|
+
if (key.startsWith(LDT_PREFIX)) {
|
|
464
|
+
const original = key.slice(LDT_PREFIX.length);
|
|
465
|
+
obj[original] = decodeValue(obj[key], decodeLocalDateTime);
|
|
466
|
+
delete obj[key];
|
|
467
|
+
} else if (key.startsWith(LD_PREFIX)) {
|
|
468
|
+
const original = key.slice(LD_PREFIX.length);
|
|
469
|
+
obj[original] = decodeValue(obj[key], decodeLocalDate);
|
|
470
|
+
delete obj[key];
|
|
471
|
+
}
|
|
472
|
+
}
|
|
284
473
|
for (const key of Object.keys(obj)) {
|
|
285
474
|
const value = obj[key];
|
|
286
475
|
if (value && typeof value === "object") {
|
|
@@ -326,22 +515,129 @@ function resolveNestedRefs(value, flatMap) {
|
|
|
326
515
|
}
|
|
327
516
|
}
|
|
328
517
|
|
|
518
|
+
// src/transform-variables.ts
|
|
519
|
+
function transformVariables(value) {
|
|
520
|
+
if (value === null || value === void 0) {
|
|
521
|
+
return value;
|
|
522
|
+
}
|
|
523
|
+
if (Array.isArray(value)) {
|
|
524
|
+
return value.map((item) => transformVariables(item));
|
|
525
|
+
}
|
|
526
|
+
if (typeof value !== "object") {
|
|
527
|
+
return value;
|
|
528
|
+
}
|
|
529
|
+
if (isLocalDateTime(value)) {
|
|
530
|
+
return encodeLocalDateTime(value);
|
|
531
|
+
}
|
|
532
|
+
if (isLocalDate(value)) {
|
|
533
|
+
return encodeLocalDate(value);
|
|
534
|
+
}
|
|
535
|
+
const obj = value;
|
|
536
|
+
const out = {};
|
|
537
|
+
for (const key of Object.keys(obj)) {
|
|
538
|
+
out[key] = transformVariables(obj[key]);
|
|
539
|
+
}
|
|
540
|
+
return out;
|
|
541
|
+
}
|
|
542
|
+
|
|
329
543
|
// src/rpc.ts
|
|
330
|
-
function buildRpc(operations,
|
|
544
|
+
function buildRpc(operations, subscribe) {
|
|
331
545
|
const rpc = {};
|
|
332
546
|
for (const [name, def] of Object.entries(operations)) {
|
|
333
547
|
if (def.kind === "subscription") {
|
|
334
|
-
rpc[name] = (variables) =>
|
|
548
|
+
rpc[name] = (variables) => mapAsync(subscribe(def.document, transformVariables(variables)), transformResponse);
|
|
335
549
|
} else {
|
|
336
550
|
rpc[name] = (variables) => wrap(
|
|
337
551
|
`rpc:${name}`,
|
|
338
|
-
() =>
|
|
552
|
+
() => firstAsync(mapAsync(subscribe(def.document, transformVariables(variables)), transformResponse))
|
|
339
553
|
);
|
|
340
554
|
}
|
|
341
555
|
}
|
|
342
556
|
return rpc;
|
|
343
557
|
}
|
|
344
558
|
|
|
559
|
+
// src/source-subscription.ts
|
|
560
|
+
var cache = /* @__PURE__ */ new WeakMap();
|
|
561
|
+
function createSelection(source) {
|
|
562
|
+
const spread = `{ ...${source.fragment} }`;
|
|
563
|
+
switch (source.__cardinality) {
|
|
564
|
+
case "singleton":
|
|
565
|
+
return `subscription { source: ${source.subscription} ${spread} }`;
|
|
566
|
+
case "entity":
|
|
567
|
+
return `subscription($ids: [ID!]) { source: ${source.subscription}(request: { filter: { ids: $ids } }) ${spread} }`;
|
|
568
|
+
case "route":
|
|
569
|
+
case "asset":
|
|
570
|
+
return `subscription { source: ${source.subscription} ${spread} }`;
|
|
571
|
+
case "i18nMessage":
|
|
572
|
+
return `subscription($collection: String!) { source: ${source.subscription}(collectionReferenceName: $collection) ${spread} }`;
|
|
573
|
+
}
|
|
574
|
+
}
|
|
575
|
+
function createSourceSubscription(source) {
|
|
576
|
+
const cached = cache.get(source);
|
|
577
|
+
if (cached !== void 0) {
|
|
578
|
+
return cached;
|
|
579
|
+
}
|
|
580
|
+
const doc = `${source.document}
|
|
581
|
+
${createSelection(source)}`;
|
|
582
|
+
cache.set(source, doc);
|
|
583
|
+
return doc;
|
|
584
|
+
}
|
|
585
|
+
|
|
586
|
+
// src/sources.ts
|
|
587
|
+
function createSources(sources, subscribe) {
|
|
588
|
+
const out = {};
|
|
589
|
+
for (const [name, source] of Object.entries(sources)) {
|
|
590
|
+
const def = source;
|
|
591
|
+
const doc = createSourceSubscription(def);
|
|
592
|
+
switch (def.__cardinality) {
|
|
593
|
+
case "singleton":
|
|
594
|
+
out[name] = {
|
|
595
|
+
fetch: () => wrap(
|
|
596
|
+
`source:${name}`,
|
|
597
|
+
() => firstAsync(mapAsync(subscribe(doc), (r) => transformResponse(r).source))
|
|
598
|
+
)
|
|
599
|
+
};
|
|
600
|
+
break;
|
|
601
|
+
case "entity":
|
|
602
|
+
out[name] = {
|
|
603
|
+
fetch: (vars) => wrap(
|
|
604
|
+
`source:${name}`,
|
|
605
|
+
() => firstAsync(
|
|
606
|
+
mapAsync(
|
|
607
|
+
subscribe(doc, transformVariables({ ids: vars?.ids })),
|
|
608
|
+
(r) => transformResponse(r).source
|
|
609
|
+
)
|
|
610
|
+
)
|
|
611
|
+
)
|
|
612
|
+
};
|
|
613
|
+
break;
|
|
614
|
+
case "route":
|
|
615
|
+
case "asset":
|
|
616
|
+
out[name] = {
|
|
617
|
+
fetch: () => wrap(
|
|
618
|
+
`source:${name}`,
|
|
619
|
+
() => firstAsync(mapAsync(subscribe(doc), (r) => transformResponse(r).source))
|
|
620
|
+
)
|
|
621
|
+
};
|
|
622
|
+
break;
|
|
623
|
+
case "i18nMessage":
|
|
624
|
+
out[name] = {
|
|
625
|
+
fetch: (vars) => wrap(
|
|
626
|
+
`source:${name}`,
|
|
627
|
+
() => firstAsync(
|
|
628
|
+
mapAsync(
|
|
629
|
+
subscribe(doc, { collection: vars.collection }),
|
|
630
|
+
(r) => transformResponse(r).source
|
|
631
|
+
)
|
|
632
|
+
)
|
|
633
|
+
)
|
|
634
|
+
};
|
|
635
|
+
break;
|
|
636
|
+
}
|
|
637
|
+
}
|
|
638
|
+
return out;
|
|
639
|
+
}
|
|
640
|
+
|
|
345
641
|
// src/upload.ts
|
|
346
642
|
var DEFAULT_CHUNK_SIZE = 4 * 1024 * 1024;
|
|
347
643
|
function isUint8Array(data) {
|
|
@@ -427,23 +723,24 @@ function upload(url, project, token, data, meta, options) {
|
|
|
427
723
|
|
|
428
724
|
// src/utils.ts
|
|
429
725
|
var MAX_DELAY_MS = 3e4;
|
|
430
|
-
var
|
|
431
|
-
var
|
|
432
|
-
function
|
|
433
|
-
const
|
|
434
|
-
return new Promise((resolve) => setTimeout(resolve,
|
|
726
|
+
var JITTER_MIN_MS2 = 300;
|
|
727
|
+
var JITTER_MAX_MS2 = 3e3;
|
|
728
|
+
function wsRetryWait(retries) {
|
|
729
|
+
const delay2 = Math.min(Math.pow(2, retries) * 1e3, MAX_DELAY_MS) + Math.floor(Math.random() * (JITTER_MAX_MS2 - JITTER_MIN_MS2) + JITTER_MIN_MS2);
|
|
730
|
+
return new Promise((resolve) => setTimeout(resolve, delay2));
|
|
435
731
|
}
|
|
436
732
|
|
|
437
733
|
// src/client.ts
|
|
438
734
|
var ContelloClient = class {
|
|
439
735
|
_pool;
|
|
440
736
|
_rpc;
|
|
737
|
+
_sources;
|
|
441
738
|
_url;
|
|
442
739
|
_project;
|
|
443
740
|
_token;
|
|
444
741
|
_agent;
|
|
445
742
|
constructor(options) {
|
|
446
|
-
const { url, project, token,
|
|
743
|
+
const { url, project, token, schema, connections = 1, connectionEvents } = options;
|
|
447
744
|
this._url = url;
|
|
448
745
|
this._project = project;
|
|
449
746
|
this._token = token;
|
|
@@ -475,7 +772,7 @@ var ContelloClient = class {
|
|
|
475
772
|
lazy: false,
|
|
476
773
|
keepAlive: 3e4,
|
|
477
774
|
retryAttempts: Infinity,
|
|
478
|
-
retryWait:
|
|
775
|
+
retryWait: wsRetryWait,
|
|
479
776
|
shouldRetry: () => true,
|
|
480
777
|
jsonMessageReplacer: (key, value) => {
|
|
481
778
|
if (!key) {
|
|
@@ -505,14 +802,21 @@ var ContelloClient = class {
|
|
|
505
802
|
}
|
|
506
803
|
});
|
|
507
804
|
}, connections);
|
|
508
|
-
this._rpc = operations ? buildRpc(operations, this) : void 0;
|
|
805
|
+
this._rpc = schema?.operations ? buildRpc(schema.operations, (q, v) => this.subscribe(q, v)) : void 0;
|
|
806
|
+
this._sources = schema?.sources ? createSources(schema.sources, (q, v) => this.subscribe(q, v)) : void 0;
|
|
509
807
|
}
|
|
510
808
|
get rpc() {
|
|
511
809
|
if (!this._rpc) {
|
|
512
|
-
throw new Error("@contello/client: .rpc accessed without operations");
|
|
810
|
+
throw new Error("@contello/client: .rpc accessed without a schema containing operations");
|
|
513
811
|
}
|
|
514
812
|
return this._rpc;
|
|
515
813
|
}
|
|
814
|
+
get sources() {
|
|
815
|
+
if (!this._sources) {
|
|
816
|
+
throw new Error("@contello/client: .sources accessed without a schema containing sources");
|
|
817
|
+
}
|
|
818
|
+
return this._sources;
|
|
819
|
+
}
|
|
516
820
|
async init() {
|
|
517
821
|
await this._pool.connect();
|
|
518
822
|
}
|
|
@@ -521,13 +825,83 @@ var ContelloClient = class {
|
|
|
521
825
|
await this._agent.close();
|
|
522
826
|
}
|
|
523
827
|
async ping() {
|
|
524
|
-
return ping((query) =>
|
|
828
|
+
return ping((query) => firstAsync(this.subscribe(query)));
|
|
525
829
|
}
|
|
830
|
+
/**
|
|
831
|
+
* Open a subscription on the websocket. Each `[Symbol.asyncIterator]()` call (the implicit one
|
|
832
|
+
* from `for await`, or via `rxjs.from(this)`) starts a fresh server subscription and tears it
|
|
833
|
+
* down when the iterator is returned (`break`, error, or explicit stop).
|
|
834
|
+
*/
|
|
526
835
|
subscribe(query, variables) {
|
|
527
|
-
const
|
|
528
|
-
return
|
|
529
|
-
|
|
530
|
-
|
|
836
|
+
const pool = this._pool;
|
|
837
|
+
return {
|
|
838
|
+
[Symbol.asyncIterator]() {
|
|
839
|
+
const wsClient = pool.get();
|
|
840
|
+
const queue = [];
|
|
841
|
+
let pending;
|
|
842
|
+
let done = false;
|
|
843
|
+
let error;
|
|
844
|
+
const unsubscribe = wsClient.subscribe(
|
|
845
|
+
{ query, variables },
|
|
846
|
+
{
|
|
847
|
+
next(msg) {
|
|
848
|
+
const value = msg.data;
|
|
849
|
+
if (value === void 0 || value === null) {
|
|
850
|
+
return;
|
|
851
|
+
}
|
|
852
|
+
if (pending) {
|
|
853
|
+
const p = pending;
|
|
854
|
+
pending = void 0;
|
|
855
|
+
p.resolve({ value, done: false });
|
|
856
|
+
} else {
|
|
857
|
+
queue.push(value);
|
|
858
|
+
}
|
|
859
|
+
},
|
|
860
|
+
error(err) {
|
|
861
|
+
done = true;
|
|
862
|
+
error = err;
|
|
863
|
+
if (pending) {
|
|
864
|
+
const p = pending;
|
|
865
|
+
pending = void 0;
|
|
866
|
+
p.reject(err);
|
|
867
|
+
}
|
|
868
|
+
},
|
|
869
|
+
complete() {
|
|
870
|
+
done = true;
|
|
871
|
+
if (pending) {
|
|
872
|
+
const p = pending;
|
|
873
|
+
pending = void 0;
|
|
874
|
+
p.resolve({ value: void 0, done: true });
|
|
875
|
+
}
|
|
876
|
+
}
|
|
877
|
+
}
|
|
878
|
+
);
|
|
879
|
+
return {
|
|
880
|
+
next() {
|
|
881
|
+
if (queue.length > 0) {
|
|
882
|
+
return Promise.resolve({ value: queue.shift(), done: false });
|
|
883
|
+
}
|
|
884
|
+
if (error !== void 0) {
|
|
885
|
+
return Promise.reject(error);
|
|
886
|
+
}
|
|
887
|
+
if (done) {
|
|
888
|
+
return Promise.resolve({ value: void 0, done: true });
|
|
889
|
+
}
|
|
890
|
+
return new Promise((resolve, reject) => {
|
|
891
|
+
pending = { resolve, reject };
|
|
892
|
+
});
|
|
893
|
+
},
|
|
894
|
+
return() {
|
|
895
|
+
unsubscribe();
|
|
896
|
+
return Promise.resolve({ value: void 0, done: true });
|
|
897
|
+
},
|
|
898
|
+
throw(err) {
|
|
899
|
+
unsubscribe();
|
|
900
|
+
return Promise.reject(err);
|
|
901
|
+
}
|
|
902
|
+
};
|
|
903
|
+
}
|
|
904
|
+
};
|
|
531
905
|
}
|
|
532
906
|
download(fileId) {
|
|
533
907
|
return wrap("@contello/client:download", () => downloadFile(this._agent, this._url, this._token, fileId));
|
|
@@ -542,13 +916,137 @@ var ContelloClient = class {
|
|
|
542
916
|
);
|
|
543
917
|
}
|
|
544
918
|
execute(query, variables) {
|
|
545
|
-
return wrap("@contello/client:execute", () =>
|
|
919
|
+
return wrap("@contello/client:execute", () => firstAsync(this.subscribe(query, variables)));
|
|
546
920
|
}
|
|
547
921
|
};
|
|
548
922
|
function createContelloClient(options) {
|
|
549
923
|
return new ContelloClient(options);
|
|
550
924
|
}
|
|
925
|
+
|
|
926
|
+
// src/async-iterable-subject.ts
|
|
927
|
+
function createAsyncIterableSubject() {
|
|
928
|
+
const consumers = /* @__PURE__ */ new Set();
|
|
929
|
+
let status = { state: "open" };
|
|
930
|
+
const next = (value) => {
|
|
931
|
+
if (status.state !== "open") {
|
|
932
|
+
return;
|
|
933
|
+
}
|
|
934
|
+
for (const consumer of consumers) {
|
|
935
|
+
if (consumer.kind === "listener") {
|
|
936
|
+
consumer.next?.(value);
|
|
937
|
+
} else if (consumer.pending) {
|
|
938
|
+
const p = consumer.pending;
|
|
939
|
+
consumer.pending = void 0;
|
|
940
|
+
p.resolve({ value, done: false });
|
|
941
|
+
} else {
|
|
942
|
+
consumer.queue.push(value);
|
|
943
|
+
}
|
|
944
|
+
}
|
|
945
|
+
};
|
|
946
|
+
const error = (err) => {
|
|
947
|
+
if (status.state !== "open") {
|
|
948
|
+
return;
|
|
949
|
+
}
|
|
950
|
+
status = { state: "errored", err };
|
|
951
|
+
for (const consumer of consumers) {
|
|
952
|
+
if (consumer.kind === "listener") {
|
|
953
|
+
consumer.error?.(err);
|
|
954
|
+
} else if (consumer.pending) {
|
|
955
|
+
const p = consumer.pending;
|
|
956
|
+
consumer.pending = void 0;
|
|
957
|
+
p.reject(err);
|
|
958
|
+
}
|
|
959
|
+
}
|
|
960
|
+
consumers.clear();
|
|
961
|
+
};
|
|
962
|
+
const complete = () => {
|
|
963
|
+
if (status.state !== "open") {
|
|
964
|
+
return;
|
|
965
|
+
}
|
|
966
|
+
status = { state: "completed" };
|
|
967
|
+
for (const consumer of consumers) {
|
|
968
|
+
if (consumer.kind === "listener") {
|
|
969
|
+
consumer.complete?.();
|
|
970
|
+
} else if (consumer.pending) {
|
|
971
|
+
const p = consumer.pending;
|
|
972
|
+
consumer.pending = void 0;
|
|
973
|
+
p.resolve({ value: void 0, done: true });
|
|
974
|
+
}
|
|
975
|
+
}
|
|
976
|
+
consumers.clear();
|
|
977
|
+
};
|
|
978
|
+
const subscribe = (observerOrNext) => {
|
|
979
|
+
if (status.state !== "open") {
|
|
980
|
+
return () => {
|
|
981
|
+
};
|
|
982
|
+
}
|
|
983
|
+
const observer = typeof observerOrNext === "function" ? { next: observerOrNext } : observerOrNext;
|
|
984
|
+
const consumer = { kind: "listener", ...observer };
|
|
985
|
+
consumers.add(consumer);
|
|
986
|
+
return () => {
|
|
987
|
+
consumers.delete(consumer);
|
|
988
|
+
};
|
|
989
|
+
};
|
|
990
|
+
const iterable = {
|
|
991
|
+
[Symbol.asyncIterator]() {
|
|
992
|
+
if (status.state === "errored") {
|
|
993
|
+
const err = status.err;
|
|
994
|
+
return {
|
|
995
|
+
next: () => Promise.reject(err),
|
|
996
|
+
return: () => Promise.resolve({ value: void 0, done: true })
|
|
997
|
+
};
|
|
998
|
+
}
|
|
999
|
+
if (status.state === "completed") {
|
|
1000
|
+
return {
|
|
1001
|
+
next: () => Promise.resolve({ value: void 0, done: true }),
|
|
1002
|
+
return: () => Promise.resolve({ value: void 0, done: true })
|
|
1003
|
+
};
|
|
1004
|
+
}
|
|
1005
|
+
const consumer = { kind: "iterator", queue: [] };
|
|
1006
|
+
consumers.add(consumer);
|
|
1007
|
+
return {
|
|
1008
|
+
next() {
|
|
1009
|
+
if (consumer.queue.length > 0) {
|
|
1010
|
+
return Promise.resolve({ value: consumer.queue.shift(), done: false });
|
|
1011
|
+
}
|
|
1012
|
+
if (status.state === "errored") {
|
|
1013
|
+
return Promise.reject(status.err);
|
|
1014
|
+
}
|
|
1015
|
+
if (status.state === "completed") {
|
|
1016
|
+
return Promise.resolve({ value: void 0, done: true });
|
|
1017
|
+
}
|
|
1018
|
+
return new Promise((resolve, reject) => {
|
|
1019
|
+
consumer.pending = { resolve, reject };
|
|
1020
|
+
});
|
|
1021
|
+
},
|
|
1022
|
+
return() {
|
|
1023
|
+
consumers.delete(consumer);
|
|
1024
|
+
if (consumer.pending) {
|
|
1025
|
+
const p = consumer.pending;
|
|
1026
|
+
consumer.pending = void 0;
|
|
1027
|
+
p.resolve({ value: void 0, done: true });
|
|
1028
|
+
}
|
|
1029
|
+
return Promise.resolve({ value: void 0, done: true });
|
|
1030
|
+
},
|
|
1031
|
+
throw(err) {
|
|
1032
|
+
consumers.delete(consumer);
|
|
1033
|
+
return Promise.reject(err);
|
|
1034
|
+
}
|
|
1035
|
+
};
|
|
1036
|
+
}
|
|
1037
|
+
};
|
|
1038
|
+
return Object.assign(iterable, { next, error, complete, subscribe });
|
|
1039
|
+
}
|
|
551
1040
|
export {
|
|
552
1041
|
ContelloClient,
|
|
553
|
-
|
|
1042
|
+
asyncKeepalive,
|
|
1043
|
+
collectAsync,
|
|
1044
|
+
createAsyncIterableSubject,
|
|
1045
|
+
createContelloClient,
|
|
1046
|
+
createSourceSubscription,
|
|
1047
|
+
exponentialBackoff,
|
|
1048
|
+
filterAsync,
|
|
1049
|
+
firstAsync,
|
|
1050
|
+
mapAsync,
|
|
1051
|
+
runWithBackoff
|
|
554
1052
|
};
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@contello/client",
|
|
3
|
-
"version": "1.
|
|
3
|
+
"version": "2.1.0",
|
|
4
4
|
"description": "GraphQL client for Contello CMS with WebSocket transport, connection pooling, and built-in codegen",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"license": "MIT",
|
|
@@ -15,12 +15,6 @@
|
|
|
15
15
|
"graphql-ws": "^6.0.8",
|
|
16
16
|
"undici": "^8.1.0"
|
|
17
17
|
},
|
|
18
|
-
"peerDependencies": {
|
|
19
|
-
"rxjs": "^7.0.0"
|
|
20
|
-
},
|
|
21
|
-
"devDependencies": {
|
|
22
|
-
"rxjs": "^7.8.2"
|
|
23
|
-
},
|
|
24
18
|
"bin": {
|
|
25
19
|
"contello-client": "./dist/cli.js"
|
|
26
20
|
},
|