@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.
@@ -121,22 +121,9 @@ function shouldRetry(err, attempt, options) {
121
121
  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);
122
122
  return (isSafe || isQuery) && isRetriableResponse ? !0 : middleware.retry.shouldRetry(err, attempt, options);
123
123
  }
124
- function getSelection(sel) {
125
- if (typeof sel == "string")
126
- return { id: sel };
127
- if (Array.isArray(sel))
128
- return { query: "*[_id in $ids]", params: { ids: sel } };
129
- if (typeof sel == "object" && sel !== null && "query" in sel && typeof sel.query == "string")
130
- return "params" in sel && typeof sel.params == "object" && sel.params !== null ? { query: sel.query, params: sel.params } : { query: sel.query };
131
- const selectionOpts = [
132
- "* Document ID (<docId>)",
133
- "* Array of document IDs",
134
- "* Object containing `query`"
135
- ].join(`
136
- `);
137
- throw new Error(`Unknown selection - must be one of:
138
-
139
- ${selectionOpts}`);
124
+ const BASE_URL = "https://www.sanity.io/help/";
125
+ function generateHelpUrl(slug) {
126
+ return BASE_URL + slug;
140
127
  }
141
128
  const VALID_ASSET_TYPES = ["image", "file"], VALID_INSERT_LOCATIONS = ["before", "after", "replace"], dataset = (name) => {
142
129
  if (!/^(~[a-z0-9]{1}[-\w]{0,63}|[a-z0-9]{1}[-\w]{0,63})$/.test(name))
@@ -180,6 +167,125 @@ const VALID_ASSET_TYPES = ["image", "file"], VALID_INSERT_LOCATIONS = ["before",
180
167
  );
181
168
  return tag;
182
169
  };
170
+ function once(fn) {
171
+ let didCall = !1, returnValue;
172
+ return (...args) => (didCall || (returnValue = fn(...args), didCall = !0), returnValue);
173
+ }
174
+ const createWarningPrinter = (message) => (
175
+ // eslint-disable-next-line no-console
176
+ once((...args) => console.warn(message.join(" "), ...args))
177
+ ), printCdnAndWithCredentialsWarning = createWarningPrinter([
178
+ "Because you set `withCredentials` to true, we will override your `useCdn`",
179
+ "setting to be false since (cookie-based) credentials are never set on the CDN"
180
+ ]), printCdnWarning = createWarningPrinter([
181
+ "Since you haven't set a value for `useCdn`, we will deliver content using our",
182
+ "global, edge-cached API-CDN. If you wish to have content delivered faster, set",
183
+ "`useCdn: false` to use the Live API. Note: You may incur higher costs using the live API."
184
+ ]), printCdnPreviewDraftsWarning = createWarningPrinter([
185
+ "The Sanity client is configured with the `perspective` set to `previewDrafts`, which doesn't support the API-CDN.",
186
+ "The Live API will be used instead. Set `useCdn: false` in your configuration to hide this warning."
187
+ ]), printBrowserTokenWarning = createWarningPrinter([
188
+ "You have configured Sanity client to use a token in the browser. This may cause unintentional security issues.",
189
+ `See ${generateHelpUrl(
190
+ "js-client-browser-token"
191
+ )} for more information and how to hide this warning.`
192
+ ]), printNoApiVersionSpecifiedWarning = createWarningPrinter([
193
+ "Using the Sanity client without specifying an API version is deprecated.",
194
+ `See ${generateHelpUrl("js-client-api-version")}`
195
+ ]), printNoDefaultExport = createWarningPrinter([
196
+ "The default export of @sanity/client has been deprecated. Use the named export `createClient` instead."
197
+ ]), defaultCdnHost = "apicdn.sanity.io", defaultConfig = {
198
+ apiHost: "https://api.sanity.io",
199
+ apiVersion: "1",
200
+ useProjectHostname: !0,
201
+ stega: { enabled: !1 }
202
+ }, LOCALHOSTS = ["localhost", "127.0.0.1", "0.0.0.0"], isLocal = (host) => LOCALHOSTS.indexOf(host) !== -1;
203
+ function validateApiVersion(apiVersion) {
204
+ if (apiVersion === "1" || apiVersion === "X")
205
+ return;
206
+ const apiDate = new Date(apiVersion);
207
+ if (!(/^\d{4}-\d{2}-\d{2}$/.test(apiVersion) && apiDate instanceof Date && apiDate.getTime() > 0))
208
+ throw new Error("Invalid API version string, expected `1` or date in format `YYYY-MM-DD`");
209
+ }
210
+ function validateApiPerspective(perspective) {
211
+ if (Array.isArray(perspective)) {
212
+ for (const perspectiveValue of perspective)
213
+ if (perspectiveValue !== "published" && perspectiveValue !== "drafts" && !(typeof perspectiveValue == "string" && perspectiveValue.startsWith("r") && perspectiveValue !== "raw"))
214
+ throw new TypeError(
215
+ "Invalid API perspective value, expected `published`, `drafts` or a valid release identifier string"
216
+ );
217
+ return;
218
+ }
219
+ switch (perspective) {
220
+ case "previewDrafts":
221
+ case "drafts":
222
+ case "published":
223
+ case "raw":
224
+ return;
225
+ default:
226
+ throw new TypeError(
227
+ "Invalid API perspective string, expected `published`, `previewDrafts` or `raw`"
228
+ );
229
+ }
230
+ }
231
+ const initConfig = (config, prevConfig) => {
232
+ const specifiedConfig = {
233
+ ...prevConfig,
234
+ ...config,
235
+ stega: {
236
+ ...typeof prevConfig.stega == "boolean" ? { enabled: prevConfig.stega } : prevConfig.stega || defaultConfig.stega,
237
+ ...typeof config.stega == "boolean" ? { enabled: config.stega } : config.stega || {}
238
+ }
239
+ };
240
+ specifiedConfig.apiVersion || printNoApiVersionSpecifiedWarning();
241
+ const newConfig = {
242
+ ...defaultConfig,
243
+ ...specifiedConfig
244
+ }, projectBased = newConfig.useProjectHostname;
245
+ if (typeof Promise > "u") {
246
+ const helpUrl = generateHelpUrl("js-client-promise-polyfill");
247
+ throw new Error(`No native Promise-implementation found, polyfill needed - see ${helpUrl}`);
248
+ }
249
+ if (projectBased && !newConfig.projectId)
250
+ throw new Error("Configuration must contain `projectId`");
251
+ if (typeof newConfig.perspective < "u" && validateApiPerspective(newConfig.perspective), "encodeSourceMap" in newConfig)
252
+ throw new Error(
253
+ "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'?"
254
+ );
255
+ if ("encodeSourceMapAtPath" in newConfig)
256
+ throw new Error(
257
+ "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'?"
258
+ );
259
+ if (typeof newConfig.stega.enabled != "boolean")
260
+ throw new Error(`stega.enabled must be a boolean, received ${newConfig.stega.enabled}`);
261
+ if (newConfig.stega.enabled && newConfig.stega.studioUrl === void 0)
262
+ throw new Error("stega.studioUrl must be defined when stega.enabled is true");
263
+ if (newConfig.stega.enabled && typeof newConfig.stega.studioUrl != "string" && typeof newConfig.stega.studioUrl != "function")
264
+ throw new Error(
265
+ `stega.studioUrl must be a string or a function, received ${newConfig.stega.studioUrl}`
266
+ );
267
+ const isBrowser = typeof window < "u" && window.location && window.location.hostname, isLocalhost = isBrowser && isLocal(window.location.hostname);
268
+ 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);
269
+ const hostParts = newConfig.apiHost.split("://", 2), protocol = hostParts[0], host = hostParts[1], cdnHost = newConfig.isDefaultApi ? defaultCdnHost : host;
270
+ 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;
271
+ };
272
+ function getSelection(sel) {
273
+ if (typeof sel == "string")
274
+ return { id: sel };
275
+ if (Array.isArray(sel))
276
+ return { query: "*[_id in $ids]", params: { ids: sel } };
277
+ if (typeof sel == "object" && sel !== null && "query" in sel && typeof sel.query == "string")
278
+ return "params" in sel && typeof sel.params == "object" && sel.params !== null ? { query: sel.query, params: sel.params } : { query: sel.query };
279
+ const selectionOpts = [
280
+ "* Document ID (<docId>)",
281
+ "* Array of document IDs",
282
+ "* Object containing `query`"
283
+ ].join(`
284
+ `);
285
+ throw new Error(`Unknown selection - must be one of:
286
+
287
+ ${selectionOpts}`);
288
+ }
183
289
  class BasePatch {
184
290
  selection;
185
291
  operations;
@@ -493,111 +599,7 @@ class ObservableTransaction extends BaseTransaction {
493
599
  return this._add({ patch: { id: patchOrDocumentId, ...patchOps } });
494
600
  }
495
601
  }
496
- const BASE_URL = "https://www.sanity.io/help/";
497
- function generateHelpUrl(slug) {
498
- return BASE_URL + slug;
499
- }
500
- function once(fn) {
501
- let didCall = !1, returnValue;
502
- return (...args) => (didCall || (returnValue = fn(...args), didCall = !0), returnValue);
503
- }
504
- const createWarningPrinter = (message) => (
505
- // eslint-disable-next-line no-console
506
- once((...args) => console.warn(message.join(" "), ...args))
507
- ), printCdnAndWithCredentialsWarning = createWarningPrinter([
508
- "Because you set `withCredentials` to true, we will override your `useCdn`",
509
- "setting to be false since (cookie-based) credentials are never set on the CDN"
510
- ]), printCdnWarning = createWarningPrinter([
511
- "Since you haven't set a value for `useCdn`, we will deliver content using our",
512
- "global, edge-cached API-CDN. If you wish to have content delivered faster, set",
513
- "`useCdn: false` to use the Live API. Note: You may incur higher costs using the live API."
514
- ]), printCdnPreviewDraftsWarning = createWarningPrinter([
515
- "The Sanity client is configured with the `perspective` set to `previewDrafts`, which doesn't support the API-CDN.",
516
- "The Live API will be used instead. Set `useCdn: false` in your configuration to hide this warning."
517
- ]), printBrowserTokenWarning = createWarningPrinter([
518
- "You have configured Sanity client to use a token in the browser. This may cause unintentional security issues.",
519
- `See ${generateHelpUrl(
520
- "js-client-browser-token"
521
- )} for more information and how to hide this warning.`
522
- ]), printNoApiVersionSpecifiedWarning = createWarningPrinter([
523
- "Using the Sanity client without specifying an API version is deprecated.",
524
- `See ${generateHelpUrl("js-client-api-version")}`
525
- ]), printNoDefaultExport = createWarningPrinter([
526
- "The default export of @sanity/client has been deprecated. Use the named export `createClient` instead."
527
- ]), defaultCdnHost = "apicdn.sanity.io", defaultConfig = {
528
- apiHost: "https://api.sanity.io",
529
- apiVersion: "1",
530
- useProjectHostname: !0,
531
- stega: { enabled: !1 }
532
- }, LOCALHOSTS = ["localhost", "127.0.0.1", "0.0.0.0"], isLocal = (host) => LOCALHOSTS.indexOf(host) !== -1;
533
- function validateApiVersion(apiVersion) {
534
- if (apiVersion === "1" || apiVersion === "X")
535
- return;
536
- const apiDate = new Date(apiVersion);
537
- if (!(/^\d{4}-\d{2}-\d{2}$/.test(apiVersion) && apiDate instanceof Date && apiDate.getTime() > 0))
538
- throw new Error("Invalid API version string, expected `1` or date in format `YYYY-MM-DD`");
539
- }
540
- const validateApiPerspective = function(perspective) {
541
- if (Array.isArray(perspective)) {
542
- for (const perspectiveValue of perspective)
543
- if (perspectiveValue !== "published" && perspectiveValue !== "drafts" && !(typeof perspectiveValue == "string" && perspectiveValue.startsWith("r") && perspectiveValue !== "raw"))
544
- throw new TypeError(
545
- "Invalid API perspective value, expected `published`, `drafts` or a valid release identifier string"
546
- );
547
- return;
548
- }
549
- switch (perspective) {
550
- case "previewDrafts":
551
- case "drafts":
552
- case "published":
553
- case "raw":
554
- return;
555
- default:
556
- throw new TypeError(
557
- "Invalid API perspective string, expected `published`, `previewDrafts` or `raw`"
558
- );
559
- }
560
- }, initConfig = (config, prevConfig) => {
561
- const specifiedConfig = {
562
- ...prevConfig,
563
- ...config,
564
- stega: {
565
- ...typeof prevConfig.stega == "boolean" ? { enabled: prevConfig.stega } : prevConfig.stega || defaultConfig.stega,
566
- ...typeof config.stega == "boolean" ? { enabled: config.stega } : config.stega || {}
567
- }
568
- };
569
- specifiedConfig.apiVersion || printNoApiVersionSpecifiedWarning();
570
- const newConfig = {
571
- ...defaultConfig,
572
- ...specifiedConfig
573
- }, projectBased = newConfig.useProjectHostname;
574
- if (typeof Promise > "u") {
575
- const helpUrl = generateHelpUrl("js-client-promise-polyfill");
576
- throw new Error(`No native Promise-implementation found, polyfill needed - see ${helpUrl}`);
577
- }
578
- if (projectBased && !newConfig.projectId)
579
- throw new Error("Configuration must contain `projectId`");
580
- if (typeof newConfig.perspective < "u" && validateApiPerspective(newConfig.perspective), "encodeSourceMap" in newConfig)
581
- throw new Error(
582
- "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'?"
583
- );
584
- if ("encodeSourceMapAtPath" in newConfig)
585
- throw new Error(
586
- "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'?"
587
- );
588
- if (typeof newConfig.stega.enabled != "boolean")
589
- throw new Error(`stega.enabled must be a boolean, received ${newConfig.stega.enabled}`);
590
- if (newConfig.stega.enabled && newConfig.stega.studioUrl === void 0)
591
- throw new Error("stega.studioUrl must be defined when stega.enabled is true");
592
- if (newConfig.stega.enabled && typeof newConfig.stega.studioUrl != "string" && typeof newConfig.stega.studioUrl != "function")
593
- throw new Error(
594
- `stega.studioUrl must be a string or a function, received ${newConfig.stega.studioUrl}`
595
- );
596
- const isBrowser = typeof window < "u" && window.location && window.location.hostname, isLocalhost = isBrowser && isLocal(window.location.hostname);
597
- 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);
598
- const hostParts = newConfig.apiHost.split("://", 2), protocol = hostParts[0], host = hostParts[1], cdnHost = newConfig.isDefaultApi ? defaultCdnHost : host;
599
- 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;
600
- }, projectHeader = "X-Sanity-Project-ID";
602
+ const projectHeader = "X-Sanity-Project-ID";
601
603
  function requestOptions(config, overrides = {}) {
602
604
  const headers = {}, token = overrides.token || config.token;
603
605
  token && (headers.Authorization = `Bearer ${token}`), !overrides.useGlobalApi && !config.useProjectHostname && config.projectId && (headers[projectHeader] = config.projectId);
@@ -1609,4 +1611,5 @@ exports.Transaction = Transaction;
1609
1611
  exports.createClient = createClient;
1610
1612
  exports.default = deprecatedCreateClient;
1611
1613
  exports.requester = requester;
1614
+ exports.validateApiPerspective = validateApiPerspective;
1612
1615
  //# sourceMappingURL=index.browser.cjs.map