@sanity/client 6.27.1 → 6.27.2-resources.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.
@@ -388,6 +388,13 @@ export declare interface ClientConfig {
388
388
  * Options for how, if enabled, Content Source Maps are encoded into query results using steganography
389
389
  */
390
390
  stega?: StegaConfig | boolean
391
+ /**
392
+ * @deprecated Don't use
393
+ */
394
+ experimental_resource?: {
395
+ id: string
396
+ type: string
397
+ }
391
398
  }
392
399
 
393
400
  /** @public */
@@ -993,13 +1000,12 @@ export declare class LiveClient {
993
1000
  #private
994
1001
  constructor(client: SanityClient | ObservableSanityClient)
995
1002
  /**
996
- * Requires `apiVersion` to be `2021-03-26` or later.
1003
+ * Requires `apiVersion` to be `2021-03-25` or later.
997
1004
  */
998
1005
  events({
999
1006
  includeDrafts,
1000
1007
  tag: _tag,
1001
1008
  }?: {
1002
- /** @alpha this API is experimental and may change or even be removed */
1003
1009
  includeDrafts?: boolean
1004
1010
  /**
1005
1011
  * Optional request tag for the listener. Use to identify the request in logs.
@@ -1007,9 +1013,16 @@ export declare class LiveClient {
1007
1013
  * @defaultValue `undefined`
1008
1014
  */
1009
1015
  tag?: string
1010
- }): Observable<LiveEventMessage | LiveEventRestart | LiveEventReconnect | LiveEventWelcome>
1016
+ }): Observable<LiveEvent>
1011
1017
  }
1012
1018
 
1019
+ /** @public */
1020
+ export declare type LiveEvent =
1021
+ | LiveEventRestart
1022
+ | LiveEventReconnect
1023
+ | LiveEventMessage
1024
+ | LiveEventWelcome
1025
+
1013
1026
  /** @public */
1014
1027
  export declare interface LiveEventMessage {
1015
1028
  type: 'message'
@@ -2177,7 +2190,7 @@ export declare interface RawQueryResponse<R> {
2177
2190
  ms: number
2178
2191
  result: R
2179
2192
  resultSourceMap?: ContentSourceMap
2180
- /** Requires `apiVersion` to be `2021-03-26` or later. */
2193
+ /** Requires `apiVersion` to be `2021-03-25` or later. */
2181
2194
  syncTags?: SyncTag[]
2182
2195
  }
2183
2196
 
@@ -388,6 +388,13 @@ export declare interface ClientConfig {
388
388
  * Options for how, if enabled, Content Source Maps are encoded into query results using steganography
389
389
  */
390
390
  stega?: StegaConfig | boolean
391
+ /**
392
+ * @deprecated Don't use
393
+ */
394
+ experimental_resource?: {
395
+ id: string
396
+ type: string
397
+ }
391
398
  }
392
399
 
393
400
  /** @public */
@@ -993,13 +1000,12 @@ export declare class LiveClient {
993
1000
  #private
994
1001
  constructor(client: SanityClient | ObservableSanityClient)
995
1002
  /**
996
- * Requires `apiVersion` to be `2021-03-26` or later.
1003
+ * Requires `apiVersion` to be `2021-03-25` or later.
997
1004
  */
998
1005
  events({
999
1006
  includeDrafts,
1000
1007
  tag: _tag,
1001
1008
  }?: {
1002
- /** @alpha this API is experimental and may change or even be removed */
1003
1009
  includeDrafts?: boolean
1004
1010
  /**
1005
1011
  * Optional request tag for the listener. Use to identify the request in logs.
@@ -1007,9 +1013,16 @@ export declare class LiveClient {
1007
1013
  * @defaultValue `undefined`
1008
1014
  */
1009
1015
  tag?: string
1010
- }): Observable<LiveEventMessage | LiveEventRestart | LiveEventReconnect | LiveEventWelcome>
1016
+ }): Observable<LiveEvent>
1011
1017
  }
1012
1018
 
1019
+ /** @public */
1020
+ export declare type LiveEvent =
1021
+ | LiveEventRestart
1022
+ | LiveEventReconnect
1023
+ | LiveEventMessage
1024
+ | LiveEventWelcome
1025
+
1013
1026
  /** @public */
1014
1027
  export declare interface LiveEventMessage {
1015
1028
  type: 'message'
@@ -2177,7 +2190,7 @@ export declare interface RawQueryResponse<R> {
2177
2190
  ms: number
2178
2191
  result: R
2179
2192
  resultSourceMap?: ContentSourceMap
2180
- /** Requires `apiVersion` to be `2021-03-26` or later. */
2193
+ /** Requires `apiVersion` to be `2021-03-25` or later. */
2181
2194
  syncTags?: SyncTag[]
2182
2195
  }
2183
2196
 
@@ -230,7 +230,9 @@ const initConfig = (config, prevConfig) => {
230
230
  const newConfig = {
231
231
  ...defaultConfig,
232
232
  ...specifiedConfig
233
- }, projectBased = newConfig.useProjectHostname;
233
+ }, experimentalResource = newConfig.experimental_resource;
234
+ experimentalResource && (newConfig.useProjectHostname = !1);
235
+ const projectBased = newConfig.useProjectHostname;
234
236
  if (typeof Promise > "u") {
235
237
  const helpUrl = generateHelpUrl("js-client-promise-polyfill");
236
238
  throw new Error(`No native Promise-implementation found, polyfill needed - see ${helpUrl}`);
@@ -256,7 +258,11 @@ const initConfig = (config, prevConfig) => {
256
258
  const isBrowser = typeof window < "u" && window.location && window.location.hostname, isLocalhost = isBrowser && isLocal(window.location.hostname);
257
259
  isBrowser && isLocalhost && newConfig.token && newConfig.ignoreBrowserTokenWarning !== !0 ? printBrowserTokenWarning() : typeof newConfig.useCdn > "u" && printCdnWarning(), projectBased && projectId(newConfig.projectId), newConfig.dataset && dataset(newConfig.dataset), "requestTagPrefix" in newConfig && (newConfig.requestTagPrefix = newConfig.requestTagPrefix ? requestTag(newConfig.requestTagPrefix).replace(/\.+$/, "") : void 0), newConfig.apiVersion = `${newConfig.apiVersion}`.replace(/^v/, ""), newConfig.isDefaultApi = newConfig.apiHost === defaultConfig.apiHost, newConfig.useCdn === !0 && newConfig.withCredentials && printCdnAndWithCredentialsWarning(), newConfig.useCdn = newConfig.useCdn !== !1 && !newConfig.withCredentials, validateApiVersion(newConfig.apiVersion);
258
260
  const hostParts = newConfig.apiHost.split("://", 2), protocol = hostParts[0], host = hostParts[1], cdnHost = newConfig.isDefaultApi ? defaultCdnHost : host;
259
- return newConfig.useProjectHostname ? (newConfig.url = `${protocol}://${newConfig.projectId}.${host}/v${newConfig.apiVersion}`, newConfig.cdnUrl = `${protocol}://${newConfig.projectId}.${cdnHost}/v${newConfig.apiVersion}`) : (newConfig.url = `${newConfig.apiHost}/v${newConfig.apiVersion}`, newConfig.cdnUrl = newConfig.url), newConfig;
261
+ if (newConfig.useProjectHostname ? (newConfig.url = `${protocol}://${newConfig.projectId}.${host}/v${newConfig.apiVersion}`, newConfig.cdnUrl = `${protocol}://${newConfig.projectId}.${cdnHost}/v${newConfig.apiVersion}`) : (newConfig.url = `${newConfig.apiHost}/v${newConfig.apiVersion}`, newConfig.cdnUrl = newConfig.url), experimentalResource) {
262
+ const resourceSuffix = `${experimentalResource.type}/${experimentalResource.id}`;
263
+ newConfig.url = `${newConfig.url}/${resourceSuffix}`, newConfig.cdnUrl = `${newConfig.cdnUrl}/${resourceSuffix}`;
264
+ }
265
+ return newConfig;
260
266
  };
261
267
  class ConnectionFailedError extends Error {
262
268
  name = "ConnectionFailedError";
@@ -867,11 +873,20 @@ function _create(client, httpRequest, doc, op, options = {}) {
867
873
  const mutation = { [op]: doc }, opts = Object.assign({ returnFirst: !0, returnDocuments: !0 }, options);
868
874
  return _dataRequest(client, httpRequest, "mutate", { mutations: [mutation] }, opts);
869
875
  }
876
+ function _resourceBase(resource) {
877
+ return `/${resource.type}/${resource.id}`;
878
+ }
879
+ function isDataRequestUri(uri, resource) {
880
+ return resource ? uri.indexOf(`/${_resourceBase(resource)}/data/`) === 0 : uri.indexOf("/data/") === 0;
881
+ }
882
+ function isDataQueryRequestUri(uri, resource) {
883
+ return resource ? uri.indexOf(`/${_resourceBase(resource)}/data/query`) === 0 : uri.indexOf("/data/query") === 0;
884
+ }
870
885
  function _requestObservable(client, httpRequest, options) {
871
- const uri = options.url || options.uri, config = client.config(), canUseCdn = typeof options.canUseCdn > "u" ? ["GET", "HEAD"].indexOf(options.method || "GET") >= 0 && uri.indexOf("/data/") === 0 : options.canUseCdn;
886
+ const uri = options.url || options.uri, config = client.config(), canUseCdn = typeof options.canUseCdn > "u" ? ["GET", "HEAD"].indexOf(options.method || "GET") >= 0 && isDataRequestUri(uri, config.experimental_resource) : options.canUseCdn;
872
887
  let useCdn = (options.useCdn ?? config.useCdn) && canUseCdn;
873
888
  const tag = options.tag && config.requestTagPrefix ? [config.requestTagPrefix, options.tag].join(".") : options.tag || config.requestTagPrefix;
874
- if (tag && options.tag !== null && (options.query = { tag: requestTag(tag), ...options.query }), ["GET", "HEAD", "POST"].indexOf(options.method || "GET") >= 0 && uri.indexOf("/data/query/") === 0) {
889
+ if (tag && options.tag !== null && (options.query = { tag: requestTag(tag), ...options.query }), ["GET", "HEAD", "POST"].indexOf(options.method || "GET") >= 0 && isDataQueryRequestUri(uri, config.experimental_resource)) {
875
890
  const resultSourceMap = options.resultSourceMap ?? config.resultSourceMap;
876
891
  resultSourceMap !== void 0 && resultSourceMap !== !1 && (options.query = { resultSourceMap, ...options.query });
877
892
  const perspectiveOption = options.perspective || config.perspective;
@@ -897,12 +912,18 @@ function _request(client, httpRequest, options) {
897
912
  );
898
913
  }
899
914
  function _getDataUrl(client, operation, path) {
900
- const config = client.config(), catalog = hasDataset(config), baseUri = `/${operation}/${catalog}`;
915
+ const config = client.config();
916
+ if (config.experimental_resource) {
917
+ const baseUri2 = `/${operation}`;
918
+ return (path ? `${baseUri2}/${path}` : baseUri2).replace(/\/($|\?)/, "$1");
919
+ }
920
+ const catalog = hasDataset(config), baseUri = `/${operation}/${catalog}`;
901
921
  return `/data${path ? `${baseUri}/${path}` : baseUri}`.replace(/\/($|\?)/, "$1");
902
922
  }
903
923
  function _getUrl(client, uri, canUseCdn = !1) {
904
- const { url, cdnUrl } = client.config();
905
- return `${canUseCdn ? cdnUrl : url}/${uri.replace(/^\//, "")}`;
924
+ const { url, cdnUrl, experimental_resource } = client.config();
925
+ let base = canUseCdn ? cdnUrl : url;
926
+ return experimental_resource && uri.indexOf("/users") !== -1 && (base = base.replace(_resourceBase(experimental_resource), "")), `${base}/${uri.replace(/^\//, "")}`;
906
927
  }
907
928
  function _withAbortSignal(signal) {
908
929
  return (input) => new Observable((observer) => {
@@ -956,7 +977,7 @@ function _upload(client, httpRequest, assetType, body, opts = {}) {
956
977
  validateAssetType(assetType);
957
978
  let meta = opts.extract || void 0;
958
979
  meta && !meta.length && (meta = ["none"]);
959
- const dataset2 = hasDataset(client.config()), assetEndpoint = assetType === "image" ? "images" : "files", options = optionsFromFile(opts, body), { tag, label, title, description, creditLine, filename, source } = options, query = {
980
+ const config = client.config(), dataset2 = config.experimental_resource ? void 0 : hasDataset(config), assetEndpoint = assetType === "image" ? "images" : "files", options = optionsFromFile(opts, body), { tag, label, title, description, creditLine, filename, source } = options, query = {
960
981
  label,
961
982
  title,
962
983
  description,
@@ -968,7 +989,7 @@ function _upload(client, httpRequest, assetType, body, opts = {}) {
968
989
  tag,
969
990
  method: "POST",
970
991
  timeout: options.timeout || 0,
971
- uri: `/assets/${assetEndpoint}/${dataset2}`,
992
+ uri: dataset2 ? `/assets/${assetEndpoint}/${dataset2}` : `/assets/${assetEndpoint}`,
972
993
  headers: options.contentType ? { "Content-Type": options.contentType } : {},
973
994
  query,
974
995
  body
@@ -1026,14 +1047,14 @@ function _listen(query, params, opts = {}) {
1026
1047
  )
1027
1048
  );
1028
1049
  }
1029
- const requiredApiVersion = "2021-03-26";
1050
+ const requiredApiVersion = "2021-03-25";
1030
1051
  class LiveClient {
1031
1052
  #client;
1032
1053
  constructor(client) {
1033
1054
  this.#client = client;
1034
1055
  }
1035
1056
  /**
1036
- * Requires `apiVersion` to be `2021-03-26` or later.
1057
+ * Requires `apiVersion` to be `2021-03-25` or later.
1037
1058
  */
1038
1059
  events({
1039
1060
  includeDrafts = !1,
@@ -1054,10 +1075,6 @@ class LiveClient {
1054
1075
  throw new Error(
1055
1076
  "The live events API requires a token or withCredentials when 'includeDrafts: true'. Please update your client configuration. The token should have the lowest possible access role."
1056
1077
  );
1057
- if (includeDrafts && apiVersion !== "X")
1058
- throw new Error(
1059
- "The live events API requires API version X when 'includeDrafts: true'. This API is experimental and may change or even be removed."
1060
- );
1061
1078
  const path = _getDataUrl(this.#client, "live/events"), url = new URL(this.#client.getUrl(path, !1)), tag = _tag && requestTagPrefix ? [requestTagPrefix, _tag].join(".") : _tag;
1062
1079
  tag && url.searchParams.set("tag", tag), includeDrafts && url.searchParams.set("includeDrafts", "true");
1063
1080
  const esOptions = {};
@@ -1195,6 +1212,23 @@ class DatasetsClient {
1195
1212
  );
1196
1213
  }
1197
1214
  }
1215
+ class ThrowingDatasetsClient extends DatasetsClient {
1216
+ constructor(client, httpRequest) {
1217
+ super(client, httpRequest);
1218
+ }
1219
+ create() {
1220
+ throw new Error("cannot create dataset with the current client configuration");
1221
+ }
1222
+ edit() {
1223
+ throw new Error("cannot edit dataset with the current client configuration");
1224
+ }
1225
+ delete() {
1226
+ throw new Error("cannot delete dataset with the current client configuration");
1227
+ }
1228
+ list() {
1229
+ throw new Error("cannot list dataset with the current client configuration");
1230
+ }
1231
+ }
1198
1232
  function _modify(client, httpRequest, method, name, options) {
1199
1233
  return dataset(name), _request(client, httpRequest, {
1200
1234
  method,
@@ -1450,7 +1484,7 @@ class SanityClient {
1450
1484
  */
1451
1485
  listen = _listen;
1452
1486
  constructor(httpRequest, config = defaultConfig) {
1453
- this.config(config), this.#httpRequest = httpRequest, this.assets = new AssetsClient(this, this.#httpRequest), this.datasets = new DatasetsClient(this, this.#httpRequest), this.live = new LiveClient(this), this.projects = new ProjectsClient(this, this.#httpRequest), this.users = new UsersClient(this, this.#httpRequest), this.observable = new ObservableSanityClient(httpRequest, config);
1487
+ this.config(config), this.#httpRequest = httpRequest, this.assets = new AssetsClient(this, this.#httpRequest), this.datasets = this.#clientConfig.experimental_resource ? new ThrowingDatasetsClient(this, this.#httpRequest) : new DatasetsClient(this, this.#httpRequest), this.live = new LiveClient(this), this.projects = new ProjectsClient(this, this.#httpRequest), this.users = new UsersClient(this, this.#httpRequest), this.observable = new ObservableSanityClient(httpRequest, config);
1454
1488
  }
1455
1489
  /**
1456
1490
  * Clone the client - returns a new instance