@sanity/client 7.22.0 → 7.22.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.
@@ -1046,9 +1046,9 @@ export declare interface ContentSourceMapValueMapping {
1046
1046
 
1047
1047
  /** @public */
1048
1048
  export declare class CorsOriginError extends Error {
1049
- projectId: string
1049
+ projectId?: string
1050
1050
  addOriginUrl?: URL
1051
- constructor({projectId}: {projectId: string})
1051
+ constructor({projectId, credentials}?: {projectId?: string; credentials?: boolean})
1052
1052
  }
1053
1053
 
1054
1054
  /**
@@ -1046,9 +1046,9 @@ export declare interface ContentSourceMapValueMapping {
1046
1046
 
1047
1047
  /** @public */
1048
1048
  export declare class CorsOriginError extends Error {
1049
- projectId: string
1049
+ projectId?: string
1050
1050
  addOriginUrl?: URL
1051
- constructor({projectId}: {projectId: string})
1051
+ constructor({projectId, credentials}?: {projectId?: string; credentials?: boolean})
1052
1052
  }
1053
1053
 
1054
1054
  /**
@@ -174,14 +174,11 @@ function sliceWithEllipsis(str, max) {
174
174
  class CorsOriginError extends Error {
175
175
  projectId;
176
176
  addOriginUrl;
177
- constructor({ projectId: projectId2 }) {
178
- super("CorsOriginError"), this.name = "CorsOriginError", this.projectId = projectId2;
179
- const url = new URL(`https://sanity.io/manage/project/${projectId2}/api`);
180
- if (typeof location < "u") {
181
- const { origin } = location;
182
- url.searchParams.set("cors", "add"), url.searchParams.set("origin", origin), this.addOriginUrl = url, this.message = `The current origin is not allowed to connect to the Live Content API. Add it here: ${url}`;
183
- } else
184
- this.message = `The current origin is not allowed to connect to the Live Content API. Change your configuration here: ${url}`;
177
+ constructor({ projectId: projectId2, credentials } = {}) {
178
+ if (super("CorsOriginError"), this.name = "CorsOriginError", this.projectId = projectId2, projectId2 && typeof location < "u") {
179
+ const url = new URL(`https://sanity.io/manage/project/${projectId2}/api`), { origin } = location;
180
+ url.searchParams.set("cors", "add"), url.searchParams.set("origin", origin), credentials && url.searchParams.set("credentials", ""), this.addOriginUrl = url, this.message = `The current origin is not allowed to connect to the Live Content API. Add it here: ${url}`;
181
+ } else projectId2 ? this.message = `The current origin is not allowed to connect to the Live Content API. Change your configuration here: https://sanity.io/manage/project/${projectId2}/api` : this.message = "The current origin is not allowed to connect to the Live Content API.";
185
182
  }
186
183
  }
187
184
  const httpError = {
@@ -1498,19 +1495,14 @@ class LiveClient {
1498
1495
  "welcome",
1499
1496
  "reconnect",
1500
1497
  "goaway"
1501
- ]), checkCors = fetchObservable(url, {
1502
- method: "OPTIONS",
1503
- mode: "cors",
1504
- credentials: esOptions.withCredentials ? "include" : "omit",
1505
- headers: esOptions.headers
1506
- }).pipe(
1507
- catchError(() => {
1508
- throw new CorsOriginError({ projectId: projectId2 });
1509
- })
1498
+ ]), checkCors = checkCorsObservable(
1499
+ new URL(this.#client.getUrl("/check/cors", !1)),
1500
+ projectId2,
1501
+ esOptions.withCredentials === !0
1510
1502
  ), observable2 = events.pipe(
1511
1503
  reconnectOnConnectionFailure(),
1512
1504
  mergeMap((event) => event.type === "reconnect" ? checkCors.pipe(mergeMap(() => of(event))) : of(event)),
1513
- catchError((err) => checkCors.pipe(
1505
+ catchError((err) => err instanceof CorsOriginError ? throwError(() => err) : checkCors.pipe(
1514
1506
  mergeMap(() => {
1515
1507
  throw err;
1516
1508
  })
@@ -1531,17 +1523,27 @@ class LiveClient {
1531
1523
  return eventsCache.set(key, observable2), observable2;
1532
1524
  }
1533
1525
  }
1534
- function fetchObservable(url, init) {
1526
+ function checkCorsObservable(url, projectId2, requireCredentials) {
1535
1527
  return new Observable((observer) => {
1536
- const controller = new AbortController(), signal = controller.signal;
1537
- return fetch(url, { ...init, signal: controller.signal }).then(
1538
- (response) => {
1539
- observer.next(response), observer.complete();
1540
- },
1541
- (err) => {
1542
- signal.aborted || observer.error(err);
1528
+ const controller = new AbortController(), { signal } = controller;
1529
+ return fetch(url, { method: "GET", mode: "cors", credentials: "omit", signal }).then((response) => {
1530
+ if (!(signal.aborted || !response.ok))
1531
+ return response.json();
1532
+ }).then((body) => {
1533
+ if (!signal.aborted) {
1534
+ if (requireCredentials && body?.result?.withCredentials === !1) {
1535
+ observer.error(new CorsOriginError({ projectId: projectId2, credentials: !0 }));
1536
+ return;
1537
+ }
1538
+ if (body?.result?.allowed === !1) {
1539
+ observer.error(new CorsOriginError({ projectId: projectId2, credentials: requireCredentials }));
1540
+ return;
1541
+ }
1542
+ observer.next(), observer.complete();
1543
1543
  }
1544
- ), () => controller.abort();
1544
+ }).catch(() => {
1545
+ signal.aborted || observer.closed || (observer.next(), observer.complete());
1546
+ }), () => controller.abort();
1545
1547
  });
1546
1548
  }
1547
1549
  const eventsCache = /* @__PURE__ */ new Map();