@sanity/client 6.28.4 → 6.28.5-goaway.0

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
@@ -123,8 +123,8 @@ function defineHttpRequest(envMiddleware) {
123
123
  }
124
124
  function shouldRetry(err, attempt, options) {
125
125
  if (options.maxRetries === 0) return !1;
126
- const isSafe = options.method === "GET" || options.method === "HEAD", isQuery = (options.uri || options.url).startsWith("/data/query"), isRetriableResponse = err.response && (err.response.statusCode === 429 || err.response.statusCode === 502 || err.response.statusCode === 503);
127
- return (isSafe || isQuery) && isRetriableResponse ? !0 : middleware$1.retry.shouldRetry(err, attempt, options);
126
+ const isSafe = options.method === "GET" || options.method === "HEAD", isQuery2 = (options.uri || options.url).startsWith("/data/query"), isRetriableResponse = err.response && (err.response.statusCode === 429 || err.response.statusCode === 502 || err.response.statusCode === 503);
127
+ return (isSafe || isQuery2) && isRetriableResponse ? !0 : middleware$1.retry.shouldRetry(err, attempt, options);
128
128
  }
129
129
  class ConnectionFailedError extends Error {
130
130
  name = "ConnectionFailedError";
@@ -694,7 +694,7 @@ function _action(client, httpRequest, actions, options) {
694
694
  );
695
695
  }
696
696
  function _dataRequest(client, httpRequest, endpoint, body, options = {}) {
697
- const isMutation = endpoint === "mutate", isAction = endpoint === "actions", isQuery = endpoint === "query", strQuery = isMutation || isAction ? "" : encodeQueryString(body), useGet = !isMutation && !isAction && strQuery.length < getQuerySizeLimit, stringQuery = useGet ? strQuery : "", returnFirst = options.returnFirst, { timeout, token, tag, headers, returnQuery, lastLiveEventId, cacheMode } = options, uri = _getDataUrl(client, endpoint, stringQuery), reqOptions = {
697
+ const isMutation = endpoint === "mutate", isAction = endpoint === "actions", isQuery2 = endpoint === "query", strQuery = isMutation || isAction ? "" : encodeQueryString(body), useGet = !isMutation && !isAction && strQuery.length < getQuerySizeLimit, stringQuery = useGet ? strQuery : "", returnFirst = options.returnFirst, { timeout, token, tag, headers, returnQuery, lastLiveEventId, cacheMode } = options, uri = _getDataUrl(client, endpoint, stringQuery), reqOptions = {
698
698
  method: useGet ? "GET" : "POST",
699
699
  uri,
700
700
  json: !0,
@@ -709,7 +709,7 @@ function _dataRequest(client, httpRequest, endpoint, body, options = {}) {
709
709
  resultSourceMap: options.resultSourceMap,
710
710
  lastLiveEventId: Array.isArray(lastLiveEventId) ? lastLiveEventId[0] : lastLiveEventId,
711
711
  cacheMode,
712
- canUseCdn: isQuery,
712
+ canUseCdn: isQuery2,
713
713
  signal: options.signal,
714
714
  fetch: options.fetch,
715
715
  useAbortSignal: options.useAbortSignal,
@@ -737,11 +737,12 @@ function _create(client, httpRequest, doc, op, options = {}) {
737
737
  const mutation = { [op]: doc }, opts = Object.assign({ returnFirst: !0, returnDocuments: !0 }, options);
738
738
  return _dataRequest(client, httpRequest, "mutate", { mutations: [mutation] }, opts);
739
739
  }
740
+ const hasDataConfig = (client) => client.config().dataset !== void 0 && client.config().projectId !== void 0 || client.config()["~experimental_resource"] !== void 0, isQuery = (client, uri) => hasDataConfig(client) && uri.startsWith(_getDataUrl(client, "query")), isMutate = (client, uri) => hasDataConfig(client) && uri.startsWith(_getDataUrl(client, "mutate")), isDoc = (client, uri) => hasDataConfig(client) && uri.startsWith(_getDataUrl(client, "doc", "")), isListener = (client, uri) => hasDataConfig(client) && uri.startsWith(_getDataUrl(client, "listen")), isHistory = (client, uri) => hasDataConfig(client) && uri.startsWith(_getDataUrl(client, "history", "")), isData = (client, uri) => uri.startsWith("/data/") || isQuery(client, uri) || isMutate(client, uri) || isDoc(client, uri) || isListener(client, uri) || isHistory(client, uri);
740
741
  function _requestObservable(client, httpRequest, options) {
741
- const uri = options.url || options.uri, config$1 = client.config(), canUseCdn = typeof options.canUseCdn > "u" ? ["GET", "HEAD"].indexOf(options.method || "GET") >= 0 && uri.indexOf("/data/") === 0 : options.canUseCdn;
742
+ const uri = options.url || options.uri, config$1 = client.config(), canUseCdn = typeof options.canUseCdn > "u" ? ["GET", "HEAD"].indexOf(options.method || "GET") >= 0 && isData(client, uri) : options.canUseCdn;
742
743
  let useCdn = (options.useCdn ?? config$1.useCdn) && canUseCdn;
743
744
  const tag = options.tag && config$1.requestTagPrefix ? [config$1.requestTagPrefix, options.tag].join(".") : options.tag || config$1.requestTagPrefix;
744
- if (tag && options.tag !== null && (options.query = { tag: config.requestTag(tag), ...options.query }), ["GET", "HEAD", "POST"].indexOf(options.method || "GET") >= 0 && uri.indexOf("/data/query/") === 0) {
745
+ if (tag && options.tag !== null && (options.query = { tag: config.requestTag(tag), ...options.query }), ["GET", "HEAD", "POST"].indexOf(options.method || "GET") >= 0 && isQuery(client, uri)) {
745
746
  const resultSourceMap = options.resultSourceMap ?? config$1.resultSourceMap;
746
747
  resultSourceMap !== void 0 && resultSourceMap !== !1 && (options.query = { resultSourceMap, ...options.query });
747
748
  const perspectiveOption = options.perspective || config$1.perspective;
@@ -768,8 +769,14 @@ function _request(client, httpRequest, options) {
768
769
  );
769
770
  }
770
771
  function _getDataUrl(client, operation, path) {
771
- const config$1 = client.config(), catalog = config.hasDataset(config$1), baseUri = `/${operation}/${catalog}`;
772
- return `/data${path ? `${baseUri}/${path}` : baseUri}`.replace(/\/($|\?)/, "$1");
772
+ const config$1 = client.config();
773
+ if (config$1["~experimental_resource"]) {
774
+ config.resourceConfig(config$1);
775
+ const resourceBase = resourceDataBase(config$1), uri2 = path !== void 0 ? `${operation}/${path}` : operation;
776
+ return `${resourceBase}/${uri2}`.replace(/\/($|\?)/, "$1");
777
+ }
778
+ const catalog = config.hasDataset(config$1), baseUri = `/${operation}/${catalog}`;
779
+ return `/data${path !== void 0 ? `${baseUri}/${path}` : baseUri}`.replace(/\/($|\?)/, "$1");
773
780
  }
774
781
  function _getUrl(client, uri, canUseCdn = !1) {
775
782
  const { url, cdnUrl } = client.config();
@@ -795,6 +802,27 @@ function _createAbortError(signal) {
795
802
  const error = new Error(signal?.reason ?? "The operation was aborted.");
796
803
  return error.name = "AbortError", error;
797
804
  }
805
+ const resourceDataBase = (config2) => {
806
+ if (!config2["~experimental_resource"])
807
+ throw new Error("`resource` must be provided to perform resource queries");
808
+ const { type, id } = config2["~experimental_resource"];
809
+ switch (type) {
810
+ case "dataset": {
811
+ const segments = id.split(".");
812
+ if (segments.length !== 2)
813
+ throw new Error('Dataset ID must be in the format "project.dataset"');
814
+ return `/projects/${segments[0]}/datasets/${segments[1]}`;
815
+ }
816
+ case "canvas":
817
+ return `/canvases/${id}`;
818
+ case "media-library":
819
+ return `/media-libraries/${id}`;
820
+ case "dashboard":
821
+ return `/dashboards/${id}`;
822
+ default:
823
+ throw new Error(`Unsupported resource type: ${type.toString()}`);
824
+ }
825
+ };
798
826
  class ObservableAssetsClient {
799
827
  #client;
800
828
  #httpRequest;
@@ -827,7 +855,7 @@ function _upload(client, httpRequest, assetType, body, opts = {}) {
827
855
  config.validateAssetType(assetType);
828
856
  let meta = opts.extract || void 0;
829
857
  meta && !meta.length && (meta = ["none"]);
830
- const dataset = config.hasDataset(client.config()), assetEndpoint = assetType === "image" ? "images" : "files", options = optionsFromFile(opts, body), { tag, label, title, description, creditLine, filename, source } = options, query = {
858
+ const config$1 = client.config(), options = optionsFromFile(opts, body), { tag, label, title, description, creditLine, filename, source } = options, query = {
831
859
  label,
832
860
  title,
833
861
  description,
@@ -839,12 +867,34 @@ function _upload(client, httpRequest, assetType, body, opts = {}) {
839
867
  tag,
840
868
  method: "POST",
841
869
  timeout: options.timeout || 0,
842
- uri: `/assets/${assetEndpoint}/${dataset}`,
870
+ uri: buildAssetUploadUrl(config$1, assetType),
843
871
  headers: options.contentType ? { "Content-Type": options.contentType } : {},
844
872
  query,
845
873
  body
846
874
  });
847
875
  }
876
+ function buildAssetUploadUrl(config$1, assetType) {
877
+ const assetTypeEndpoint = assetType === "image" ? "images" : "files";
878
+ if (config$1["~experimental_resource"]) {
879
+ const { type, id } = config$1["~experimental_resource"];
880
+ switch (type) {
881
+ case "dataset":
882
+ throw new Error(
883
+ "Assets are not supported for dataset resources, yet. Configure the client with `{projectId: <projectId>, dataset: <datasetId>}` instead."
884
+ );
885
+ case "canvas":
886
+ return `/canvases/${id}/assets/${assetTypeEndpoint}`;
887
+ case "media-library":
888
+ return `/media-libraries/${id}/upload`;
889
+ case "dashboard":
890
+ return `/dashboards/${id}/assets/${assetTypeEndpoint}`;
891
+ default:
892
+ throw new Error(`Unsupported resource type: ${type.toString()}`);
893
+ }
894
+ }
895
+ const dataset = config.hasDataset(config$1);
896
+ return `assets/${assetTypeEndpoint}/${dataset}`;
897
+ }
848
898
  function optionsFromFile(opts, file) {
849
899
  return typeof File > "u" || !(file instanceof File) ? opts : Object.assign(
850
900
  {
@@ -936,6 +986,7 @@ class LiveClient {
936
986
  includeDrafts = !1,
937
987
  tag: _tag
938
988
  } = {}) {
989
+ config.resourceGuard("live", this.#client.config());
939
990
  const {
940
991
  projectId,
941
992
  apiVersion: _apiVersion,
@@ -967,7 +1018,8 @@ class LiveClient {
967
1018
  "message",
968
1019
  "restart",
969
1020
  "welcome",
970
- "reconnect"
1021
+ "reconnect",
1022
+ "goaway"
971
1023
  ]).pipe(
972
1024
  reconnectOnConnectionFailure(),
973
1025
  operators.map((event) => {
@@ -1065,7 +1117,7 @@ class DatasetsClient {
1065
1117
  * @param options - Options for the dataset
1066
1118
  */
1067
1119
  create(name2, options) {
1068
- return rxjs.lastValueFrom(
1120
+ return config.resourceGuard("dataset", this.#client.config()), rxjs.lastValueFrom(
1069
1121
  _modify(this.#client, this.#httpRequest, "PUT", name2, options)
1070
1122
  );
1071
1123
  }
@@ -1076,7 +1128,7 @@ class DatasetsClient {
1076
1128
  * @param options - New options for the dataset
1077
1129
  */
1078
1130
  edit(name2, options) {
1079
- return rxjs.lastValueFrom(
1131
+ return config.resourceGuard("dataset", this.#client.config()), rxjs.lastValueFrom(
1080
1132
  _modify(this.#client, this.#httpRequest, "PATCH", name2, options)
1081
1133
  );
1082
1134
  }
@@ -1086,19 +1138,19 @@ class DatasetsClient {
1086
1138
  * @param name - Name of the dataset to delete
1087
1139
  */
1088
1140
  delete(name2) {
1089
- return rxjs.lastValueFrom(_modify(this.#client, this.#httpRequest, "DELETE", name2));
1141
+ return config.resourceGuard("dataset", this.#client.config()), rxjs.lastValueFrom(_modify(this.#client, this.#httpRequest, "DELETE", name2));
1090
1142
  }
1091
1143
  /**
1092
1144
  * Fetch a list of datasets for the configured project
1093
1145
  */
1094
1146
  list() {
1095
- return rxjs.lastValueFrom(
1147
+ return config.resourceGuard("dataset", this.#client.config()), rxjs.lastValueFrom(
1096
1148
  _request(this.#client, this.#httpRequest, { uri: "/datasets", tag: null })
1097
1149
  );
1098
1150
  }
1099
1151
  }
1100
1152
  function _modify(client, httpRequest, method, name2, options) {
1101
- return config.dataset(name2), _request(client, httpRequest, {
1153
+ return config.resourceGuard("dataset", client.config()), config.dataset(name2), _request(client, httpRequest, {
1102
1154
  method,
1103
1155
  uri: `/datasets/${name2}`,
1104
1156
  body: options,
@@ -1112,6 +1164,7 @@ class ObservableProjectsClient {
1112
1164
  this.#client = client, this.#httpRequest = httpRequest;
1113
1165
  }
1114
1166
  list(options) {
1167
+ config.resourceGuard("projects", this.#client.config());
1115
1168
  const uri = options?.includeMembers === !1 ? "/projects?includeMembers=false" : "/projects";
1116
1169
  return _request(this.#client, this.#httpRequest, { uri });
1117
1170
  }
@@ -1121,7 +1174,7 @@ class ObservableProjectsClient {
1121
1174
  * @param projectId - ID of the project to fetch
1122
1175
  */
1123
1176
  getById(projectId) {
1124
- return _request(this.#client, this.#httpRequest, { uri: `/projects/${projectId}` });
1177
+ return config.resourceGuard("projects", this.#client.config()), _request(this.#client, this.#httpRequest, { uri: `/projects/${projectId}` });
1125
1178
  }
1126
1179
  }
1127
1180
  class ProjectsClient {
@@ -1131,6 +1184,7 @@ class ProjectsClient {
1131
1184
  this.#client = client, this.#httpRequest = httpRequest;
1132
1185
  }
1133
1186
  list(options) {
1187
+ config.resourceGuard("projects", this.#client.config());
1134
1188
  const uri = options?.includeMembers === !1 ? "/projects?includeMembers=false" : "/projects";
1135
1189
  return rxjs.lastValueFrom(_request(this.#client, this.#httpRequest, { uri }));
1136
1190
  }
@@ -1140,7 +1194,7 @@ class ProjectsClient {
1140
1194
  * @param projectId - ID of the project to fetch
1141
1195
  */
1142
1196
  getById(projectId) {
1143
- return rxjs.lastValueFrom(
1197
+ return config.resourceGuard("projects", this.#client.config()), rxjs.lastValueFrom(
1144
1198
  _request(this.#client, this.#httpRequest, { uri: `/projects/${projectId}` })
1145
1199
  );
1146
1200
  }
@@ -1528,7 +1582,7 @@ function defineDeprecatedCreateClient(createClient2) {
1528
1582
  return config.printNoDefaultExport(), createClient2(config$1);
1529
1583
  };
1530
1584
  }
1531
- var name = "@sanity/client", version = "6.28.4";
1585
+ var name = "@sanity/client", version = "6.28.5-goaway.0";
1532
1586
  const middleware = [
1533
1587
  middleware$1.debug({ verbose: !0, namespace: "sanity:client" }),
1534
1588
  middleware$1.headers({ "User-Agent": `${name} ${version}` }),