@sanity/client 6.4.5 → 6.4.6-canary.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.
@@ -142,47 +142,9 @@ function shouldRetry(err, attempt, options) {
142
142
  if ((isSafe || isQuery) && isRetriableResponse) return true;
143
143
  return middleware.retry.shouldRetry(err, attempt, options);
144
144
  }
145
- const projectHeader = "X-Sanity-Project-ID";
146
- function requestOptions(config) {
147
- let overrides = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
148
- const headers = {};
149
- const token = overrides.token || config.token;
150
- if (token) {
151
- headers.Authorization = "Bearer ".concat(token);
152
- }
153
- if (!overrides.useGlobalApi && !config.useProjectHostname && config.projectId) {
154
- headers[projectHeader] = config.projectId;
155
- }
156
- const withCredentials = Boolean(typeof overrides.withCredentials === "undefined" ? config.token || config.withCredentials : overrides.withCredentials);
157
- const timeout = typeof overrides.timeout === "undefined" ? config.timeout : overrides.timeout;
158
- return Object.assign({}, overrides, {
159
- headers: Object.assign({}, headers, overrides.headers || {}),
160
- timeout: typeof timeout === "undefined" ? 5 * 60 * 1e3 : timeout,
161
- proxy: overrides.proxy || config.proxy,
162
- json: true,
163
- withCredentials,
164
- fetch: typeof overrides.fetch === "object" && typeof config.fetch === "object" ? {
165
- ...config.fetch,
166
- ...overrides.fetch
167
- } : overrides.fetch || config.fetch
168
- });
169
- }
170
- function getSelection(sel) {
171
- if (typeof sel === "string" || Array.isArray(sel)) {
172
- return {
173
- id: sel
174
- };
175
- }
176
- if (typeof sel === "object" && sel !== null && "query" in sel && typeof sel.query === "string") {
177
- return "params" in sel && typeof sel.params === "object" && sel.params !== null ? {
178
- query: sel.query,
179
- params: sel.params
180
- } : {
181
- query: sel.query
182
- };
183
- }
184
- const selectionOpts = ["* Document ID (<docId>)", "* Array of document IDs", "* Object containing `query`"].join("\n");
185
- throw new Error("Unknown selection - must be one of:\n\n".concat(selectionOpts));
145
+ const BASE_URL = "https://www.sanity.io/help/";
146
+ function generateHelpUrl(slug) {
147
+ return BASE_URL + slug;
186
148
  }
187
149
  const VALID_ASSET_TYPES = ["image", "file"];
188
150
  const VALID_INSERT_LOCATIONS = ["before", "after", "replace"];
@@ -242,6 +204,153 @@ const requestTag = tag => {
242
204
  }
243
205
  return tag;
244
206
  };
207
+ function once(fn) {
208
+ let didCall = false;
209
+ let returnValue;
210
+ return function () {
211
+ if (didCall) {
212
+ return returnValue;
213
+ }
214
+ returnValue = fn(...arguments);
215
+ didCall = true;
216
+ return returnValue;
217
+ };
218
+ }
219
+ const createWarningPrinter = message =>
220
+ // eslint-disable-next-line no-console
221
+ once(function () {
222
+ for (var _len = arguments.length, args = new Array(_len), _key = 0; _key < _len; _key++) {
223
+ args[_key] = arguments[_key];
224
+ }
225
+ return console.warn(message.join(" "), ...args);
226
+ });
227
+ const printCdnWarning = createWarningPrinter(["Since you haven't set a value for `useCdn`, we will deliver content using our", "global, edge-cached API-CDN. If you wish to have content delivered faster, set", "`useCdn: false` to use the Live API. Note: You may incur higher costs using the live API."]);
228
+ const printBrowserTokenWarning = createWarningPrinter(["You have configured Sanity client to use a token in the browser. This may cause unintentional security issues.", "See ".concat(generateHelpUrl("js-client-browser-token"), " for more information and how to hide this warning.")]);
229
+ const printNoApiVersionSpecifiedWarning = createWarningPrinter(["Using the Sanity client without specifying an API version is deprecated.", "See ".concat(generateHelpUrl("js-client-api-version"))]);
230
+ const printNoDefaultExport = createWarningPrinter(["The default export of @sanity/client has been deprecated. Use the named export `createClient` instead."]);
231
+ const defaultCdnHost = "apicdn.sanity.io";
232
+ const defaultConfig = {
233
+ apiHost: "https://api.sanity.io",
234
+ apiVersion: "1",
235
+ useProjectHostname: true
236
+ };
237
+ const LOCALHOSTS = ["localhost", "127.0.0.1", "0.0.0.0"];
238
+ const isLocal = host => LOCALHOSTS.indexOf(host) !== -1;
239
+ const validateApiVersion = function validateApiVersion2(apiVersion) {
240
+ if (apiVersion === "1" || apiVersion === "X") {
241
+ return;
242
+ }
243
+ const apiDate = new Date(apiVersion);
244
+ const apiVersionValid = /^\d{4}-\d{2}-\d{2}$/.test(apiVersion) && apiDate instanceof Date && apiDate.getTime() > 0;
245
+ if (!apiVersionValid) {
246
+ throw new Error("Invalid API version string, expected `1` or date in format `YYYY-MM-DD`");
247
+ }
248
+ };
249
+ const validateApiPerspective = function validateApiPerspective2(perspective) {
250
+ switch (perspective) {
251
+ case "previewDrafts":
252
+ case "published":
253
+ case "raw":
254
+ return;
255
+ default:
256
+ throw new TypeError("Invalid API perspective string, expected `published`, `previewDrafts` or `raw`");
257
+ }
258
+ };
259
+ const initConfig = (config, prevConfig) => {
260
+ const specifiedConfig = Object.assign({}, prevConfig, config);
261
+ if (!specifiedConfig.apiVersion) {
262
+ printNoApiVersionSpecifiedWarning();
263
+ }
264
+ const newConfig = Object.assign({}, defaultConfig, specifiedConfig);
265
+ const projectBased = newConfig.useProjectHostname;
266
+ if (typeof Promise === "undefined") {
267
+ const helpUrl = generateHelpUrl("js-client-promise-polyfill");
268
+ throw new Error("No native Promise-implementation found, polyfill needed - see ".concat(helpUrl));
269
+ }
270
+ if (projectBased && !newConfig.projectId) {
271
+ throw new Error("Configuration must contain `projectId`");
272
+ }
273
+ if (typeof newConfig.perspective === "string") {
274
+ validateApiPerspective(newConfig.perspective);
275
+ }
276
+ if ("encodeSourceMapAtPath" in newConfig || "encodeSourceMap" in newConfig || "studioUrl" in newConfig || "logger" in newConfig) {
277
+ throw new Error("It looks like you're using options meant for '@sanity/preview-kit/client', such as 'encodeSourceMapAtPath', 'encodeSourceMap', 'studioUrl' and 'logger'. Make sure you're using the right import.");
278
+ }
279
+ const isBrowser = typeof window !== "undefined" && window.location && window.location.hostname;
280
+ const isLocalhost = isBrowser && isLocal(window.location.hostname);
281
+ if (isBrowser && isLocalhost && newConfig.token && newConfig.ignoreBrowserTokenWarning !== true) {
282
+ printBrowserTokenWarning();
283
+ } else if (typeof newConfig.useCdn === "undefined") {
284
+ printCdnWarning();
285
+ }
286
+ if (projectBased) {
287
+ projectId(newConfig.projectId);
288
+ }
289
+ if (newConfig.dataset) {
290
+ dataset(newConfig.dataset);
291
+ }
292
+ if ("requestTagPrefix" in newConfig) {
293
+ newConfig.requestTagPrefix = newConfig.requestTagPrefix ? requestTag(newConfig.requestTagPrefix).replace(/\.+$/, "") : void 0;
294
+ }
295
+ newConfig.apiVersion = "".concat(newConfig.apiVersion).replace(/^v/, "");
296
+ newConfig.isDefaultApi = newConfig.apiHost === defaultConfig.apiHost;
297
+ newConfig.useCdn = newConfig.useCdn !== false && !newConfig.withCredentials;
298
+ validateApiVersion(newConfig.apiVersion);
299
+ const hostParts = newConfig.apiHost.split("://", 2);
300
+ const protocol = hostParts[0];
301
+ const host = hostParts[1];
302
+ const cdnHost = newConfig.isDefaultApi ? defaultCdnHost : host;
303
+ if (newConfig.useProjectHostname) {
304
+ newConfig.url = "".concat(protocol, "://").concat(newConfig.projectId, ".").concat(host, "/v").concat(newConfig.apiVersion);
305
+ newConfig.cdnUrl = "".concat(protocol, "://").concat(newConfig.projectId, ".").concat(cdnHost, "/v").concat(newConfig.apiVersion);
306
+ } else {
307
+ newConfig.url = "".concat(newConfig.apiHost, "/v").concat(newConfig.apiVersion);
308
+ newConfig.cdnUrl = newConfig.url;
309
+ }
310
+ return newConfig;
311
+ };
312
+ const projectHeader = "X-Sanity-Project-ID";
313
+ function requestOptions(config) {
314
+ let overrides = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
315
+ const headers = {};
316
+ const token = overrides.token || config.token;
317
+ if (token) {
318
+ headers.Authorization = "Bearer ".concat(token);
319
+ }
320
+ if (!overrides.useGlobalApi && !config.useProjectHostname && config.projectId) {
321
+ headers[projectHeader] = config.projectId;
322
+ }
323
+ const withCredentials = Boolean(typeof overrides.withCredentials === "undefined" ? config.token || config.withCredentials : overrides.withCredentials);
324
+ const timeout = typeof overrides.timeout === "undefined" ? config.timeout : overrides.timeout;
325
+ return Object.assign({}, overrides, {
326
+ headers: Object.assign({}, headers, overrides.headers || {}),
327
+ timeout: typeof timeout === "undefined" ? 5 * 60 * 1e3 : timeout,
328
+ proxy: overrides.proxy || config.proxy,
329
+ json: true,
330
+ withCredentials,
331
+ fetch: typeof overrides.fetch === "object" && typeof config.fetch === "object" ? {
332
+ ...config.fetch,
333
+ ...overrides.fetch
334
+ } : overrides.fetch || config.fetch
335
+ });
336
+ }
337
+ function getSelection(sel) {
338
+ if (typeof sel === "string" || Array.isArray(sel)) {
339
+ return {
340
+ id: sel
341
+ };
342
+ }
343
+ if (typeof sel === "object" && sel !== null && "query" in sel && typeof sel.query === "string") {
344
+ return "params" in sel && typeof sel.params === "object" && sel.params !== null ? {
345
+ query: sel.query,
346
+ params: sel.params
347
+ } : {
348
+ query: sel.query
349
+ };
350
+ }
351
+ const selectionOpts = ["* Document ID (<docId>)", "* Array of document IDs", "* Object containing `query`"].join("\n");
352
+ throw new Error("Unknown selection - must be one of:\n\n".concat(selectionOpts));
353
+ }
245
354
  const encodeQueryString = _ref2 => {
246
355
  let {
247
356
  query,
@@ -849,6 +958,8 @@ function _dataRequest(client, httpRequest, endpoint, body) {
849
958
  headers,
850
959
  token,
851
960
  tag,
961
+ perspective: options.perspective,
962
+ resultSourceMap: options.resultSourceMap,
852
963
  canUseCdn: isQuery,
853
964
  signal: options.signal,
854
965
  fetch: options.fetch
@@ -884,6 +995,7 @@ function _create(client, httpRequest, doc, op) {
884
995
  }, opts);
885
996
  }
886
997
  function _requestObservable(client, httpRequest, options) {
998
+ var _a;
887
999
  const uri = options.url || options.uri;
888
1000
  const config = client.config();
889
1001
  const canUseCdn = typeof options.canUseCdn === "undefined" ? ["GET", "HEAD"].indexOf(options.method || "GET") >= 0 && uri.indexOf("/data/") === 0 : options.canUseCdn;
@@ -896,15 +1008,17 @@ function _requestObservable(client, httpRequest, options) {
896
1008
  };
897
1009
  }
898
1010
  if (["GET", "HEAD", "POST"].indexOf(options.method || "GET") >= 0 && uri.indexOf("/data/query/") === 0) {
899
- if (config.resultSourceMap) {
1011
+ if ((_a = options.resultSourceMap) != null ? _a : config.resultSourceMap) {
900
1012
  options.query = {
901
1013
  resultSourceMap: true,
902
1014
  ...options.query
903
1015
  };
904
1016
  }
905
- if (typeof config.perspective === "string" && config.perspective !== "raw") {
1017
+ const perspective = options.perspective || config.perspective;
1018
+ if (typeof perspective === "string" && perspective !== "raw") {
1019
+ validateApiPerspective(perspective);
906
1020
  options.query = {
907
- perspective: config.perspective,
1021
+ perspective,
908
1022
  ...options.query
909
1023
  };
910
1024
  }
@@ -1059,115 +1173,6 @@ function optionsFromFile(opts, file) {
1059
1173
  contentType: file.type
1060
1174
  }, opts);
1061
1175
  }
1062
- const BASE_URL = "https://www.sanity.io/help/";
1063
- function generateHelpUrl(slug) {
1064
- return BASE_URL + slug;
1065
- }
1066
- function once(fn) {
1067
- let didCall = false;
1068
- let returnValue;
1069
- return function () {
1070
- if (didCall) {
1071
- return returnValue;
1072
- }
1073
- returnValue = fn(...arguments);
1074
- didCall = true;
1075
- return returnValue;
1076
- };
1077
- }
1078
- const createWarningPrinter = message =>
1079
- // eslint-disable-next-line no-console
1080
- once(function () {
1081
- for (var _len = arguments.length, args = new Array(_len), _key = 0; _key < _len; _key++) {
1082
- args[_key] = arguments[_key];
1083
- }
1084
- return console.warn(message.join(" "), ...args);
1085
- });
1086
- const printCdnWarning = createWarningPrinter(["Since you haven't set a value for `useCdn`, we will deliver content using our", "global, edge-cached API-CDN. If you wish to have content delivered faster, set", "`useCdn: false` to use the Live API. Note: You may incur higher costs using the live API."]);
1087
- const printBrowserTokenWarning = createWarningPrinter(["You have configured Sanity client to use a token in the browser. This may cause unintentional security issues.", "See ".concat(generateHelpUrl("js-client-browser-token"), " for more information and how to hide this warning.")]);
1088
- const printNoApiVersionSpecifiedWarning = createWarningPrinter(["Using the Sanity client without specifying an API version is deprecated.", "See ".concat(generateHelpUrl("js-client-api-version"))]);
1089
- const printNoDefaultExport = createWarningPrinter(["The default export of @sanity/client has been deprecated. Use the named export `createClient` instead."]);
1090
- const defaultCdnHost = "apicdn.sanity.io";
1091
- const defaultConfig = {
1092
- apiHost: "https://api.sanity.io",
1093
- apiVersion: "1",
1094
- useProjectHostname: true
1095
- };
1096
- const LOCALHOSTS = ["localhost", "127.0.0.1", "0.0.0.0"];
1097
- const isLocal = host => LOCALHOSTS.indexOf(host) !== -1;
1098
- const validateApiVersion = function validateApiVersion2(apiVersion) {
1099
- if (apiVersion === "1" || apiVersion === "X") {
1100
- return;
1101
- }
1102
- const apiDate = new Date(apiVersion);
1103
- const apiVersionValid = /^\d{4}-\d{2}-\d{2}$/.test(apiVersion) && apiDate instanceof Date && apiDate.getTime() > 0;
1104
- if (!apiVersionValid) {
1105
- throw new Error("Invalid API version string, expected `1` or date in format `YYYY-MM-DD`");
1106
- }
1107
- };
1108
- const validateApiPerspective = function validateApiPerspective2(perspective) {
1109
- switch (perspective) {
1110
- case "previewDrafts":
1111
- case "published":
1112
- case "raw":
1113
- return;
1114
- default:
1115
- throw new TypeError("Invalid API perspective string, expected `published`, `previewDrafts` or `raw`");
1116
- }
1117
- };
1118
- const initConfig = (config, prevConfig) => {
1119
- const specifiedConfig = Object.assign({}, prevConfig, config);
1120
- if (!specifiedConfig.apiVersion) {
1121
- printNoApiVersionSpecifiedWarning();
1122
- }
1123
- const newConfig = Object.assign({}, defaultConfig, specifiedConfig);
1124
- const projectBased = newConfig.useProjectHostname;
1125
- if (typeof Promise === "undefined") {
1126
- const helpUrl = generateHelpUrl("js-client-promise-polyfill");
1127
- throw new Error("No native Promise-implementation found, polyfill needed - see ".concat(helpUrl));
1128
- }
1129
- if (projectBased && !newConfig.projectId) {
1130
- throw new Error("Configuration must contain `projectId`");
1131
- }
1132
- if (typeof newConfig.perspective === "string") {
1133
- validateApiPerspective(newConfig.perspective);
1134
- }
1135
- if ("encodeSourceMapAtPath" in newConfig || "encodeSourceMap" in newConfig || "studioUrl" in newConfig || "logger" in newConfig) {
1136
- throw new Error("It looks like you're using options meant for '@sanity/preview-kit/client', such as 'encodeSourceMapAtPath', 'encodeSourceMap', 'studioUrl' and 'logger'. Make sure you're using the right import.");
1137
- }
1138
- const isBrowser = typeof window !== "undefined" && window.location && window.location.hostname;
1139
- const isLocalhost = isBrowser && isLocal(window.location.hostname);
1140
- if (isBrowser && isLocalhost && newConfig.token && newConfig.ignoreBrowserTokenWarning !== true) {
1141
- printBrowserTokenWarning();
1142
- } else if (typeof newConfig.useCdn === "undefined") {
1143
- printCdnWarning();
1144
- }
1145
- if (projectBased) {
1146
- projectId(newConfig.projectId);
1147
- }
1148
- if (newConfig.dataset) {
1149
- dataset(newConfig.dataset);
1150
- }
1151
- if ("requestTagPrefix" in newConfig) {
1152
- newConfig.requestTagPrefix = newConfig.requestTagPrefix ? requestTag(newConfig.requestTagPrefix).replace(/\.+$/, "") : void 0;
1153
- }
1154
- newConfig.apiVersion = "".concat(newConfig.apiVersion).replace(/^v/, "");
1155
- newConfig.isDefaultApi = newConfig.apiHost === defaultConfig.apiHost;
1156
- newConfig.useCdn = newConfig.useCdn !== false && !newConfig.withCredentials;
1157
- validateApiVersion(newConfig.apiVersion);
1158
- const hostParts = newConfig.apiHost.split("://", 2);
1159
- const protocol = hostParts[0];
1160
- const host = hostParts[1];
1161
- const cdnHost = newConfig.isDefaultApi ? defaultCdnHost : host;
1162
- if (newConfig.useProjectHostname) {
1163
- newConfig.url = "".concat(protocol, "://").concat(newConfig.projectId, ".").concat(host, "/v").concat(newConfig.apiVersion);
1164
- newConfig.cdnUrl = "".concat(protocol, "://").concat(newConfig.projectId, ".").concat(cdnHost, "/v").concat(newConfig.apiVersion);
1165
- } else {
1166
- newConfig.url = "".concat(newConfig.apiHost, "/v").concat(newConfig.apiVersion);
1167
- newConfig.cdnUrl = newConfig.url;
1168
- }
1169
- return newConfig;
1170
- };
1171
1176
  var defaults = (obj, defaults) => Object.keys(defaults).concat(Object.keys(obj)).reduce((target, prop) => {
1172
1177
  target[prop] = typeof obj[prop] === "undefined" ? defaults[prop] : obj[prop];
1173
1178
  return target;