@sanity/client 5.2.2 → 5.3.0

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.
@@ -4,6 +4,7 @@ import { Observable, lastValueFrom } from 'rxjs';
4
4
  import { map, filter } from 'rxjs/operators';
5
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
  };