@sanity/client 0.0.0-dev.3 → 0.0.0-dev.5

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
@@ -7,15 +7,8 @@ var middleware$1 = require('get-it/middleware');
7
7
  var getIt = require('get-it');
8
8
  var rxjs = require('rxjs');
9
9
  var operators = require('rxjs/operators');
10
- var polyfilledEventSource = require('@sanity/eventsource');
11
- function _interopDefaultCompat(e) {
12
- return e && typeof e === 'object' && 'default' in e ? e : {
13
- default: e
14
- };
15
- }
16
- var polyfilledEventSource__default = /*#__PURE__*/_interopDefaultCompat(polyfilledEventSource);
17
10
  var name = "@sanity/client";
18
- var version = "0.0.0-dev.3";
11
+ var version = "0.0.0-dev.5";
19
12
  const middleware = [middleware$1.debug({
20
13
  verbose: true,
21
14
  namespace: "sanity:client"
@@ -24,6 +17,7 @@ const middleware = [middleware$1.debug({
24
17
  }), middleware$1.retry({
25
18
  maxRetries: 3
26
19
  })];
20
+ const MAX_ITEMS_IN_ERROR_MESSAGE = 5;
27
21
  class ClientError extends Error {
28
22
  constructor(res) {
29
23
  const props = extractErrorProps(res);
@@ -53,6 +47,20 @@ function extractErrorProps(res) {
53
47
  props.message = "".concat(body.error, " - ").concat(body.message);
54
48
  return props;
55
49
  }
50
+ if (isMutationError(body)) {
51
+ const allItems = body.error.items || [];
52
+ const items = allItems.slice(0, MAX_ITEMS_IN_ERROR_MESSAGE).map(item => {
53
+ var _a;
54
+ return (_a = item.error) == null ? void 0 : _a.description;
55
+ }).filter(Boolean);
56
+ let itemsStr = items.length ? ":\n- ".concat(items.join("\n- ")) : "";
57
+ if (allItems.length > MAX_ITEMS_IN_ERROR_MESSAGE) {
58
+ itemsStr += "\n...and ".concat(allItems.length - MAX_ITEMS_IN_ERROR_MESSAGE, " more");
59
+ }
60
+ props.message = "".concat(body.error.description).concat(itemsStr);
61
+ props.details = body.error;
62
+ return props;
63
+ }
56
64
  if (body.error && body.error.description) {
57
65
  props.message = body.error.description;
58
66
  props.details = body.error;
@@ -61,6 +69,12 @@ function extractErrorProps(res) {
61
69
  props.message = body.error || body.message || httpErrorMessage(res);
62
70
  return props;
63
71
  }
72
+ function isMutationError(body) {
73
+ return isPlainObject(body) && isPlainObject(body.error) && body.error.type === "mutationError" && typeof body.error.description === "string";
74
+ }
75
+ function isPlainObject(obj) {
76
+ return typeof obj === "object" && obj !== null && !Array.isArray(obj);
77
+ }
64
78
  function httpErrorMessage(res) {
65
79
  const statusMessage = res.statusMessage ? " ".concat(res.statusMessage) : "";
66
80
  return "".concat(res.method, "-request to ").concat(res.url, " resulted in HTTP ").concat(res.statusCode).concat(statusMessage);
@@ -163,7 +177,7 @@ const validateObject = (op, val) => {
163
177
  }
164
178
  };
165
179
  const validateDocumentId = (op, id) => {
166
- if (typeof id !== "string" || !/^[a-z0-9_.-]+$/i.test(id)) {
180
+ if (typeof id !== "string" || !/^[a-z0-9_][a-z0-9_.-]{0,127}$/i.test(id) || id.includes("..")) {
167
181
  throw new Error("".concat(op, "(): \"").concat(id, "\" is not a valid document ID"));
168
182
  }
169
183
  };
@@ -680,23 +694,9 @@ const indexBy = (docs, attr) => docs.reduce((indexed, doc) => {
680
694
  return indexed;
681
695
  }, /* @__PURE__ */Object.create(null));
682
696
  const getQuerySizeLimit = 11264;
683
- function _fetch(client, httpRequest, query, _params) {
697
+ function _fetch(client, httpRequest, query, params) {
684
698
  let options = arguments.length > 4 && arguments[4] !== undefined ? arguments[4] : {};
685
699
  const mapResponse = options.filterResponse === false ? res => res : res => res.result;
686
- let params = _params;
687
- const {
688
- unstable_overlayDrafts,
689
- apiVersion
690
- } = client.config();
691
- if (unstable_overlayDrafts) {
692
- if (apiVersion !== "X") {
693
- console.error("You need to set `apiVersion` to `X` to use `unstable_overlayDrafts");
694
- }
695
- params = {
696
- ...params,
697
- draftMode: true
698
- };
699
- }
700
700
  return _dataRequest(client, httpRequest, "query", {
701
701
  query,
702
702
  params
@@ -817,6 +817,12 @@ function _requestObservable(client, httpRequest, options) {
817
817
  ...options.query
818
818
  };
819
819
  }
820
+ if (config.resultSourceMap) {
821
+ options.query = {
822
+ resultSourceMap: true,
823
+ ...options.query
824
+ };
825
+ }
820
826
  const reqOptions = requestOptions(config, Object.assign({}, options, {
821
827
  url: _getUrl(client, uri, useCdn)
822
828
  }));
@@ -961,7 +967,7 @@ function _upload(client, httpRequest, assetType, body) {
961
967
  });
962
968
  }
963
969
  function optionsFromFile(opts, file) {
964
- if (typeof window === "undefined" || !(file instanceof window.File)) {
970
+ if (typeof File === "undefined" || !(file instanceof File)) {
965
971
  return opts;
966
972
  }
967
973
  return Object.assign({
@@ -1074,7 +1080,6 @@ const pick = (obj, props) => props.reduce((selection, prop) => {
1074
1080
  return selection;
1075
1081
  }, {});
1076
1082
  const MAX_URL_LENGTH = 16e3 - 1200;
1077
- const EventSource = polyfilledEventSource__default.default;
1078
1083
  const possibleOptions = ["includePreviousRevision", "includeResult", "visibility", "effectFormat", "tag"];
1079
1084
  const defaultOptions = {
1080
1085
  includeResult: true
@@ -1117,7 +1122,13 @@ function _listen(query, params) {
1117
1122
  };
1118
1123
  }
1119
1124
  return new rxjs.Observable(observer => {
1120
- let es = getEventSource();
1125
+ let es;
1126
+ getEventSource().then(eventSource => {
1127
+ es = eventSource;
1128
+ }).catch(reason => {
1129
+ observer.error(reason);
1130
+ stop();
1131
+ });
1121
1132
  let reconnectTimer;
1122
1133
  let stopped = false;
1123
1134
  function onError() {
@@ -1128,7 +1139,7 @@ function _listen(query, params) {
1128
1139
  if (stopped) {
1129
1140
  return;
1130
1141
  }
1131
- if (es.readyState === EventSource.CLOSED) {
1142
+ if (es.readyState === es.CLOSED) {
1132
1143
  unsubscribe();
1133
1144
  clearTimeout(reconnectTimer);
1134
1145
  reconnectTimer = setTimeout(open, 100);
@@ -1147,10 +1158,11 @@ function _listen(query, params) {
1147
1158
  observer.complete();
1148
1159
  }
1149
1160
  function unsubscribe() {
1150
- es.removeEventListener("error", onError, false);
1151
- es.removeEventListener("channelError", onChannelError, false);
1152
- es.removeEventListener("disconnect", onDisconnect, false);
1153
- listenFor.forEach(type => es.removeEventListener(type, onMessage, false));
1161
+ if (!es) return;
1162
+ es.removeEventListener("error", onError);
1163
+ es.removeEventListener("channelError", onChannelError);
1164
+ es.removeEventListener("disconnect", onDisconnect);
1165
+ listenFor.forEach(type => es.removeEventListener(type, onMessage));
1154
1166
  es.close();
1155
1167
  }
1156
1168
  function emitReconnect() {
@@ -1160,16 +1172,24 @@ function _listen(query, params) {
1160
1172
  });
1161
1173
  }
1162
1174
  }
1163
- function getEventSource() {
1175
+ async function getEventSource() {
1176
+ const {
1177
+ default: EventSource
1178
+ } = await import('@sanity/eventsource');
1164
1179
  const evs = new EventSource(uri, esOptions);
1165
- evs.addEventListener("error", onError, false);
1166
- evs.addEventListener("channelError", onChannelError, false);
1167
- evs.addEventListener("disconnect", onDisconnect, false);
1168
- listenFor.forEach(type => evs.addEventListener(type, onMessage, false));
1180
+ evs.addEventListener("error", onError);
1181
+ evs.addEventListener("channelError", onChannelError);
1182
+ evs.addEventListener("disconnect", onDisconnect);
1183
+ listenFor.forEach(type => evs.addEventListener(type, onMessage));
1169
1184
  return evs;
1170
1185
  }
1171
1186
  function open() {
1172
- es = getEventSource();
1187
+ getEventSource().then(eventSource => {
1188
+ es = eventSource;
1189
+ }).catch(reason => {
1190
+ observer.error(reason);
1191
+ stop();
1192
+ });
1173
1193
  }
1174
1194
  function stop() {
1175
1195
  stopped = true;
@@ -1758,6 +1778,18 @@ function deprecatedCreateClient(config) {
1758
1778
  printNoDefaultExport();
1759
1779
  return new SanityClient(httpRequest, config);
1760
1780
  }
1781
+ Object.defineProperty(exports, 'unstable__adapter', {
1782
+ enumerable: true,
1783
+ get: function () {
1784
+ return getIt.adapter;
1785
+ }
1786
+ });
1787
+ Object.defineProperty(exports, 'unstable__environment', {
1788
+ enumerable: true,
1789
+ get: function () {
1790
+ return getIt.environment;
1791
+ }
1792
+ });
1761
1793
  exports.BasePatch = BasePatch;
1762
1794
  exports.BaseTransaction = BaseTransaction;
1763
1795
  exports.ClientError = ClientError;
package/dist/index.cjs.js CHANGED
@@ -1,5 +1,7 @@
1
1
  import cjs from './index.cjs';
2
2
 
3
+ export const unstable__adapter = cjs.unstable__adapter;
4
+ export const unstable__environment = cjs.unstable__environment;
3
5
  export const BasePatch = cjs.BasePatch;
4
6
  export const BaseTransaction = cjs.BaseTransaction;
5
7
  export const ClientError = cjs.ClientError;