@sanity/client 6.28.3-instruct.1 → 6.28.4-beta.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/src/config.ts CHANGED
@@ -28,15 +28,30 @@ function validateApiVersion(apiVersion: string) {
28
28
  }
29
29
  }
30
30
 
31
+ const VALID_PERSPECTIVE = /^[a-z0-9_]+$/i
32
+
31
33
  /**
32
34
  * @internal - it may have breaking changes in any release
33
35
  */
34
36
  export function validateApiPerspective(
35
37
  perspective: unknown,
36
38
  ): asserts perspective is ClientPerspective {
37
- if (Array.isArray(perspective) && perspective.length > 1 && perspective.includes('raw')) {
39
+ if (Array.isArray(perspective)) {
40
+ if (perspective.includes('raw')) {
41
+ throw new TypeError(
42
+ `Invalid API perspective value: "raw". The raw-perspective can not be combined with other perspectives`,
43
+ )
44
+ }
45
+ }
46
+
47
+ const invalid = (Array.isArray(perspective) ? perspective : [perspective]).filter(
48
+ (perspectiveName) =>
49
+ typeof perspectiveName !== 'string' || !VALID_PERSPECTIVE.test(perspectiveName),
50
+ )
51
+ if (invalid.length > 0) {
52
+ const formatted = invalid.map((v) => JSON.stringify(v))
38
53
  throw new TypeError(
39
- `Invalid API perspective value: "raw". The raw-perspective can not be combined with other perspectives`,
54
+ `Invalid API perspective value${invalid.length === 1 ? '' : 's'}: ${formatted.join(', ')}, expected \`published\`, \`drafts\`, \`raw\` or a release identifier string`,
40
55
  )
41
56
  }
42
57
  }
@@ -108,9 +123,11 @@ export const initConfig = (
108
123
  const isLocalhost = isBrowser && isLocal(window.location.hostname)
109
124
 
110
125
  const hasToken = Boolean(newConfig.token)
111
- if (newConfig.withCredentials && hasToken) {
126
+ let withCredentials = Boolean(newConfig.withCredentials)
127
+
128
+ if (withCredentials && hasToken) {
112
129
  warnings.printCredentialedTokenWarning()
113
- newConfig.withCredentials = false
130
+ withCredentials = false
114
131
  }
115
132
 
116
133
  if (isBrowser && isLocalhost && hasToken && newConfig.ignoreBrowserTokenWarning !== true) {
@@ -137,12 +154,12 @@ export const initConfig = (
137
154
  newConfig.apiVersion = `${newConfig.apiVersion}`.replace(/^v/, '')
138
155
  newConfig.isDefaultApi = newConfig.apiHost === defaultConfig.apiHost
139
156
 
140
- if (newConfig.useCdn === true && newConfig.withCredentials) {
157
+ if (newConfig.useCdn === true && withCredentials) {
141
158
  warnings.printCdnAndWithCredentialsWarning()
142
159
  }
143
160
 
144
161
  // If `useCdn` is undefined, we treat it as `true`
145
- newConfig.useCdn = newConfig.useCdn !== false && !newConfig.withCredentials
162
+ newConfig.useCdn = newConfig.useCdn !== false && !withCredentials
146
163
 
147
164
  validateApiVersion(newConfig.apiVersion)
148
165
 
package/src/types.ts CHANGED
@@ -1336,16 +1336,6 @@ export type ClientReturn<
1336
1336
  Fallback = Any,
1337
1337
  > = GroqString extends keyof SanityQueries ? SanityQueries[GroqString] : Fallback
1338
1338
 
1339
- export type {
1340
- InstructAsyncInstruction,
1341
- InstructConstantInstructionParam,
1342
- InstructFieldInstructionParam,
1343
- InstructGroqInstructionParam,
1344
- InstructInstruction,
1345
- InstructInstructionParam,
1346
- InstructInstructionParams,
1347
- InstructSyncInstruction,
1348
- } from './ai/types'
1349
1339
  export type {
1350
1340
  ContentSourceMapParsedPath,
1351
1341
  ContentSourceMapParsedPathKeyedSegment,
@@ -2109,11 +2109,21 @@
2109
2109
  if (!(/^\d{4}-\d{2}-\d{2}$/.test(apiVersion) && apiDate instanceof Date && apiDate.getTime() > 0))
2110
2110
  throw new Error("Invalid API version string, expected `1` or date in format `YYYY-MM-DD`");
2111
2111
  }
2112
+ const VALID_PERSPECTIVE = /^[a-z0-9_]+$/i;
2112
2113
  function validateApiPerspective(perspective) {
2113
- if (Array.isArray(perspective) && perspective.length > 1 && perspective.includes("raw"))
2114
+ if (Array.isArray(perspective) && perspective.includes("raw"))
2114
2115
  throw new TypeError(
2115
2116
  'Invalid API perspective value: "raw". The raw-perspective can not be combined with other perspectives'
2116
2117
  );
2118
+ const invalid = (Array.isArray(perspective) ? perspective : [perspective]).filter(
2119
+ (perspectiveName) => typeof perspectiveName != "string" || !VALID_PERSPECTIVE.test(perspectiveName)
2120
+ );
2121
+ if (invalid.length > 0) {
2122
+ const formatted = invalid.map((v) => JSON.stringify(v));
2123
+ throw new TypeError(
2124
+ `Invalid API perspective value${invalid.length === 1 ? "" : "s"}: ${formatted.join(", ")}, expected \`published\`, \`drafts\`, \`raw\` or a release identifier string`
2125
+ );
2126
+ }
2117
2127
  }
2118
2128
  const initConfig = (config, prevConfig) => {
2119
2129
  const specifiedConfig = {
@@ -2152,7 +2162,8 @@
2152
2162
  `stega.studioUrl must be a string or a function, received ${newConfig.stega.studioUrl}`
2153
2163
  );
2154
2164
  const isBrowser = typeof window < "u" && window.location && window.location.hostname, isLocalhost = isBrowser && isLocal(window.location.hostname), hasToken = !!newConfig.token;
2155
- newConfig.withCredentials && hasToken && (printCredentialedTokenWarning(), newConfig.withCredentials = false), isBrowser && isLocalhost && hasToken && 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);
2165
+ let withCredentials = !!newConfig.withCredentials;
2166
+ withCredentials && hasToken && (printCredentialedTokenWarning(), withCredentials = false), isBrowser && isLocalhost && hasToken && 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 && withCredentials && printCdnAndWithCredentialsWarning(), newConfig.useCdn = newConfig.useCdn !== false && !withCredentials, validateApiVersion(newConfig.apiVersion);
2156
2167
  const hostParts = newConfig.apiHost.split("://", 2), protocol = hostParts[0], host = hostParts[1], cdnHost = newConfig.isDefaultApi ? defaultCdnHost : host;
2157
2168
  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;
2158
2169
  };
@@ -2823,42 +2834,6 @@ ${selectionOpts}`);
2823
2834
  const error = new Error(signal?.reason ?? "The operation was aborted.");
2824
2835
  return error.name = "AbortError", error;
2825
2836
  }
2826
- function _instruct(client, httpRequest, request) {
2827
- const dataset2 = hasDataset(client.config());
2828
- return _request(client, httpRequest, {
2829
- method: "POST",
2830
- uri: `/assist/tasks/instruct/${dataset2}`,
2831
- body: request
2832
- });
2833
- }
2834
- class ObservableAiClient {
2835
- #client;
2836
- #httpRequest;
2837
- constructor(client, httpRequest) {
2838
- this.#client = client, this.#httpRequest = httpRequest;
2839
- }
2840
- /**
2841
- * Run an ad-hoc instruction for a target document.
2842
- * @param request instruction request
2843
- */
2844
- instruct(request) {
2845
- return _instruct(this.#client, this.#httpRequest, request);
2846
- }
2847
- }
2848
- class AiClient {
2849
- #client;
2850
- #httpRequest;
2851
- constructor(client, httpRequest) {
2852
- this.#client = client, this.#httpRequest = httpRequest;
2853
- }
2854
- /**
2855
- * Run an ad-hoc instruction for a target document.
2856
- * @param request instruction request
2857
- */
2858
- instruct(request) {
2859
- return lastValueFrom(_instruct(this.#client, this.#httpRequest, request));
2860
- }
2861
- }
2862
2837
  class ObservableAssetsClient {
2863
2838
  #client;
2864
2839
  #httpRequest;
@@ -3253,7 +3228,6 @@ ${selectionOpts}`);
3253
3228
  live;
3254
3229
  projects;
3255
3230
  users;
3256
- ai;
3257
3231
  /**
3258
3232
  * Private properties
3259
3233
  */
@@ -3264,7 +3238,7 @@ ${selectionOpts}`);
3264
3238
  */
3265
3239
  listen = _listen;
3266
3240
  constructor(httpRequest, config = defaultConfig) {
3267
- 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.ai = new ObservableAiClient(this, this.#httpRequest);
3241
+ 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);
3268
3242
  }
3269
3243
  /**
3270
3244
  * Clone the client - returns a new instance
@@ -3403,7 +3377,6 @@ ${selectionOpts}`);
3403
3377
  live;
3404
3378
  projects;
3405
3379
  users;
3406
- ai;
3407
3380
  /**
3408
3381
  * Observable version of the Sanity client, with the same configuration as the promise-based one
3409
3382
  */
@@ -3418,7 +3391,7 @@ ${selectionOpts}`);
3418
3391
  */
3419
3392
  listen = _listen;
3420
3393
  constructor(httpRequest, config = defaultConfig) {
3421
- 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.ai = new AiClient(this, this.#httpRequest), this.observable = new ObservableSanityClient(httpRequest, config);
3394
+ 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);
3422
3395
  }
3423
3396
  /**
3424
3397
  * Clone the client - returns a new instance