@sanity/client 7.8.1 → 7.8.2-datasets.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.
@@ -2250,6 +2250,7 @@ ${codeFrame(query, { start, end }, description)}${withTag}`;
2250
2250
  throw new Error('Dataset resource ID must be in the format "project.dataset"');
2251
2251
  return;
2252
2252
  }
2253
+ case "project":
2253
2254
  case "dashboard":
2254
2255
  case "media-library":
2255
2256
  case "canvas":
@@ -3074,10 +3075,24 @@ ${selectionOpts}`);
3074
3075
  function _getDataUrl(client, operation, path) {
3075
3076
  const config = client.config();
3076
3077
  if (config["~experimental_resource"]) {
3077
- resourceConfig(config);
3078
- const resourceBase = resourceDataBase(config), uri2 = path !== void 0 ? `${operation}/${path}` : operation;
3078
+ if (resourceConfig(config), operation === "" && path?.startsWith("datasets")) {
3079
+ const { type, id } = config["~experimental_resource"];
3080
+ if (type === "dataset") {
3081
+ const segments = id.split(".");
3082
+ if (segments.length !== 2)
3083
+ throw new Error('Invalid dataset resource ID. Expected format "project.dataset".');
3084
+ const base = `/projects/${segments[0]}`, uri3 = [path].filter(Boolean).join("/");
3085
+ return `${base}/${uri3}`.replace(/\/($|\?)/, "$1");
3086
+ } else if (type === "project") {
3087
+ const base = `/projects/${id}`, uri3 = [path].filter(Boolean).join("/");
3088
+ return `${base}/${uri3}`.replace(/\/($|\?)/, "$1");
3089
+ }
3090
+ }
3091
+ const resourceBase = resourceDataBase(config), uri2 = [operation, path].filter(Boolean).join("/");
3079
3092
  return `${resourceBase}/${uri2}`.replace(/\/($|\?)/, "$1");
3080
3093
  }
3094
+ if (operation === "")
3095
+ return `/${path || ""}`;
3081
3096
  const catalog = hasDataset(config), baseUri = `/${operation}/${catalog}`;
3082
3097
  return `/data${path !== void 0 ? `${baseUri}/${path}` : baseUri}`.replace(/\/($|\?)/, "$1");
3083
3098
  }
@@ -3122,6 +3137,8 @@ ${selectionOpts}`);
3122
3137
  return `/media-libraries/${id}`;
3123
3138
  case "dashboard":
3124
3139
  return `/dashboards/${id}`;
3140
+ case "project":
3141
+ return `/projects/${id}`;
3125
3142
  default:
3126
3143
  throw new Error(`Unsupported resource type: ${type.toString()}`);
3127
3144
  }
@@ -3292,6 +3309,10 @@ ${selectionOpts}`);
3292
3309
  if (config["~experimental_resource"]) {
3293
3310
  const { type, id } = config["~experimental_resource"];
3294
3311
  switch (type) {
3312
+ case "project":
3313
+ throw new Error(
3314
+ "Assets are not supported for project resources, yet. Configure the client with `{projectId: <projectId>}` instead."
3315
+ );
3295
3316
  case "dataset":
3296
3317
  throw new Error(
3297
3318
  "Assets are not supported for dataset resources, yet. Configure the client with `{projectId: <projectId>, dataset: <datasetId>}` instead."
@@ -3509,8 +3530,12 @@ ${selectionOpts}`);
3509
3530
  * Fetch a list of datasets for the configured project
3510
3531
  */
3511
3532
  list() {
3533
+ const resource = this.#client.config()["~experimental_resource"];
3534
+ if (resource && resource.type !== "project")
3535
+ throw new Error('`dataset.list()` requires a resource type of "project".');
3536
+ const uri = _getDataUrl(this.#client, "", "datasets");
3512
3537
  return _request(this.#client, this.#httpRequest, {
3513
- uri: "/datasets",
3538
+ uri,
3514
3539
  tag: null
3515
3540
  });
3516
3541
  }
@@ -3528,7 +3553,7 @@ ${selectionOpts}`);
3528
3553
  * @param options - Options for the dataset
3529
3554
  */
3530
3555
  create(name, options) {
3531
- return resourceGuard("dataset", this.#client.config()), lastValueFrom(
3556
+ return lastValueFrom(
3532
3557
  _modify(this.#client, this.#httpRequest, "PUT", name, options)
3533
3558
  );
3534
3559
  }
@@ -3539,7 +3564,7 @@ ${selectionOpts}`);
3539
3564
  * @param options - New options for the dataset
3540
3565
  */
3541
3566
  edit(name, options) {
3542
- return resourceGuard("dataset", this.#client.config()), lastValueFrom(
3567
+ return lastValueFrom(
3543
3568
  _modify(this.#client, this.#httpRequest, "PATCH", name, options)
3544
3569
  );
3545
3570
  }
@@ -3549,21 +3574,38 @@ ${selectionOpts}`);
3549
3574
  * @param name - Name of the dataset to delete
3550
3575
  */
3551
3576
  delete(name) {
3552
- return resourceGuard("dataset", this.#client.config()), lastValueFrom(_modify(this.#client, this.#httpRequest, "DELETE", name));
3577
+ return lastValueFrom(_modify(this.#client, this.#httpRequest, "DELETE", name));
3553
3578
  }
3554
3579
  /**
3555
3580
  * Fetch a list of datasets for the configured project
3556
3581
  */
3557
3582
  list() {
3558
- return resourceGuard("dataset", this.#client.config()), lastValueFrom(
3559
- _request(this.#client, this.#httpRequest, { uri: "/datasets", tag: null })
3583
+ const resource = this.#client.config()["~experimental_resource"];
3584
+ if (resource && resource.type !== "project")
3585
+ throw new Error('`dataset.list()` requires a resource type of "project".');
3586
+ const uri = _getDataUrl(this.#client, "", "datasets");
3587
+ return lastValueFrom(
3588
+ _request(this.#client, this.#httpRequest, { uri, tag: null })
3560
3589
  );
3561
3590
  }
3562
3591
  }
3563
3592
  function _modify(client, httpRequest, method, name, options) {
3564
- return resourceGuard("dataset", client.config()), dataset(name), _request(client, httpRequest, {
3593
+ dataset(name);
3594
+ const resource = client.config()["~experimental_resource"];
3595
+ if (resource)
3596
+ if (resource.type === "dataset") {
3597
+ const segments = resource.id.split(".");
3598
+ if (segments.length !== 2)
3599
+ throw new Error('Dataset resource ID must be in the format "project.dataset"');
3600
+ const datasetName = segments[1];
3601
+ if (name !== datasetName)
3602
+ throw new Error(`Dataset name "${name}" does not match resource dataset "${datasetName}"`);
3603
+ } else
3604
+ throw new Error('Dataset create/edit/delete operations require a resource type of "dataset"');
3605
+ const uri = _getDataUrl(client, "", `datasets/${name}`);
3606
+ return _request(client, httpRequest, {
3565
3607
  method,
3566
- uri: `/datasets/${name}`,
3608
+ uri,
3567
3609
  body: options,
3568
3610
  tag: null
3569
3611
  });
@@ -3575,7 +3617,6 @@ ${selectionOpts}`);
3575
3617
  this.#client = client, this.#httpRequest = httpRequest;
3576
3618
  }
3577
3619
  list(options) {
3578
- resourceGuard("projects", this.#client.config());
3579
3620
  const uri = options?.includeMembers === false ? "/projects?includeMembers=false" : "/projects";
3580
3621
  return _request(this.#client, this.#httpRequest, { uri });
3581
3622
  }
@@ -3585,7 +3626,7 @@ ${selectionOpts}`);
3585
3626
  * @param projectId - ID of the project to fetch
3586
3627
  */
3587
3628
  getById(projectId2) {
3588
- return resourceGuard("projects", this.#client.config()), _request(this.#client, this.#httpRequest, { uri: `/projects/${projectId2}` });
3629
+ return _request(this.#client, this.#httpRequest, { uri: `/projects/${projectId2}` });
3589
3630
  }
3590
3631
  }
3591
3632
  class ProjectsClient {
@@ -3595,7 +3636,6 @@ ${selectionOpts}`);
3595
3636
  this.#client = client, this.#httpRequest = httpRequest;
3596
3637
  }
3597
3638
  list(options) {
3598
- resourceGuard("projects", this.#client.config());
3599
3639
  const uri = options?.includeMembers === false ? "/projects?includeMembers=false" : "/projects";
3600
3640
  return lastValueFrom(_request(this.#client, this.#httpRequest, { uri }));
3601
3641
  }
@@ -3605,7 +3645,7 @@ ${selectionOpts}`);
3605
3645
  * @param projectId - ID of the project to fetch
3606
3646
  */
3607
3647
  getById(projectId2) {
3608
- return resourceGuard("projects", this.#client.config()), lastValueFrom(
3648
+ return lastValueFrom(
3609
3649
  _request(this.#client, this.#httpRequest, { uri: `/projects/${projectId2}` })
3610
3650
  );
3611
3651
  }
@@ -4964,13 +5004,40 @@ ${selectionOpts}`);
4964
5004
  function isValidDate(dateString) {
4965
5005
  return /^\d{4}-\d{2}-\d{2}/.test(dateString) ? !!Date.parse(dateString) : false;
4966
5006
  }
5007
+ const allowedProtocols = /* @__PURE__ */ new Set([
5008
+ "app:",
5009
+ "data:",
5010
+ "discord:",
5011
+ "file:",
5012
+ "ftp:",
5013
+ "ftps:",
5014
+ "geo:",
5015
+ "http:",
5016
+ "https:",
5017
+ "imap:",
5018
+ "javascript:",
5019
+ "magnet:",
5020
+ "mailto:",
5021
+ "maps:",
5022
+ "ms-excel:",
5023
+ "ms-powerpoint:",
5024
+ "ms-word:",
5025
+ "slack:",
5026
+ "sms:",
5027
+ "spotify:",
5028
+ "steam:",
5029
+ "teams:",
5030
+ "tel:",
5031
+ "vscode:",
5032
+ "zoom:"
5033
+ ]);
4967
5034
  function isValidURL(url) {
4968
5035
  try {
4969
- new URL(url, url.startsWith("/") ? "https://acme.com" : void 0);
5036
+ const { protocol } = new URL(url, url.startsWith("/") ? "https://acme.com" : void 0);
5037
+ return allowedProtocols.has(protocol) || protocol.startsWith("web+");
4970
5038
  } catch {
4971
5039
  return false;
4972
5040
  }
4973
- return true;
4974
5041
  }
4975
5042
  function hasTypeLike(path) {
4976
5043
  return path.some((segment) => typeof segment == "string" && segment.match(/type/i) !== null);