@sanity/client 6.22.3-canary.0 → 6.22.3

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.
@@ -484,6 +484,13 @@ export declare interface ContentSourceMapValueMapping {
484
484
  source: ContentSourceMapSource
485
485
  }
486
486
 
487
+ /** @public */
488
+ export declare class CorsOriginError extends Error {
489
+ projectId: string
490
+ addOriginUrl?: URL
491
+ constructor({projectId}: {projectId: string})
492
+ }
493
+
487
494
  /**
488
495
  * Creates a new draft document. The published version of the document must not already exist.
489
496
  * If the draft version of the document already exists the action will fail by default, but
@@ -484,6 +484,13 @@ export declare interface ContentSourceMapValueMapping {
484
484
  source: ContentSourceMapSource
485
485
  }
486
486
 
487
+ /** @public */
488
+ export declare class CorsOriginError extends Error {
489
+ projectId: string
490
+ addOriginUrl?: URL
491
+ constructor({projectId}: {projectId: string})
492
+ }
493
+
487
494
  /**
488
495
  * Creates a new draft document. The published version of the document must not already exist.
489
496
  * If the draft version of the document already exists the action will fail by default, but
@@ -60,6 +60,19 @@ function httpErrorMessage(res) {
60
60
  function stringifyBody(body, res) {
61
61
  return (res.headers["content-type"] || "").toLowerCase().indexOf("application/json") !== -1 ? JSON.stringify(body, null, 2) : body;
62
62
  }
63
+ class CorsOriginError extends Error {
64
+ projectId;
65
+ addOriginUrl;
66
+ constructor({ projectId: projectId2 }) {
67
+ super("CorsOriginError"), this.name = "CorsOriginError", this.projectId = projectId2;
68
+ const url = new URL(`https://sanity.io/manage/project/${projectId2}/api`);
69
+ if (typeof location < "u") {
70
+ const { origin } = location;
71
+ 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}`;
72
+ } else
73
+ this.message = `The current origin is not allowed to connect to the Live Content API. Change your configuration here: ${url}`;
74
+ }
75
+ }
63
76
  const httpError = {
64
77
  onResponse: (res) => {
65
78
  if (res.statusCode >= 500)
@@ -931,6 +944,7 @@ class LiveClient {
931
944
  tag: _tag
932
945
  } = {}) {
933
946
  const {
947
+ projectId: projectId2,
934
948
  apiVersion: _apiVersion,
935
949
  token,
936
950
  withCredentials,
@@ -981,6 +995,17 @@ class LiveClient {
981
995
  const EventSourceImplementation = typeof EventSource > "u" || esOptions.headers || esOptions.withCredentials ? (await import("@sanity/eventsource")).default : EventSource;
982
996
  if (unsubscribed)
983
997
  return;
998
+ try {
999
+ if (await fetch(url, {
1000
+ method: "OPTIONS",
1001
+ mode: "cors",
1002
+ credentials: esOptions.withCredentials ? "include" : "omit",
1003
+ headers: esOptions.headers
1004
+ }), unsubscribed)
1005
+ return;
1006
+ } catch {
1007
+ throw new CorsOriginError({ projectId: projectId2 });
1008
+ }
984
1009
  const evs = new EventSourceImplementation(url.toString(), esOptions);
985
1010
  evs.addEventListener("error", onError);
986
1011
  for (const type of listenFor)
@@ -1531,6 +1556,7 @@ export {
1531
1556
  BasePatch,
1532
1557
  BaseTransaction,
1533
1558
  ClientError,
1559
+ CorsOriginError,
1534
1560
  ObservablePatch,
1535
1561
  ObservableSanityClient,
1536
1562
  ObservableTransaction,