@sanity/client 5.2.2 → 5.3.1

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.d.ts CHANGED
@@ -21,6 +21,13 @@ export declare type AllDocumentsMutationOptions = BaseMutationOptions & {
21
21
  */
22
22
  export declare type Any = any
23
23
 
24
+ /** @internal */
25
+ export declare interface ApiError {
26
+ error: string
27
+ message: string
28
+ statusCode: number
29
+ }
30
+
24
31
  /** @public */
25
32
  export declare type AssetMetadataType =
26
33
  | 'location'
@@ -538,6 +545,24 @@ export declare type Mutation<R extends Record<string, Any> = Record<string, Any>
538
545
  patch: PatchMutationOperation
539
546
  }
540
547
 
548
+ /** @internal */
549
+ export declare interface MutationError {
550
+ error: {
551
+ type: 'mutationError'
552
+ description: string
553
+ items?: MutationErrorItem[]
554
+ }
555
+ }
556
+
557
+ /** @internal */
558
+ export declare interface MutationErrorItem {
559
+ error: {
560
+ type: string
561
+ description: string
562
+ value?: unknown
563
+ }
564
+ }
565
+
541
566
  /** @public */
542
567
  declare type MutationEvent_2<R extends Record<string, Any> = Record<string, Any>> = {
543
568
  type: 'mutation'
package/dist/index.js CHANGED
@@ -4,7 +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 name = "@sanity/client";
7
- var version = "5.2.2";
7
+ var version = "5.3.1";
8
8
  const middleware = [debug({
9
9
  verbose: true,
10
10
  namespace: "sanity:client"
@@ -13,6 +13,7 @@ const middleware = [debug({
13
13
  }), retry({
14
14
  maxRetries: 3
15
15
  })];
16
+ const MAX_ITEMS_IN_ERROR_MESSAGE = 5;
16
17
  class ClientError extends Error {
17
18
  constructor(res) {
18
19
  const props = extractErrorProps(res);
@@ -42,6 +43,20 @@ function extractErrorProps(res) {
42
43
  props.message = "".concat(body.error, " - ").concat(body.message);
43
44
  return props;
44
45
  }
46
+ if (isMutationError(body)) {
47
+ const allItems = body.error.items || [];
48
+ const items = allItems.slice(0, MAX_ITEMS_IN_ERROR_MESSAGE).map(item => {
49
+ var _a;
50
+ return (_a = item.error) == null ? void 0 : _a.description;
51
+ }).filter(Boolean);
52
+ let itemsStr = items.length ? ":\n- ".concat(items.join("\n- ")) : "";
53
+ if (allItems.length > MAX_ITEMS_IN_ERROR_MESSAGE) {
54
+ itemsStr += "\n...and ".concat(allItems.length - MAX_ITEMS_IN_ERROR_MESSAGE, " more");
55
+ }
56
+ props.message = "".concat(body.error.description).concat(itemsStr);
57
+ props.details = body.error;
58
+ return props;
59
+ }
45
60
  if (body.error && body.error.description) {
46
61
  props.message = body.error.description;
47
62
  props.details = body.error;
@@ -50,6 +65,12 @@ function extractErrorProps(res) {
50
65
  props.message = body.error || body.message || httpErrorMessage(res);
51
66
  return props;
52
67
  }
68
+ function isMutationError(body) {
69
+ return isPlainObject(body) && isPlainObject(body.error) && body.error.type === "mutationError" && typeof body.error.description === "string";
70
+ }
71
+ function isPlainObject(obj) {
72
+ return typeof obj === "object" && obj !== null && !Array.isArray(obj);
73
+ }
53
74
  function httpErrorMessage(res) {
54
75
  const statusMessage = res.statusMessage ? " ".concat(res.statusMessage) : "";
55
76
  return "".concat(res.method, "-request to ").concat(res.url, " resulted in HTTP ").concat(res.statusCode).concat(statusMessage);
@@ -152,7 +173,7 @@ const validateObject = (op, val) => {
152
173
  }
153
174
  };
154
175
  const validateDocumentId = (op, id) => {
155
- if (typeof id !== "string" || !/^[a-z0-9_.-]+$/i.test(id)) {
176
+ if (typeof id !== "string" || !/^[a-z0-9_][a-z0-9_.-]{0,127}$/i.test(id) || id.includes("..")) {
156
177
  throw new Error("".concat(op, "(): \"").concat(id, "\" is not a valid document ID"));
157
178
  }
158
179
  };
@@ -1122,10 +1143,10 @@ function _listen(query, params) {
1122
1143
  observer.complete();
1123
1144
  }
1124
1145
  function unsubscribe() {
1125
- es.removeEventListener("error", onError, false);
1126
- es.removeEventListener("channelError", onChannelError, false);
1127
- es.removeEventListener("disconnect", onDisconnect, false);
1128
- listenFor.forEach(type => es.removeEventListener(type, onMessage, false));
1146
+ es.removeEventListener("error", onError);
1147
+ es.removeEventListener("channelError", onChannelError);
1148
+ es.removeEventListener("disconnect", onDisconnect);
1149
+ listenFor.forEach(type => es.removeEventListener(type, onMessage));
1129
1150
  es.close();
1130
1151
  }
1131
1152
  function emitReconnect() {
@@ -1137,10 +1158,10 @@ function _listen(query, params) {
1137
1158
  }
1138
1159
  function getEventSource() {
1139
1160
  const evs = new EventSource(uri, esOptions);
1140
- evs.addEventListener("error", onError, false);
1141
- evs.addEventListener("channelError", onChannelError, false);
1142
- evs.addEventListener("disconnect", onDisconnect, false);
1143
- listenFor.forEach(type => evs.addEventListener(type, onMessage, false));
1161
+ evs.addEventListener("error", onError);
1162
+ evs.addEventListener("channelError", onChannelError);
1163
+ evs.addEventListener("disconnect", onDisconnect);
1164
+ listenFor.forEach(type => evs.addEventListener(type, onMessage));
1144
1165
  return evs;
1145
1166
  }
1146
1167
  function open() {