@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.
@@ -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
  };
@@ -660,23 +681,9 @@ const indexBy = (docs, attr) => docs.reduce((indexed, doc) => {
660
681
  return indexed;
661
682
  }, /* @__PURE__ */Object.create(null));
662
683
  const getQuerySizeLimit = 11264;
663
- function _fetch(client, httpRequest, query, _params) {
684
+ function _fetch(client, httpRequest, query, params) {
664
685
  let options = arguments.length > 4 && arguments[4] !== undefined ? arguments[4] : {};
665
686
  const mapResponse = options.filterResponse === false ? res => res : res => res.result;
666
- let params = _params;
667
- const {
668
- unstable_overlayDrafts,
669
- apiVersion
670
- } = client.config();
671
- if (unstable_overlayDrafts) {
672
- if (apiVersion !== "X") {
673
- console.error("You need to set `apiVersion` to `X` to use `unstable_overlayDrafts");
674
- }
675
- params = {
676
- ...params,
677
- draftMode: true
678
- };
679
- }
680
687
  return _dataRequest(client, httpRequest, "query", {
681
688
  query,
682
689
  params
@@ -797,6 +804,12 @@ function _requestObservable(client, httpRequest, options) {
797
804
  ...options.query
798
805
  };
799
806
  }
807
+ if (config.resultSourceMap) {
808
+ options.query = {
809
+ resultSourceMap: true,
810
+ ...options.query
811
+ };
812
+ }
800
813
  const reqOptions = requestOptions(config, Object.assign({}, options, {
801
814
  url: _getUrl(client, uri, useCdn)
802
815
  }));
@@ -941,7 +954,7 @@ function _upload(client, httpRequest, assetType, body) {
941
954
  });
942
955
  }
943
956
  function optionsFromFile(opts, file) {
944
- if (typeof window === "undefined" || !(file instanceof window.File)) {
957
+ if (typeof File === "undefined" || !(file instanceof File)) {
945
958
  return opts;
946
959
  }
947
960
  return Object.assign({
@@ -1054,7 +1067,6 @@ const pick = (obj, props) => props.reduce((selection, prop) => {
1054
1067
  return selection;
1055
1068
  }, {});
1056
1069
  const MAX_URL_LENGTH = 16e3 - 1200;
1057
- const EventSource = polyfilledEventSource;
1058
1070
  const possibleOptions = ["includePreviousRevision", "includeResult", "visibility", "effectFormat", "tag"];
1059
1071
  const defaultOptions = {
1060
1072
  includeResult: true
@@ -1097,7 +1109,13 @@ function _listen(query, params) {
1097
1109
  };
1098
1110
  }
1099
1111
  return new Observable(observer => {
1100
- let es = getEventSource();
1112
+ let es;
1113
+ getEventSource().then(eventSource => {
1114
+ es = eventSource;
1115
+ }).catch(reason => {
1116
+ observer.error(reason);
1117
+ stop();
1118
+ });
1101
1119
  let reconnectTimer;
1102
1120
  let stopped = false;
1103
1121
  function onError() {
@@ -1108,7 +1126,7 @@ function _listen(query, params) {
1108
1126
  if (stopped) {
1109
1127
  return;
1110
1128
  }
1111
- if (es.readyState === EventSource.CLOSED) {
1129
+ if (es.readyState === es.CLOSED) {
1112
1130
  unsubscribe();
1113
1131
  clearTimeout(reconnectTimer);
1114
1132
  reconnectTimer = setTimeout(open, 100);
@@ -1127,10 +1145,11 @@ function _listen(query, params) {
1127
1145
  observer.complete();
1128
1146
  }
1129
1147
  function unsubscribe() {
1130
- es.removeEventListener("error", onError, false);
1131
- es.removeEventListener("channelError", onChannelError, false);
1132
- es.removeEventListener("disconnect", onDisconnect, false);
1133
- 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));
1134
1153
  es.close();
1135
1154
  }
1136
1155
  function emitReconnect() {
@@ -1140,16 +1159,24 @@ function _listen(query, params) {
1140
1159
  });
1141
1160
  }
1142
1161
  }
1143
- function getEventSource() {
1162
+ async function getEventSource() {
1163
+ const {
1164
+ default: EventSource
1165
+ } = await import('@sanity/eventsource');
1144
1166
  const evs = new EventSource(uri, esOptions);
1145
- evs.addEventListener("error", onError, false);
1146
- evs.addEventListener("channelError", onChannelError, false);
1147
- evs.addEventListener("disconnect", onDisconnect, false);
1148
- 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));
1149
1171
  return evs;
1150
1172
  }
1151
1173
  function open() {
1152
- es = getEventSource();
1174
+ getEventSource().then(eventSource => {
1175
+ es = eventSource;
1176
+ }).catch(reason => {
1177
+ observer.error(reason);
1178
+ stop();
1179
+ });
1153
1180
  }
1154
1181
  function stop() {
1155
1182
  stopped = true;