@sanity/client 6.24.0-canary.0 → 6.24.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.
@@ -3140,6 +3140,13 @@ export declare class UsersClient {
3140
3140
  getById<T extends 'me' | string>(id: T): Promise<T extends 'me' ? CurrentSanityUser : SanityUser>
3141
3141
  }
3142
3142
 
3143
+ /**
3144
+ * @internal - it may have breaking changes in any release
3145
+ */
3146
+ export declare function validateApiPerspective(
3147
+ perspective: unknown,
3148
+ ): asserts perspective is ClientPerspective
3149
+
3143
3150
  /**
3144
3151
  * The listener has been established, and will start receiving events.
3145
3152
  * Note that this is also emitted upon _reconnection_.
@@ -3140,6 +3140,13 @@ export declare class UsersClient {
3140
3140
  getById<T extends 'me' | string>(id: T): Promise<T extends 'me' ? CurrentSanityUser : SanityUser>
3141
3141
  }
3142
3142
 
3143
+ /**
3144
+ * @internal - it may have breaking changes in any release
3145
+ */
3146
+ export declare function validateApiPerspective(
3147
+ perspective: unknown,
3148
+ ): asserts perspective is ClientPerspective
3149
+
3143
3150
  /**
3144
3151
  * The listener has been established, and will start receiving events.
3145
3152
  * Note that this is also emitted upon _reconnection_.
@@ -105,22 +105,9 @@ function shouldRetry(err, attempt, options) {
105
105
  const isSafe = options.method === "GET" || options.method === "HEAD", isQuery = (options.uri || options.url).startsWith("/data/query"), isRetriableResponse = err.response && (err.response.statusCode === 429 || err.response.statusCode === 502 || err.response.statusCode === 503);
106
106
  return (isSafe || isQuery) && isRetriableResponse ? !0 : retry.shouldRetry(err, attempt, options);
107
107
  }
108
- function getSelection(sel) {
109
- if (typeof sel == "string")
110
- return { id: sel };
111
- if (Array.isArray(sel))
112
- return { query: "*[_id in $ids]", params: { ids: sel } };
113
- if (typeof sel == "object" && sel !== null && "query" in sel && typeof sel.query == "string")
114
- return "params" in sel && typeof sel.params == "object" && sel.params !== null ? { query: sel.query, params: sel.params } : { query: sel.query };
115
- const selectionOpts = [
116
- "* Document ID (<docId>)",
117
- "* Array of document IDs",
118
- "* Object containing `query`"
119
- ].join(`
120
- `);
121
- throw new Error(`Unknown selection - must be one of:
122
-
123
- ${selectionOpts}`);
108
+ const BASE_URL = "https://www.sanity.io/help/";
109
+ function generateHelpUrl(slug) {
110
+ return BASE_URL + slug;
124
111
  }
125
112
  const VALID_ASSET_TYPES = ["image", "file"], VALID_INSERT_LOCATIONS = ["before", "after", "replace"], dataset = (name) => {
126
113
  if (!/^(~[a-z0-9]{1}[-\w]{0,63}|[a-z0-9]{1}[-\w]{0,63})$/.test(name))
@@ -164,6 +151,125 @@ const VALID_ASSET_TYPES = ["image", "file"], VALID_INSERT_LOCATIONS = ["before",
164
151
  );
165
152
  return tag;
166
153
  };
154
+ function once(fn) {
155
+ let didCall = !1, returnValue;
156
+ return (...args) => (didCall || (returnValue = fn(...args), didCall = !0), returnValue);
157
+ }
158
+ const createWarningPrinter = (message) => (
159
+ // eslint-disable-next-line no-console
160
+ once((...args) => console.warn(message.join(" "), ...args))
161
+ ), printCdnAndWithCredentialsWarning = createWarningPrinter([
162
+ "Because you set `withCredentials` to true, we will override your `useCdn`",
163
+ "setting to be false since (cookie-based) credentials are never set on the CDN"
164
+ ]), printCdnWarning = createWarningPrinter([
165
+ "Since you haven't set a value for `useCdn`, we will deliver content using our",
166
+ "global, edge-cached API-CDN. If you wish to have content delivered faster, set",
167
+ "`useCdn: false` to use the Live API. Note: You may incur higher costs using the live API."
168
+ ]), printCdnPreviewDraftsWarning = createWarningPrinter([
169
+ "The Sanity client is configured with the `perspective` set to `previewDrafts`, which doesn't support the API-CDN.",
170
+ "The Live API will be used instead. Set `useCdn: false` in your configuration to hide this warning."
171
+ ]), printBrowserTokenWarning = createWarningPrinter([
172
+ "You have configured Sanity client to use a token in the browser. This may cause unintentional security issues.",
173
+ `See ${generateHelpUrl(
174
+ "js-client-browser-token"
175
+ )} for more information and how to hide this warning.`
176
+ ]), printNoApiVersionSpecifiedWarning = createWarningPrinter([
177
+ "Using the Sanity client without specifying an API version is deprecated.",
178
+ `See ${generateHelpUrl("js-client-api-version")}`
179
+ ]), printNoDefaultExport = createWarningPrinter([
180
+ "The default export of @sanity/client has been deprecated. Use the named export `createClient` instead."
181
+ ]), defaultCdnHost = "apicdn.sanity.io", defaultConfig = {
182
+ apiHost: "https://api.sanity.io",
183
+ apiVersion: "1",
184
+ useProjectHostname: !0,
185
+ stega: { enabled: !1 }
186
+ }, LOCALHOSTS = ["localhost", "127.0.0.1", "0.0.0.0"], isLocal = (host) => LOCALHOSTS.indexOf(host) !== -1;
187
+ function validateApiVersion(apiVersion) {
188
+ if (apiVersion === "1" || apiVersion === "X")
189
+ return;
190
+ const apiDate = new Date(apiVersion);
191
+ if (!(/^\d{4}-\d{2}-\d{2}$/.test(apiVersion) && apiDate instanceof Date && apiDate.getTime() > 0))
192
+ throw new Error("Invalid API version string, expected `1` or date in format `YYYY-MM-DD`");
193
+ }
194
+ function validateApiPerspective(perspective) {
195
+ if (Array.isArray(perspective)) {
196
+ for (const perspectiveValue of perspective)
197
+ if (perspectiveValue !== "published" && perspectiveValue !== "drafts" && !(typeof perspectiveValue == "string" && perspectiveValue.startsWith("r") && perspectiveValue !== "raw"))
198
+ throw new TypeError(
199
+ "Invalid API perspective value, expected `published`, `drafts` or a valid release identifier string"
200
+ );
201
+ return;
202
+ }
203
+ switch (perspective) {
204
+ case "previewDrafts":
205
+ case "drafts":
206
+ case "published":
207
+ case "raw":
208
+ return;
209
+ default:
210
+ throw new TypeError(
211
+ "Invalid API perspective string, expected `published`, `previewDrafts` or `raw`"
212
+ );
213
+ }
214
+ }
215
+ const initConfig = (config, prevConfig) => {
216
+ const specifiedConfig = {
217
+ ...prevConfig,
218
+ ...config,
219
+ stega: {
220
+ ...typeof prevConfig.stega == "boolean" ? { enabled: prevConfig.stega } : prevConfig.stega || defaultConfig.stega,
221
+ ...typeof config.stega == "boolean" ? { enabled: config.stega } : config.stega || {}
222
+ }
223
+ };
224
+ specifiedConfig.apiVersion || printNoApiVersionSpecifiedWarning();
225
+ const newConfig = {
226
+ ...defaultConfig,
227
+ ...specifiedConfig
228
+ }, projectBased = newConfig.useProjectHostname;
229
+ if (typeof Promise > "u") {
230
+ const helpUrl = generateHelpUrl("js-client-promise-polyfill");
231
+ throw new Error(`No native Promise-implementation found, polyfill needed - see ${helpUrl}`);
232
+ }
233
+ if (projectBased && !newConfig.projectId)
234
+ throw new Error("Configuration must contain `projectId`");
235
+ if (typeof newConfig.perspective < "u" && validateApiPerspective(newConfig.perspective), "encodeSourceMap" in newConfig)
236
+ throw new Error(
237
+ "It looks like you're using options meant for '@sanity/preview-kit/client'. 'encodeSourceMap' is not supported in '@sanity/client'. Did you mean 'stega.enabled'?"
238
+ );
239
+ if ("encodeSourceMapAtPath" in newConfig)
240
+ throw new Error(
241
+ "It looks like you're using options meant for '@sanity/preview-kit/client'. 'encodeSourceMapAtPath' is not supported in '@sanity/client'. Did you mean 'stega.filter'?"
242
+ );
243
+ if (typeof newConfig.stega.enabled != "boolean")
244
+ throw new Error(`stega.enabled must be a boolean, received ${newConfig.stega.enabled}`);
245
+ if (newConfig.stega.enabled && newConfig.stega.studioUrl === void 0)
246
+ throw new Error("stega.studioUrl must be defined when stega.enabled is true");
247
+ if (newConfig.stega.enabled && typeof newConfig.stega.studioUrl != "string" && typeof newConfig.stega.studioUrl != "function")
248
+ throw new Error(
249
+ `stega.studioUrl must be a string or a function, received ${newConfig.stega.studioUrl}`
250
+ );
251
+ const isBrowser = typeof window < "u" && window.location && window.location.hostname, isLocalhost = isBrowser && isLocal(window.location.hostname);
252
+ 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);
253
+ const hostParts = newConfig.apiHost.split("://", 2), protocol = hostParts[0], host = hostParts[1], cdnHost = newConfig.isDefaultApi ? defaultCdnHost : host;
254
+ 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;
255
+ };
256
+ function getSelection(sel) {
257
+ if (typeof sel == "string")
258
+ return { id: sel };
259
+ if (Array.isArray(sel))
260
+ return { query: "*[_id in $ids]", params: { ids: sel } };
261
+ if (typeof sel == "object" && sel !== null && "query" in sel && typeof sel.query == "string")
262
+ return "params" in sel && typeof sel.params == "object" && sel.params !== null ? { query: sel.query, params: sel.params } : { query: sel.query };
263
+ const selectionOpts = [
264
+ "* Document ID (<docId>)",
265
+ "* Array of document IDs",
266
+ "* Object containing `query`"
267
+ ].join(`
268
+ `);
269
+ throw new Error(`Unknown selection - must be one of:
270
+
271
+ ${selectionOpts}`);
272
+ }
167
273
  class BasePatch {
168
274
  selection;
169
275
  operations;
@@ -477,111 +583,7 @@ class ObservableTransaction extends BaseTransaction {
477
583
  return this._add({ patch: { id: patchOrDocumentId, ...patchOps } });
478
584
  }
479
585
  }
480
- const BASE_URL = "https://www.sanity.io/help/";
481
- function generateHelpUrl(slug) {
482
- return BASE_URL + slug;
483
- }
484
- function once(fn) {
485
- let didCall = !1, returnValue;
486
- return (...args) => (didCall || (returnValue = fn(...args), didCall = !0), returnValue);
487
- }
488
- const createWarningPrinter = (message) => (
489
- // eslint-disable-next-line no-console
490
- once((...args) => console.warn(message.join(" "), ...args))
491
- ), printCdnAndWithCredentialsWarning = createWarningPrinter([
492
- "Because you set `withCredentials` to true, we will override your `useCdn`",
493
- "setting to be false since (cookie-based) credentials are never set on the CDN"
494
- ]), printCdnWarning = createWarningPrinter([
495
- "Since you haven't set a value for `useCdn`, we will deliver content using our",
496
- "global, edge-cached API-CDN. If you wish to have content delivered faster, set",
497
- "`useCdn: false` to use the Live API. Note: You may incur higher costs using the live API."
498
- ]), printCdnPreviewDraftsWarning = createWarningPrinter([
499
- "The Sanity client is configured with the `perspective` set to `previewDrafts`, which doesn't support the API-CDN.",
500
- "The Live API will be used instead. Set `useCdn: false` in your configuration to hide this warning."
501
- ]), printBrowserTokenWarning = createWarningPrinter([
502
- "You have configured Sanity client to use a token in the browser. This may cause unintentional security issues.",
503
- `See ${generateHelpUrl(
504
- "js-client-browser-token"
505
- )} for more information and how to hide this warning.`
506
- ]), printNoApiVersionSpecifiedWarning = createWarningPrinter([
507
- "Using the Sanity client without specifying an API version is deprecated.",
508
- `See ${generateHelpUrl("js-client-api-version")}`
509
- ]), printNoDefaultExport = createWarningPrinter([
510
- "The default export of @sanity/client has been deprecated. Use the named export `createClient` instead."
511
- ]), defaultCdnHost = "apicdn.sanity.io", defaultConfig = {
512
- apiHost: "https://api.sanity.io",
513
- apiVersion: "1",
514
- useProjectHostname: !0,
515
- stega: { enabled: !1 }
516
- }, LOCALHOSTS = ["localhost", "127.0.0.1", "0.0.0.0"], isLocal = (host) => LOCALHOSTS.indexOf(host) !== -1;
517
- function validateApiVersion(apiVersion) {
518
- if (apiVersion === "1" || apiVersion === "X")
519
- return;
520
- const apiDate = new Date(apiVersion);
521
- if (!(/^\d{4}-\d{2}-\d{2}$/.test(apiVersion) && apiDate instanceof Date && apiDate.getTime() > 0))
522
- throw new Error("Invalid API version string, expected `1` or date in format `YYYY-MM-DD`");
523
- }
524
- const validateApiPerspective = function(perspective) {
525
- if (Array.isArray(perspective)) {
526
- for (const perspectiveValue of perspective)
527
- if (perspectiveValue !== "published" && perspectiveValue !== "drafts" && !(typeof perspectiveValue == "string" && perspectiveValue.startsWith("r") && perspectiveValue !== "raw"))
528
- throw new TypeError(
529
- "Invalid API perspective value, expected `published`, `drafts` or a valid release identifier string"
530
- );
531
- return;
532
- }
533
- switch (perspective) {
534
- case "previewDrafts":
535
- case "drafts":
536
- case "published":
537
- case "raw":
538
- return;
539
- default:
540
- throw new TypeError(
541
- "Invalid API perspective string, expected `published`, `previewDrafts` or `raw`"
542
- );
543
- }
544
- }, initConfig = (config, prevConfig) => {
545
- const specifiedConfig = {
546
- ...prevConfig,
547
- ...config,
548
- stega: {
549
- ...typeof prevConfig.stega == "boolean" ? { enabled: prevConfig.stega } : prevConfig.stega || defaultConfig.stega,
550
- ...typeof config.stega == "boolean" ? { enabled: config.stega } : config.stega || {}
551
- }
552
- };
553
- specifiedConfig.apiVersion || printNoApiVersionSpecifiedWarning();
554
- const newConfig = {
555
- ...defaultConfig,
556
- ...specifiedConfig
557
- }, projectBased = newConfig.useProjectHostname;
558
- if (typeof Promise > "u") {
559
- const helpUrl = generateHelpUrl("js-client-promise-polyfill");
560
- throw new Error(`No native Promise-implementation found, polyfill needed - see ${helpUrl}`);
561
- }
562
- if (projectBased && !newConfig.projectId)
563
- throw new Error("Configuration must contain `projectId`");
564
- if (typeof newConfig.perspective < "u" && validateApiPerspective(newConfig.perspective), "encodeSourceMap" in newConfig)
565
- throw new Error(
566
- "It looks like you're using options meant for '@sanity/preview-kit/client'. 'encodeSourceMap' is not supported in '@sanity/client'. Did you mean 'stega.enabled'?"
567
- );
568
- if ("encodeSourceMapAtPath" in newConfig)
569
- throw new Error(
570
- "It looks like you're using options meant for '@sanity/preview-kit/client'. 'encodeSourceMapAtPath' is not supported in '@sanity/client'. Did you mean 'stega.filter'?"
571
- );
572
- if (typeof newConfig.stega.enabled != "boolean")
573
- throw new Error(`stega.enabled must be a boolean, received ${newConfig.stega.enabled}`);
574
- if (newConfig.stega.enabled && newConfig.stega.studioUrl === void 0)
575
- throw new Error("stega.studioUrl must be defined when stega.enabled is true");
576
- if (newConfig.stega.enabled && typeof newConfig.stega.studioUrl != "string" && typeof newConfig.stega.studioUrl != "function")
577
- throw new Error(
578
- `stega.studioUrl must be a string or a function, received ${newConfig.stega.studioUrl}`
579
- );
580
- const isBrowser = typeof window < "u" && window.location && window.location.hostname, isLocalhost = isBrowser && isLocal(window.location.hostname);
581
- 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);
582
- const hostParts = newConfig.apiHost.split("://", 2), protocol = hostParts[0], host = hostParts[1], cdnHost = newConfig.isDefaultApi ? defaultCdnHost : host;
583
- 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;
584
- }, projectHeader = "X-Sanity-Project-ID";
586
+ const projectHeader = "X-Sanity-Project-ID";
585
587
  function requestOptions(config, overrides = {}) {
586
588
  const headers = {}, token = overrides.token || config.token;
587
589
  token && (headers.Authorization = `Bearer ${token}`), !overrides.useGlobalApi && !config.useProjectHostname && config.projectId && (headers[projectHeader] = config.projectId);
@@ -1581,6 +1583,7 @@ export {
1581
1583
  deprecatedCreateClient as default,
1582
1584
  requester,
1583
1585
  adapter as unstable__adapter,
1584
- environment as unstable__environment
1586
+ environment as unstable__environment,
1587
+ validateApiPerspective
1585
1588
  };
1586
1589
  //# sourceMappingURL=index.browser.js.map