@sanity/client 6.24.0 → 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 +185 -128
- package/dist/index.browser.cjs.map +1 -1
- package/dist/index.browser.d.cts +104 -1
- package/dist/index.browser.d.ts +104 -1
- package/dist/index.browser.js +186 -129
- package/dist/index.browser.js.map +1 -1
- package/dist/index.cjs +186 -129
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +104 -1
- package/dist/index.d.ts +104 -1
- package/dist/index.js +187 -130
- package/dist/index.js.map +1 -1
- package/package.json +16 -16
- package/src/config.ts +3 -1
- 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 +981 -139
- package/umd/sanityClient.min.js +2 -2
package/dist/index.browser.cjs
CHANGED
|
@@ -207,7 +207,7 @@ function validateApiVersion(apiVersion) {
|
|
|
207
207
|
if (!(/^\d{4}-\d{2}-\d{2}$/.test(apiVersion) && apiDate instanceof Date && apiDate.getTime() > 0))
|
|
208
208
|
throw new Error("Invalid API version string, expected `1` or date in format `YYYY-MM-DD`");
|
|
209
209
|
}
|
|
210
|
-
|
|
210
|
+
function validateApiPerspective(perspective) {
|
|
211
211
|
if (Array.isArray(perspective)) {
|
|
212
212
|
for (const perspectiveValue of perspective)
|
|
213
213
|
if (perspectiveValue !== "published" && perspectiveValue !== "drafts" && !(typeof perspectiveValue == "string" && perspectiveValue.startsWith("r") && perspectiveValue !== "raw"))
|
|
@@ -227,7 +227,8 @@ const validateApiPerspective = function(perspective) {
|
|
|
227
227
|
"Invalid API perspective string, expected `published`, `previewDrafts` or `raw`"
|
|
228
228
|
);
|
|
229
229
|
}
|
|
230
|
-
}
|
|
230
|
+
}
|
|
231
|
+
const initConfig = (config, prevConfig) => {
|
|
231
232
|
const specifiedConfig = {
|
|
232
233
|
...prevConfig,
|
|
233
234
|
...config,
|
|
@@ -268,6 +269,112 @@ const validateApiPerspective = function(perspective) {
|
|
|
268
269
|
const hostParts = newConfig.apiHost.split("://", 2), protocol = hostParts[0], host = hostParts[1], cdnHost = newConfig.isDefaultApi ? defaultCdnHost : host;
|
|
269
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;
|
|
270
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
|
+
}
|
|
271
378
|
function getSelection(sel) {
|
|
272
379
|
if (typeof sel == "string")
|
|
273
380
|
return { id: sel };
|
|
@@ -886,7 +993,18 @@ function optionsFromFile(opts, file) {
|
|
|
886
993
|
);
|
|
887
994
|
}
|
|
888
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), {});
|
|
889
|
-
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 = [
|
|
890
1008
|
"includePreviousRevision",
|
|
891
1009
|
"includeResult",
|
|
892
1010
|
"includeMutations",
|
|
@@ -899,68 +1017,23 @@ const pick = (obj, props) => props.reduce((selection, prop) => (typeof obj[prop]
|
|
|
899
1017
|
function _listen(query, params, opts = {}) {
|
|
900
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)}`;
|
|
901
1019
|
if (uri.length > MAX_URL_LENGTH)
|
|
902
|
-
return
|
|
903
|
-
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 = {};
|
|
904
1022
|
return (token || withCredentials) && (esOptions.withCredentials = !0), token && (esOptions.headers = {
|
|
905
1023
|
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";
|
|
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
|
+
);
|
|
964
1037
|
}
|
|
965
1038
|
const requiredApiVersion = "2021-03-26";
|
|
966
1039
|
class LiveClient {
|
|
@@ -996,75 +1069,53 @@ class LiveClient {
|
|
|
996
1069
|
);
|
|
997
1070
|
const path = _getDataUrl(this.#client, "live/events"), url = new URL(this.#client.getUrl(path, !1)), tag = _tag && requestTagPrefix ? [requestTagPrefix, _tag].join(".") : _tag;
|
|
998
1071
|
tag && url.searchParams.set("tag", tag), includeDrafts && url.searchParams.set("includeDrafts", "true");
|
|
999
|
-
const
|
|
1000
|
-
|
|
1072
|
+
const esOptions = {};
|
|
1073
|
+
includeDrafts && token && (esOptions.headers = {
|
|
1001
1074
|
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 });
|
|
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 };
|
|
1040
1090
|
}
|
|
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
|
-
});
|
|
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);
|
|
1059
1105
|
}
|
|
1060
1106
|
}
|
|
1061
|
-
function
|
|
1062
|
-
|
|
1063
|
-
const
|
|
1064
|
-
return {
|
|
1065
|
-
|
|
1066
|
-
|
|
1067
|
-
|
|
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
|
+
});
|
|
1068
1119
|
}
|
|
1069
1120
|
class ObservableDatasetsClient {
|
|
1070
1121
|
#client;
|
|
@@ -1598,8 +1649,13 @@ Object.defineProperty(exports, "unstable__environment", {
|
|
|
1598
1649
|
});
|
|
1599
1650
|
exports.BasePatch = BasePatch;
|
|
1600
1651
|
exports.BaseTransaction = BaseTransaction;
|
|
1652
|
+
exports.ChannelError = ChannelError;
|
|
1601
1653
|
exports.ClientError = ClientError;
|
|
1654
|
+
exports.ConnectionFailedError = ConnectionFailedError;
|
|
1602
1655
|
exports.CorsOriginError = CorsOriginError;
|
|
1656
|
+
exports.DisconnectError = DisconnectError;
|
|
1657
|
+
exports.MessageError = MessageError;
|
|
1658
|
+
exports.MessageParseError = MessageParseError;
|
|
1603
1659
|
exports.ObservablePatch = ObservablePatch;
|
|
1604
1660
|
exports.ObservableSanityClient = ObservableSanityClient;
|
|
1605
1661
|
exports.ObservableTransaction = ObservableTransaction;
|
|
@@ -1607,6 +1663,7 @@ exports.Patch = Patch;
|
|
|
1607
1663
|
exports.SanityClient = SanityClient;
|
|
1608
1664
|
exports.ServerError = ServerError;
|
|
1609
1665
|
exports.Transaction = Transaction;
|
|
1666
|
+
exports.connectEventSource = connectEventSource;
|
|
1610
1667
|
exports.createClient = createClient;
|
|
1611
1668
|
exports.default = deprecatedCreateClient;
|
|
1612
1669
|
exports.requester = requester;
|