@sanity/client 6.4.6-canary.0 → 6.4.6

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,
@@ -850,6 +959,7 @@ function _dataRequest(client, httpRequest, endpoint, body) {
850
959
  token,
851
960
  tag,
852
961
  perspective: options.perspective,
962
+ resultSourceMap: options.resultSourceMap,
853
963
  canUseCdn: isQuery,
854
964
  signal: options.signal,
855
965
  fetch: options.fetch
@@ -885,6 +995,7 @@ function _create(client, httpRequest, doc, op) {
885
995
  }, opts);
886
996
  }
887
997
  function _requestObservable(client, httpRequest, options) {
998
+ var _a;
888
999
  const uri = options.url || options.uri;
889
1000
  const config = client.config();
890
1001
  const canUseCdn = typeof options.canUseCdn === "undefined" ? ["GET", "HEAD"].indexOf(options.method || "GET") >= 0 && uri.indexOf("/data/") === 0 : options.canUseCdn;
@@ -897,15 +1008,17 @@ function _requestObservable(client, httpRequest, options) {
897
1008
  };
898
1009
  }
899
1010
  if (["GET", "HEAD", "POST"].indexOf(options.method || "GET") >= 0 && uri.indexOf("/data/query/") === 0) {
900
- if (config.resultSourceMap) {
1011
+ if ((_a = options.resultSourceMap) != null ? _a : config.resultSourceMap) {
901
1012
  options.query = {
902
1013
  resultSourceMap: true,
903
1014
  ...options.query
904
1015
  };
905
1016
  }
906
- 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);
907
1020
  options.query = {
908
- perspective: config.perspective,
1021
+ perspective,
909
1022
  ...options.query
910
1023
  };
911
1024
  }
@@ -1060,115 +1173,6 @@ function optionsFromFile(opts, file) {
1060
1173
  contentType: file.type
1061
1174
  }, opts);
1062
1175
  }
1063
- const BASE_URL = "https://www.sanity.io/help/";
1064
- function generateHelpUrl(slug) {
1065
- return BASE_URL + slug;
1066
- }
1067
- function once(fn) {
1068
- let didCall = false;
1069
- let returnValue;
1070
- return function () {
1071
- if (didCall) {
1072
- return returnValue;
1073
- }
1074
- returnValue = fn(...arguments);
1075
- didCall = true;
1076
- return returnValue;
1077
- };
1078
- }
1079
- const createWarningPrinter = message =>
1080
- // eslint-disable-next-line no-console
1081
- once(function () {
1082
- for (var _len = arguments.length, args = new Array(_len), _key = 0; _key < _len; _key++) {
1083
- args[_key] = arguments[_key];
1084
- }
1085
- return console.warn(message.join(" "), ...args);
1086
- });
1087
- 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."]);
1088
- 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.")]);
1089
- const printNoApiVersionSpecifiedWarning = createWarningPrinter(["Using the Sanity client without specifying an API version is deprecated.", "See ".concat(generateHelpUrl("js-client-api-version"))]);
1090
- const printNoDefaultExport = createWarningPrinter(["The default export of @sanity/client has been deprecated. Use the named export `createClient` instead."]);
1091
- const defaultCdnHost = "apicdn.sanity.io";
1092
- const defaultConfig = {
1093
- apiHost: "https://api.sanity.io",
1094
- apiVersion: "1",
1095
- useProjectHostname: true
1096
- };
1097
- const LOCALHOSTS = ["localhost", "127.0.0.1", "0.0.0.0"];
1098
- const isLocal = host => LOCALHOSTS.indexOf(host) !== -1;
1099
- const validateApiVersion = function validateApiVersion2(apiVersion) {
1100
- if (apiVersion === "1" || apiVersion === "X") {
1101
- return;
1102
- }
1103
- const apiDate = new Date(apiVersion);
1104
- const apiVersionValid = /^\d{4}-\d{2}-\d{2}$/.test(apiVersion) && apiDate instanceof Date && apiDate.getTime() > 0;
1105
- if (!apiVersionValid) {
1106
- throw new Error("Invalid API version string, expected `1` or date in format `YYYY-MM-DD`");
1107
- }
1108
- };
1109
- const validateApiPerspective = function validateApiPerspective2(perspective) {
1110
- switch (perspective) {
1111
- case "previewDrafts":
1112
- case "published":
1113
- case "raw":
1114
- return;
1115
- default:
1116
- throw new TypeError("Invalid API perspective string, expected `published`, `previewDrafts` or `raw`");
1117
- }
1118
- };
1119
- const initConfig = (config, prevConfig) => {
1120
- const specifiedConfig = Object.assign({}, prevConfig, config);
1121
- if (!specifiedConfig.apiVersion) {
1122
- printNoApiVersionSpecifiedWarning();
1123
- }
1124
- const newConfig = Object.assign({}, defaultConfig, specifiedConfig);
1125
- const projectBased = newConfig.useProjectHostname;
1126
- if (typeof Promise === "undefined") {
1127
- const helpUrl = generateHelpUrl("js-client-promise-polyfill");
1128
- throw new Error("No native Promise-implementation found, polyfill needed - see ".concat(helpUrl));
1129
- }
1130
- if (projectBased && !newConfig.projectId) {
1131
- throw new Error("Configuration must contain `projectId`");
1132
- }
1133
- if (typeof newConfig.perspective === "string") {
1134
- validateApiPerspective(newConfig.perspective);
1135
- }
1136
- if ("encodeSourceMapAtPath" in newConfig || "encodeSourceMap" in newConfig || "studioUrl" in newConfig || "logger" in newConfig) {
1137
- 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.");
1138
- }
1139
- const isBrowser = typeof window !== "undefined" && window.location && window.location.hostname;
1140
- const isLocalhost = isBrowser && isLocal(window.location.hostname);
1141
- if (isBrowser && isLocalhost && newConfig.token && newConfig.ignoreBrowserTokenWarning !== true) {
1142
- printBrowserTokenWarning();
1143
- } else if (typeof newConfig.useCdn === "undefined") {
1144
- printCdnWarning();
1145
- }
1146
- if (projectBased) {
1147
- projectId(newConfig.projectId);
1148
- }
1149
- if (newConfig.dataset) {
1150
- dataset(newConfig.dataset);
1151
- }
1152
- if ("requestTagPrefix" in newConfig) {
1153
- newConfig.requestTagPrefix = newConfig.requestTagPrefix ? requestTag(newConfig.requestTagPrefix).replace(/\.+$/, "") : void 0;
1154
- }
1155
- newConfig.apiVersion = "".concat(newConfig.apiVersion).replace(/^v/, "");
1156
- newConfig.isDefaultApi = newConfig.apiHost === defaultConfig.apiHost;
1157
- newConfig.useCdn = newConfig.useCdn !== false && !newConfig.withCredentials;
1158
- validateApiVersion(newConfig.apiVersion);
1159
- const hostParts = newConfig.apiHost.split("://", 2);
1160
- const protocol = hostParts[0];
1161
- const host = hostParts[1];
1162
- const cdnHost = newConfig.isDefaultApi ? defaultCdnHost : host;
1163
- if (newConfig.useProjectHostname) {
1164
- newConfig.url = "".concat(protocol, "://").concat(newConfig.projectId, ".").concat(host, "/v").concat(newConfig.apiVersion);
1165
- newConfig.cdnUrl = "".concat(protocol, "://").concat(newConfig.projectId, ".").concat(cdnHost, "/v").concat(newConfig.apiVersion);
1166
- } else {
1167
- newConfig.url = "".concat(newConfig.apiHost, "/v").concat(newConfig.apiVersion);
1168
- newConfig.cdnUrl = newConfig.url;
1169
- }
1170
- return newConfig;
1171
- };
1172
1176
  var defaults = (obj, defaults) => Object.keys(defaults).concat(Object.keys(obj)).reduce((target, prop) => {
1173
1177
  target[prop] = typeof obj[prop] === "undefined" ? defaults[prop] : obj[prop];
1174
1178
  return target;