@sanity/client 5.4.2-dev.0 → 5.4.3-dev.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 +41 -20
- package/dist/index.browser.cjs +47 -15
- package/dist/index.browser.cjs.map +1 -1
- package/dist/index.browser.js +48 -16
- package/dist/index.browser.js.map +1 -1
- package/dist/index.cjs +48 -18
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.ts +20 -4
- package/dist/index.js +48 -18
- package/dist/index.js.map +1 -1
- package/package.json +5 -5
- package/src/SanityClient.ts +7 -4
- package/src/config.ts +2 -1
- package/src/data/dataMethods.ts +9 -8
- package/src/http/nodeMiddleware.ts +1 -2
- package/src/http/request.ts +42 -2
- package/src/index.browser.ts +9 -2
- package/src/index.ts +9 -2
- package/src/types.ts +15 -0
- package/src/warnings.ts +4 -5
- package/umd/sanityClient.js +93 -32
- package/umd/sanityClient.min.js +3 -3
package/dist/index.d.ts
CHANGED
|
@@ -107,6 +107,7 @@ export declare type BaseMutationOptions = RequestOptions & {
|
|
|
107
107
|
dryRun?: boolean
|
|
108
108
|
autoGenerateArrayKeys?: boolean
|
|
109
109
|
skipCrossDatasetReferenceValidation?: boolean
|
|
110
|
+
transactionId?: string
|
|
110
111
|
}
|
|
111
112
|
|
|
112
113
|
/** @internal */
|
|
@@ -279,6 +280,7 @@ export declare type ChannelErrorEvent = {
|
|
|
279
280
|
export declare interface ClientConfig {
|
|
280
281
|
projectId?: string
|
|
281
282
|
dataset?: string
|
|
283
|
+
/** @defaultValue true */
|
|
282
284
|
useCdn?: boolean
|
|
283
285
|
token?: string
|
|
284
286
|
apiHost?: string
|
|
@@ -289,6 +291,17 @@ export declare interface ClientConfig {
|
|
|
289
291
|
withCredentials?: boolean
|
|
290
292
|
allowReconfigure?: boolean
|
|
291
293
|
timeout?: number
|
|
294
|
+
/** Number of retries for requests. Defaults to 5. */
|
|
295
|
+
maxRetries?: number
|
|
296
|
+
/**
|
|
297
|
+
* The amount of time, in milliseconds, to wait before retrying, given an attemptNumber (starting at 0).
|
|
298
|
+
*
|
|
299
|
+
* Defaults to exponential back-off, starting at 100ms, doubling for each attempt, together with random
|
|
300
|
+
* jitter between 0 and 100 milliseconds. More specifically the following algorithm is used:
|
|
301
|
+
*
|
|
302
|
+
* Delay = 100 * 2^attemptNumber + randomNumberBetween0and100
|
|
303
|
+
*/
|
|
304
|
+
retryDelay?: (attemptNumber: number) => number
|
|
292
305
|
/**
|
|
293
306
|
* @deprecated Don't use
|
|
294
307
|
*/
|
|
@@ -1952,19 +1965,22 @@ export declare class SanityClient {
|
|
|
1952
1965
|
operations?: Mutation<R>[]
|
|
1953
1966
|
): Transaction
|
|
1954
1967
|
/**
|
|
1955
|
-
*
|
|
1968
|
+
* Perform a request against the Sanity API
|
|
1969
|
+
* NOTE: Only use this for Sanity API endpoints, not for your own APIs!
|
|
1956
1970
|
*
|
|
1957
|
-
* @deprecated Use your own request library!
|
|
1958
1971
|
* @param options - Request options
|
|
1972
|
+
* @returns Promise resolving to the response body
|
|
1959
1973
|
*/
|
|
1960
1974
|
request<R = Any>(options: RawRequestOptions): Promise<R>
|
|
1961
1975
|
/**
|
|
1962
|
-
*
|
|
1976
|
+
* Perform an HTTP request a `/data` sub-endpoint
|
|
1977
|
+
* NOTE: Considered internal, thus marked as deprecated. Use `request` instead.
|
|
1963
1978
|
*
|
|
1964
|
-
* @deprecated Use your own
|
|
1979
|
+
* @deprecated - Use `request()` or your own HTTP library instead
|
|
1965
1980
|
* @param endpoint - Endpoint to hit (mutate, query etc)
|
|
1966
1981
|
* @param body - Request body
|
|
1967
1982
|
* @param options - Request options
|
|
1983
|
+
* @internal
|
|
1968
1984
|
*/
|
|
1969
1985
|
dataRequest(endpoint: string, body: unknown, options?: BaseMutationOptions): Promise<Any>
|
|
1970
1986
|
/**
|
package/dist/index.js
CHANGED
|
@@ -4,14 +4,12 @@ export { adapter as unstable__adapter, environment as unstable__environment } fr
|
|
|
4
4
|
import { Observable, lastValueFrom } from 'rxjs';
|
|
5
5
|
import { map, filter } from 'rxjs/operators';
|
|
6
6
|
var name = "@sanity/client";
|
|
7
|
-
var version = "5.4.
|
|
7
|
+
var version = "5.4.3-dev.0";
|
|
8
8
|
const middleware = [debug({
|
|
9
9
|
verbose: true,
|
|
10
10
|
namespace: "sanity:client"
|
|
11
11
|
}), headers({
|
|
12
12
|
"User-Agent": "".concat(name, " ").concat(version)
|
|
13
|
-
}), retry({
|
|
14
|
-
maxRetries: 3
|
|
15
13
|
})];
|
|
16
14
|
const MAX_ITEMS_IN_ERROR_MESSAGE = 5;
|
|
17
15
|
class ClientError extends Error {
|
|
@@ -98,8 +96,18 @@ const printWarnings = {
|
|
|
98
96
|
return res;
|
|
99
97
|
}
|
|
100
98
|
};
|
|
101
|
-
function defineHttpRequest(envMiddleware) {
|
|
102
|
-
|
|
99
|
+
function defineHttpRequest(envMiddleware, _ref) {
|
|
100
|
+
let {
|
|
101
|
+
maxRetries = 5,
|
|
102
|
+
retryDelay
|
|
103
|
+
} = _ref;
|
|
104
|
+
const request = getIt([maxRetries > 0 ? retry({
|
|
105
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
106
|
+
retryDelay,
|
|
107
|
+
// This option is typed incorrectly in get-it.
|
|
108
|
+
maxRetries,
|
|
109
|
+
shouldRetry
|
|
110
|
+
}) : {}, ...envMiddleware, printWarnings, jsonRequest(), jsonResponse(), progress(), httpError, observable({
|
|
103
111
|
implementation: Observable
|
|
104
112
|
})]);
|
|
105
113
|
function httpRequest(options) {
|
|
@@ -112,6 +120,13 @@ function defineHttpRequest(envMiddleware) {
|
|
|
112
120
|
httpRequest.defaultRequester = request;
|
|
113
121
|
return httpRequest;
|
|
114
122
|
}
|
|
123
|
+
function shouldRetry(err, attempt, options) {
|
|
124
|
+
const isSafe = options.method === "GET" || options.method === "HEAD";
|
|
125
|
+
const isQuery = options.uri.startsWith("/data/query");
|
|
126
|
+
const isRetriableResponse = err.response && (err.response.statusCode === 429 || err.response.statusCode === 502 || err.response.statusCode === 503);
|
|
127
|
+
if ((isSafe || isQuery) && isRetriableResponse) return true;
|
|
128
|
+
return retry.shouldRetry(err, attempt, options);
|
|
129
|
+
}
|
|
115
130
|
const projectHeader = "X-Sanity-Project-ID";
|
|
116
131
|
function requestOptions(config) {
|
|
117
132
|
let overrides = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
|
|
@@ -208,12 +223,12 @@ const requestTag = tag => {
|
|
|
208
223
|
}
|
|
209
224
|
return tag;
|
|
210
225
|
};
|
|
211
|
-
const encodeQueryString =
|
|
226
|
+
const encodeQueryString = _ref2 => {
|
|
212
227
|
let {
|
|
213
228
|
query,
|
|
214
229
|
params = {},
|
|
215
230
|
options = {}
|
|
216
|
-
} =
|
|
231
|
+
} = _ref2;
|
|
217
232
|
const searchParams = new URLSearchParams();
|
|
218
233
|
const {
|
|
219
234
|
tag,
|
|
@@ -735,9 +750,18 @@ function _delete(client, httpRequest, selection, options) {
|
|
|
735
750
|
}, options);
|
|
736
751
|
}
|
|
737
752
|
function _mutate(client, httpRequest, mutations, options) {
|
|
738
|
-
|
|
753
|
+
let mut;
|
|
754
|
+
if (mutations instanceof Patch || mutations instanceof ObservablePatch) {
|
|
755
|
+
mut = {
|
|
756
|
+
patch: mutations.serialize()
|
|
757
|
+
};
|
|
758
|
+
} else if (mutations instanceof Transaction || mutations instanceof ObservableTransaction) {
|
|
759
|
+
mut = mutations.serialize();
|
|
760
|
+
} else {
|
|
761
|
+
mut = mutations;
|
|
762
|
+
}
|
|
739
763
|
const muts = Array.isArray(mut) ? mut : [mut];
|
|
740
|
-
const transactionId = options && options.transactionId;
|
|
764
|
+
const transactionId = options && options.transactionId || void 0;
|
|
741
765
|
return _dataRequest(client, httpRequest, "mutate", {
|
|
742
766
|
mutations: muts,
|
|
743
767
|
transactionId
|
|
@@ -995,10 +1019,10 @@ once(function () {
|
|
|
995
1019
|
}
|
|
996
1020
|
return console.warn(message.join(" "), ...args);
|
|
997
1021
|
});
|
|
998
|
-
const printCdnWarning = createWarningPrinter(["
|
|
1022
|
+
const printCdnWarning = createWarningPrinter(["Since you haven't set a value for `useCdn`, we will deliver content using our", "global, edge-cached API-CDN. If you wish to have content delivered faster, set", "`useCdn: false` to use the Live API. Note: You may incur higher costs using the live API."]);
|
|
999
1023
|
const printBrowserTokenWarning = createWarningPrinter(["You have configured Sanity client to use a token in the browser. This may cause unintentional security issues.", "See ".concat(generateHelpUrl("js-client-browser-token"), " for more information and how to hide this warning.")]);
|
|
1000
1024
|
const printNoApiVersionSpecifiedWarning = createWarningPrinter(["Using the Sanity client without specifying an API version is deprecated.", "See ".concat(generateHelpUrl("js-client-api-version"))]);
|
|
1001
|
-
const printNoDefaultExport = createWarningPrinter(["The default export of @sanity/client has been deprecated. Use the named export `createClient` instead"]);
|
|
1025
|
+
const printNoDefaultExport = createWarningPrinter(["The default export of @sanity/client has been deprecated. Use the named export `createClient` instead."]);
|
|
1002
1026
|
const defaultCdnHost = "apicdn.sanity.io";
|
|
1003
1027
|
const defaultConfig = {
|
|
1004
1028
|
apiHost: "https://api.sanity.io",
|
|
@@ -1049,7 +1073,7 @@ const initConfig = (config, prevConfig) => {
|
|
|
1049
1073
|
}
|
|
1050
1074
|
newConfig.apiVersion = "".concat(newConfig.apiVersion).replace(/^v/, "");
|
|
1051
1075
|
newConfig.isDefaultApi = newConfig.apiHost === defaultConfig.apiHost;
|
|
1052
|
-
newConfig.useCdn =
|
|
1076
|
+
newConfig.useCdn = newConfig.useCdn !== false && !newConfig.withCredentials;
|
|
1053
1077
|
validateApiVersion(newConfig.apiVersion);
|
|
1054
1078
|
const hostParts = newConfig.apiHost.split("://", 2);
|
|
1055
1079
|
const protocol = hostParts[0];
|
|
@@ -1726,21 +1750,24 @@ const _SanityClient = class {
|
|
|
1726
1750
|
return new Transaction(operations, this);
|
|
1727
1751
|
}
|
|
1728
1752
|
/**
|
|
1729
|
-
*
|
|
1753
|
+
* Perform a request against the Sanity API
|
|
1754
|
+
* NOTE: Only use this for Sanity API endpoints, not for your own APIs!
|
|
1730
1755
|
*
|
|
1731
|
-
* @deprecated Use your own request library!
|
|
1732
1756
|
* @param options - Request options
|
|
1757
|
+
* @returns Promise resolving to the response body
|
|
1733
1758
|
*/
|
|
1734
1759
|
request(options) {
|
|
1735
1760
|
return lastValueFrom(_request(this, __privateGet(this, _httpRequest2), options));
|
|
1736
1761
|
}
|
|
1737
1762
|
/**
|
|
1738
|
-
*
|
|
1763
|
+
* Perform an HTTP request a `/data` sub-endpoint
|
|
1764
|
+
* NOTE: Considered internal, thus marked as deprecated. Use `request` instead.
|
|
1739
1765
|
*
|
|
1740
|
-
* @deprecated Use your own
|
|
1766
|
+
* @deprecated - Use `request()` or your own HTTP library instead
|
|
1741
1767
|
* @param endpoint - Endpoint to hit (mutate, query etc)
|
|
1742
1768
|
* @param body - Request body
|
|
1743
1769
|
* @param options - Request options
|
|
1770
|
+
* @internal
|
|
1744
1771
|
*/
|
|
1745
1772
|
dataRequest(endpoint, body, options) {
|
|
1746
1773
|
return lastValueFrom(_dataRequest(this, __privateGet(this, _httpRequest2), endpoint, body, options));
|
|
@@ -1767,9 +1794,12 @@ const _SanityClient = class {
|
|
|
1767
1794
|
let SanityClient = _SanityClient;
|
|
1768
1795
|
_clientConfig2 = new WeakMap();
|
|
1769
1796
|
_httpRequest2 = new WeakMap();
|
|
1770
|
-
const httpRequest = defineHttpRequest(middleware);
|
|
1797
|
+
const httpRequest = defineHttpRequest(middleware, {});
|
|
1771
1798
|
const requester = httpRequest.defaultRequester;
|
|
1772
|
-
const createClient = config => new SanityClient(
|
|
1799
|
+
const createClient = config => new SanityClient(defineHttpRequest(middleware, {
|
|
1800
|
+
maxRetries: config.maxRetries,
|
|
1801
|
+
retryDelay: config.retryDelay
|
|
1802
|
+
}), config);
|
|
1773
1803
|
function deprecatedCreateClient(config) {
|
|
1774
1804
|
printNoDefaultExport();
|
|
1775
1805
|
return new SanityClient(httpRequest, config);
|