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