@sanity/client 6.28.0-instruct.0 → 6.28.0-resources-projects.1

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/src/config.ts CHANGED
@@ -78,6 +78,25 @@ export const initConfig = (
78
78
  ...defaultConfig,
79
79
  ...specifiedConfig,
80
80
  } as InitializedClientConfig
81
+
82
+ const projectId = newConfig.projectId
83
+
84
+ //this "feature" is meant for internal Sanity use only. Do not use.
85
+ if (projectId?.startsWith('resource.')) {
86
+ const [, resourceType, resourceId] = projectId.split('.')
87
+ if (resourceType && resourceId) {
88
+ newConfig.experimental_resource = {
89
+ type: resourceType,
90
+ id: resourceId,
91
+ }
92
+ }
93
+ }
94
+ // resource oriented clients should not use project hostname in base url
95
+ const experimentalResource = newConfig.experimental_resource
96
+ if (experimentalResource) {
97
+ newConfig.useProjectHostname = false
98
+ }
99
+
81
100
  const projectBased = newConfig.useProjectHostname
82
101
 
83
102
  if (typeof Promise === 'undefined') {
@@ -168,5 +187,17 @@ export const initConfig = (
168
187
  newConfig.cdnUrl = newConfig.url
169
188
  }
170
189
 
190
+ if (experimentalResource && experimentalResource.type !== 'projects') {
191
+ const resourceSuffix = `${experimentalResource.type}/${experimentalResource.id}`
192
+ newConfig.url = `${newConfig.url}/${resourceSuffix}`
193
+ newConfig.cdnUrl = `${newConfig.cdnUrl}/${resourceSuffix}`
194
+ }
195
+
196
+ if (experimentalResource?.type === 'projects' && 'dataset' in experimentalResource) {
197
+ const suffix = `projects/${experimentalResource.id}/datasets/${experimentalResource.dataset}`
198
+ newConfig.url = `${newConfig.url}/${suffix}`
199
+ newConfig.cdnUrl = `${newConfig.cdnUrl}/${suffix}`
200
+ }
201
+
171
202
  return newConfig
172
203
  }
@@ -12,6 +12,7 @@ import type {
12
12
  Any,
13
13
  BaseActionOptions,
14
14
  BaseMutationOptions,
15
+ ClientConfig,
15
16
  FirstDocumentIdMutationOptions,
16
17
  FirstDocumentMutationOptions,
17
18
  HttpRequest,
@@ -38,6 +39,8 @@ import {encodeQueryString} from './encodeQueryString'
38
39
  import {ObservablePatch, Patch} from './patch'
39
40
  import {ObservableTransaction, Transaction} from './transaction'
40
41
 
42
+ type ExperimentalResourceConfig = Exclude<ClientConfig['experimental_resource'], undefined>
43
+
41
44
  const excludeFalsey = (param: Any, defValue: Any) => {
42
45
  const value = typeof param === 'undefined' ? defValue : param
43
46
  return param === false ? undefined : value
@@ -367,6 +370,33 @@ export function _create<R extends Record<string, Any>>(
367
370
  return _dataRequest(client, httpRequest, 'mutate', {mutations: [mutation]}, opts)
368
371
  }
369
372
 
373
+ function _resourceBase(resource: ExperimentalResourceConfig) {
374
+ if (resource.type === 'projects' && 'dataset' in resource) {
375
+ return `/projects/${resource.id}/datasets/${resource.dataset}`
376
+ }
377
+
378
+ return `/${resource.type}/${resource.id}`
379
+ }
380
+
381
+ function isDataRequestUri(uri: string, resource?: ExperimentalResourceConfig) {
382
+ if (resource) {
383
+ return uri.indexOf(`/${_resourceBase(resource)}/data/`) === 0
384
+ }
385
+ return uri.indexOf('/data/') === 0
386
+ }
387
+
388
+ function isDataQueryRequestUri(uri: string, resource?: ExperimentalResourceConfig) {
389
+ if (resource && resource.type !== 'projects') {
390
+ return uri.indexOf(`/${_resourceBase(resource)}/data/query`) === 0
391
+ }
392
+
393
+ if (resource && resource.type === 'projects' && 'dataset' in resource) {
394
+ return uri.indexOf(`/projects/${resource.id}/datasets/${resource.dataset}/query`) === 0
395
+ }
396
+
397
+ return uri.indexOf('/data/query') === 0
398
+ }
399
+
370
400
  /**
371
401
  * @internal
372
402
  */
@@ -382,7 +412,8 @@ export function _requestObservable<R>(
382
412
  // Only the /data endpoint is currently available through API-CDN.
383
413
  const canUseCdn =
384
414
  typeof options.canUseCdn === 'undefined'
385
- ? ['GET', 'HEAD'].indexOf(options.method || 'GET') >= 0 && uri.indexOf('/data/') === 0
415
+ ? ['GET', 'HEAD'].indexOf(options.method || 'GET') >= 0 &&
416
+ isDataRequestUri(uri, config.experimental_resource)
386
417
  : options.canUseCdn
387
418
 
388
419
  let useCdn = (options.useCdn ?? config.useCdn) && canUseCdn
@@ -399,7 +430,7 @@ export function _requestObservable<R>(
399
430
  // GROQ query-only parameters
400
431
  if (
401
432
  ['GET', 'HEAD', 'POST'].indexOf(options.method || 'GET') >= 0 &&
402
- uri.indexOf('/data/query/') === 0
433
+ isDataQueryRequestUri(uri, config.experimental_resource)
403
434
  ) {
404
435
  const resultSourceMap = options.resultSourceMap ?? config.resultSourceMap
405
436
  if (resultSourceMap !== undefined && resultSourceMap !== false) {
@@ -482,6 +513,12 @@ export function _getDataUrl(
482
513
  path?: string,
483
514
  ): string {
484
515
  const config = client.config()
516
+
517
+ if (config.experimental_resource) {
518
+ const baseUri = `/${operation}`
519
+ const uri = path ? `${baseUri}/${path}` : baseUri
520
+ return uri.replace(/\/($|\?)/, '$1')
521
+ }
485
522
  const catalog = validators.hasDataset(config)
486
523
  const baseUri = `/${operation}/${catalog}`
487
524
  const uri = path ? `${baseUri}/${path}` : baseUri
@@ -496,8 +533,15 @@ export function _getUrl(
496
533
  uri: string,
497
534
  canUseCdn = false,
498
535
  ): string {
499
- const {url, cdnUrl} = client.config()
500
- const base = canUseCdn ? cdnUrl : url
536
+ const {url, cdnUrl, experimental_resource} = client.config()
537
+ let base = canUseCdn ? cdnUrl : url
538
+
539
+ if (experimental_resource) {
540
+ if (uri.indexOf('/users') !== -1 || uri.indexOf('/assets') !== -1) {
541
+ base = base.replace(_resourceBase(experimental_resource), '')
542
+ }
543
+ }
544
+
501
545
  return `${base}/${uri.replace(/^\//, '')}`
502
546
  }
503
547
 
@@ -106,6 +106,28 @@ export class DatasetsClient {
106
106
  }
107
107
  }
108
108
 
109
+ export class ThrowingDatasetsClient extends DatasetsClient {
110
+ constructor(client: SanityClient, httpRequest: HttpRequest) {
111
+ super(client, httpRequest)
112
+ }
113
+
114
+ create(): Promise<DatasetResponse> {
115
+ throw new Error('cannot create dataset with the current client configuration')
116
+ }
117
+
118
+ edit(): Promise<DatasetResponse> {
119
+ throw new Error('cannot edit dataset with the current client configuration')
120
+ }
121
+
122
+ delete(): Promise<{deleted: true}> {
123
+ throw new Error('cannot delete dataset with the current client configuration')
124
+ }
125
+
126
+ list(): Promise<DatasetsResponse> {
127
+ throw new Error('cannot list dataset with the current client configuration')
128
+ }
129
+ }
130
+
109
131
  function _modify<R = unknown>(
110
132
  client: SanityClient | ObservableSanityClient,
111
133
  httpRequest: HttpRequest,
package/src/types.ts CHANGED
@@ -115,6 +115,13 @@ export interface ClientConfig {
115
115
  * Options for how, if enabled, Content Source Maps are encoded into query results using steganography
116
116
  */
117
117
  stega?: StegaConfig | boolean
118
+
119
+ /**
120
+ * @deprecated Don't use
121
+ */
122
+ experimental_resource?:
123
+ | {type: 'projects'; id: string; dataset: string}
124
+ | {type: string; id: string}
118
125
  }
119
126
 
120
127
  /** @public */
@@ -1320,16 +1327,6 @@ export type ClientReturn<
1320
1327
  Fallback = Any,
1321
1328
  > = GroqString extends keyof SanityQueries ? SanityQueries[GroqString] : Fallback
1322
1329
 
1323
- export type {
1324
- AssistAsyncInstruction,
1325
- AssistInstruction,
1326
- AssistSyncInstruction,
1327
- ConstantInstructionParam,
1328
- FieldInstructionParam,
1329
- GroqInstructionParam,
1330
- InstructionParam,
1331
- InstructionParams,
1332
- } from './assist/types'
1333
1330
  export type {
1334
1331
  ContentSourceMapParsedPath,
1335
1332
  ContentSourceMapParsedPathKeyedSegment,
@@ -2133,7 +2133,17 @@
2133
2133
  const newConfig = {
2134
2134
  ...defaultConfig,
2135
2135
  ...specifiedConfig
2136
- }, projectBased = newConfig.useProjectHostname;
2136
+ }, projectId$1 = newConfig.projectId;
2137
+ if (projectId$1?.startsWith("resource.")) {
2138
+ const [, resourceType, resourceId] = projectId$1.split(".");
2139
+ resourceType && resourceId && (newConfig.experimental_resource = {
2140
+ type: resourceType,
2141
+ id: resourceId
2142
+ });
2143
+ }
2144
+ const experimentalResource = newConfig.experimental_resource;
2145
+ experimentalResource && (newConfig.useProjectHostname = false);
2146
+ const projectBased = newConfig.useProjectHostname;
2137
2147
  if (typeof Promise > "u") {
2138
2148
  const helpUrl = generateHelpUrl("js-client-promise-polyfill");
2139
2149
  throw new Error(`No native Promise-implementation found, polyfill needed - see ${helpUrl}`);
@@ -2159,7 +2169,15 @@
2159
2169
  const isBrowser = typeof window < "u" && window.location && window.location.hostname, isLocalhost = isBrowser && isLocal(window.location.hostname);
2160
2170
  isBrowser && isLocalhost && newConfig.token && newConfig.ignoreBrowserTokenWarning !== true ? 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 === true && newConfig.withCredentials && printCdnAndWithCredentialsWarning(), newConfig.useCdn = newConfig.useCdn !== false && !newConfig.withCredentials, validateApiVersion(newConfig.apiVersion);
2161
2171
  const hostParts = newConfig.apiHost.split("://", 2), protocol = hostParts[0], host = hostParts[1], cdnHost = newConfig.isDefaultApi ? defaultCdnHost : host;
2162
- 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;
2172
+ 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 && experimentalResource.type !== "projects") {
2173
+ const resourceSuffix = `${experimentalResource.type}/${experimentalResource.id}`;
2174
+ newConfig.url = `${newConfig.url}/${resourceSuffix}`, newConfig.cdnUrl = `${newConfig.cdnUrl}/${resourceSuffix}`;
2175
+ }
2176
+ if (experimentalResource?.type === "projects" && "dataset" in experimentalResource) {
2177
+ const suffix = `projects/${experimentalResource.id}/datasets/${experimentalResource.dataset}`;
2178
+ newConfig.url = `${newConfig.url}/${suffix}`, newConfig.cdnUrl = `${newConfig.cdnUrl}/${suffix}`;
2179
+ }
2180
+ return newConfig;
2163
2181
  };
2164
2182
  class ConnectionFailedError extends Error {
2165
2183
  name = "ConnectionFailedError";
@@ -2770,11 +2788,20 @@ ${selectionOpts}`);
2770
2788
  const mutation = { [op]: doc }, opts = Object.assign({ returnFirst: true, returnDocuments: true }, options);
2771
2789
  return _dataRequest(client, httpRequest, "mutate", { mutations: [mutation] }, opts);
2772
2790
  }
2791
+ function _resourceBase(resource) {
2792
+ return resource.type === "projects" && "dataset" in resource ? `/projects/${resource.id}/datasets/${resource.dataset}` : `/${resource.type}/${resource.id}`;
2793
+ }
2794
+ function isDataRequestUri(uri, resource) {
2795
+ return resource ? uri.indexOf(`/${_resourceBase(resource)}/data/`) === 0 : uri.indexOf("/data/") === 0;
2796
+ }
2797
+ function isDataQueryRequestUri(uri, resource) {
2798
+ return resource && resource.type !== "projects" ? uri.indexOf(`/${_resourceBase(resource)}/data/query`) === 0 : resource && resource.type === "projects" && "dataset" in resource ? uri.indexOf(`/projects/${resource.id}/datasets/${resource.dataset}/query`) === 0 : uri.indexOf("/data/query") === 0;
2799
+ }
2773
2800
  function _requestObservable(client, httpRequest, options) {
2774
- 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;
2801
+ 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;
2775
2802
  let useCdn = (options.useCdn ?? config.useCdn) && canUseCdn;
2776
2803
  const tag = options.tag && config.requestTagPrefix ? [config.requestTagPrefix, options.tag].join(".") : options.tag || config.requestTagPrefix;
2777
- 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) {
2804
+ 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)) {
2778
2805
  const resultSourceMap = options.resultSourceMap ?? config.resultSourceMap;
2779
2806
  resultSourceMap !== void 0 && resultSourceMap !== false && (options.query = { resultSourceMap, ...options.query });
2780
2807
  const perspectiveOption = options.perspective || config.perspective;
@@ -2801,12 +2828,18 @@ ${selectionOpts}`);
2801
2828
  );
2802
2829
  }
2803
2830
  function _getDataUrl(client, operation, path) {
2804
- const config = client.config(), catalog = hasDataset(config), baseUri = `/${operation}/${catalog}`;
2831
+ const config = client.config();
2832
+ if (config.experimental_resource) {
2833
+ const baseUri2 = `/${operation}`;
2834
+ return (path ? `${baseUri2}/${path}` : baseUri2).replace(/\/($|\?)/, "$1");
2835
+ }
2836
+ const catalog = hasDataset(config), baseUri = `/${operation}/${catalog}`;
2805
2837
  return `/data${path ? `${baseUri}/${path}` : baseUri}`.replace(/\/($|\?)/, "$1");
2806
2838
  }
2807
2839
  function _getUrl(client, uri, canUseCdn = false) {
2808
- const { url, cdnUrl } = client.config();
2809
- return `${canUseCdn ? cdnUrl : url}/${uri.replace(/^\//, "")}`;
2840
+ const { url, cdnUrl, experimental_resource } = client.config();
2841
+ let base = canUseCdn ? cdnUrl : url;
2842
+ return experimental_resource && (uri.indexOf("/users") !== -1 || uri.indexOf("/assets") !== -1) && (base = base.replace(_resourceBase(experimental_resource), "")), `${base}/${uri.replace(/^\//, "")}`;
2810
2843
  }
2811
2844
  function _withAbortSignal(signal) {
2812
2845
  return (input) => new Observable((observer) => {
@@ -2860,7 +2893,7 @@ ${selectionOpts}`);
2860
2893
  validateAssetType(assetType);
2861
2894
  let meta = opts.extract || void 0;
2862
2895
  meta && !meta.length && (meta = ["none"]);
2863
- const dataset2 = hasDataset(client.config()), assetEndpoint = assetType === "image" ? "images" : "files", options = optionsFromFile(opts, body), { tag, label, title, description, creditLine, filename, source } = options, query = {
2896
+ const config = client.config(), resource = config.experimental_resource, dataset2 = resource ? void 0 : hasDataset(config), assetEndpoint = assetType === "image" ? "images" : "files", options = optionsFromFile(opts, body), { tag, label, title, description, creditLine, filename, source } = options, query = {
2864
2897
  label,
2865
2898
  title,
2866
2899
  description,
@@ -2872,7 +2905,7 @@ ${selectionOpts}`);
2872
2905
  tag,
2873
2906
  method: "POST",
2874
2907
  timeout: options.timeout || 0,
2875
- uri: `/assets/${assetEndpoint}/${dataset2}`,
2908
+ uri: resource ? `/assets/${resource.type}/${resource.id}/${assetEndpoint}` : `/assets/${assetEndpoint}/${dataset2}`,
2876
2909
  headers: options.contentType ? { "Content-Type": options.contentType } : {},
2877
2910
  query,
2878
2911
  body
@@ -2887,42 +2920,6 @@ ${selectionOpts}`);
2887
2920
  opts
2888
2921
  );
2889
2922
  }
2890
- function _instruct(client, httpRequest, request) {
2891
- const dataset2 = hasDataset(client.config());
2892
- return _request(client, httpRequest, {
2893
- method: "POST",
2894
- uri: `/assist/tasks/instruct/${dataset2}`,
2895
- body: request
2896
- });
2897
- }
2898
- class ObservableAssistClient {
2899
- #client;
2900
- #httpRequest;
2901
- constructor(client, httpRequest) {
2902
- this.#client = client, this.#httpRequest = httpRequest;
2903
- }
2904
- /**
2905
- * Run an ad-hoc instruction for a target document.
2906
- * @param request instruction request
2907
- */
2908
- instruct(request) {
2909
- return _instruct(this.#client, this.#httpRequest, request);
2910
- }
2911
- }
2912
- class AssistClient {
2913
- #client;
2914
- #httpRequest;
2915
- constructor(client, httpRequest) {
2916
- this.#client = client, this.#httpRequest = httpRequest;
2917
- }
2918
- /**
2919
- * Run an ad-hoc instruction for a target document.
2920
- * @param request instruction request
2921
- */
2922
- instruct(request) {
2923
- return lastValueFrom(_instruct(this.#client, this.#httpRequest, request));
2924
- }
2925
- }
2926
2923
  var defaults = (obj, defaults2) => Object.keys(defaults2).concat(Object.keys(obj)).reduce((target, prop) => (target[prop] = typeof obj[prop] > "u" ? defaults2[prop] : obj[prop], target), {});
2927
2924
  const pick = (obj, props) => props.reduce((selection, prop) => (typeof obj[prop] > "u" || (selection[prop] = obj[prop]), selection), {}), eventSourcePolyfill = defer(() => Promise.resolve().then(function () { return browser$1; })).pipe(
2928
2925
  map(({ default: EventSource2 }) => EventSource2),
@@ -3163,6 +3160,23 @@ ${selectionOpts}`);
3163
3160
  );
3164
3161
  }
3165
3162
  }
3163
+ class ThrowingDatasetsClient extends DatasetsClient {
3164
+ constructor(client, httpRequest) {
3165
+ super(client, httpRequest);
3166
+ }
3167
+ create() {
3168
+ throw new Error("cannot create dataset with the current client configuration");
3169
+ }
3170
+ edit() {
3171
+ throw new Error("cannot edit dataset with the current client configuration");
3172
+ }
3173
+ delete() {
3174
+ throw new Error("cannot delete dataset with the current client configuration");
3175
+ }
3176
+ list() {
3177
+ throw new Error("cannot list dataset with the current client configuration");
3178
+ }
3179
+ }
3166
3180
  function _modify(client, httpRequest, method, name, options) {
3167
3181
  return dataset(name), _request(client, httpRequest, {
3168
3182
  method,
@@ -3255,7 +3269,6 @@ ${selectionOpts}`);
3255
3269
  live;
3256
3270
  projects;
3257
3271
  users;
3258
- assist;
3259
3272
  /**
3260
3273
  * Private properties
3261
3274
  */
@@ -3266,7 +3279,7 @@ ${selectionOpts}`);
3266
3279
  */
3267
3280
  listen = _listen;
3268
3281
  constructor(httpRequest, config = defaultConfig) {
3269
- this.config(config), this.#httpRequest = httpRequest, this.assets = new ObservableAssetsClient(this, this.#httpRequest), this.datasets = new ObservableDatasetsClient(this, this.#httpRequest), this.live = new LiveClient(this), this.projects = new ObservableProjectsClient(this, this.#httpRequest), this.users = new ObservableUsersClient(this, this.#httpRequest), this.assist = new ObservableAssistClient(this, this.#httpRequest);
3282
+ this.config(config), this.#httpRequest = httpRequest, this.assets = new ObservableAssetsClient(this, this.#httpRequest), this.datasets = new ObservableDatasetsClient(this, this.#httpRequest), this.live = new LiveClient(this), this.projects = new ObservableProjectsClient(this, this.#httpRequest), this.users = new ObservableUsersClient(this, this.#httpRequest);
3270
3283
  }
3271
3284
  /**
3272
3285
  * Clone the client - returns a new instance
@@ -3405,7 +3418,6 @@ ${selectionOpts}`);
3405
3418
  live;
3406
3419
  projects;
3407
3420
  users;
3408
- assist;
3409
3421
  /**
3410
3422
  * Observable version of the Sanity client, with the same configuration as the promise-based one
3411
3423
  */
@@ -3420,7 +3432,7 @@ ${selectionOpts}`);
3420
3432
  */
3421
3433
  listen = _listen;
3422
3434
  constructor(httpRequest, config = defaultConfig) {
3423
- 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.assist = new AssistClient(this, this.#httpRequest), this.observable = new ObservableSanityClient(httpRequest, config);
3435
+ 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);
3424
3436
  }
3425
3437
  /**
3426
3438
  * Clone the client - returns a new instance