@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.
package/dist/index.cjs CHANGED
@@ -945,10 +945,24 @@ function _request(client, httpRequest, options) {
945
945
  function _getDataUrl(client, operation, path) {
946
946
  const config$1 = client.config();
947
947
  if (config$1["~experimental_resource"]) {
948
- config.resourceConfig(config$1);
949
- const resourceBase = resourceDataBase(config$1), uri2 = path !== void 0 ? `${operation}/${path}` : operation;
948
+ if (config.resourceConfig(config$1), operation === "" && path?.startsWith("datasets")) {
949
+ const { type, id } = config$1["~experimental_resource"];
950
+ if (type === "dataset") {
951
+ const segments = id.split(".");
952
+ if (segments.length !== 2)
953
+ throw new Error('Invalid dataset resource ID. Expected format "project.dataset".');
954
+ const base = `/projects/${segments[0]}`, uri3 = [path].filter(Boolean).join("/");
955
+ return `${base}/${uri3}`.replace(/\/($|\?)/, "$1");
956
+ } else if (type === "project") {
957
+ const base = `/projects/${id}`, uri3 = [path].filter(Boolean).join("/");
958
+ return `${base}/${uri3}`.replace(/\/($|\?)/, "$1");
959
+ }
960
+ }
961
+ const resourceBase = resourceDataBase(config$1), uri2 = [operation, path].filter(Boolean).join("/");
950
962
  return `${resourceBase}/${uri2}`.replace(/\/($|\?)/, "$1");
951
963
  }
964
+ if (operation === "")
965
+ return `/${path || ""}`;
952
966
  const catalog = config.hasDataset(config$1), baseUri = `/${operation}/${catalog}`;
953
967
  return `/data${path !== void 0 ? `${baseUri}/${path}` : baseUri}`.replace(/\/($|\?)/, "$1");
954
968
  }
@@ -993,6 +1007,8 @@ const resourceDataBase = (config2) => {
993
1007
  return `/media-libraries/${id}`;
994
1008
  case "dashboard":
995
1009
  return `/dashboards/${id}`;
1010
+ case "project":
1011
+ return `/projects/${id}`;
996
1012
  default:
997
1013
  throw new Error(`Unsupported resource type: ${type.toString()}`);
998
1014
  }
@@ -1163,6 +1179,10 @@ function buildAssetUploadUrl(config$1, assetType) {
1163
1179
  if (config$1["~experimental_resource"]) {
1164
1180
  const { type, id } = config$1["~experimental_resource"];
1165
1181
  switch (type) {
1182
+ case "project":
1183
+ throw new Error(
1184
+ "Assets are not supported for project resources, yet. Configure the client with `{projectId: <projectId>}` instead."
1185
+ );
1166
1186
  case "dataset":
1167
1187
  throw new Error(
1168
1188
  "Assets are not supported for dataset resources, yet. Configure the client with `{projectId: <projectId>, dataset: <datasetId>}` instead."
@@ -1380,8 +1400,12 @@ class ObservableDatasetsClient {
1380
1400
  * Fetch a list of datasets for the configured project
1381
1401
  */
1382
1402
  list() {
1403
+ const resource = this.#client.config()["~experimental_resource"];
1404
+ if (resource && resource.type !== "project")
1405
+ throw new Error('`dataset.list()` requires a resource type of "project".');
1406
+ const uri = _getDataUrl(this.#client, "", "datasets");
1383
1407
  return _request(this.#client, this.#httpRequest, {
1384
- uri: "/datasets",
1408
+ uri,
1385
1409
  tag: null
1386
1410
  });
1387
1411
  }
@@ -1399,7 +1423,7 @@ class DatasetsClient {
1399
1423
  * @param options - Options for the dataset
1400
1424
  */
1401
1425
  create(name2, options) {
1402
- return config.resourceGuard("dataset", this.#client.config()), rxjs.lastValueFrom(
1426
+ return rxjs.lastValueFrom(
1403
1427
  _modify(this.#client, this.#httpRequest, "PUT", name2, options)
1404
1428
  );
1405
1429
  }
@@ -1410,7 +1434,7 @@ class DatasetsClient {
1410
1434
  * @param options - New options for the dataset
1411
1435
  */
1412
1436
  edit(name2, options) {
1413
- return config.resourceGuard("dataset", this.#client.config()), rxjs.lastValueFrom(
1437
+ return rxjs.lastValueFrom(
1414
1438
  _modify(this.#client, this.#httpRequest, "PATCH", name2, options)
1415
1439
  );
1416
1440
  }
@@ -1420,21 +1444,38 @@ class DatasetsClient {
1420
1444
  * @param name - Name of the dataset to delete
1421
1445
  */
1422
1446
  delete(name2) {
1423
- return config.resourceGuard("dataset", this.#client.config()), rxjs.lastValueFrom(_modify(this.#client, this.#httpRequest, "DELETE", name2));
1447
+ return rxjs.lastValueFrom(_modify(this.#client, this.#httpRequest, "DELETE", name2));
1424
1448
  }
1425
1449
  /**
1426
1450
  * Fetch a list of datasets for the configured project
1427
1451
  */
1428
1452
  list() {
1429
- return config.resourceGuard("dataset", this.#client.config()), rxjs.lastValueFrom(
1430
- _request(this.#client, this.#httpRequest, { uri: "/datasets", tag: null })
1453
+ const resource = this.#client.config()["~experimental_resource"];
1454
+ if (resource && resource.type !== "project")
1455
+ throw new Error('`dataset.list()` requires a resource type of "project".');
1456
+ const uri = _getDataUrl(this.#client, "", "datasets");
1457
+ return rxjs.lastValueFrom(
1458
+ _request(this.#client, this.#httpRequest, { uri, tag: null })
1431
1459
  );
1432
1460
  }
1433
1461
  }
1434
1462
  function _modify(client, httpRequest, method, name2, options) {
1435
- return config.resourceGuard("dataset", client.config()), config.dataset(name2), _request(client, httpRequest, {
1463
+ config.dataset(name2);
1464
+ const resource = client.config()["~experimental_resource"];
1465
+ if (resource)
1466
+ if (resource.type === "dataset") {
1467
+ const segments = resource.id.split(".");
1468
+ if (segments.length !== 2)
1469
+ throw new Error('Dataset resource ID must be in the format "project.dataset"');
1470
+ const datasetName = segments[1];
1471
+ if (name2 !== datasetName)
1472
+ throw new Error(`Dataset name "${name2}" does not match resource dataset "${datasetName}"`);
1473
+ } else
1474
+ throw new Error('Dataset create/edit/delete operations require a resource type of "dataset"');
1475
+ const uri = _getDataUrl(client, "", `datasets/${name2}`);
1476
+ return _request(client, httpRequest, {
1436
1477
  method,
1437
- uri: `/datasets/${name2}`,
1478
+ uri,
1438
1479
  body: options,
1439
1480
  tag: null
1440
1481
  });
@@ -1446,7 +1487,6 @@ class ObservableProjectsClient {
1446
1487
  this.#client = client, this.#httpRequest = httpRequest;
1447
1488
  }
1448
1489
  list(options) {
1449
- config.resourceGuard("projects", this.#client.config());
1450
1490
  const uri = options?.includeMembers === !1 ? "/projects?includeMembers=false" : "/projects";
1451
1491
  return _request(this.#client, this.#httpRequest, { uri });
1452
1492
  }
@@ -1456,7 +1496,7 @@ class ObservableProjectsClient {
1456
1496
  * @param projectId - ID of the project to fetch
1457
1497
  */
1458
1498
  getById(projectId) {
1459
- return config.resourceGuard("projects", this.#client.config()), _request(this.#client, this.#httpRequest, { uri: `/projects/${projectId}` });
1499
+ return _request(this.#client, this.#httpRequest, { uri: `/projects/${projectId}` });
1460
1500
  }
1461
1501
  }
1462
1502
  class ProjectsClient {
@@ -1466,7 +1506,6 @@ class ProjectsClient {
1466
1506
  this.#client = client, this.#httpRequest = httpRequest;
1467
1507
  }
1468
1508
  list(options) {
1469
- config.resourceGuard("projects", this.#client.config());
1470
1509
  const uri = options?.includeMembers === !1 ? "/projects?includeMembers=false" : "/projects";
1471
1510
  return rxjs.lastValueFrom(_request(this.#client, this.#httpRequest, { uri }));
1472
1511
  }
@@ -1476,7 +1515,7 @@ class ProjectsClient {
1476
1515
  * @param projectId - ID of the project to fetch
1477
1516
  */
1478
1517
  getById(projectId) {
1479
- return config.resourceGuard("projects", this.#client.config()), rxjs.lastValueFrom(
1518
+ return rxjs.lastValueFrom(
1480
1519
  _request(this.#client, this.#httpRequest, { uri: `/projects/${projectId}` })
1481
1520
  );
1482
1521
  }
@@ -2574,7 +2613,7 @@ function defineDeprecatedCreateClient(createClient2) {
2574
2613
  return config.printNoDefaultExport(), createClient2(config$1);
2575
2614
  };
2576
2615
  }
2577
- var name = "@sanity/client", version = "7.8.1";
2616
+ var name = "@sanity/client", version = "7.8.2-datasets.0";
2578
2617
  const middleware = [
2579
2618
  middleware$1.debug({ verbose: !0, namespace: "sanity:client" }),
2580
2619
  middleware$1.headers({ "User-Agent": `${name} ${version}` }),