@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.
@@ -1,9 +1,10 @@
1
1
  import { getIt } from 'get-it';
2
+ export { adapter as unstable__adapter, environment as unstable__environment } from 'get-it';
2
3
  import { jsonRequest, jsonResponse, progress, observable } from 'get-it/middleware';
3
4
  import { Observable, lastValueFrom } from 'rxjs';
4
5
  import { map, filter } from 'rxjs/operators';
5
- import polyfilledEventSource from '@sanity/eventsource';
6
6
  var envMiddleware = [];
7
+ const MAX_ITEMS_IN_ERROR_MESSAGE = 5;
7
8
  class ClientError extends Error {
8
9
  constructor(res) {
9
10
  const props = extractErrorProps(res);
@@ -33,6 +34,20 @@ function extractErrorProps(res) {
33
34
  props.message = "".concat(body.error, " - ").concat(body.message);
34
35
  return props;
35
36
  }
37
+ if (isMutationError(body)) {
38
+ const allItems = body.error.items || [];
39
+ const items = allItems.slice(0, MAX_ITEMS_IN_ERROR_MESSAGE).map(item => {
40
+ var _a;
41
+ return (_a = item.error) == null ? void 0 : _a.description;
42
+ }).filter(Boolean);
43
+ let itemsStr = items.length ? ":\n- ".concat(items.join("\n- ")) : "";
44
+ if (allItems.length > MAX_ITEMS_IN_ERROR_MESSAGE) {
45
+ itemsStr += "\n...and ".concat(allItems.length - MAX_ITEMS_IN_ERROR_MESSAGE, " more");
46
+ }
47
+ props.message = "".concat(body.error.description).concat(itemsStr);
48
+ props.details = body.error;
49
+ return props;
50
+ }
36
51
  if (body.error && body.error.description) {
37
52
  props.message = body.error.description;
38
53
  props.details = body.error;
@@ -41,6 +56,12 @@ function extractErrorProps(res) {
41
56
  props.message = body.error || body.message || httpErrorMessage(res);
42
57
  return props;
43
58
  }
59
+ function isMutationError(body) {
60
+ return isPlainObject(body) && isPlainObject(body.error) && body.error.type === "mutationError" && typeof body.error.description === "string";
61
+ }
62
+ function isPlainObject(obj) {
63
+ return typeof obj === "object" && obj !== null && !Array.isArray(obj);
64
+ }
44
65
  function httpErrorMessage(res) {
45
66
  const statusMessage = res.statusMessage ? " ".concat(res.statusMessage) : "";
46
67
  return "".concat(res.method, "-request to ").concat(res.url, " resulted in HTTP ").concat(res.statusCode).concat(statusMessage);
@@ -143,7 +164,7 @@ const validateObject = (op, val) => {
143
164
  }
144
165
  };
145
166
  const validateDocumentId = (op, id) => {
146
- if (typeof id !== "string" || !/^[a-z0-9_.-]+$/i.test(id)) {
167
+ if (typeof id !== "string" || !/^[a-z0-9_][a-z0-9_.-]{0,127}$/i.test(id) || id.includes("..")) {
147
168
  throw new Error("".concat(op, "(): \"").concat(id, "\" is not a valid document ID"));
148
169
  }
149
170
  };
@@ -783,16 +804,10 @@ function _requestObservable(client, httpRequest, options) {
783
804
  ...options.query
784
805
  };
785
806
  }
786
- if (config.unstable_overlayDrafts) {
787
- if (config.apiVersion !== "X") {
788
- console.error("You need to set `apiVersion` to `X` to use `unstable_overlayDrafts");
789
- }
807
+ if (config.encodeStegaSourceMap) {
790
808
  options.query = {
791
- ...options.query,
792
- params: {
793
- ...(options.query.params || {}),
794
- draftMode: true
795
- }
809
+ encodeHackySourceMap: true,
810
+ ...options.query
796
811
  };
797
812
  }
798
813
  const reqOptions = requestOptions(config, Object.assign({}, options, {
@@ -939,7 +954,7 @@ function _upload(client, httpRequest, assetType, body) {
939
954
  });
940
955
  }
941
956
  function optionsFromFile(opts, file) {
942
- if (typeof window === "undefined" || !(file instanceof window.File)) {
957
+ if (typeof File === "undefined" || !(file instanceof File)) {
943
958
  return opts;
944
959
  }
945
960
  return Object.assign({
@@ -1052,7 +1067,6 @@ const pick = (obj, props) => props.reduce((selection, prop) => {
1052
1067
  return selection;
1053
1068
  }, {});
1054
1069
  const MAX_URL_LENGTH = 16e3 - 1200;
1055
- const EventSource = polyfilledEventSource;
1056
1070
  const possibleOptions = ["includePreviousRevision", "includeResult", "visibility", "effectFormat", "tag"];
1057
1071
  const defaultOptions = {
1058
1072
  includeResult: true
@@ -1095,7 +1109,13 @@ function _listen(query, params) {
1095
1109
  };
1096
1110
  }
1097
1111
  return new Observable(observer => {
1098
- let es = getEventSource();
1112
+ let es;
1113
+ getEventSource().then(eventSource => {
1114
+ es = eventSource;
1115
+ }).catch(reason => {
1116
+ observer.error(reason);
1117
+ stop();
1118
+ });
1099
1119
  let reconnectTimer;
1100
1120
  let stopped = false;
1101
1121
  function onError() {
@@ -1106,7 +1126,7 @@ function _listen(query, params) {
1106
1126
  if (stopped) {
1107
1127
  return;
1108
1128
  }
1109
- if (es.readyState === EventSource.CLOSED) {
1129
+ if (es.readyState === es.CLOSED) {
1110
1130
  unsubscribe();
1111
1131
  clearTimeout(reconnectTimer);
1112
1132
  reconnectTimer = setTimeout(open, 100);
@@ -1125,10 +1145,11 @@ function _listen(query, params) {
1125
1145
  observer.complete();
1126
1146
  }
1127
1147
  function unsubscribe() {
1128
- es.removeEventListener("error", onError, false);
1129
- es.removeEventListener("channelError", onChannelError, false);
1130
- es.removeEventListener("disconnect", onDisconnect, false);
1131
- listenFor.forEach(type => es.removeEventListener(type, onMessage, false));
1148
+ if (!es) return;
1149
+ es.removeEventListener("error", onError);
1150
+ es.removeEventListener("channelError", onChannelError);
1151
+ es.removeEventListener("disconnect", onDisconnect);
1152
+ listenFor.forEach(type => es.removeEventListener(type, onMessage));
1132
1153
  es.close();
1133
1154
  }
1134
1155
  function emitReconnect() {
@@ -1138,16 +1159,24 @@ function _listen(query, params) {
1138
1159
  });
1139
1160
  }
1140
1161
  }
1141
- function getEventSource() {
1162
+ async function getEventSource() {
1163
+ const {
1164
+ default: EventSource
1165
+ } = await import('@sanity/eventsource');
1142
1166
  const evs = new EventSource(uri, esOptions);
1143
- evs.addEventListener("error", onError, false);
1144
- evs.addEventListener("channelError", onChannelError, false);
1145
- evs.addEventListener("disconnect", onDisconnect, false);
1146
- listenFor.forEach(type => evs.addEventListener(type, onMessage, false));
1167
+ evs.addEventListener("error", onError);
1168
+ evs.addEventListener("channelError", onChannelError);
1169
+ evs.addEventListener("disconnect", onDisconnect);
1170
+ listenFor.forEach(type => evs.addEventListener(type, onMessage));
1147
1171
  return evs;
1148
1172
  }
1149
1173
  function open() {
1150
- es = getEventSource();
1174
+ getEventSource().then(eventSource => {
1175
+ es = eventSource;
1176
+ }).catch(reason => {
1177
+ observer.error(reason);
1178
+ stop();
1179
+ });
1151
1180
  }
1152
1181
  function stop() {
1153
1182
  stopped = true;