@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.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@sanity/client",
3
- "version": "6.24.0-canary.0",
3
+ "version": "6.24.1",
4
4
  "description": "Client for retrieving, creating and patching data from Sanity.io",
5
5
  "keywords": [
6
6
  "sanity",
package/src/config.ts CHANGED
@@ -28,7 +28,12 @@ function validateApiVersion(apiVersion: string) {
28
28
  }
29
29
  }
30
30
 
31
- export const validateApiPerspective = function validateApiPerspective(perspective: unknown) {
31
+ /**
32
+ * @internal - it may have breaking changes in any release
33
+ */
34
+ export function validateApiPerspective(
35
+ perspective: unknown,
36
+ ): asserts perspective is ClientPerspective {
32
37
  if (Array.isArray(perspective)) {
33
38
  for (const perspectiveValue of perspective) {
34
39
  if (perspectiveValue === 'published') {
@@ -3,6 +3,7 @@ import type {Middlewares} from 'get-it'
3
3
  import {defineHttpRequest} from './http/request'
4
4
  import type {Any, ClientConfig, HttpRequest} from './types'
5
5
 
6
+ export {validateApiPerspective} from './config'
6
7
  export * from './data/patch'
7
8
  export * from './data/transaction'
8
9
  export {ClientError, CorsOriginError, ServerError} from './http/errors'
@@ -1178,22 +1178,9 @@
1178
1178
  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);
1179
1179
  return (isSafe || isQuery) && isRetriableResponse ? !0 : _$1.shouldRetry(err, attempt, options);
1180
1180
  }
1181
- function getSelection(sel) {
1182
- if (typeof sel == "string")
1183
- return { id: sel };
1184
- if (Array.isArray(sel))
1185
- return { query: "*[_id in $ids]", params: { ids: sel } };
1186
- if (typeof sel == "object" && sel !== null && "query" in sel && typeof sel.query == "string")
1187
- return "params" in sel && typeof sel.params == "object" && sel.params !== null ? { query: sel.query, params: sel.params } : { query: sel.query };
1188
- const selectionOpts = [
1189
- "* Document ID (<docId>)",
1190
- "* Array of document IDs",
1191
- "* Object containing `query`"
1192
- ].join(`
1193
- `);
1194
- throw new Error(`Unknown selection - must be one of:
1195
-
1196
- ${selectionOpts}`);
1181
+ const BASE_URL = "https://www.sanity.io/help/";
1182
+ function generateHelpUrl(slug) {
1183
+ return BASE_URL + slug;
1197
1184
  }
1198
1185
  const VALID_ASSET_TYPES = ["image", "file"], VALID_INSERT_LOCATIONS = ["before", "after", "replace"], dataset = (name) => {
1199
1186
  if (!/^(~[a-z0-9]{1}[-\w]{0,63}|[a-z0-9]{1}[-\w]{0,63})$/.test(name))
@@ -1237,6 +1224,125 @@ ${selectionOpts}`);
1237
1224
  );
1238
1225
  return tag;
1239
1226
  };
1227
+ function once(fn) {
1228
+ let didCall = !1, returnValue;
1229
+ return (...args) => (didCall || (returnValue = fn(...args), didCall = !0), returnValue);
1230
+ }
1231
+ const createWarningPrinter = (message) => (
1232
+ // eslint-disable-next-line no-console
1233
+ once((...args) => console.warn(message.join(" "), ...args))
1234
+ ), printCdnAndWithCredentialsWarning = createWarningPrinter([
1235
+ "Because you set `withCredentials` to true, we will override your `useCdn`",
1236
+ "setting to be false since (cookie-based) credentials are never set on the CDN"
1237
+ ]), printCdnWarning = createWarningPrinter([
1238
+ "Since you haven't set a value for `useCdn`, we will deliver content using our",
1239
+ "global, edge-cached API-CDN. If you wish to have content delivered faster, set",
1240
+ "`useCdn: false` to use the Live API. Note: You may incur higher costs using the live API."
1241
+ ]), printCdnPreviewDraftsWarning = createWarningPrinter([
1242
+ "The Sanity client is configured with the `perspective` set to `previewDrafts`, which doesn't support the API-CDN.",
1243
+ "The Live API will be used instead. Set `useCdn: false` in your configuration to hide this warning."
1244
+ ]), printBrowserTokenWarning = createWarningPrinter([
1245
+ "You have configured Sanity client to use a token in the browser. This may cause unintentional security issues.",
1246
+ `See ${generateHelpUrl(
1247
+ "js-client-browser-token"
1248
+ )} for more information and how to hide this warning.`
1249
+ ]), printNoApiVersionSpecifiedWarning = createWarningPrinter([
1250
+ "Using the Sanity client without specifying an API version is deprecated.",
1251
+ `See ${generateHelpUrl("js-client-api-version")}`
1252
+ ]), printNoDefaultExport = createWarningPrinter([
1253
+ "The default export of @sanity/client has been deprecated. Use the named export `createClient` instead."
1254
+ ]), defaultCdnHost = "apicdn.sanity.io", defaultConfig = {
1255
+ apiHost: "https://api.sanity.io",
1256
+ apiVersion: "1",
1257
+ useProjectHostname: !0,
1258
+ stega: { enabled: !1 }
1259
+ }, LOCALHOSTS = ["localhost", "127.0.0.1", "0.0.0.0"], isLocal = (host) => LOCALHOSTS.indexOf(host) !== -1;
1260
+ function validateApiVersion(apiVersion) {
1261
+ if (apiVersion === "1" || apiVersion === "X")
1262
+ return;
1263
+ const apiDate = new Date(apiVersion);
1264
+ if (!(/^\d{4}-\d{2}-\d{2}$/.test(apiVersion) && apiDate instanceof Date && apiDate.getTime() > 0))
1265
+ throw new Error("Invalid API version string, expected `1` or date in format `YYYY-MM-DD`");
1266
+ }
1267
+ function validateApiPerspective(perspective) {
1268
+ if (Array.isArray(perspective)) {
1269
+ for (const perspectiveValue of perspective)
1270
+ if (perspectiveValue !== "published" && perspectiveValue !== "drafts" && !(typeof perspectiveValue == "string" && perspectiveValue.startsWith("r") && perspectiveValue !== "raw"))
1271
+ throw new TypeError(
1272
+ "Invalid API perspective value, expected `published`, `drafts` or a valid release identifier string"
1273
+ );
1274
+ return;
1275
+ }
1276
+ switch (perspective) {
1277
+ case "previewDrafts":
1278
+ case "drafts":
1279
+ case "published":
1280
+ case "raw":
1281
+ return;
1282
+ default:
1283
+ throw new TypeError(
1284
+ "Invalid API perspective string, expected `published`, `previewDrafts` or `raw`"
1285
+ );
1286
+ }
1287
+ }
1288
+ const initConfig = (config, prevConfig) => {
1289
+ const specifiedConfig = {
1290
+ ...prevConfig,
1291
+ ...config,
1292
+ stega: {
1293
+ ...typeof prevConfig.stega == "boolean" ? { enabled: prevConfig.stega } : prevConfig.stega || defaultConfig.stega,
1294
+ ...typeof config.stega == "boolean" ? { enabled: config.stega } : config.stega || {}
1295
+ }
1296
+ };
1297
+ specifiedConfig.apiVersion || printNoApiVersionSpecifiedWarning();
1298
+ const newConfig = {
1299
+ ...defaultConfig,
1300
+ ...specifiedConfig
1301
+ }, projectBased = newConfig.useProjectHostname;
1302
+ if (typeof Promise > "u") {
1303
+ const helpUrl = generateHelpUrl("js-client-promise-polyfill");
1304
+ throw new Error(`No native Promise-implementation found, polyfill needed - see ${helpUrl}`);
1305
+ }
1306
+ if (projectBased && !newConfig.projectId)
1307
+ throw new Error("Configuration must contain `projectId`");
1308
+ if (typeof newConfig.perspective < "u" && validateApiPerspective(newConfig.perspective), "encodeSourceMap" in newConfig)
1309
+ throw new Error(
1310
+ "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'?"
1311
+ );
1312
+ if ("encodeSourceMapAtPath" in newConfig)
1313
+ throw new Error(
1314
+ "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'?"
1315
+ );
1316
+ if (typeof newConfig.stega.enabled != "boolean")
1317
+ throw new Error(`stega.enabled must be a boolean, received ${newConfig.stega.enabled}`);
1318
+ if (newConfig.stega.enabled && newConfig.stega.studioUrl === void 0)
1319
+ throw new Error("stega.studioUrl must be defined when stega.enabled is true");
1320
+ if (newConfig.stega.enabled && typeof newConfig.stega.studioUrl != "string" && typeof newConfig.stega.studioUrl != "function")
1321
+ throw new Error(
1322
+ `stega.studioUrl must be a string or a function, received ${newConfig.stega.studioUrl}`
1323
+ );
1324
+ const isBrowser = typeof window < "u" && window.location && window.location.hostname, isLocalhost = isBrowser && isLocal(window.location.hostname);
1325
+ 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);
1326
+ const hostParts = newConfig.apiHost.split("://", 2), protocol = hostParts[0], host = hostParts[1], cdnHost = newConfig.isDefaultApi ? defaultCdnHost : host;
1327
+ 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;
1328
+ };
1329
+ function getSelection(sel) {
1330
+ if (typeof sel == "string")
1331
+ return { id: sel };
1332
+ if (Array.isArray(sel))
1333
+ return { query: "*[_id in $ids]", params: { ids: sel } };
1334
+ if (typeof sel == "object" && sel !== null && "query" in sel && typeof sel.query == "string")
1335
+ return "params" in sel && typeof sel.params == "object" && sel.params !== null ? { query: sel.query, params: sel.params } : { query: sel.query };
1336
+ const selectionOpts = [
1337
+ "* Document ID (<docId>)",
1338
+ "* Array of document IDs",
1339
+ "* Object containing `query`"
1340
+ ].join(`
1341
+ `);
1342
+ throw new Error(`Unknown selection - must be one of:
1343
+
1344
+ ${selectionOpts}`);
1345
+ }
1240
1346
  class BasePatch {
1241
1347
  selection;
1242
1348
  operations;
@@ -1550,111 +1656,7 @@ ${selectionOpts}`);
1550
1656
  return this._add({ patch: { id: patchOrDocumentId, ...patchOps } });
1551
1657
  }
1552
1658
  }
1553
- const BASE_URL = "https://www.sanity.io/help/";
1554
- function generateHelpUrl(slug) {
1555
- return BASE_URL + slug;
1556
- }
1557
- function once(fn) {
1558
- let didCall = !1, returnValue;
1559
- return (...args) => (didCall || (returnValue = fn(...args), didCall = !0), returnValue);
1560
- }
1561
- const createWarningPrinter = (message) => (
1562
- // eslint-disable-next-line no-console
1563
- once((...args) => console.warn(message.join(" "), ...args))
1564
- ), printCdnAndWithCredentialsWarning = createWarningPrinter([
1565
- "Because you set `withCredentials` to true, we will override your `useCdn`",
1566
- "setting to be false since (cookie-based) credentials are never set on the CDN"
1567
- ]), printCdnWarning = createWarningPrinter([
1568
- "Since you haven't set a value for `useCdn`, we will deliver content using our",
1569
- "global, edge-cached API-CDN. If you wish to have content delivered faster, set",
1570
- "`useCdn: false` to use the Live API. Note: You may incur higher costs using the live API."
1571
- ]), printCdnPreviewDraftsWarning = createWarningPrinter([
1572
- "The Sanity client is configured with the `perspective` set to `previewDrafts`, which doesn't support the API-CDN.",
1573
- "The Live API will be used instead. Set `useCdn: false` in your configuration to hide this warning."
1574
- ]), printBrowserTokenWarning = createWarningPrinter([
1575
- "You have configured Sanity client to use a token in the browser. This may cause unintentional security issues.",
1576
- `See ${generateHelpUrl(
1577
- "js-client-browser-token"
1578
- )} for more information and how to hide this warning.`
1579
- ]), printNoApiVersionSpecifiedWarning = createWarningPrinter([
1580
- "Using the Sanity client without specifying an API version is deprecated.",
1581
- `See ${generateHelpUrl("js-client-api-version")}`
1582
- ]), printNoDefaultExport = createWarningPrinter([
1583
- "The default export of @sanity/client has been deprecated. Use the named export `createClient` instead."
1584
- ]), defaultCdnHost = "apicdn.sanity.io", defaultConfig = {
1585
- apiHost: "https://api.sanity.io",
1586
- apiVersion: "1",
1587
- useProjectHostname: !0,
1588
- stega: { enabled: !1 }
1589
- }, LOCALHOSTS = ["localhost", "127.0.0.1", "0.0.0.0"], isLocal = (host) => LOCALHOSTS.indexOf(host) !== -1;
1590
- function validateApiVersion(apiVersion) {
1591
- if (apiVersion === "1" || apiVersion === "X")
1592
- return;
1593
- const apiDate = new Date(apiVersion);
1594
- if (!(/^\d{4}-\d{2}-\d{2}$/.test(apiVersion) && apiDate instanceof Date && apiDate.getTime() > 0))
1595
- throw new Error("Invalid API version string, expected `1` or date in format `YYYY-MM-DD`");
1596
- }
1597
- const validateApiPerspective = function(perspective) {
1598
- if (Array.isArray(perspective)) {
1599
- for (const perspectiveValue of perspective)
1600
- if (perspectiveValue !== "published" && perspectiveValue !== "drafts" && !(typeof perspectiveValue == "string" && perspectiveValue.startsWith("r") && perspectiveValue !== "raw"))
1601
- throw new TypeError(
1602
- "Invalid API perspective value, expected `published`, `drafts` or a valid release identifier string"
1603
- );
1604
- return;
1605
- }
1606
- switch (perspective) {
1607
- case "previewDrafts":
1608
- case "drafts":
1609
- case "published":
1610
- case "raw":
1611
- return;
1612
- default:
1613
- throw new TypeError(
1614
- "Invalid API perspective string, expected `published`, `previewDrafts` or `raw`"
1615
- );
1616
- }
1617
- }, initConfig = (config, prevConfig) => {
1618
- const specifiedConfig = {
1619
- ...prevConfig,
1620
- ...config,
1621
- stega: {
1622
- ...typeof prevConfig.stega == "boolean" ? { enabled: prevConfig.stega } : prevConfig.stega || defaultConfig.stega,
1623
- ...typeof config.stega == "boolean" ? { enabled: config.stega } : config.stega || {}
1624
- }
1625
- };
1626
- specifiedConfig.apiVersion || printNoApiVersionSpecifiedWarning();
1627
- const newConfig = {
1628
- ...defaultConfig,
1629
- ...specifiedConfig
1630
- }, projectBased = newConfig.useProjectHostname;
1631
- if (typeof Promise > "u") {
1632
- const helpUrl = generateHelpUrl("js-client-promise-polyfill");
1633
- throw new Error(`No native Promise-implementation found, polyfill needed - see ${helpUrl}`);
1634
- }
1635
- if (projectBased && !newConfig.projectId)
1636
- throw new Error("Configuration must contain `projectId`");
1637
- if (typeof newConfig.perspective < "u" && validateApiPerspective(newConfig.perspective), "encodeSourceMap" in newConfig)
1638
- throw new Error(
1639
- "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'?"
1640
- );
1641
- if ("encodeSourceMapAtPath" in newConfig)
1642
- throw new Error(
1643
- "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'?"
1644
- );
1645
- if (typeof newConfig.stega.enabled != "boolean")
1646
- throw new Error(`stega.enabled must be a boolean, received ${newConfig.stega.enabled}`);
1647
- if (newConfig.stega.enabled && newConfig.stega.studioUrl === void 0)
1648
- throw new Error("stega.studioUrl must be defined when stega.enabled is true");
1649
- if (newConfig.stega.enabled && typeof newConfig.stega.studioUrl != "string" && typeof newConfig.stega.studioUrl != "function")
1650
- throw new Error(
1651
- `stega.studioUrl must be a string or a function, received ${newConfig.stega.studioUrl}`
1652
- );
1653
- const isBrowser = typeof window < "u" && window.location && window.location.hostname, isLocalhost = isBrowser && isLocal(window.location.hostname);
1654
- 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);
1655
- const hostParts = newConfig.apiHost.split("://", 2), protocol = hostParts[0], host = hostParts[1], cdnHost = newConfig.isDefaultApi ? defaultCdnHost : host;
1656
- 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;
1657
- }, projectHeader = "X-Sanity-Project-ID";
1659
+ const projectHeader = "X-Sanity-Project-ID";
1658
1660
  function requestOptions(config, overrides = {}) {
1659
1661
  const headers = {}, token = overrides.token || config.token;
1660
1662
  token && (headers.Authorization = `Bearer ${token}`), !overrides.useGlobalApi && !config.useProjectHostname && config.projectId && (headers[projectHeader] = config.projectId);
@@ -4073,6 +4075,7 @@ ${selectionOpts}`);
4073
4075
  exports.requester = requester;
4074
4076
  exports.unstable__adapter = l$1;
4075
4077
  exports.unstable__environment = p$1;
4078
+ exports.validateApiPerspective = validateApiPerspective;
4076
4079
 
4077
4080
  Object.defineProperty(exports, '__esModule', { value: true });
4078
4081