@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.
@@ -188,14 +188,11 @@ function sliceWithEllipsis(str, max) {
188
188
  class CorsOriginError extends Error {
189
189
  projectId;
190
190
  addOriginUrl;
191
- constructor({ projectId: projectId2 }) {
192
- super("CorsOriginError"), this.name = "CorsOriginError", this.projectId = projectId2;
193
- const url = new URL(`https://sanity.io/manage/project/${projectId2}/api`);
194
- if (typeof location < "u") {
195
- const { origin } = location;
196
- 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}`;
197
- } else
198
- this.message = `The current origin is not allowed to connect to the Live Content API. Change your configuration here: ${url}`;
191
+ constructor({ projectId: projectId2, credentials } = {}) {
192
+ if (super("CorsOriginError"), this.name = "CorsOriginError", this.projectId = projectId2, projectId2 && typeof location < "u") {
193
+ const url = new URL(`https://sanity.io/manage/project/${projectId2}/api`), { origin } = location;
194
+ 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}`;
195
+ } 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.";
199
196
  }
200
197
  }
201
198
  const httpError = {
@@ -1514,19 +1511,14 @@ class LiveClient {
1514
1511
  "welcome",
1515
1512
  "reconnect",
1516
1513
  "goaway"
1517
- ]), checkCors = fetchObservable(url, {
1518
- method: "OPTIONS",
1519
- mode: "cors",
1520
- credentials: esOptions.withCredentials ? "include" : "omit",
1521
- headers: esOptions.headers
1522
- }).pipe(
1523
- rxjs.catchError(() => {
1524
- throw new CorsOriginError({ projectId: projectId2 });
1525
- })
1514
+ ]), checkCors = checkCorsObservable(
1515
+ new URL(this.#client.getUrl("/check/cors", !1)),
1516
+ projectId2,
1517
+ esOptions.withCredentials === !0
1526
1518
  ), observable = events.pipe(
1527
1519
  reconnectOnConnectionFailure(),
1528
1520
  rxjs.mergeMap((event) => event.type === "reconnect" ? checkCors.pipe(rxjs.mergeMap(() => rxjs.of(event))) : rxjs.of(event)),
1529
- rxjs.catchError((err) => checkCors.pipe(
1521
+ rxjs.catchError((err) => err instanceof CorsOriginError ? rxjs.throwError(() => err) : checkCors.pipe(
1530
1522
  rxjs.mergeMap(() => {
1531
1523
  throw err;
1532
1524
  })
@@ -1547,17 +1539,27 @@ class LiveClient {
1547
1539
  return eventsCache.set(key, observable), observable;
1548
1540
  }
1549
1541
  }
1550
- function fetchObservable(url, init) {
1542
+ function checkCorsObservable(url, projectId2, requireCredentials) {
1551
1543
  return new rxjs.Observable((observer) => {
1552
- const controller = new AbortController(), signal = controller.signal;
1553
- return fetch(url, { ...init, signal: controller.signal }).then(
1554
- (response) => {
1555
- observer.next(response), observer.complete();
1556
- },
1557
- (err) => {
1558
- signal.aborted || observer.error(err);
1544
+ const controller = new AbortController(), { signal } = controller;
1545
+ return fetch(url, { method: "GET", mode: "cors", credentials: "omit", signal }).then((response) => {
1546
+ if (!(signal.aborted || !response.ok))
1547
+ return response.json();
1548
+ }).then((body) => {
1549
+ if (!signal.aborted) {
1550
+ if (requireCredentials && body?.result?.withCredentials === !1) {
1551
+ observer.error(new CorsOriginError({ projectId: projectId2, credentials: !0 }));
1552
+ return;
1553
+ }
1554
+ if (body?.result?.allowed === !1) {
1555
+ observer.error(new CorsOriginError({ projectId: projectId2, credentials: requireCredentials }));
1556
+ return;
1557
+ }
1558
+ observer.next(), observer.complete();
1559
1559
  }
1560
- ), () => controller.abort();
1560
+ }).catch(() => {
1561
+ signal.aborted || observer.closed || (observer.next(), observer.complete());
1562
+ }), () => controller.abort();
1561
1563
  });
1562
1564
  }
1563
1565
  const eventsCache = /* @__PURE__ */ new Map();