@sanity/client 6.27.2 → 6.27.3-canary.1

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.
@@ -405,7 +405,7 @@ export declare type ClientPerspective =
405
405
  | 'published'
406
406
  | 'drafts'
407
407
  | 'raw'
408
- | ('published' | 'drafts' | ReleaseId)[]
408
+ | StackablePerspective[]
409
409
 
410
410
  /** @public */
411
411
  export declare type ClientReturn<
@@ -2219,7 +2219,10 @@ export declare type ReconnectEvent = {
2219
2219
  type: 'reconnect'
2220
2220
  }
2221
2221
 
2222
- /** @public */
2222
+ /**
2223
+ * @public
2224
+ * @deprecated – The `r`-prefix is not required, use `string` instead
2225
+ */
2223
2226
  export declare type ReleaseId = `r${string}`
2224
2227
 
2225
2228
  /**
@@ -2994,6 +2997,9 @@ export declare interface SingleMutationResult {
2994
2997
  }[]
2995
2998
  }
2996
2999
 
3000
+ /** @public */
3001
+ export declare type StackablePerspective = ('published' | 'drafts' | string) & {}
3002
+
2997
3003
  /** @public */
2998
3004
  export declare interface StegaConfig {
2999
3005
  /**
@@ -405,7 +405,7 @@ export declare type ClientPerspective =
405
405
  | 'published'
406
406
  | 'drafts'
407
407
  | 'raw'
408
- | ('published' | 'drafts' | ReleaseId)[]
408
+ | StackablePerspective[]
409
409
 
410
410
  /** @public */
411
411
  export declare type ClientReturn<
@@ -2219,7 +2219,10 @@ export declare type ReconnectEvent = {
2219
2219
  type: 'reconnect'
2220
2220
  }
2221
2221
 
2222
- /** @public */
2222
+ /**
2223
+ * @public
2224
+ * @deprecated – The `r`-prefix is not required, use `string` instead
2225
+ */
2223
2226
  export declare type ReleaseId = `r${string}`
2224
2227
 
2225
2228
  /**
@@ -2994,6 +2997,9 @@ export declare interface SingleMutationResult {
2994
2997
  }[]
2995
2998
  }
2996
2999
 
3000
+ /** @public */
3001
+ export declare type StackablePerspective = ('published' | 'drafts' | string) & {}
3002
+
2997
3003
  /** @public */
2998
3004
  export declare interface StegaConfig {
2999
3005
  /**
@@ -1,9 +1,9 @@
1
1
  import { getIt } from "get-it";
2
2
  import { adapter, environment } from "get-it";
3
3
  import { retry, jsonRequest, jsonResponse, progress, observable } from "get-it/middleware";
4
- import { Observable, defer, isObservable, of, mergeMap, from, lastValueFrom, shareReplay, catchError, concat, timer, throwError, EMPTY } from "rxjs";
4
+ import { Observable, defer, isObservable, of, mergeMap, from, lastValueFrom, shareReplay, catchError, concat, timer, throwError, tap, finalize, share, merge, EMPTY } from "rxjs";
5
5
  import { stegaClean } from "./_chunks-es/stegaClean.js";
6
- import { combineLatestWith, map, filter } from "rxjs/operators";
6
+ import { combineLatestWith, map, filter, finalize as finalize$1 } from "rxjs/operators";
7
7
  class ClientError extends Error {
8
8
  response;
9
9
  statusCode = 400;
@@ -196,25 +196,20 @@ function validateApiVersion(apiVersion) {
196
196
  if (!(/^\d{4}-\d{2}-\d{2}$/.test(apiVersion) && apiDate instanceof Date && apiDate.getTime() > 0))
197
197
  throw new Error("Invalid API version string, expected `1` or date in format `YYYY-MM-DD`");
198
198
  }
199
+ const VALID_PERSPECTIVE = /^[a-z0-9_]+$/i;
199
200
  function validateApiPerspective(perspective) {
200
- if (Array.isArray(perspective)) {
201
- for (const perspectiveValue of perspective)
202
- if (perspectiveValue !== "published" && perspectiveValue !== "drafts" && !(typeof perspectiveValue == "string" && perspectiveValue.startsWith("r") && perspectiveValue !== "raw"))
203
- throw new TypeError(
204
- "Invalid API perspective value, expected `published`, `drafts` or a valid release identifier string"
205
- );
206
- return;
207
- }
208
- switch (perspective) {
209
- case "previewDrafts":
210
- case "drafts":
211
- case "published":
212
- case "raw":
213
- return;
214
- default:
215
- throw new TypeError(
216
- "Invalid API perspective string, expected `published`, `previewDrafts` or `raw`"
217
- );
201
+ if (Array.isArray(perspective) && perspective.includes("raw"))
202
+ throw new TypeError(
203
+ 'Invalid API perspective value: "raw". The raw-perspective can not be combined with other perspectives'
204
+ );
205
+ const invalid = (Array.isArray(perspective) ? perspective : [perspective]).filter(
206
+ (perspectiveName) => typeof perspectiveName != "string" || !VALID_PERSPECTIVE.test(perspectiveName)
207
+ );
208
+ if (invalid.length > 0) {
209
+ const formatted = invalid.map((v) => JSON.stringify(v));
210
+ throw new TypeError(
211
+ `Invalid API perspective value${invalid.length === 1 ? "" : "s"}: ${formatted.join(", ")}, expected \`published\`, \`drafts\`, \`raw\` or a release identifier string`
212
+ );
218
213
  }
219
214
  }
220
215
  const initConfig = (config, prevConfig) => {
@@ -507,9 +502,9 @@ class BasePatch {
507
502
  reset() {
508
503
  return this.operations = {}, this;
509
504
  }
510
- _assign(op, props, merge = !0) {
505
+ _assign(op, props, merge2 = !0) {
511
506
  return validateObject(op, props), this.operations = Object.assign({}, this.operations, {
512
- [op]: Object.assign({}, merge && this.operations[op] || {}, props)
507
+ [op]: Object.assign({}, merge2 && this.operations[op] || {}, props)
513
508
  }), this;
514
509
  }
515
510
  _set(op, props) {
@@ -1026,6 +1021,28 @@ function _listen(query, params, opts = {}) {
1026
1021
  )
1027
1022
  );
1028
1023
  }
1024
+ function shareReplayLatest(configOrPredicate, config) {
1025
+ return _shareReplayLatest(
1026
+ typeof configOrPredicate == "function" ? { predicate: configOrPredicate, ...config } : configOrPredicate
1027
+ );
1028
+ }
1029
+ function _shareReplayLatest(config) {
1030
+ return (source) => {
1031
+ let latest, emitted = !1;
1032
+ const { predicate, ...shareConfig } = config, wrapped = source.pipe(
1033
+ tap((value) => {
1034
+ config.predicate(value) && (emitted = !0, latest = value);
1035
+ }),
1036
+ finalize(() => {
1037
+ emitted = !1, latest = void 0;
1038
+ }),
1039
+ share(shareConfig)
1040
+ ), emitLatest = new Observable((subscriber) => {
1041
+ emitted && subscriber.next(latest), subscriber.complete();
1042
+ });
1043
+ return merge(wrapped, emitLatest);
1044
+ };
1045
+ }
1029
1046
  const requiredApiVersion = "2021-03-25";
1030
1047
  class LiveClient {
1031
1048
  #client;
@@ -1060,6 +1077,9 @@ class LiveClient {
1060
1077
  includeDrafts && token && (esOptions.headers = {
1061
1078
  Authorization: `Bearer ${token}`
1062
1079
  }), includeDrafts && withCredentials && (esOptions.withCredentials = !0);
1080
+ const key = `${url.href}::${JSON.stringify(esOptions)}`, existing = eventsCache.get(key);
1081
+ if (existing)
1082
+ return existing;
1063
1083
  const events = connectEventSource(() => (
1064
1084
  // use polyfill if there is no global EventSource or if we need to set headers
1065
1085
  (typeof EventSource > "u" || esOptions.headers ? eventSourcePolyfill : of(EventSource)).pipe(map((EventSource2) => new EventSource2(url.href, esOptions)))
@@ -1087,8 +1107,13 @@ class LiveClient {
1087
1107
  catchError(() => {
1088
1108
  throw new CorsOriginError({ projectId: projectId2 });
1089
1109
  })
1110
+ ), observable2 = concat(checkCors, events).pipe(
1111
+ finalize$1(() => eventsCache.delete(key)),
1112
+ shareReplayLatest({
1113
+ predicate: (event) => event.type === "welcome"
1114
+ })
1090
1115
  );
1091
- return concat(checkCors, events);
1116
+ return eventsCache.set(key, observable2), observable2;
1092
1117
  }
1093
1118
  }
1094
1119
  function fetchObservable(url, init) {
@@ -1104,6 +1129,7 @@ function fetchObservable(url, init) {
1104
1129
  ), () => controller.abort();
1105
1130
  });
1106
1131
  }
1132
+ const eventsCache = /* @__PURE__ */ new Map();
1107
1133
  class ObservableDatasetsClient {
1108
1134
  #client;
1109
1135
  #httpRequest;