@sanity/client 6.28.3-resources.1 → 6.28.3-resources.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.
package/dist/index.cjs CHANGED
@@ -737,21 +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 resourceDatasetRegex = /^\/projects\/([^/]+)\/datasets\/([^/]+)\/([^/^?]+)\/?/, isResourceDataEndpoint = (config2, uri, endpoint) => {
741
- if (!config2["~experimental_resource"])
742
- return !1;
743
- if (uri.startsWith(
744
- `/${config2["~experimental_resource"].type}/${config2["~experimental_resource"].id}/${endpoint}`
745
- ))
746
- return !0;
747
- const match = uri.match(resourceDatasetRegex);
748
- return match ? match[3] === endpoint : !1;
749
- }, isQuery = (config2, uri) => uri.startsWith("/data/query/") || isResourceDataEndpoint(config2, uri, "query"), isMutate = (config2, uri) => uri.startsWith("/data/mutate/") || isResourceDataEndpoint(config2, uri, "mutate"), isDoc = (config2, uri) => uri.startsWith("/data/doc/") || isResourceDataEndpoint(config2, uri, "doc"), isListener = (config2, uri) => uri.startsWith("/data/listen/") || isResourceDataEndpoint(config2, uri, "listen"), isHistory = (config2, uri) => uri.startsWith("/data/history/") || isResourceDataEndpoint(config2, uri, "history"), isData = (config2, uri) => uri.startsWith("/data/") || isQuery(config2, uri) || isMutate(config2, uri) || isDoc(config2, uri) || isListener(config2, uri) || isHistory(config2, uri);
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);
750
741
  function _requestObservable(client, httpRequest, options) {
751
- const uri = options.url || options.uri, config$1 = client.config(), canUseCdn = typeof options.canUseCdn > "u" ? ["GET", "HEAD"].indexOf(options.method || "GET") >= 0 && isData(config$1, uri) : 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;
752
743
  let useCdn = (options.useCdn ?? config$1.useCdn) && canUseCdn;
753
744
  const tag = options.tag && config$1.requestTagPrefix ? [config$1.requestTagPrefix, options.tag].join(".") : options.tag || config$1.requestTagPrefix;
754
- if (tag && options.tag !== null && (options.query = { tag: config.requestTag(tag), ...options.query }), ["GET", "HEAD", "POST"].indexOf(options.method || "GET") >= 0 && isQuery(config$1, uri)) {
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)) {
755
746
  const resultSourceMap = options.resultSourceMap ?? config$1.resultSourceMap;
756
747
  resultSourceMap !== void 0 && resultSourceMap !== !1 && (options.query = { resultSourceMap, ...options.query });
757
748
  const perspectiveOption = options.perspective || config$1.perspective;
@@ -780,11 +771,12 @@ function _request(client, httpRequest, options) {
780
771
  function _getDataUrl(client, operation, path) {
781
772
  const config$1 = client.config();
782
773
  if (config$1["~experimental_resource"]) {
783
- const resourceBase = config.resourceBase(config$1), uri2 = path ? `${operation}/${path}` : operation;
774
+ config.resourceConfig(config$1);
775
+ const resourceBase = resourceDataBase(config$1), uri2 = path !== void 0 ? `${operation}/${path}` : operation;
784
776
  return `${resourceBase}/${uri2}`.replace(/\/($|\?)/, "$1");
785
777
  }
786
778
  const catalog = config.hasDataset(config$1), baseUri = `/${operation}/${catalog}`;
787
- return `/data${path ? `${baseUri}/${path}` : baseUri}`.replace(/\/($|\?)/, "$1");
779
+ return `/data${path !== void 0 ? `${baseUri}/${path}` : baseUri}`.replace(/\/($|\?)/, "$1");
788
780
  }
789
781
  function _getUrl(client, uri, canUseCdn = !1) {
790
782
  const { url, cdnUrl } = client.config();
@@ -810,6 +802,27 @@ function _createAbortError(signal) {
810
802
  const error = new Error(signal?.reason ?? "The operation was aborted.");
811
803
  return error.name = "AbortError", error;
812
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
+ };
813
826
  class ObservableAssetsClient {
814
827
  #client;
815
828
  #httpRequest;
@@ -862,8 +875,23 @@ function _upload(client, httpRequest, assetType, body, opts = {}) {
862
875
  }
863
876
  function buildAssetUploadUrl(config$1, assetType) {
864
877
  const assetTypeEndpoint = assetType === "image" ? "images" : "files";
865
- if (config$1["~experimental_resource"])
866
- return `${config$1["~experimental_resource"].type}/${config$1["~experimental_resource"].id}/assets/${assetTypeEndpoint}`;
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
+ }
867
895
  const dataset = config.hasDataset(config$1);
868
896
  return `assets/${assetTypeEndpoint}/${dataset}`;
869
897
  }
@@ -1553,7 +1581,7 @@ function defineDeprecatedCreateClient(createClient2) {
1553
1581
  return config.printNoDefaultExport(), createClient2(config$1);
1554
1582
  };
1555
1583
  }
1556
- var name = "@sanity/client", version = "6.28.3-resources.1";
1584
+ var name = "@sanity/client", version = "6.28.3-resources.3";
1557
1585
  const middleware = [
1558
1586
  middleware$1.debug({ verbose: !0, namespace: "sanity:client" }),
1559
1587
  middleware$1.headers({ "User-Agent": `${name} ${version}` }),