@sanity/client 0.0.0-dev.2 → 0.0.0-dev.4

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.2";
11
+ var version = "0.0.0-dev.4";
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
  };
@@ -803,16 +817,10 @@ function _requestObservable(client, httpRequest, options) {
803
817
  ...options.query
804
818
  };
805
819
  }
806
- if (config.unstable_overlayDrafts) {
807
- if (config.apiVersion !== "X") {
808
- console.error("You need to set `apiVersion` to `X` to use `unstable_overlayDrafts");
809
- }
820
+ if (config.encodeStegaSourceMap) {
810
821
  options.query = {
811
- ...options.query,
812
- params: {
813
- ...(options.query.params || {}),
814
- draftMode: true
815
- }
822
+ encodeHackySourceMap: true,
823
+ ...options.query
816
824
  };
817
825
  }
818
826
  const reqOptions = requestOptions(config, Object.assign({}, options, {
@@ -959,7 +967,7 @@ function _upload(client, httpRequest, assetType, body) {
959
967
  });
960
968
  }
961
969
  function optionsFromFile(opts, file) {
962
- if (typeof window === "undefined" || !(file instanceof window.File)) {
970
+ if (typeof File === "undefined" || !(file instanceof File)) {
963
971
  return opts;
964
972
  }
965
973
  return Object.assign({
@@ -1072,7 +1080,6 @@ const pick = (obj, props) => props.reduce((selection, prop) => {
1072
1080
  return selection;
1073
1081
  }, {});
1074
1082
  const MAX_URL_LENGTH = 16e3 - 1200;
1075
- const EventSource = polyfilledEventSource__default.default;
1076
1083
  const possibleOptions = ["includePreviousRevision", "includeResult", "visibility", "effectFormat", "tag"];
1077
1084
  const defaultOptions = {
1078
1085
  includeResult: true
@@ -1115,7 +1122,13 @@ function _listen(query, params) {
1115
1122
  };
1116
1123
  }
1117
1124
  return new rxjs.Observable(observer => {
1118
- let es = getEventSource();
1125
+ let es;
1126
+ getEventSource().then(eventSource => {
1127
+ es = eventSource;
1128
+ }).catch(reason => {
1129
+ observer.error(reason);
1130
+ stop();
1131
+ });
1119
1132
  let reconnectTimer;
1120
1133
  let stopped = false;
1121
1134
  function onError() {
@@ -1126,7 +1139,7 @@ function _listen(query, params) {
1126
1139
  if (stopped) {
1127
1140
  return;
1128
1141
  }
1129
- if (es.readyState === EventSource.CLOSED) {
1142
+ if (es.readyState === es.CLOSED) {
1130
1143
  unsubscribe();
1131
1144
  clearTimeout(reconnectTimer);
1132
1145
  reconnectTimer = setTimeout(open, 100);
@@ -1145,10 +1158,11 @@ function _listen(query, params) {
1145
1158
  observer.complete();
1146
1159
  }
1147
1160
  function unsubscribe() {
1148
- es.removeEventListener("error", onError, false);
1149
- es.removeEventListener("channelError", onChannelError, false);
1150
- es.removeEventListener("disconnect", onDisconnect, false);
1151
- 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));
1152
1166
  es.close();
1153
1167
  }
1154
1168
  function emitReconnect() {
@@ -1158,16 +1172,24 @@ function _listen(query, params) {
1158
1172
  });
1159
1173
  }
1160
1174
  }
1161
- function getEventSource() {
1175
+ async function getEventSource() {
1176
+ const {
1177
+ default: EventSource
1178
+ } = await import('@sanity/eventsource');
1162
1179
  const evs = new EventSource(uri, esOptions);
1163
- evs.addEventListener("error", onError, false);
1164
- evs.addEventListener("channelError", onChannelError, false);
1165
- evs.addEventListener("disconnect", onDisconnect, false);
1166
- 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));
1167
1184
  return evs;
1168
1185
  }
1169
1186
  function open() {
1170
- es = getEventSource();
1187
+ getEventSource().then(eventSource => {
1188
+ es = eventSource;
1189
+ }).catch(reason => {
1190
+ observer.error(reason);
1191
+ stop();
1192
+ });
1171
1193
  }
1172
1194
  function stop() {
1173
1195
  stopped = true;
@@ -1756,6 +1778,18 @@ function deprecatedCreateClient(config) {
1756
1778
  printNoDefaultExport();
1757
1779
  return new SanityClient(httpRequest, config);
1758
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
+ });
1759
1793
  exports.BasePatch = BasePatch;
1760
1794
  exports.BaseTransaction = BaseTransaction;
1761
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;