@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.
package/dist/index.cjs CHANGED
@@ -8,7 +8,7 @@ var getIt = require('get-it');
8
8
  var rxjs = require('rxjs');
9
9
  var operators = require('rxjs/operators');
10
10
  var name = "@sanity/client";
11
- var version = "6.4.5";
11
+ var version = "6.4.6-canary.1";
12
12
  const middleware = [middleware$1.debug({
13
13
  verbose: true,
14
14
  namespace: "sanity:client"
@@ -162,47 +162,9 @@ function shouldRetry(err, attempt, options) {
162
162
  if ((isSafe || isQuery) && isRetriableResponse) return true;
163
163
  return middleware$1.retry.shouldRetry(err, attempt, options);
164
164
  }
165
- const projectHeader = "X-Sanity-Project-ID";
166
- function requestOptions(config) {
167
- let overrides = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
168
- const headers = {};
169
- const token = overrides.token || config.token;
170
- if (token) {
171
- headers.Authorization = "Bearer ".concat(token);
172
- }
173
- if (!overrides.useGlobalApi && !config.useProjectHostname && config.projectId) {
174
- headers[projectHeader] = config.projectId;
175
- }
176
- const withCredentials = Boolean(typeof overrides.withCredentials === "undefined" ? config.token || config.withCredentials : overrides.withCredentials);
177
- const timeout = typeof overrides.timeout === "undefined" ? config.timeout : overrides.timeout;
178
- return Object.assign({}, overrides, {
179
- headers: Object.assign({}, headers, overrides.headers || {}),
180
- timeout: typeof timeout === "undefined" ? 5 * 60 * 1e3 : timeout,
181
- proxy: overrides.proxy || config.proxy,
182
- json: true,
183
- withCredentials,
184
- fetch: typeof overrides.fetch === "object" && typeof config.fetch === "object" ? {
185
- ...config.fetch,
186
- ...overrides.fetch
187
- } : overrides.fetch || config.fetch
188
- });
189
- }
190
- function getSelection(sel) {
191
- if (typeof sel === "string" || Array.isArray(sel)) {
192
- return {
193
- id: sel
194
- };
195
- }
196
- if (typeof sel === "object" && sel !== null && "query" in sel && typeof sel.query === "string") {
197
- return "params" in sel && typeof sel.params === "object" && sel.params !== null ? {
198
- query: sel.query,
199
- params: sel.params
200
- } : {
201
- query: sel.query
202
- };
203
- }
204
- const selectionOpts = ["* Document ID (<docId>)", "* Array of document IDs", "* Object containing `query`"].join("\n");
205
- throw new Error("Unknown selection - must be one of:\n\n".concat(selectionOpts));
165
+ const BASE_URL = "https://www.sanity.io/help/";
166
+ function generateHelpUrl(slug) {
167
+ return BASE_URL + slug;
206
168
  }
207
169
  const VALID_ASSET_TYPES = ["image", "file"];
208
170
  const VALID_INSERT_LOCATIONS = ["before", "after", "replace"];
@@ -262,6 +224,153 @@ const requestTag = tag => {
262
224
  }
263
225
  return tag;
264
226
  };
227
+ function once(fn) {
228
+ let didCall = false;
229
+ let returnValue;
230
+ return function () {
231
+ if (didCall) {
232
+ return returnValue;
233
+ }
234
+ returnValue = fn(...arguments);
235
+ didCall = true;
236
+ return returnValue;
237
+ };
238
+ }
239
+ const createWarningPrinter = message =>
240
+ // eslint-disable-next-line no-console
241
+ once(function () {
242
+ for (var _len = arguments.length, args = new Array(_len), _key = 0; _key < _len; _key++) {
243
+ args[_key] = arguments[_key];
244
+ }
245
+ return console.warn(message.join(" "), ...args);
246
+ });
247
+ 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."]);
248
+ 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.")]);
249
+ const printNoApiVersionSpecifiedWarning = createWarningPrinter(["Using the Sanity client without specifying an API version is deprecated.", "See ".concat(generateHelpUrl("js-client-api-version"))]);
250
+ const printNoDefaultExport = createWarningPrinter(["The default export of @sanity/client has been deprecated. Use the named export `createClient` instead."]);
251
+ const defaultCdnHost = "apicdn.sanity.io";
252
+ const defaultConfig = {
253
+ apiHost: "https://api.sanity.io",
254
+ apiVersion: "1",
255
+ useProjectHostname: true
256
+ };
257
+ const LOCALHOSTS = ["localhost", "127.0.0.1", "0.0.0.0"];
258
+ const isLocal = host => LOCALHOSTS.indexOf(host) !== -1;
259
+ const validateApiVersion = function validateApiVersion2(apiVersion) {
260
+ if (apiVersion === "1" || apiVersion === "X") {
261
+ return;
262
+ }
263
+ const apiDate = new Date(apiVersion);
264
+ const apiVersionValid = /^\d{4}-\d{2}-\d{2}$/.test(apiVersion) && apiDate instanceof Date && apiDate.getTime() > 0;
265
+ if (!apiVersionValid) {
266
+ throw new Error("Invalid API version string, expected `1` or date in format `YYYY-MM-DD`");
267
+ }
268
+ };
269
+ const validateApiPerspective = function validateApiPerspective2(perspective) {
270
+ switch (perspective) {
271
+ case "previewDrafts":
272
+ case "published":
273
+ case "raw":
274
+ return;
275
+ default:
276
+ throw new TypeError("Invalid API perspective string, expected `published`, `previewDrafts` or `raw`");
277
+ }
278
+ };
279
+ const initConfig = (config, prevConfig) => {
280
+ const specifiedConfig = Object.assign({}, prevConfig, config);
281
+ if (!specifiedConfig.apiVersion) {
282
+ printNoApiVersionSpecifiedWarning();
283
+ }
284
+ const newConfig = Object.assign({}, defaultConfig, specifiedConfig);
285
+ const projectBased = newConfig.useProjectHostname;
286
+ if (typeof Promise === "undefined") {
287
+ const helpUrl = generateHelpUrl("js-client-promise-polyfill");
288
+ throw new Error("No native Promise-implementation found, polyfill needed - see ".concat(helpUrl));
289
+ }
290
+ if (projectBased && !newConfig.projectId) {
291
+ throw new Error("Configuration must contain `projectId`");
292
+ }
293
+ if (typeof newConfig.perspective === "string") {
294
+ validateApiPerspective(newConfig.perspective);
295
+ }
296
+ if ("encodeSourceMapAtPath" in newConfig || "encodeSourceMap" in newConfig || "studioUrl" in newConfig || "logger" in newConfig) {
297
+ 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.");
298
+ }
299
+ const isBrowser = typeof window !== "undefined" && window.location && window.location.hostname;
300
+ const isLocalhost = isBrowser && isLocal(window.location.hostname);
301
+ if (isBrowser && isLocalhost && newConfig.token && newConfig.ignoreBrowserTokenWarning !== true) {
302
+ printBrowserTokenWarning();
303
+ } else if (typeof newConfig.useCdn === "undefined") {
304
+ printCdnWarning();
305
+ }
306
+ if (projectBased) {
307
+ projectId(newConfig.projectId);
308
+ }
309
+ if (newConfig.dataset) {
310
+ dataset(newConfig.dataset);
311
+ }
312
+ if ("requestTagPrefix" in newConfig) {
313
+ newConfig.requestTagPrefix = newConfig.requestTagPrefix ? requestTag(newConfig.requestTagPrefix).replace(/\.+$/, "") : void 0;
314
+ }
315
+ newConfig.apiVersion = "".concat(newConfig.apiVersion).replace(/^v/, "");
316
+ newConfig.isDefaultApi = newConfig.apiHost === defaultConfig.apiHost;
317
+ newConfig.useCdn = newConfig.useCdn !== false && !newConfig.withCredentials;
318
+ validateApiVersion(newConfig.apiVersion);
319
+ const hostParts = newConfig.apiHost.split("://", 2);
320
+ const protocol = hostParts[0];
321
+ const host = hostParts[1];
322
+ const cdnHost = newConfig.isDefaultApi ? defaultCdnHost : host;
323
+ if (newConfig.useProjectHostname) {
324
+ newConfig.url = "".concat(protocol, "://").concat(newConfig.projectId, ".").concat(host, "/v").concat(newConfig.apiVersion);
325
+ newConfig.cdnUrl = "".concat(protocol, "://").concat(newConfig.projectId, ".").concat(cdnHost, "/v").concat(newConfig.apiVersion);
326
+ } else {
327
+ newConfig.url = "".concat(newConfig.apiHost, "/v").concat(newConfig.apiVersion);
328
+ newConfig.cdnUrl = newConfig.url;
329
+ }
330
+ return newConfig;
331
+ };
332
+ const projectHeader = "X-Sanity-Project-ID";
333
+ function requestOptions(config) {
334
+ let overrides = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
335
+ const headers = {};
336
+ const token = overrides.token || config.token;
337
+ if (token) {
338
+ headers.Authorization = "Bearer ".concat(token);
339
+ }
340
+ if (!overrides.useGlobalApi && !config.useProjectHostname && config.projectId) {
341
+ headers[projectHeader] = config.projectId;
342
+ }
343
+ const withCredentials = Boolean(typeof overrides.withCredentials === "undefined" ? config.token || config.withCredentials : overrides.withCredentials);
344
+ const timeout = typeof overrides.timeout === "undefined" ? config.timeout : overrides.timeout;
345
+ return Object.assign({}, overrides, {
346
+ headers: Object.assign({}, headers, overrides.headers || {}),
347
+ timeout: typeof timeout === "undefined" ? 5 * 60 * 1e3 : timeout,
348
+ proxy: overrides.proxy || config.proxy,
349
+ json: true,
350
+ withCredentials,
351
+ fetch: typeof overrides.fetch === "object" && typeof config.fetch === "object" ? {
352
+ ...config.fetch,
353
+ ...overrides.fetch
354
+ } : overrides.fetch || config.fetch
355
+ });
356
+ }
357
+ function getSelection(sel) {
358
+ if (typeof sel === "string" || Array.isArray(sel)) {
359
+ return {
360
+ id: sel
361
+ };
362
+ }
363
+ if (typeof sel === "object" && sel !== null && "query" in sel && typeof sel.query === "string") {
364
+ return "params" in sel && typeof sel.params === "object" && sel.params !== null ? {
365
+ query: sel.query,
366
+ params: sel.params
367
+ } : {
368
+ query: sel.query
369
+ };
370
+ }
371
+ const selectionOpts = ["* Document ID (<docId>)", "* Array of document IDs", "* Object containing `query`"].join("\n");
372
+ throw new Error("Unknown selection - must be one of:\n\n".concat(selectionOpts));
373
+ }
265
374
  const encodeQueryString = _ref2 => {
266
375
  let {
267
376
  query,
@@ -869,6 +978,8 @@ function _dataRequest(client, httpRequest, endpoint, body) {
869
978
  headers,
870
979
  token,
871
980
  tag,
981
+ perspective: options.perspective,
982
+ resultSourceMap: options.resultSourceMap,
872
983
  canUseCdn: isQuery,
873
984
  signal: options.signal,
874
985
  fetch: options.fetch
@@ -904,6 +1015,7 @@ function _create(client, httpRequest, doc, op) {
904
1015
  }, opts);
905
1016
  }
906
1017
  function _requestObservable(client, httpRequest, options) {
1018
+ var _a;
907
1019
  const uri = options.url || options.uri;
908
1020
  const config = client.config();
909
1021
  const canUseCdn = typeof options.canUseCdn === "undefined" ? ["GET", "HEAD"].indexOf(options.method || "GET") >= 0 && uri.indexOf("/data/") === 0 : options.canUseCdn;
@@ -916,15 +1028,17 @@ function _requestObservable(client, httpRequest, options) {
916
1028
  };
917
1029
  }
918
1030
  if (["GET", "HEAD", "POST"].indexOf(options.method || "GET") >= 0 && uri.indexOf("/data/query/") === 0) {
919
- if (config.resultSourceMap) {
1031
+ if ((_a = options.resultSourceMap) != null ? _a : config.resultSourceMap) {
920
1032
  options.query = {
921
1033
  resultSourceMap: true,
922
1034
  ...options.query
923
1035
  };
924
1036
  }
925
- if (typeof config.perspective === "string" && config.perspective !== "raw") {
1037
+ const perspective = options.perspective || config.perspective;
1038
+ if (typeof perspective === "string" && perspective !== "raw") {
1039
+ validateApiPerspective(perspective);
926
1040
  options.query = {
927
- perspective: config.perspective,
1041
+ perspective,
928
1042
  ...options.query
929
1043
  };
930
1044
  }
@@ -1079,115 +1193,6 @@ function optionsFromFile(opts, file) {
1079
1193
  contentType: file.type
1080
1194
  }, opts);
1081
1195
  }
1082
- const BASE_URL = "https://www.sanity.io/help/";
1083
- function generateHelpUrl(slug) {
1084
- return BASE_URL + slug;
1085
- }
1086
- function once(fn) {
1087
- let didCall = false;
1088
- let returnValue;
1089
- return function () {
1090
- if (didCall) {
1091
- return returnValue;
1092
- }
1093
- returnValue = fn(...arguments);
1094
- didCall = true;
1095
- return returnValue;
1096
- };
1097
- }
1098
- const createWarningPrinter = message =>
1099
- // eslint-disable-next-line no-console
1100
- once(function () {
1101
- for (var _len = arguments.length, args = new Array(_len), _key = 0; _key < _len; _key++) {
1102
- args[_key] = arguments[_key];
1103
- }
1104
- return console.warn(message.join(" "), ...args);
1105
- });
1106
- 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."]);
1107
- 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.")]);
1108
- const printNoApiVersionSpecifiedWarning = createWarningPrinter(["Using the Sanity client without specifying an API version is deprecated.", "See ".concat(generateHelpUrl("js-client-api-version"))]);
1109
- const printNoDefaultExport = createWarningPrinter(["The default export of @sanity/client has been deprecated. Use the named export `createClient` instead."]);
1110
- const defaultCdnHost = "apicdn.sanity.io";
1111
- const defaultConfig = {
1112
- apiHost: "https://api.sanity.io",
1113
- apiVersion: "1",
1114
- useProjectHostname: true
1115
- };
1116
- const LOCALHOSTS = ["localhost", "127.0.0.1", "0.0.0.0"];
1117
- const isLocal = host => LOCALHOSTS.indexOf(host) !== -1;
1118
- const validateApiVersion = function validateApiVersion2(apiVersion) {
1119
- if (apiVersion === "1" || apiVersion === "X") {
1120
- return;
1121
- }
1122
- const apiDate = new Date(apiVersion);
1123
- const apiVersionValid = /^\d{4}-\d{2}-\d{2}$/.test(apiVersion) && apiDate instanceof Date && apiDate.getTime() > 0;
1124
- if (!apiVersionValid) {
1125
- throw new Error("Invalid API version string, expected `1` or date in format `YYYY-MM-DD`");
1126
- }
1127
- };
1128
- const validateApiPerspective = function validateApiPerspective2(perspective) {
1129
- switch (perspective) {
1130
- case "previewDrafts":
1131
- case "published":
1132
- case "raw":
1133
- return;
1134
- default:
1135
- throw new TypeError("Invalid API perspective string, expected `published`, `previewDrafts` or `raw`");
1136
- }
1137
- };
1138
- const initConfig = (config, prevConfig) => {
1139
- const specifiedConfig = Object.assign({}, prevConfig, config);
1140
- if (!specifiedConfig.apiVersion) {
1141
- printNoApiVersionSpecifiedWarning();
1142
- }
1143
- const newConfig = Object.assign({}, defaultConfig, specifiedConfig);
1144
- const projectBased = newConfig.useProjectHostname;
1145
- if (typeof Promise === "undefined") {
1146
- const helpUrl = generateHelpUrl("js-client-promise-polyfill");
1147
- throw new Error("No native Promise-implementation found, polyfill needed - see ".concat(helpUrl));
1148
- }
1149
- if (projectBased && !newConfig.projectId) {
1150
- throw new Error("Configuration must contain `projectId`");
1151
- }
1152
- if (typeof newConfig.perspective === "string") {
1153
- validateApiPerspective(newConfig.perspective);
1154
- }
1155
- if ("encodeSourceMapAtPath" in newConfig || "encodeSourceMap" in newConfig || "studioUrl" in newConfig || "logger" in newConfig) {
1156
- 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.");
1157
- }
1158
- const isBrowser = typeof window !== "undefined" && window.location && window.location.hostname;
1159
- const isLocalhost = isBrowser && isLocal(window.location.hostname);
1160
- if (isBrowser && isLocalhost && newConfig.token && newConfig.ignoreBrowserTokenWarning !== true) {
1161
- printBrowserTokenWarning();
1162
- } else if (typeof newConfig.useCdn === "undefined") {
1163
- printCdnWarning();
1164
- }
1165
- if (projectBased) {
1166
- projectId(newConfig.projectId);
1167
- }
1168
- if (newConfig.dataset) {
1169
- dataset(newConfig.dataset);
1170
- }
1171
- if ("requestTagPrefix" in newConfig) {
1172
- newConfig.requestTagPrefix = newConfig.requestTagPrefix ? requestTag(newConfig.requestTagPrefix).replace(/\.+$/, "") : void 0;
1173
- }
1174
- newConfig.apiVersion = "".concat(newConfig.apiVersion).replace(/^v/, "");
1175
- newConfig.isDefaultApi = newConfig.apiHost === defaultConfig.apiHost;
1176
- newConfig.useCdn = newConfig.useCdn !== false && !newConfig.withCredentials;
1177
- validateApiVersion(newConfig.apiVersion);
1178
- const hostParts = newConfig.apiHost.split("://", 2);
1179
- const protocol = hostParts[0];
1180
- const host = hostParts[1];
1181
- const cdnHost = newConfig.isDefaultApi ? defaultCdnHost : host;
1182
- if (newConfig.useProjectHostname) {
1183
- newConfig.url = "".concat(protocol, "://").concat(newConfig.projectId, ".").concat(host, "/v").concat(newConfig.apiVersion);
1184
- newConfig.cdnUrl = "".concat(protocol, "://").concat(newConfig.projectId, ".").concat(cdnHost, "/v").concat(newConfig.apiVersion);
1185
- } else {
1186
- newConfig.url = "".concat(newConfig.apiHost, "/v").concat(newConfig.apiVersion);
1187
- newConfig.cdnUrl = newConfig.url;
1188
- }
1189
- return newConfig;
1190
- };
1191
1196
  var defaults = (obj, defaults) => Object.keys(defaults).concat(Object.keys(obj)).reduce((target, prop) => {
1192
1197
  target[prop] = typeof obj[prop] === "undefined" ? defaults[prop] : obj[prop];
1193
1198
  return target;