@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.d.cts CHANGED
@@ -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 {
package/dist/index.d.ts CHANGED
@@ -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 {
package/dist/index.js CHANGED
@@ -4,7 +4,7 @@ import { retry, jsonRequest, jsonResponse, progress, observable, debug, headers,
4
4
  import { Observable, defer, of, isObservable, mergeMap, from, lastValueFrom, shareReplay, catchError, concat, throwError, timer, tap, finalize, share, merge, EMPTY } from "rxjs";
5
5
  import { stegaClean } from "./_chunks-es/stegaClean.js";
6
6
  import { combineLatestWith, map, filter, finalize as finalize$1 } from "rxjs/operators";
7
- import { validateObject, validateInsert, requireDocumentId, validateDocumentId, resourceBase, hasDataset, requestTag, printPreviewDraftsDeprecationWarning, validateApiPerspective, printCdnPreviewDraftsWarning, validateAssetType, resourceGuard, dataset, defaultConfig, initConfig, printNoDefaultExport } from "./_chunks-es/config.js";
7
+ import { validateObject, validateInsert, requireDocumentId, validateDocumentId, resourceConfig, hasDataset, requestTag, printPreviewDraftsDeprecationWarning, validateApiPerspective, printCdnPreviewDraftsWarning, validateAssetType, resourceGuard, dataset, defaultConfig, initConfig, printNoDefaultExport } from "./_chunks-es/config.js";
8
8
  class ClientError extends Error {
9
9
  response;
10
10
  statusCode = 400;
@@ -720,21 +720,12 @@ function _create(client, httpRequest, doc, op, options = {}) {
720
720
  const mutation = { [op]: doc }, opts = Object.assign({ returnFirst: !0, returnDocuments: !0 }, options);
721
721
  return _dataRequest(client, httpRequest, "mutate", { mutations: [mutation] }, opts);
722
722
  }
723
- const resourceDatasetRegex = /^\/projects\/([^/]+)\/datasets\/([^/]+)\/([^/^?]+)\/?/, isResourceDataEndpoint = (config, uri, endpoint) => {
724
- if (!config["~experimental_resource"])
725
- return !1;
726
- if (uri.startsWith(
727
- `/${config["~experimental_resource"].type}/${config["~experimental_resource"].id}/${endpoint}`
728
- ))
729
- return !0;
730
- const match = uri.match(resourceDatasetRegex);
731
- return match ? match[3] === endpoint : !1;
732
- }, 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);
723
+ 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);
733
724
  function _requestObservable(client, httpRequest, options) {
734
- 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;
725
+ 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;
735
726
  let useCdn = (options.useCdn ?? config.useCdn) && canUseCdn;
736
727
  const tag = options.tag && config.requestTagPrefix ? [config.requestTagPrefix, options.tag].join(".") : options.tag || config.requestTagPrefix;
737
- if (tag && options.tag !== null && (options.query = { tag: requestTag(tag), ...options.query }), ["GET", "HEAD", "POST"].indexOf(options.method || "GET") >= 0 && isQuery(config, uri)) {
728
+ if (tag && options.tag !== null && (options.query = { tag: requestTag(tag), ...options.query }), ["GET", "HEAD", "POST"].indexOf(options.method || "GET") >= 0 && isQuery(client, uri)) {
738
729
  const resultSourceMap = options.resultSourceMap ?? config.resultSourceMap;
739
730
  resultSourceMap !== void 0 && resultSourceMap !== !1 && (options.query = { resultSourceMap, ...options.query });
740
731
  const perspectiveOption = options.perspective || config.perspective;
@@ -763,11 +754,12 @@ function _request(client, httpRequest, options) {
763
754
  function _getDataUrl(client, operation, path) {
764
755
  const config = client.config();
765
756
  if (config["~experimental_resource"]) {
766
- const resourceBase$1 = resourceBase(config), uri2 = path ? `${operation}/${path}` : operation;
767
- return `${resourceBase$1}/${uri2}`.replace(/\/($|\?)/, "$1");
757
+ resourceConfig(config);
758
+ const resourceBase = resourceDataBase(config), uri2 = path !== void 0 ? `${operation}/${path}` : operation;
759
+ return `${resourceBase}/${uri2}`.replace(/\/($|\?)/, "$1");
768
760
  }
769
761
  const catalog = hasDataset(config), baseUri = `/${operation}/${catalog}`;
770
- return `/data${path ? `${baseUri}/${path}` : baseUri}`.replace(/\/($|\?)/, "$1");
762
+ return `/data${path !== void 0 ? `${baseUri}/${path}` : baseUri}`.replace(/\/($|\?)/, "$1");
771
763
  }
772
764
  function _getUrl(client, uri, canUseCdn = !1) {
773
765
  const { url, cdnUrl } = client.config();
@@ -793,6 +785,27 @@ function _createAbortError(signal) {
793
785
  const error = new Error(signal?.reason ?? "The operation was aborted.");
794
786
  return error.name = "AbortError", error;
795
787
  }
788
+ const resourceDataBase = (config) => {
789
+ if (!config["~experimental_resource"])
790
+ throw new Error("`resource` must be provided to perform resource queries");
791
+ const { type, id } = config["~experimental_resource"];
792
+ switch (type) {
793
+ case "dataset": {
794
+ const segments = id.split(".");
795
+ if (segments.length !== 2)
796
+ throw new Error('Dataset ID must be in the format "project.dataset"');
797
+ return `/projects/${segments[0]}/datasets/${segments[1]}`;
798
+ }
799
+ case "canvas":
800
+ return `/canvases/${id}`;
801
+ case "media-library":
802
+ return `/media-libraries/${id}`;
803
+ case "dashboard":
804
+ return `/dashboards/${id}`;
805
+ default:
806
+ throw new Error(`Unsupported resource type: ${type.toString()}`);
807
+ }
808
+ };
796
809
  class ObservableAssetsClient {
797
810
  #client;
798
811
  #httpRequest;
@@ -845,8 +858,23 @@ function _upload(client, httpRequest, assetType, body, opts = {}) {
845
858
  }
846
859
  function buildAssetUploadUrl(config, assetType) {
847
860
  const assetTypeEndpoint = assetType === "image" ? "images" : "files";
848
- if (config["~experimental_resource"])
849
- return `${config["~experimental_resource"].type}/${config["~experimental_resource"].id}/assets/${assetTypeEndpoint}`;
861
+ if (config["~experimental_resource"]) {
862
+ const { type, id } = config["~experimental_resource"];
863
+ switch (type) {
864
+ case "dataset":
865
+ throw new Error(
866
+ "Assets are not supported for dataset resources, yet. Configure the client with `{projectId: <projectId>, dataset: <datasetId>}` instead."
867
+ );
868
+ case "canvas":
869
+ return `/canvases/${id}/assets/${assetTypeEndpoint}`;
870
+ case "media-library":
871
+ return `/media-libraries/${id}/upload`;
872
+ case "dashboard":
873
+ return `/dashboards/${id}/assets/${assetTypeEndpoint}`;
874
+ default:
875
+ throw new Error(`Unsupported resource type: ${type.toString()}`);
876
+ }
877
+ }
850
878
  const dataset2 = hasDataset(config);
851
879
  return `assets/${assetTypeEndpoint}/${dataset2}`;
852
880
  }
@@ -1536,7 +1564,7 @@ function defineDeprecatedCreateClient(createClient2) {
1536
1564
  return printNoDefaultExport(), createClient2(config);
1537
1565
  };
1538
1566
  }
1539
- var name = "@sanity/client", version = "6.28.3-resources.1";
1567
+ var name = "@sanity/client", version = "6.28.3-resources.3";
1540
1568
  const middleware = [
1541
1569
  debug({ verbose: !0, namespace: "sanity:client" }),
1542
1570
  headers({ "User-Agent": `${name} ${version}` }),