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