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