@sanity/client 7.5.0 → 7.7.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/README.md +7 -0
- package/dist/index.browser.cjs +17 -7
- package/dist/index.browser.cjs.map +1 -1
- package/dist/index.browser.d.cts +104 -2
- package/dist/index.browser.d.ts +104 -2
- package/dist/index.browser.js +17 -7
- package/dist/index.browser.js.map +1 -1
- package/dist/index.cjs +18 -7
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +104 -2
- package/dist/index.d.ts +104 -2
- package/dist/index.js +18 -7
- package/dist/index.js.map +1 -1
- package/dist/stega.browser.d.cts +104 -2
- package/dist/stega.browser.d.ts +104 -2
- package/dist/stega.d.cts +104 -2
- package/dist/stega.d.ts +104 -2
- package/package.json +1 -1
- package/src/agent/actions/transform.ts +32 -2
- package/src/defineCreateClient.ts +5 -1
- package/src/http/errors.ts +53 -0
- package/src/http/request.ts +31 -3
- package/src/types.ts +44 -0
- package/umd/sanityClient.js +17 -7
- package/umd/sanityClient.min.js +2 -2
package/src/http/errors.ts
CHANGED
|
@@ -6,6 +6,59 @@ import {isRecord} from '../util/isRecord'
|
|
|
6
6
|
|
|
7
7
|
const MAX_ITEMS_IN_ERROR_MESSAGE = 5
|
|
8
8
|
|
|
9
|
+
/**
|
|
10
|
+
* Shared properties for HTTP errors (eg both ClientError and ServerError)
|
|
11
|
+
* Use `isHttpError` for type narrowing and accessing response properties.
|
|
12
|
+
*
|
|
13
|
+
* @public
|
|
14
|
+
*/
|
|
15
|
+
export interface HttpError {
|
|
16
|
+
statusCode: number
|
|
17
|
+
message: string
|
|
18
|
+
response: {
|
|
19
|
+
body: unknown
|
|
20
|
+
url: string
|
|
21
|
+
method: string
|
|
22
|
+
headers: Record<string, string>
|
|
23
|
+
statusCode: number
|
|
24
|
+
statusMessage: string | null
|
|
25
|
+
}
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
/**
|
|
29
|
+
* Checks if the provided error is an HTTP error.
|
|
30
|
+
*
|
|
31
|
+
* @param error - The error to check.
|
|
32
|
+
* @returns `true` if the error is an HTTP error, `false` otherwise.
|
|
33
|
+
* @public
|
|
34
|
+
*/
|
|
35
|
+
export function isHttpError(error: unknown): error is HttpError {
|
|
36
|
+
if (!isRecord(error)) {
|
|
37
|
+
return false
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
const response = error.response
|
|
41
|
+
if (
|
|
42
|
+
typeof error.statusCode !== 'number' ||
|
|
43
|
+
typeof error.message !== 'string' ||
|
|
44
|
+
!isRecord(response)
|
|
45
|
+
) {
|
|
46
|
+
return false
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
if (
|
|
50
|
+
typeof response.body === 'undefined' ||
|
|
51
|
+
typeof response.url !== 'string' ||
|
|
52
|
+
typeof response.method !== 'string' ||
|
|
53
|
+
typeof response.headers !== 'object' ||
|
|
54
|
+
typeof response.statusCode !== 'number'
|
|
55
|
+
) {
|
|
56
|
+
return false
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
return true
|
|
60
|
+
}
|
|
61
|
+
|
|
9
62
|
/** @public */
|
|
10
63
|
export class ClientError extends Error {
|
|
11
64
|
response: ErrorProps['response']
|
package/src/http/request.ts
CHANGED
|
@@ -17,14 +17,39 @@ const httpError = {
|
|
|
17
17
|
},
|
|
18
18
|
}
|
|
19
19
|
|
|
20
|
-
function printWarnings() {
|
|
20
|
+
function printWarnings(config: {ignoreWarnings?: string | RegExp | Array<string | RegExp>} = {}) {
|
|
21
21
|
const seen: Record<string, boolean> = {}
|
|
22
|
+
|
|
23
|
+
// Helper function to check if a warning should be ignored
|
|
24
|
+
const shouldIgnoreWarning = (message: string): boolean => {
|
|
25
|
+
if (config.ignoreWarnings === undefined) return false
|
|
26
|
+
|
|
27
|
+
const patterns = Array.isArray(config.ignoreWarnings)
|
|
28
|
+
? config.ignoreWarnings
|
|
29
|
+
: [config.ignoreWarnings]
|
|
30
|
+
|
|
31
|
+
return patterns.some((pattern) => {
|
|
32
|
+
if (typeof pattern === 'string') {
|
|
33
|
+
return message.includes(pattern)
|
|
34
|
+
} else if (pattern instanceof RegExp) {
|
|
35
|
+
return pattern.test(message)
|
|
36
|
+
}
|
|
37
|
+
return false
|
|
38
|
+
})
|
|
39
|
+
}
|
|
40
|
+
|
|
22
41
|
return {
|
|
23
42
|
onResponse: (res: Any) => {
|
|
24
43
|
const warn = res.headers['x-sanity-warning']
|
|
25
44
|
const warnings = Array.isArray(warn) ? warn : [warn]
|
|
26
45
|
for (const msg of warnings) {
|
|
27
46
|
if (!msg || seen[msg]) continue
|
|
47
|
+
|
|
48
|
+
// Skip warnings that match ignore patterns
|
|
49
|
+
if (shouldIgnoreWarning(msg)) {
|
|
50
|
+
continue
|
|
51
|
+
}
|
|
52
|
+
|
|
28
53
|
seen[msg] = true
|
|
29
54
|
console.warn(msg) // eslint-disable-line no-console
|
|
30
55
|
}
|
|
@@ -34,11 +59,14 @@ function printWarnings() {
|
|
|
34
59
|
}
|
|
35
60
|
|
|
36
61
|
/** @internal */
|
|
37
|
-
export function defineHttpRequest(
|
|
62
|
+
export function defineHttpRequest(
|
|
63
|
+
envMiddleware: Middlewares,
|
|
64
|
+
config: {ignoreWarnings?: string | RegExp | Array<string | RegExp>} = {},
|
|
65
|
+
): Requester {
|
|
38
66
|
return getIt([
|
|
39
67
|
retry({shouldRetry}),
|
|
40
68
|
...envMiddleware,
|
|
41
|
-
printWarnings(),
|
|
69
|
+
printWarnings(config),
|
|
42
70
|
jsonRequest(),
|
|
43
71
|
jsonResponse(),
|
|
44
72
|
progress(),
|
package/src/types.ts
CHANGED
|
@@ -111,6 +111,30 @@ export interface ClientConfig {
|
|
|
111
111
|
headers?: Record<string, string>
|
|
112
112
|
|
|
113
113
|
ignoreBrowserTokenWarning?: boolean
|
|
114
|
+
/**
|
|
115
|
+
* Ignore specific warning messages from the client.
|
|
116
|
+
*
|
|
117
|
+
* @remarks
|
|
118
|
+
* - String values perform substring matching (not exact matching) against warning messages
|
|
119
|
+
* - RegExp values are tested against the full warning message
|
|
120
|
+
* - Array values allow multiple patterns to be specified
|
|
121
|
+
*
|
|
122
|
+
* @example
|
|
123
|
+
* ```typescript
|
|
124
|
+
* // Ignore warnings containing "experimental"
|
|
125
|
+
* ignoreWarnings: 'experimental'
|
|
126
|
+
*
|
|
127
|
+
* // Ignore multiple warning types
|
|
128
|
+
* ignoreWarnings: ['experimental', 'deprecated']
|
|
129
|
+
*
|
|
130
|
+
* // Use regex for exact matching
|
|
131
|
+
* ignoreWarnings: /^This is an experimental API version$/
|
|
132
|
+
*
|
|
133
|
+
* // Mix strings and regex patterns
|
|
134
|
+
* ignoreWarnings: ['rate limit', /^deprecated/i]
|
|
135
|
+
* ```
|
|
136
|
+
*/
|
|
137
|
+
ignoreWarnings?: string | RegExp | Array<string | RegExp>
|
|
114
138
|
withCredentials?: boolean
|
|
115
139
|
allowReconfigure?: boolean
|
|
116
140
|
timeout?: number
|
|
@@ -1660,3 +1684,23 @@ export type {
|
|
|
1660
1684
|
StudioBaseUrl,
|
|
1661
1685
|
StudioUrl,
|
|
1662
1686
|
} from './stega/types'
|
|
1687
|
+
|
|
1688
|
+
/**
|
|
1689
|
+
* A string constant containing the experimental API version warning message.
|
|
1690
|
+
* Use this with the `ignoreWarnings` option to suppress warnings when using experimental API versions.
|
|
1691
|
+
*
|
|
1692
|
+
* @example
|
|
1693
|
+
* ```typescript
|
|
1694
|
+
* import { createClient, EXPERIMENTAL_API_WARNING } from '@sanity/client'
|
|
1695
|
+
*
|
|
1696
|
+
* const client = createClient({
|
|
1697
|
+
* projectId: 'your-project-id',
|
|
1698
|
+
* dataset: 'production',
|
|
1699
|
+
* apiVersion: 'vX', // experimental version
|
|
1700
|
+
* ignoreWarnings: EXPERIMENTAL_API_WARNING
|
|
1701
|
+
* })
|
|
1702
|
+
* ```
|
|
1703
|
+
*
|
|
1704
|
+
* @public
|
|
1705
|
+
*/
|
|
1706
|
+
export const EXPERIMENTAL_API_WARNING = 'This is an experimental API version'
|
package/umd/sanityClient.js
CHANGED
|
@@ -2049,6 +2049,12 @@
|
|
|
2049
2049
|
};
|
|
2050
2050
|
}
|
|
2051
2051
|
const MAX_ITEMS_IN_ERROR_MESSAGE = 5;
|
|
2052
|
+
function isHttpError(error) {
|
|
2053
|
+
if (!isRecord(error))
|
|
2054
|
+
return false;
|
|
2055
|
+
const response = error.response;
|
|
2056
|
+
return !(typeof error.statusCode != "number" || typeof error.message != "string" || !isRecord(response) || typeof response.body > "u" || typeof response.url != "string" || typeof response.method != "string" || typeof response.headers != "object" || typeof response.statusCode != "number");
|
|
2057
|
+
}
|
|
2052
2058
|
class ClientError extends Error {
|
|
2053
2059
|
response;
|
|
2054
2060
|
statusCode = 400;
|
|
@@ -2149,22 +2155,22 @@ ${codeFrame(query, { start, end }, description)}${withTag}`;
|
|
|
2149
2155
|
return res;
|
|
2150
2156
|
}
|
|
2151
2157
|
};
|
|
2152
|
-
function printWarnings() {
|
|
2153
|
-
const seen = {};
|
|
2158
|
+
function printWarnings(config = {}) {
|
|
2159
|
+
const seen = {}, shouldIgnoreWarning = (message) => config.ignoreWarnings === void 0 ? false : (Array.isArray(config.ignoreWarnings) ? config.ignoreWarnings : [config.ignoreWarnings]).some((pattern) => typeof pattern == "string" ? message.includes(pattern) : pattern instanceof RegExp ? pattern.test(message) : false);
|
|
2154
2160
|
return {
|
|
2155
2161
|
onResponse: (res) => {
|
|
2156
2162
|
const warn = res.headers["x-sanity-warning"], warnings = Array.isArray(warn) ? warn : [warn];
|
|
2157
2163
|
for (const msg of warnings)
|
|
2158
|
-
!msg || seen[msg] || (seen[msg] = true, console.warn(msg));
|
|
2164
|
+
!msg || seen[msg] || shouldIgnoreWarning(msg) || (seen[msg] = true, console.warn(msg));
|
|
2159
2165
|
return res;
|
|
2160
2166
|
}
|
|
2161
2167
|
};
|
|
2162
2168
|
}
|
|
2163
|
-
function defineHttpRequest(envMiddleware2) {
|
|
2169
|
+
function defineHttpRequest(envMiddleware2, config = {}) {
|
|
2164
2170
|
return p$1([
|
|
2165
2171
|
P({ shouldRetry }),
|
|
2166
2172
|
...envMiddleware2,
|
|
2167
|
-
printWarnings(),
|
|
2173
|
+
printWarnings(config),
|
|
2168
2174
|
x(),
|
|
2169
2175
|
E$1(),
|
|
2170
2176
|
S$1(),
|
|
@@ -2254,7 +2260,7 @@ ${codeFrame(query, { start, end }, description)}${withTag}`;
|
|
|
2254
2260
|
}, resourceGuard = (service, config) => {
|
|
2255
2261
|
if (config["~experimental_resource"])
|
|
2256
2262
|
throw new Error(`\`${service}\` does not support resource-based operations`);
|
|
2257
|
-
};
|
|
2263
|
+
}, EXPERIMENTAL_API_WARNING = "This is an experimental API version";
|
|
2258
2264
|
function once(fn) {
|
|
2259
2265
|
let didCall = false, returnValue;
|
|
2260
2266
|
return (...args) => (didCall || (returnValue = fn(...args), didCall = true), returnValue);
|
|
@@ -4635,7 +4641,9 @@ ${selectionOpts}`);
|
|
|
4635
4641
|
}
|
|
4636
4642
|
function defineCreateClientExports(envMiddleware2, ClassConstructor) {
|
|
4637
4643
|
return { requester: defineHttpRequest(envMiddleware2), createClient: (config) => {
|
|
4638
|
-
const clientRequester = defineHttpRequest(envMiddleware2
|
|
4644
|
+
const clientRequester = defineHttpRequest(envMiddleware2, {
|
|
4645
|
+
ignoreWarnings: config.ignoreWarnings
|
|
4646
|
+
});
|
|
4639
4647
|
return new ClassConstructor(
|
|
4640
4648
|
(options, requester2) => (requester2 || clientRequester)({
|
|
4641
4649
|
maxRedirects: 0,
|
|
@@ -6095,6 +6103,7 @@ ${selectionOpts}`);
|
|
|
6095
6103
|
exports.ConnectionFailedError = ConnectionFailedError;
|
|
6096
6104
|
exports.CorsOriginError = CorsOriginError;
|
|
6097
6105
|
exports.DisconnectError = DisconnectError;
|
|
6106
|
+
exports.EXPERIMENTAL_API_WARNING = EXPERIMENTAL_API_WARNING;
|
|
6098
6107
|
exports.MessageError = MessageError;
|
|
6099
6108
|
exports.MessageParseError = MessageParseError;
|
|
6100
6109
|
exports.ObservablePatch = ObservablePatch;
|
|
@@ -6108,6 +6117,7 @@ ${selectionOpts}`);
|
|
|
6108
6117
|
exports.createClient = createClient;
|
|
6109
6118
|
exports.default = deprecatedCreateClient;
|
|
6110
6119
|
exports.formatQueryParseError = formatQueryParseError;
|
|
6120
|
+
exports.isHttpError = isHttpError;
|
|
6111
6121
|
exports.isQueryParseError = isQueryParseError;
|
|
6112
6122
|
exports.requester = requester;
|
|
6113
6123
|
exports.unstable__adapter = c$2;
|