@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.
@@ -401,10 +401,23 @@ export declare interface ClientConfig {
401
401
  stega?: StegaConfig | boolean
402
402
  }
403
403
 
404
- declare type ClientConfigResource = {
405
- type: string
406
- id: string
407
- }
404
+ declare type ClientConfigResource =
405
+ | {
406
+ type: 'canvas'
407
+ id: string
408
+ }
409
+ | {
410
+ type: 'media-library'
411
+ id: string
412
+ }
413
+ | {
414
+ type: 'dataset'
415
+ id: string
416
+ }
417
+ | {
418
+ type: 'dashboard'
419
+ id: string
420
+ }
408
421
 
409
422
  /** @public */
410
423
  export declare class ClientError extends Error {
@@ -401,10 +401,23 @@ export declare interface ClientConfig {
401
401
  stega?: StegaConfig | boolean
402
402
  }
403
403
 
404
- declare type ClientConfigResource = {
405
- type: string
406
- id: string
407
- }
404
+ declare type ClientConfigResource =
405
+ | {
406
+ type: 'canvas'
407
+ id: string
408
+ }
409
+ | {
410
+ type: 'media-library'
411
+ id: string
412
+ }
413
+ | {
414
+ type: 'dataset'
415
+ id: string
416
+ }
417
+ | {
418
+ type: 'dashboard'
419
+ id: string
420
+ }
408
421
 
409
422
  /** @public */
410
423
  export declare class ClientError extends Error {
@@ -155,17 +155,23 @@ const VALID_ASSET_TYPES = ["image", "file"], VALID_INSERT_LOCATIONS = ["before",
155
155
  "Tag can only contain alphanumeric characters, underscores, dashes and dots, and be between one and 75 characters long."
156
156
  );
157
157
  return tag;
158
- }, resourceBase = (config) => {
158
+ }, resourceConfig = (config) => {
159
159
  if (!config["~experimental_resource"])
160
- throw new Error("`resource` must be provided to perform queries");
161
- const resourceConfig = config["~experimental_resource"];
162
- if (resourceConfig.type === "dataset") {
163
- const segments = resourceConfig.id.split(".");
164
- if (segments.length !== 2)
165
- throw new Error('Dataset ID must be in the format "project.dataset"');
166
- return `/projects/${segments[0]}/datasets/${segments[1]}`;
167
- }
168
- return `/${resourceConfig.type}/${resourceConfig.id}`;
160
+ throw new Error("`resource` must be provided to perform resource queries");
161
+ const { type, id } = config["~experimental_resource"];
162
+ switch (type) {
163
+ case "dataset": {
164
+ if (id.split(".").length !== 2)
165
+ throw new Error('Dataset resource ID must be in the format "project.dataset"');
166
+ return;
167
+ }
168
+ case "dashboard":
169
+ case "media-library":
170
+ case "canvas":
171
+ return;
172
+ default:
173
+ throw new Error(`Unsupported resource type: ${type.toString()}`);
174
+ }
169
175
  }, resourceGuard = (service, config) => {
170
176
  if (config["~experimental_resource"])
171
177
  throw new Error(`\`${service}\` does not support resource-based operations`);
@@ -241,7 +247,7 @@ const initConfig = (config, prevConfig) => {
241
247
  }
242
248
  if (projectBased && !newConfig.projectId)
243
249
  throw new Error("Configuration must contain `projectId`");
244
- if (newConfig["~experimental_resource"] && resourceBase(newConfig), typeof newConfig.perspective < "u" && validateApiPerspective(newConfig.perspective), "encodeSourceMap" in newConfig)
250
+ if (newConfig["~experimental_resource"] && resourceConfig(newConfig), typeof newConfig.perspective < "u" && validateApiPerspective(newConfig.perspective), "encodeSourceMap" in newConfig)
245
251
  throw new Error(
246
252
  "It looks like you're using options meant for '@sanity/preview-kit/client'. 'encodeSourceMap' is not supported in '@sanity/client'. Did you mean 'stega.enabled'?"
247
253
  );
@@ -871,21 +877,12 @@ function _create(client, httpRequest, doc, op, options = {}) {
871
877
  const mutation = { [op]: doc }, opts = Object.assign({ returnFirst: !0, returnDocuments: !0 }, options);
872
878
  return _dataRequest(client, httpRequest, "mutate", { mutations: [mutation] }, opts);
873
879
  }
874
- const resourceDatasetRegex = /^\/projects\/([^/]+)\/datasets\/([^/]+)\/([^/^?]+)\/?/, isResourceDataEndpoint = (config, uri, endpoint) => {
875
- if (!config["~experimental_resource"])
876
- return !1;
877
- if (uri.startsWith(
878
- `/${config["~experimental_resource"].type}/${config["~experimental_resource"].id}/${endpoint}`
879
- ))
880
- return !0;
881
- const match = uri.match(resourceDatasetRegex);
882
- return match ? match[3] === endpoint : !1;
883
- }, isQuery = (config, uri) => uri.startsWith("/data/query/") || isResourceDataEndpoint(config, uri, "query"), isMutate = (config, uri) => uri.startsWith("/data/mutate/") || isResourceDataEndpoint(config, uri, "mutate"), isDoc = (config, uri) => uri.startsWith("/data/doc/") || isResourceDataEndpoint(config, uri, "doc"), isListener = (config, uri) => uri.startsWith("/data/listen/") || isResourceDataEndpoint(config, uri, "listen"), isHistory = (config, uri) => uri.startsWith("/data/history/") || isResourceDataEndpoint(config, uri, "history"), isData = (config, uri) => uri.startsWith("/data/") || isQuery(config, uri) || isMutate(config, uri) || isDoc(config, uri) || isListener(config, uri) || isHistory(config, uri);
880
+ 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);
884
881
  function _requestObservable(client, httpRequest, options) {
885
- const uri = options.url || options.uri, config = client.config(), canUseCdn = typeof options.canUseCdn > "u" ? ["GET", "HEAD"].indexOf(options.method || "GET") >= 0 && isData(config, uri) : options.canUseCdn;
882
+ const uri = options.url || options.uri, config = client.config(), canUseCdn = typeof options.canUseCdn > "u" ? ["GET", "HEAD"].indexOf(options.method || "GET") >= 0 && isData(client, uri) : options.canUseCdn;
886
883
  let useCdn = (options.useCdn ?? config.useCdn) && canUseCdn;
887
884
  const tag = options.tag && config.requestTagPrefix ? [config.requestTagPrefix, options.tag].join(".") : options.tag || config.requestTagPrefix;
888
- if (tag && options.tag !== null && (options.query = { tag: requestTag(tag), ...options.query }), ["GET", "HEAD", "POST"].indexOf(options.method || "GET") >= 0 && isQuery(config, uri)) {
885
+ if (tag && options.tag !== null && (options.query = { tag: requestTag(tag), ...options.query }), ["GET", "HEAD", "POST"].indexOf(options.method || "GET") >= 0 && isQuery(client, uri)) {
889
886
  const resultSourceMap = options.resultSourceMap ?? config.resultSourceMap;
890
887
  resultSourceMap !== void 0 && resultSourceMap !== !1 && (options.query = { resultSourceMap, ...options.query });
891
888
  const perspectiveOption = options.perspective || config.perspective;
@@ -914,11 +911,12 @@ function _request(client, httpRequest, options) {
914
911
  function _getDataUrl(client, operation, path) {
915
912
  const config = client.config();
916
913
  if (config["~experimental_resource"]) {
917
- const resourceBase$1 = resourceBase(config), uri2 = path ? `${operation}/${path}` : operation;
918
- return `${resourceBase$1}/${uri2}`.replace(/\/($|\?)/, "$1");
914
+ resourceConfig(config);
915
+ const resourceBase = resourceDataBase(config), uri2 = path !== void 0 ? `${operation}/${path}` : operation;
916
+ return `${resourceBase}/${uri2}`.replace(/\/($|\?)/, "$1");
919
917
  }
920
918
  const catalog = hasDataset(config), baseUri = `/${operation}/${catalog}`;
921
- return `/data${path ? `${baseUri}/${path}` : baseUri}`.replace(/\/($|\?)/, "$1");
919
+ return `/data${path !== void 0 ? `${baseUri}/${path}` : baseUri}`.replace(/\/($|\?)/, "$1");
922
920
  }
923
921
  function _getUrl(client, uri, canUseCdn = !1) {
924
922
  const { url, cdnUrl } = client.config();
@@ -944,6 +942,27 @@ function _createAbortError(signal) {
944
942
  const error = new Error(signal?.reason ?? "The operation was aborted.");
945
943
  return error.name = "AbortError", error;
946
944
  }
945
+ const resourceDataBase = (config) => {
946
+ if (!config["~experimental_resource"])
947
+ throw new Error("`resource` must be provided to perform resource queries");
948
+ const { type, id } = config["~experimental_resource"];
949
+ switch (type) {
950
+ case "dataset": {
951
+ const segments = id.split(".");
952
+ if (segments.length !== 2)
953
+ throw new Error('Dataset ID must be in the format "project.dataset"');
954
+ return `/projects/${segments[0]}/datasets/${segments[1]}`;
955
+ }
956
+ case "canvas":
957
+ return `/canvases/${id}`;
958
+ case "media-library":
959
+ return `/media-libraries/${id}`;
960
+ case "dashboard":
961
+ return `/dashboards/${id}`;
962
+ default:
963
+ throw new Error(`Unsupported resource type: ${type.toString()}`);
964
+ }
965
+ };
947
966
  class ObservableAssetsClient {
948
967
  #client;
949
968
  #httpRequest;
@@ -996,8 +1015,23 @@ function _upload(client, httpRequest, assetType, body, opts = {}) {
996
1015
  }
997
1016
  function buildAssetUploadUrl(config, assetType) {
998
1017
  const assetTypeEndpoint = assetType === "image" ? "images" : "files";
999
- if (config["~experimental_resource"])
1000
- return `${config["~experimental_resource"].type}/${config["~experimental_resource"].id}/assets/${assetTypeEndpoint}`;
1018
+ if (config["~experimental_resource"]) {
1019
+ const { type, id } = config["~experimental_resource"];
1020
+ switch (type) {
1021
+ case "dataset":
1022
+ throw new Error(
1023
+ "Assets are not supported for dataset resources, yet. Configure the client with `{projectId: <projectId>, dataset: <datasetId>}` instead."
1024
+ );
1025
+ case "canvas":
1026
+ return `/canvases/${id}/assets/${assetTypeEndpoint}`;
1027
+ case "media-library":
1028
+ return `/media-libraries/${id}/upload`;
1029
+ case "dashboard":
1030
+ return `/dashboards/${id}/assets/${assetTypeEndpoint}`;
1031
+ default:
1032
+ throw new Error(`Unsupported resource type: ${type.toString()}`);
1033
+ }
1034
+ }
1001
1035
  const dataset2 = hasDataset(config);
1002
1036
  return `assets/${assetTypeEndpoint}/${dataset2}`;
1003
1037
  }