@sanity/client 5.2.1 → 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.
- package/dist/index.browser.cjs +22 -1
- package/dist/index.browser.cjs.map +1 -1
- package/dist/index.browser.js +22 -1
- package/dist/index.browser.js.map +1 -1
- package/dist/index.cjs +23 -2
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.ts +25 -0
- package/dist/index.js +23 -2
- package/dist/index.js.map +1 -1
- package/package.json +16 -16
- package/src/http/errors.ts +32 -1
- package/src/types.ts +25 -0
- package/src/validators.ts +1 -1
- package/umd/sanityClient.js +25 -19
- package/umd/sanityClient.min.js +3 -3
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.
|
|
7
|
+
var version = "5.3.0";
|
|
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_.-]
|
|
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
|
};
|