@sanity/client 6.28.4-beta.0 → 6.28.4-resources.4

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,
@@ -1065,7 +1116,7 @@ class DatasetsClient {
1065
1116
  * @param options - Options for the dataset
1066
1117
  */
1067
1118
  create(name2, options) {
1068
- return rxjs.lastValueFrom(
1119
+ return config.resourceGuard("dataset", this.#client.config()), rxjs.lastValueFrom(
1069
1120
  _modify(this.#client, this.#httpRequest, "PUT", name2, options)
1070
1121
  );
1071
1122
  }
@@ -1076,7 +1127,7 @@ class DatasetsClient {
1076
1127
  * @param options - New options for the dataset
1077
1128
  */
1078
1129
  edit(name2, options) {
1079
- return rxjs.lastValueFrom(
1130
+ return config.resourceGuard("dataset", this.#client.config()), rxjs.lastValueFrom(
1080
1131
  _modify(this.#client, this.#httpRequest, "PATCH", name2, options)
1081
1132
  );
1082
1133
  }
@@ -1086,19 +1137,19 @@ class DatasetsClient {
1086
1137
  * @param name - Name of the dataset to delete
1087
1138
  */
1088
1139
  delete(name2) {
1089
- return rxjs.lastValueFrom(_modify(this.#client, this.#httpRequest, "DELETE", name2));
1140
+ return config.resourceGuard("dataset", this.#client.config()), rxjs.lastValueFrom(_modify(this.#client, this.#httpRequest, "DELETE", name2));
1090
1141
  }
1091
1142
  /**
1092
1143
  * Fetch a list of datasets for the configured project
1093
1144
  */
1094
1145
  list() {
1095
- return rxjs.lastValueFrom(
1146
+ return config.resourceGuard("dataset", this.#client.config()), rxjs.lastValueFrom(
1096
1147
  _request(this.#client, this.#httpRequest, { uri: "/datasets", tag: null })
1097
1148
  );
1098
1149
  }
1099
1150
  }
1100
1151
  function _modify(client, httpRequest, method, name2, options) {
1101
- return config.dataset(name2), _request(client, httpRequest, {
1152
+ return config.resourceGuard("dataset", client.config()), config.dataset(name2), _request(client, httpRequest, {
1102
1153
  method,
1103
1154
  uri: `/datasets/${name2}`,
1104
1155
  body: options,
@@ -1112,6 +1163,7 @@ class ObservableProjectsClient {
1112
1163
  this.#client = client, this.#httpRequest = httpRequest;
1113
1164
  }
1114
1165
  list(options) {
1166
+ config.resourceGuard("projects", this.#client.config());
1115
1167
  const uri = options?.includeMembers === !1 ? "/projects?includeMembers=false" : "/projects";
1116
1168
  return _request(this.#client, this.#httpRequest, { uri });
1117
1169
  }
@@ -1121,7 +1173,7 @@ class ObservableProjectsClient {
1121
1173
  * @param projectId - ID of the project to fetch
1122
1174
  */
1123
1175
  getById(projectId) {
1124
- return _request(this.#client, this.#httpRequest, { uri: `/projects/${projectId}` });
1176
+ return config.resourceGuard("projects", this.#client.config()), _request(this.#client, this.#httpRequest, { uri: `/projects/${projectId}` });
1125
1177
  }
1126
1178
  }
1127
1179
  class ProjectsClient {
@@ -1131,6 +1183,7 @@ class ProjectsClient {
1131
1183
  this.#client = client, this.#httpRequest = httpRequest;
1132
1184
  }
1133
1185
  list(options) {
1186
+ config.resourceGuard("projects", this.#client.config());
1134
1187
  const uri = options?.includeMembers === !1 ? "/projects?includeMembers=false" : "/projects";
1135
1188
  return rxjs.lastValueFrom(_request(this.#client, this.#httpRequest, { uri }));
1136
1189
  }
@@ -1140,7 +1193,7 @@ class ProjectsClient {
1140
1193
  * @param projectId - ID of the project to fetch
1141
1194
  */
1142
1195
  getById(projectId) {
1143
- return rxjs.lastValueFrom(
1196
+ return config.resourceGuard("projects", this.#client.config()), rxjs.lastValueFrom(
1144
1197
  _request(this.#client, this.#httpRequest, { uri: `/projects/${projectId}` })
1145
1198
  );
1146
1199
  }
@@ -1528,7 +1581,7 @@ function defineDeprecatedCreateClient(createClient2) {
1528
1581
  return config.printNoDefaultExport(), createClient2(config$1);
1529
1582
  };
1530
1583
  }
1531
- var name = "@sanity/client", version = "6.28.4-beta.0";
1584
+ var name = "@sanity/client", version = "6.28.4-resources.4";
1532
1585
  const middleware = [
1533
1586
  middleware$1.debug({ verbose: !0, namespace: "sanity:client" }),
1534
1587
  middleware$1.headers({ "User-Agent": `${name} ${version}` }),