@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.d.cts CHANGED
@@ -1039,9 +1039,9 @@ export declare interface ContentSourceMapValueMapping {
1039
1039
 
1040
1040
  /** @public */
1041
1041
  export declare class CorsOriginError extends Error {
1042
- projectId: string
1042
+ projectId?: string
1043
1043
  addOriginUrl?: URL
1044
- constructor({projectId}: {projectId: string})
1044
+ constructor({projectId, credentials}?: {projectId?: string; credentials?: boolean})
1045
1045
  }
1046
1046
 
1047
1047
  /**
package/dist/index.d.ts CHANGED
@@ -1039,9 +1039,9 @@ export declare interface ContentSourceMapValueMapping {
1039
1039
 
1040
1040
  /** @public */
1041
1041
  export declare class CorsOriginError extends Error {
1042
- projectId: string
1042
+ projectId?: string
1043
1043
  addOriginUrl?: URL
1044
- constructor({projectId}: {projectId: string})
1044
+ constructor({projectId, credentials}?: {projectId?: string; credentials?: boolean})
1045
1045
  }
1046
1046
 
1047
1047
  /**
package/dist/index.js CHANGED
@@ -176,14 +176,11 @@ function sliceWithEllipsis(str, max) {
176
176
  class CorsOriginError extends Error {
177
177
  projectId;
178
178
  addOriginUrl;
179
- constructor({ projectId }) {
180
- super("CorsOriginError"), this.name = "CorsOriginError", this.projectId = projectId;
181
- const url = new URL(`https://sanity.io/manage/project/${projectId}/api`);
182
- if (typeof location < "u") {
183
- const { origin } = location;
184
- 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}`;
185
- } else
186
- this.message = `The current origin is not allowed to connect to the Live Content API. Change your configuration here: ${url}`;
179
+ constructor({ projectId, credentials } = {}) {
180
+ if (super("CorsOriginError"), this.name = "CorsOriginError", this.projectId = projectId, projectId && typeof location < "u") {
181
+ const url = new URL(`https://sanity.io/manage/project/${projectId}/api`), { origin } = location;
182
+ 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}`;
183
+ } 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.";
187
184
  }
188
185
  }
189
186
  const httpError = {
@@ -1316,19 +1313,14 @@ class LiveClient {
1316
1313
  "welcome",
1317
1314
  "reconnect",
1318
1315
  "goaway"
1319
- ]), checkCors = fetchObservable(url, {
1320
- method: "OPTIONS",
1321
- mode: "cors",
1322
- credentials: esOptions.withCredentials ? "include" : "omit",
1323
- headers: esOptions.headers
1324
- }).pipe(
1325
- catchError(() => {
1326
- throw new CorsOriginError({ projectId });
1327
- })
1316
+ ]), checkCors = checkCorsObservable(
1317
+ new URL(this.#client.getUrl("/check/cors", !1)),
1318
+ projectId,
1319
+ esOptions.withCredentials === !0
1328
1320
  ), observable2 = events.pipe(
1329
1321
  reconnectOnConnectionFailure(),
1330
1322
  mergeMap((event) => event.type === "reconnect" ? checkCors.pipe(mergeMap(() => of(event))) : of(event)),
1331
- catchError((err) => checkCors.pipe(
1323
+ catchError((err) => err instanceof CorsOriginError ? throwError(() => err) : checkCors.pipe(
1332
1324
  mergeMap(() => {
1333
1325
  throw err;
1334
1326
  })
@@ -1349,17 +1341,27 @@ class LiveClient {
1349
1341
  return eventsCache.set(key, observable2), observable2;
1350
1342
  }
1351
1343
  }
1352
- function fetchObservable(url, init) {
1344
+ function checkCorsObservable(url, projectId, requireCredentials) {
1353
1345
  return new Observable((observer) => {
1354
- const controller = new AbortController(), signal = controller.signal;
1355
- return fetch(url, { ...init, signal: controller.signal }).then(
1356
- (response) => {
1357
- observer.next(response), observer.complete();
1358
- },
1359
- (err) => {
1360
- signal.aborted || observer.error(err);
1346
+ const controller = new AbortController(), { signal } = controller;
1347
+ return fetch(url, { method: "GET", mode: "cors", credentials: "omit", signal }).then((response) => {
1348
+ if (!(signal.aborted || !response.ok))
1349
+ return response.json();
1350
+ }).then((body) => {
1351
+ if (!signal.aborted) {
1352
+ if (requireCredentials && body?.result?.withCredentials === !1) {
1353
+ observer.error(new CorsOriginError({ projectId, credentials: !0 }));
1354
+ return;
1355
+ }
1356
+ if (body?.result?.allowed === !1) {
1357
+ observer.error(new CorsOriginError({ projectId, credentials: requireCredentials }));
1358
+ return;
1359
+ }
1360
+ observer.next(), observer.complete();
1361
1361
  }
1362
- ), () => controller.abort();
1362
+ }).catch(() => {
1363
+ signal.aborted || observer.closed || (observer.next(), observer.complete());
1364
+ }), () => controller.abort();
1363
1365
  });
1364
1366
  }
1365
1367
  const eventsCache = /* @__PURE__ */ new Map();
@@ -2786,7 +2788,7 @@ function defineDeprecatedCreateClient(createClient2) {
2786
2788
  return printNoDefaultExport(), createClient2(config);
2787
2789
  };
2788
2790
  }
2789
- var name = "@sanity/client", version = "7.22.0";
2791
+ var name = "@sanity/client", version = "7.22.1";
2790
2792
  const middleware = [
2791
2793
  debug({ verbose: !0, namespace: "sanity:client" }),
2792
2794
  headers({ "User-Agent": `${name} ${version}` }),