@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.browser.cjs +30 -9
- package/dist/index.browser.cjs.map +1 -1
- package/dist/index.browser.js +30 -9
- package/dist/index.browser.js.map +1 -1
- package/dist/index.cjs +31 -10
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.ts +25 -0
- package/dist/index.js +31 -10
- package/dist/index.js.map +1 -1
- package/package.json +22 -22
- package/src/data/listen.ts +8 -8
- package/src/http/errors.ts +32 -1
- package/src/types.ts +25 -0
- package/src/validators.ts +1 -1
- package/umd/sanityClient.js +35 -16
- package/umd/sanityClient.min.js +3 -3
package/dist/index.browser.cjs
CHANGED
|
@@ -15,6 +15,7 @@ function _interopDefaultCompat(e) {
|
|
|
15
15
|
}
|
|
16
16
|
var polyfilledEventSource__default = /*#__PURE__*/_interopDefaultCompat(polyfilledEventSource);
|
|
17
17
|
var envMiddleware = [];
|
|
18
|
+
const MAX_ITEMS_IN_ERROR_MESSAGE = 5;
|
|
18
19
|
class ClientError extends Error {
|
|
19
20
|
constructor(res) {
|
|
20
21
|
const props = extractErrorProps(res);
|
|
@@ -44,6 +45,20 @@ function extractErrorProps(res) {
|
|
|
44
45
|
props.message = "".concat(body.error, " - ").concat(body.message);
|
|
45
46
|
return props;
|
|
46
47
|
}
|
|
48
|
+
if (isMutationError(body)) {
|
|
49
|
+
const allItems = body.error.items || [];
|
|
50
|
+
const items = allItems.slice(0, MAX_ITEMS_IN_ERROR_MESSAGE).map(item => {
|
|
51
|
+
var _a;
|
|
52
|
+
return (_a = item.error) == null ? void 0 : _a.description;
|
|
53
|
+
}).filter(Boolean);
|
|
54
|
+
let itemsStr = items.length ? ":\n- ".concat(items.join("\n- ")) : "";
|
|
55
|
+
if (allItems.length > MAX_ITEMS_IN_ERROR_MESSAGE) {
|
|
56
|
+
itemsStr += "\n...and ".concat(allItems.length - MAX_ITEMS_IN_ERROR_MESSAGE, " more");
|
|
57
|
+
}
|
|
58
|
+
props.message = "".concat(body.error.description).concat(itemsStr);
|
|
59
|
+
props.details = body.error;
|
|
60
|
+
return props;
|
|
61
|
+
}
|
|
47
62
|
if (body.error && body.error.description) {
|
|
48
63
|
props.message = body.error.description;
|
|
49
64
|
props.details = body.error;
|
|
@@ -52,6 +67,12 @@ function extractErrorProps(res) {
|
|
|
52
67
|
props.message = body.error || body.message || httpErrorMessage(res);
|
|
53
68
|
return props;
|
|
54
69
|
}
|
|
70
|
+
function isMutationError(body) {
|
|
71
|
+
return isPlainObject(body) && isPlainObject(body.error) && body.error.type === "mutationError" && typeof body.error.description === "string";
|
|
72
|
+
}
|
|
73
|
+
function isPlainObject(obj) {
|
|
74
|
+
return typeof obj === "object" && obj !== null && !Array.isArray(obj);
|
|
75
|
+
}
|
|
55
76
|
function httpErrorMessage(res) {
|
|
56
77
|
const statusMessage = res.statusMessage ? " ".concat(res.statusMessage) : "";
|
|
57
78
|
return "".concat(res.method, "-request to ").concat(res.url, " resulted in HTTP ").concat(res.statusCode).concat(statusMessage);
|
|
@@ -154,7 +175,7 @@ const validateObject = (op, val) => {
|
|
|
154
175
|
}
|
|
155
176
|
};
|
|
156
177
|
const validateDocumentId = (op, id) => {
|
|
157
|
-
if (typeof id !== "string" || !/^[a-z0-9_.-]
|
|
178
|
+
if (typeof id !== "string" || !/^[a-z0-9_][a-z0-9_.-]{0,127}$/i.test(id) || id.includes("..")) {
|
|
158
179
|
throw new Error("".concat(op, "(): \"").concat(id, "\" is not a valid document ID"));
|
|
159
180
|
}
|
|
160
181
|
};
|
|
@@ -1124,10 +1145,10 @@ function _listen(query, params) {
|
|
|
1124
1145
|
observer.complete();
|
|
1125
1146
|
}
|
|
1126
1147
|
function unsubscribe() {
|
|
1127
|
-
es.removeEventListener("error", onError
|
|
1128
|
-
es.removeEventListener("channelError", onChannelError
|
|
1129
|
-
es.removeEventListener("disconnect", onDisconnect
|
|
1130
|
-
listenFor.forEach(type => es.removeEventListener(type, onMessage
|
|
1148
|
+
es.removeEventListener("error", onError);
|
|
1149
|
+
es.removeEventListener("channelError", onChannelError);
|
|
1150
|
+
es.removeEventListener("disconnect", onDisconnect);
|
|
1151
|
+
listenFor.forEach(type => es.removeEventListener(type, onMessage));
|
|
1131
1152
|
es.close();
|
|
1132
1153
|
}
|
|
1133
1154
|
function emitReconnect() {
|
|
@@ -1139,10 +1160,10 @@ function _listen(query, params) {
|
|
|
1139
1160
|
}
|
|
1140
1161
|
function getEventSource() {
|
|
1141
1162
|
const evs = new EventSource(uri, esOptions);
|
|
1142
|
-
evs.addEventListener("error", onError
|
|
1143
|
-
evs.addEventListener("channelError", onChannelError
|
|
1144
|
-
evs.addEventListener("disconnect", onDisconnect
|
|
1145
|
-
listenFor.forEach(type => evs.addEventListener(type, onMessage
|
|
1163
|
+
evs.addEventListener("error", onError);
|
|
1164
|
+
evs.addEventListener("channelError", onChannelError);
|
|
1165
|
+
evs.addEventListener("disconnect", onDisconnect);
|
|
1166
|
+
listenFor.forEach(type => evs.addEventListener(type, onMessage));
|
|
1146
1167
|
return evs;
|
|
1147
1168
|
}
|
|
1148
1169
|
function open() {
|