@sanity/client 6.24.3 → 6.24.4
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.browser.cjs +188 -128
- package/dist/index.browser.cjs.map +1 -1
- package/dist/index.browser.d.cts +109 -0
- package/dist/index.browser.d.ts +109 -0
- package/dist/index.browser.js +189 -129
- package/dist/index.browser.js.map +1 -1
- package/dist/index.cjs +189 -129
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +109 -0
- package/dist/index.d.ts +109 -0
- package/dist/index.js +190 -130
- package/dist/index.js.map +1 -1
- package/dist/stega.browser.d.cts +109 -0
- package/dist/stega.browser.d.ts +109 -0
- package/dist/stega.d.cts +109 -0
- package/dist/stega.d.ts +109 -0
- package/package.json +3 -3
- package/src/data/eventsource.ts +255 -0
- package/src/data/eventsourcePolyfill.ts +7 -0
- package/src/data/listen.ts +31 -142
- package/src/data/live.ts +60 -120
- package/src/data/reconnectOnConnectionFailure.ts +30 -0
- package/src/data/transaction.ts +26 -1
- package/src/defineCreateClient.ts +11 -0
- package/src/types.ts +10 -0
- package/umd/sanityClient.js +988 -144
- package/umd/sanityClient.min.js +2 -2
package/dist/index.browser.cjs
CHANGED
|
@@ -268,6 +268,112 @@ const initConfig = (config, prevConfig) => {
|
|
|
268
268
|
const hostParts = newConfig.apiHost.split("://", 2), protocol = hostParts[0], host = hostParts[1], cdnHost = newConfig.isDefaultApi ? defaultCdnHost : host;
|
|
269
269
|
return newConfig.useProjectHostname ? (newConfig.url = `${protocol}://${newConfig.projectId}.${host}/v${newConfig.apiVersion}`, newConfig.cdnUrl = `${protocol}://${newConfig.projectId}.${cdnHost}/v${newConfig.apiVersion}`) : (newConfig.url = `${newConfig.apiHost}/v${newConfig.apiVersion}`, newConfig.cdnUrl = newConfig.url), newConfig;
|
|
270
270
|
};
|
|
271
|
+
class ConnectionFailedError extends Error {
|
|
272
|
+
name = "ConnectionFailedError";
|
|
273
|
+
}
|
|
274
|
+
class DisconnectError extends Error {
|
|
275
|
+
name = "DisconnectError";
|
|
276
|
+
reason;
|
|
277
|
+
constructor(message, reason, options = {}) {
|
|
278
|
+
super(message, options), this.reason = reason;
|
|
279
|
+
}
|
|
280
|
+
}
|
|
281
|
+
class ChannelError extends Error {
|
|
282
|
+
name = "ChannelError";
|
|
283
|
+
data;
|
|
284
|
+
constructor(message, data) {
|
|
285
|
+
super(message), this.data = data;
|
|
286
|
+
}
|
|
287
|
+
}
|
|
288
|
+
class MessageError extends Error {
|
|
289
|
+
name = "MessageError";
|
|
290
|
+
data;
|
|
291
|
+
constructor(message, data, options = {}) {
|
|
292
|
+
super(message, options), this.data = data;
|
|
293
|
+
}
|
|
294
|
+
}
|
|
295
|
+
class MessageParseError extends Error {
|
|
296
|
+
name = "MessageParseError";
|
|
297
|
+
}
|
|
298
|
+
const REQUIRED_EVENTS = ["channelError", "disconnect"];
|
|
299
|
+
function connectEventSource(initEventSource, events) {
|
|
300
|
+
return rxjs.defer(() => {
|
|
301
|
+
const es = initEventSource();
|
|
302
|
+
return rxjs.isObservable(es) ? es : rxjs.of(es);
|
|
303
|
+
}).pipe(rxjs.mergeMap((es) => connectWithESInstance(es, events)));
|
|
304
|
+
}
|
|
305
|
+
function connectWithESInstance(es, events) {
|
|
306
|
+
return new rxjs.Observable((observer) => {
|
|
307
|
+
const emitOpen = events.includes("open"), emitReconnect = events.includes("reconnect");
|
|
308
|
+
function onError(evt) {
|
|
309
|
+
if ("data" in evt) {
|
|
310
|
+
const [parseError, event] = parseEvent(evt);
|
|
311
|
+
observer.error(
|
|
312
|
+
parseError ? new MessageParseError("Unable to parse EventSource error message", { cause: event }) : new MessageError((event?.data).message, event)
|
|
313
|
+
);
|
|
314
|
+
return;
|
|
315
|
+
}
|
|
316
|
+
es.readyState === es.CLOSED ? observer.error(new ConnectionFailedError("EventSource connection failed")) : emitReconnect && observer.next({ type: "reconnect" });
|
|
317
|
+
}
|
|
318
|
+
function onOpen() {
|
|
319
|
+
observer.next({ type: "open" });
|
|
320
|
+
}
|
|
321
|
+
function onMessage(message) {
|
|
322
|
+
const [parseError, event] = parseEvent(message);
|
|
323
|
+
if (parseError) {
|
|
324
|
+
observer.error(
|
|
325
|
+
new MessageParseError("Unable to parse EventSource message", { cause: parseError })
|
|
326
|
+
);
|
|
327
|
+
return;
|
|
328
|
+
}
|
|
329
|
+
if (message.type === "channelError") {
|
|
330
|
+
observer.error(new ChannelError(extractErrorMessage(event?.data), event.data));
|
|
331
|
+
return;
|
|
332
|
+
}
|
|
333
|
+
if (message.type === "disconnect") {
|
|
334
|
+
observer.error(
|
|
335
|
+
new DisconnectError(
|
|
336
|
+
`Server disconnected client: ${event.data?.reason || "unknown error"}`
|
|
337
|
+
)
|
|
338
|
+
);
|
|
339
|
+
return;
|
|
340
|
+
}
|
|
341
|
+
observer.next({
|
|
342
|
+
type: message.type,
|
|
343
|
+
id: message.lastEventId,
|
|
344
|
+
...event.data ? { data: event.data } : {}
|
|
345
|
+
});
|
|
346
|
+
}
|
|
347
|
+
es.addEventListener("error", onError), emitOpen && es.addEventListener("open", onOpen);
|
|
348
|
+
const cleanedEvents = [.../* @__PURE__ */ new Set([...REQUIRED_EVENTS, ...events])].filter((type) => type !== "error" && type !== "open" && type !== "reconnect");
|
|
349
|
+
return cleanedEvents.forEach((type) => es.addEventListener(type, onMessage)), () => {
|
|
350
|
+
es.removeEventListener("error", onError), emitOpen && es.removeEventListener("open", onOpen), cleanedEvents.forEach((type) => es.removeEventListener(type, onMessage)), es.close();
|
|
351
|
+
};
|
|
352
|
+
});
|
|
353
|
+
}
|
|
354
|
+
function parseEvent(message) {
|
|
355
|
+
try {
|
|
356
|
+
const data = typeof message.data == "string" && JSON.parse(message.data);
|
|
357
|
+
return [
|
|
358
|
+
null,
|
|
359
|
+
{
|
|
360
|
+
type: message.type,
|
|
361
|
+
id: message.lastEventId,
|
|
362
|
+
...isEmptyObject(data) ? {} : { data }
|
|
363
|
+
}
|
|
364
|
+
];
|
|
365
|
+
} catch (err) {
|
|
366
|
+
return [err, null];
|
|
367
|
+
}
|
|
368
|
+
}
|
|
369
|
+
function extractErrorMessage(err) {
|
|
370
|
+
return err.error ? err.error.description ? err.error.description : typeof err.error == "string" ? err.error : JSON.stringify(err.error, null, 2) : err.message || "Unknown listener error";
|
|
371
|
+
}
|
|
372
|
+
function isEmptyObject(data) {
|
|
373
|
+
for (const _ in data)
|
|
374
|
+
return !1;
|
|
375
|
+
return !0;
|
|
376
|
+
}
|
|
271
377
|
function getSelection(sel) {
|
|
272
378
|
if (typeof sel == "string")
|
|
273
379
|
return { id: sel };
|
|
@@ -552,8 +658,8 @@ class Transaction extends BaseTransaction {
|
|
|
552
658
|
);
|
|
553
659
|
}
|
|
554
660
|
patch(patchOrDocumentId, patchOps) {
|
|
555
|
-
const isBuilder = typeof patchOps == "function";
|
|
556
|
-
if (
|
|
661
|
+
const isBuilder = typeof patchOps == "function", isPatch = typeof patchOrDocumentId != "string" && patchOrDocumentId instanceof Patch, isMutationSelection = typeof patchOrDocumentId == "object" && ("query" in patchOrDocumentId || "id" in patchOrDocumentId);
|
|
662
|
+
if (isPatch)
|
|
557
663
|
return this._add({ patch: patchOrDocumentId.serialize() });
|
|
558
664
|
if (isBuilder) {
|
|
559
665
|
const patch = patchOps(new Patch(patchOrDocumentId, {}, this.#client));
|
|
@@ -561,6 +667,10 @@ class Transaction extends BaseTransaction {
|
|
|
561
667
|
throw new Error("function passed to `patch()` must return the patch");
|
|
562
668
|
return this._add({ patch: patch.serialize() });
|
|
563
669
|
}
|
|
670
|
+
if (isMutationSelection) {
|
|
671
|
+
const patch = new Patch(patchOrDocumentId, patchOps || {}, this.#client);
|
|
672
|
+
return this._add({ patch: patch.serialize() });
|
|
673
|
+
}
|
|
564
674
|
return this._add({ patch: { id: patchOrDocumentId, ...patchOps } });
|
|
565
675
|
}
|
|
566
676
|
}
|
|
@@ -886,7 +996,18 @@ function optionsFromFile(opts, file) {
|
|
|
886
996
|
);
|
|
887
997
|
}
|
|
888
998
|
var defaults = (obj, defaults2) => Object.keys(defaults2).concat(Object.keys(obj)).reduce((target, prop) => (target[prop] = typeof obj[prop] > "u" ? defaults2[prop] : obj[prop], target), {});
|
|
889
|
-
const pick = (obj, props) => props.reduce((selection, prop) => (typeof obj[prop] > "u" || (selection[prop] = obj[prop]), selection), {}),
|
|
999
|
+
const pick = (obj, props) => props.reduce((selection, prop) => (typeof obj[prop] > "u" || (selection[prop] = obj[prop]), selection), {}), eventSourcePolyfill = rxjs.defer(() => import("@sanity/eventsource")).pipe(
|
|
1000
|
+
operators.map(({ default: EventSource2 }) => EventSource2),
|
|
1001
|
+
rxjs.shareReplay(1)
|
|
1002
|
+
);
|
|
1003
|
+
function reconnectOnConnectionFailure() {
|
|
1004
|
+
return function(source) {
|
|
1005
|
+
return source.pipe(
|
|
1006
|
+
rxjs.catchError((err, caught) => err instanceof ConnectionFailedError ? rxjs.concat(rxjs.of({ type: "reconnect" }), rxjs.timer(1e3).pipe(rxjs.mergeMap(() => caught))) : rxjs.throwError(() => err))
|
|
1007
|
+
);
|
|
1008
|
+
};
|
|
1009
|
+
}
|
|
1010
|
+
const MAX_URL_LENGTH = 14800, possibleOptions = [
|
|
890
1011
|
"includePreviousRevision",
|
|
891
1012
|
"includeResult",
|
|
892
1013
|
"includeMutations",
|
|
@@ -899,68 +1020,23 @@ const pick = (obj, props) => props.reduce((selection, prop) => (typeof obj[prop]
|
|
|
899
1020
|
function _listen(query, params, opts = {}) {
|
|
900
1021
|
const { url, token, withCredentials, requestTagPrefix } = this.config(), tag = opts.tag && requestTagPrefix ? [requestTagPrefix, opts.tag].join(".") : opts.tag, options = { ...defaults(opts, defaultOptions), tag }, listenOpts = pick(options, possibleOptions), qs = encodeQueryString({ query, params, options: { tag, ...listenOpts } }), uri = `${url}${_getDataUrl(this, "listen", qs)}`;
|
|
901
1022
|
if (uri.length > MAX_URL_LENGTH)
|
|
902
|
-
return
|
|
903
|
-
const listenFor = options.events ? options.events : ["mutation"],
|
|
1023
|
+
return rxjs.throwError(() => new Error("Query too large for listener"));
|
|
1024
|
+
const listenFor = options.events ? options.events : ["mutation"], esOptions = {};
|
|
904
1025
|
return (token || withCredentials) && (esOptions.withCredentials = !0), token && (esOptions.headers = {
|
|
905
1026
|
Authorization: `Bearer ${token}`
|
|
906
|
-
}),
|
|
907
|
-
|
|
908
|
-
|
|
909
|
-
|
|
910
|
-
|
|
911
|
-
|
|
912
|
-
|
|
913
|
-
|
|
914
|
-
|
|
915
|
-
|
|
916
|
-
|
|
917
|
-
|
|
918
|
-
|
|
919
|
-
function onDisconnect() {
|
|
920
|
-
stopped = !0, unsubscribe(), observer.complete();
|
|
921
|
-
}
|
|
922
|
-
function unsubscribe() {
|
|
923
|
-
es && (es.removeEventListener("error", onError), es.removeEventListener("channelError", onChannelError), es.removeEventListener("disconnect", onDisconnect), listenFor.forEach((type) => es.removeEventListener(type, onMessage)), es.close());
|
|
924
|
-
}
|
|
925
|
-
function emitReconnect() {
|
|
926
|
-
shouldEmitReconnect && observer.next({ type: "reconnect" });
|
|
927
|
-
}
|
|
928
|
-
async function getEventSource() {
|
|
929
|
-
const { default: EventSource2 } = await import("@sanity/eventsource");
|
|
930
|
-
if (unsubscribed)
|
|
931
|
-
return;
|
|
932
|
-
const evs = new EventSource2(uri, esOptions);
|
|
933
|
-
return evs.addEventListener("error", onError), evs.addEventListener("channelError", onChannelError), evs.addEventListener("disconnect", onDisconnect), listenFor.forEach((type) => evs.addEventListener(type, onMessage)), evs;
|
|
934
|
-
}
|
|
935
|
-
function open() {
|
|
936
|
-
getEventSource().then((eventSource) => {
|
|
937
|
-
eventSource && (es = eventSource, unsubscribed && unsubscribe());
|
|
938
|
-
}).catch((reason) => {
|
|
939
|
-
observer.error(reason), stop();
|
|
940
|
-
});
|
|
941
|
-
}
|
|
942
|
-
function stop() {
|
|
943
|
-
stopped = !0, unsubscribe(), unsubscribed = !0;
|
|
944
|
-
}
|
|
945
|
-
return stop;
|
|
946
|
-
});
|
|
947
|
-
}
|
|
948
|
-
function parseEvent$1(event) {
|
|
949
|
-
try {
|
|
950
|
-
const data = event.data && JSON.parse(event.data) || {};
|
|
951
|
-
return Object.assign({ type: event.type }, data);
|
|
952
|
-
} catch (err) {
|
|
953
|
-
return err;
|
|
954
|
-
}
|
|
955
|
-
}
|
|
956
|
-
function cooerceError(err) {
|
|
957
|
-
if (err instanceof Error)
|
|
958
|
-
return err;
|
|
959
|
-
const evt = parseEvent$1(err);
|
|
960
|
-
return evt instanceof Error ? evt : new Error(extractErrorMessage(evt));
|
|
961
|
-
}
|
|
962
|
-
function extractErrorMessage(err) {
|
|
963
|
-
return err.error ? err.error.description ? err.error.description : typeof err.error == "string" ? err.error : JSON.stringify(err.error, null, 2) : err.message || "Unknown listener error";
|
|
1027
|
+
}), connectEventSource(() => (
|
|
1028
|
+
// use polyfill if there is no global EventSource or if we need to set headers
|
|
1029
|
+
(typeof EventSource > "u" || esOptions.headers ? eventSourcePolyfill : rxjs.of(EventSource)).pipe(operators.map((EventSource2) => new EventSource2(uri, esOptions)))
|
|
1030
|
+
), listenFor).pipe(
|
|
1031
|
+
reconnectOnConnectionFailure(),
|
|
1032
|
+
operators.filter((event) => listenFor.includes(event.type)),
|
|
1033
|
+
operators.map(
|
|
1034
|
+
(event) => ({
|
|
1035
|
+
type: event.type,
|
|
1036
|
+
..."data" in event ? event.data : {}
|
|
1037
|
+
})
|
|
1038
|
+
)
|
|
1039
|
+
);
|
|
964
1040
|
}
|
|
965
1041
|
const requiredApiVersion = "2021-03-26";
|
|
966
1042
|
class LiveClient {
|
|
@@ -996,75 +1072,53 @@ class LiveClient {
|
|
|
996
1072
|
);
|
|
997
1073
|
const path = _getDataUrl(this.#client, "live/events"), url = new URL(this.#client.getUrl(path, !1)), tag = _tag && requestTagPrefix ? [requestTagPrefix, _tag].join(".") : _tag;
|
|
998
1074
|
tag && url.searchParams.set("tag", tag), includeDrafts && url.searchParams.set("includeDrafts", "true");
|
|
999
|
-
const
|
|
1000
|
-
|
|
1075
|
+
const esOptions = {};
|
|
1076
|
+
includeDrafts && token && (esOptions.headers = {
|
|
1001
1077
|
Authorization: `Bearer ${token}`
|
|
1002
|
-
}), includeDrafts && withCredentials && (esOptions.withCredentials = !0)
|
|
1003
|
-
|
|
1004
|
-
|
|
1005
|
-
|
|
1006
|
-
|
|
1007
|
-
|
|
1008
|
-
|
|
1009
|
-
|
|
1010
|
-
|
|
1011
|
-
|
|
1012
|
-
|
|
1013
|
-
|
|
1014
|
-
|
|
1015
|
-
|
|
1016
|
-
|
|
1017
|
-
}
|
|
1018
|
-
function unsubscribe() {
|
|
1019
|
-
if (es) {
|
|
1020
|
-
es.removeEventListener("error", onError);
|
|
1021
|
-
for (const type of listenFor)
|
|
1022
|
-
es.removeEventListener(type, onMessage);
|
|
1023
|
-
es.close();
|
|
1024
|
-
}
|
|
1025
|
-
}
|
|
1026
|
-
async function getEventSource() {
|
|
1027
|
-
const EventSourceImplementation = typeof EventSource > "u" || esOptions.headers || esOptions.withCredentials ? (await import("@sanity/eventsource")).default : EventSource;
|
|
1028
|
-
if (unsubscribed)
|
|
1029
|
-
return;
|
|
1030
|
-
try {
|
|
1031
|
-
if (await fetch(url, {
|
|
1032
|
-
method: "OPTIONS",
|
|
1033
|
-
mode: "cors",
|
|
1034
|
-
credentials: esOptions.withCredentials ? "include" : "omit",
|
|
1035
|
-
headers: esOptions.headers
|
|
1036
|
-
}), unsubscribed)
|
|
1037
|
-
return;
|
|
1038
|
-
} catch {
|
|
1039
|
-
throw new CorsOriginError({ projectId: projectId2 });
|
|
1078
|
+
}), includeDrafts && withCredentials && (esOptions.withCredentials = !0);
|
|
1079
|
+
const events = connectEventSource(() => (
|
|
1080
|
+
// use polyfill if there is no global EventSource or if we need to set headers
|
|
1081
|
+
(typeof EventSource > "u" || esOptions.headers ? eventSourcePolyfill : rxjs.of(EventSource)).pipe(operators.map((EventSource2) => new EventSource2(url.href, esOptions)))
|
|
1082
|
+
), [
|
|
1083
|
+
"message",
|
|
1084
|
+
"restart",
|
|
1085
|
+
"welcome",
|
|
1086
|
+
"reconnect"
|
|
1087
|
+
]).pipe(
|
|
1088
|
+
reconnectOnConnectionFailure(),
|
|
1089
|
+
operators.map((event) => {
|
|
1090
|
+
if (event.type === "message") {
|
|
1091
|
+
const { data, ...rest } = event;
|
|
1092
|
+
return { ...rest, tags: data.tags };
|
|
1040
1093
|
}
|
|
1041
|
-
|
|
1042
|
-
|
|
1043
|
-
|
|
1044
|
-
|
|
1045
|
-
|
|
1046
|
-
|
|
1047
|
-
|
|
1048
|
-
|
|
1049
|
-
|
|
1050
|
-
|
|
1051
|
-
|
|
1052
|
-
|
|
1053
|
-
|
|
1054
|
-
|
|
1055
|
-
stopped = !0, unsubscribe(), unsubscribed = !0;
|
|
1056
|
-
}
|
|
1057
|
-
return stop;
|
|
1058
|
-
});
|
|
1094
|
+
return event;
|
|
1095
|
+
})
|
|
1096
|
+
), checkCors = fetchObservable(url, {
|
|
1097
|
+
method: "OPTIONS",
|
|
1098
|
+
mode: "cors",
|
|
1099
|
+
credentials: esOptions.withCredentials ? "include" : "omit",
|
|
1100
|
+
headers: esOptions.headers
|
|
1101
|
+
}).pipe(
|
|
1102
|
+
rxjs.mergeMap(() => rxjs.EMPTY),
|
|
1103
|
+
rxjs.catchError(() => {
|
|
1104
|
+
throw new CorsOriginError({ projectId: projectId2 });
|
|
1105
|
+
})
|
|
1106
|
+
);
|
|
1107
|
+
return rxjs.concat(checkCors, events);
|
|
1059
1108
|
}
|
|
1060
1109
|
}
|
|
1061
|
-
function
|
|
1062
|
-
|
|
1063
|
-
const
|
|
1064
|
-
return {
|
|
1065
|
-
|
|
1066
|
-
|
|
1067
|
-
|
|
1110
|
+
function fetchObservable(url, init) {
|
|
1111
|
+
return new rxjs.Observable((observer) => {
|
|
1112
|
+
const controller = new AbortController(), signal = controller.signal;
|
|
1113
|
+
return fetch(url, { ...init, signal: controller.signal }).then(
|
|
1114
|
+
(response) => {
|
|
1115
|
+
observer.next(response), observer.complete();
|
|
1116
|
+
},
|
|
1117
|
+
(err) => {
|
|
1118
|
+
signal.aborted || observer.error(err);
|
|
1119
|
+
}
|
|
1120
|
+
), () => controller.abort();
|
|
1121
|
+
});
|
|
1068
1122
|
}
|
|
1069
1123
|
class ObservableDatasetsClient {
|
|
1070
1124
|
#client;
|
|
@@ -1598,8 +1652,13 @@ Object.defineProperty(exports, "unstable__environment", {
|
|
|
1598
1652
|
});
|
|
1599
1653
|
exports.BasePatch = BasePatch;
|
|
1600
1654
|
exports.BaseTransaction = BaseTransaction;
|
|
1655
|
+
exports.ChannelError = ChannelError;
|
|
1601
1656
|
exports.ClientError = ClientError;
|
|
1657
|
+
exports.ConnectionFailedError = ConnectionFailedError;
|
|
1602
1658
|
exports.CorsOriginError = CorsOriginError;
|
|
1659
|
+
exports.DisconnectError = DisconnectError;
|
|
1660
|
+
exports.MessageError = MessageError;
|
|
1661
|
+
exports.MessageParseError = MessageParseError;
|
|
1603
1662
|
exports.ObservablePatch = ObservablePatch;
|
|
1604
1663
|
exports.ObservableSanityClient = ObservableSanityClient;
|
|
1605
1664
|
exports.ObservableTransaction = ObservableTransaction;
|
|
@@ -1607,6 +1666,7 @@ exports.Patch = Patch;
|
|
|
1607
1666
|
exports.SanityClient = SanityClient;
|
|
1608
1667
|
exports.ServerError = ServerError;
|
|
1609
1668
|
exports.Transaction = Transaction;
|
|
1669
|
+
exports.connectEventSource = connectEventSource;
|
|
1610
1670
|
exports.createClient = createClient;
|
|
1611
1671
|
exports.default = deprecatedCreateClient;
|
|
1612
1672
|
exports.requester = requester;
|