@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.
package/dist/index.cjs CHANGED
@@ -188,14 +188,11 @@ function sliceWithEllipsis(str, max) {
188
188
  class CorsOriginError extends Error {
189
189
  projectId;
190
190
  addOriginUrl;
191
- constructor({ projectId }) {
192
- super("CorsOriginError"), this.name = "CorsOriginError", this.projectId = projectId;
193
- const url = new URL(`https://sanity.io/manage/project/${projectId}/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, credentials } = {}) {
192
+ if (super("CorsOriginError"), this.name = "CorsOriginError", this.projectId = projectId, projectId && typeof location < "u") {
193
+ const url = new URL(`https://sanity.io/manage/project/${projectId}/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 projectId ? this.message = `The current origin is not allowed to connect to the Live Content API. Change your configuration here: https://sanity.io/manage/project/${projectId}/api` : this.message = "The current origin is not allowed to connect to the Live Content API.";
199
196
  }
200
197
  }
201
198
  const httpError = {
@@ -1330,19 +1327,14 @@ class LiveClient {
1330
1327
  "welcome",
1331
1328
  "reconnect",
1332
1329
  "goaway"
1333
- ]), checkCors = fetchObservable(url, {
1334
- method: "OPTIONS",
1335
- mode: "cors",
1336
- credentials: esOptions.withCredentials ? "include" : "omit",
1337
- headers: esOptions.headers
1338
- }).pipe(
1339
- rxjs.catchError(() => {
1340
- throw new CorsOriginError({ projectId });
1341
- })
1330
+ ]), checkCors = checkCorsObservable(
1331
+ new URL(this.#client.getUrl("/check/cors", !1)),
1332
+ projectId,
1333
+ esOptions.withCredentials === !0
1342
1334
  ), observable = events.pipe(
1343
1335
  reconnectOnConnectionFailure(),
1344
1336
  rxjs.mergeMap((event) => event.type === "reconnect" ? checkCors.pipe(rxjs.mergeMap(() => rxjs.of(event))) : rxjs.of(event)),
1345
- rxjs.catchError((err) => checkCors.pipe(
1337
+ rxjs.catchError((err) => err instanceof CorsOriginError ? rxjs.throwError(() => err) : checkCors.pipe(
1346
1338
  rxjs.mergeMap(() => {
1347
1339
  throw err;
1348
1340
  })
@@ -1363,17 +1355,27 @@ class LiveClient {
1363
1355
  return eventsCache.set(key, observable), observable;
1364
1356
  }
1365
1357
  }
1366
- function fetchObservable(url, init) {
1358
+ function checkCorsObservable(url, projectId, requireCredentials) {
1367
1359
  return new rxjs.Observable((observer) => {
1368
- const controller = new AbortController(), signal = controller.signal;
1369
- return fetch(url, { ...init, signal: controller.signal }).then(
1370
- (response) => {
1371
- observer.next(response), observer.complete();
1372
- },
1373
- (err) => {
1374
- signal.aborted || observer.error(err);
1360
+ const controller = new AbortController(), { signal } = controller;
1361
+ return fetch(url, { method: "GET", mode: "cors", credentials: "omit", signal }).then((response) => {
1362
+ if (!(signal.aborted || !response.ok))
1363
+ return response.json();
1364
+ }).then((body) => {
1365
+ if (!signal.aborted) {
1366
+ if (requireCredentials && body?.result?.withCredentials === !1) {
1367
+ observer.error(new CorsOriginError({ projectId, credentials: !0 }));
1368
+ return;
1369
+ }
1370
+ if (body?.result?.allowed === !1) {
1371
+ observer.error(new CorsOriginError({ projectId, credentials: requireCredentials }));
1372
+ return;
1373
+ }
1374
+ observer.next(), observer.complete();
1375
1375
  }
1376
- ), () => controller.abort();
1376
+ }).catch(() => {
1377
+ signal.aborted || observer.closed || (observer.next(), observer.complete());
1378
+ }), () => controller.abort();
1377
1379
  });
1378
1380
  }
1379
1381
  const eventsCache = /* @__PURE__ */ new Map();
@@ -2800,7 +2802,7 @@ function defineDeprecatedCreateClient(createClient2) {
2800
2802
  return config.printNoDefaultExport(), createClient2(config$1);
2801
2803
  };
2802
2804
  }
2803
- var name = "@sanity/client", version = "7.22.0";
2805
+ var name = "@sanity/client", version = "7.22.1";
2804
2806
  const middleware = [
2805
2807
  middleware$1.debug({ verbose: !0, namespace: "sanity:client" }),
2806
2808
  middleware$1.headers({ "User-Agent": `${name} ${version}` }),