@sanity/client 5.4.2 → 5.4.3-dev.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/README.md +104 -26
- package/dist/index.browser.cjs +42 -13
- package/dist/index.browser.cjs.map +1 -1
- package/dist/index.browser.js +43 -14
- package/dist/index.browser.js.map +1 -1
- package/dist/index.cjs +43 -16
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.ts +85 -7
- package/dist/index.js +43 -16
- package/dist/index.js.map +1 -1
- package/package.json +6 -5
- package/src/SanityClient.ts +7 -4
- package/src/config.ts +2 -1
- package/src/data/dataMethods.ts +4 -0
- 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 → types/_legacy.ts} +23 -3
- package/src/types/index.ts +2 -0
- package/src/types/resultSourceMap.ts +60 -0
- package/src/warnings.ts +4 -5
- package/umd/sanityClient.js +88 -30
- package/umd/sanityClient.min.js +3 -3
package/dist/index.browser.js
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { getIt } from 'get-it';
|
|
2
2
|
export { adapter as unstable__adapter, environment as unstable__environment } from 'get-it';
|
|
3
|
-
import { jsonRequest, jsonResponse, progress, observable } from 'get-it/middleware';
|
|
3
|
+
import { retry, jsonRequest, jsonResponse, progress, observable } from 'get-it/middleware';
|
|
4
4
|
import { Observable, lastValueFrom } from 'rxjs';
|
|
5
5
|
import { map, filter } from 'rxjs/operators';
|
|
6
6
|
var envMiddleware = [];
|
|
@@ -89,8 +89,18 @@ const printWarnings = {
|
|
|
89
89
|
return res;
|
|
90
90
|
}
|
|
91
91
|
};
|
|
92
|
-
function defineHttpRequest(envMiddleware) {
|
|
93
|
-
|
|
92
|
+
function defineHttpRequest(envMiddleware, _ref) {
|
|
93
|
+
let {
|
|
94
|
+
maxRetries = 5,
|
|
95
|
+
retryDelay
|
|
96
|
+
} = _ref;
|
|
97
|
+
const request = getIt([maxRetries > 0 ? retry({
|
|
98
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
99
|
+
retryDelay,
|
|
100
|
+
// This option is typed incorrectly in get-it.
|
|
101
|
+
maxRetries,
|
|
102
|
+
shouldRetry
|
|
103
|
+
}) : {}, ...envMiddleware, printWarnings, jsonRequest(), jsonResponse(), progress(), httpError, observable({
|
|
94
104
|
implementation: Observable
|
|
95
105
|
})]);
|
|
96
106
|
function httpRequest(options) {
|
|
@@ -103,6 +113,13 @@ function defineHttpRequest(envMiddleware) {
|
|
|
103
113
|
httpRequest.defaultRequester = request;
|
|
104
114
|
return httpRequest;
|
|
105
115
|
}
|
|
116
|
+
function shouldRetry(err, attempt, options) {
|
|
117
|
+
const isSafe = options.method === "GET" || options.method === "HEAD";
|
|
118
|
+
const isQuery = options.uri.startsWith("/data/query");
|
|
119
|
+
const isRetriableResponse = err.response && (err.response.statusCode === 429 || err.response.statusCode === 502 || err.response.statusCode === 503);
|
|
120
|
+
if ((isSafe || isQuery) && isRetriableResponse) return true;
|
|
121
|
+
return retry.shouldRetry(err, attempt, options);
|
|
122
|
+
}
|
|
106
123
|
const projectHeader = "X-Sanity-Project-ID";
|
|
107
124
|
function requestOptions(config) {
|
|
108
125
|
let overrides = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
|
|
@@ -199,12 +216,12 @@ const requestTag = tag => {
|
|
|
199
216
|
}
|
|
200
217
|
return tag;
|
|
201
218
|
};
|
|
202
|
-
const encodeQueryString =
|
|
219
|
+
const encodeQueryString = _ref2 => {
|
|
203
220
|
let {
|
|
204
221
|
query,
|
|
205
222
|
params = {},
|
|
206
223
|
options = {}
|
|
207
|
-
} =
|
|
224
|
+
} = _ref2;
|
|
208
225
|
const searchParams = new URLSearchParams();
|
|
209
226
|
const {
|
|
210
227
|
tag,
|
|
@@ -813,6 +830,12 @@ function _requestObservable(client, httpRequest, options) {
|
|
|
813
830
|
...options.query
|
|
814
831
|
};
|
|
815
832
|
}
|
|
833
|
+
if (config.resultSourceMap) {
|
|
834
|
+
options.query = {
|
|
835
|
+
resultSourceMap: true,
|
|
836
|
+
...options.query
|
|
837
|
+
};
|
|
838
|
+
}
|
|
816
839
|
const reqOptions = requestOptions(config, Object.assign({}, options, {
|
|
817
840
|
url: _getUrl(client, uri, useCdn)
|
|
818
841
|
}));
|
|
@@ -989,10 +1012,10 @@ once(function () {
|
|
|
989
1012
|
}
|
|
990
1013
|
return console.warn(message.join(" "), ...args);
|
|
991
1014
|
});
|
|
992
|
-
const printCdnWarning = createWarningPrinter(["
|
|
1015
|
+
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."]);
|
|
993
1016
|
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.")]);
|
|
994
1017
|
const printNoApiVersionSpecifiedWarning = createWarningPrinter(["Using the Sanity client without specifying an API version is deprecated.", "See ".concat(generateHelpUrl("js-client-api-version"))]);
|
|
995
|
-
const printNoDefaultExport = createWarningPrinter(["The default export of @sanity/client has been deprecated. Use the named export `createClient` instead"]);
|
|
1018
|
+
const printNoDefaultExport = createWarningPrinter(["The default export of @sanity/client has been deprecated. Use the named export `createClient` instead."]);
|
|
996
1019
|
const defaultCdnHost = "apicdn.sanity.io";
|
|
997
1020
|
const defaultConfig = {
|
|
998
1021
|
apiHost: "https://api.sanity.io",
|
|
@@ -1043,7 +1066,7 @@ const initConfig = (config, prevConfig) => {
|
|
|
1043
1066
|
}
|
|
1044
1067
|
newConfig.apiVersion = "".concat(newConfig.apiVersion).replace(/^v/, "");
|
|
1045
1068
|
newConfig.isDefaultApi = newConfig.apiHost === defaultConfig.apiHost;
|
|
1046
|
-
newConfig.useCdn =
|
|
1069
|
+
newConfig.useCdn = newConfig.useCdn !== false && !newConfig.withCredentials;
|
|
1047
1070
|
validateApiVersion(newConfig.apiVersion);
|
|
1048
1071
|
const hostParts = newConfig.apiHost.split("://", 2);
|
|
1049
1072
|
const protocol = hostParts[0];
|
|
@@ -1720,21 +1743,24 @@ const _SanityClient = class {
|
|
|
1720
1743
|
return new Transaction(operations, this);
|
|
1721
1744
|
}
|
|
1722
1745
|
/**
|
|
1723
|
-
*
|
|
1746
|
+
* Perform a request against the Sanity API
|
|
1747
|
+
* NOTE: Only use this for Sanity API endpoints, not for your own APIs!
|
|
1724
1748
|
*
|
|
1725
|
-
* @deprecated Use your own request library!
|
|
1726
1749
|
* @param options - Request options
|
|
1750
|
+
* @returns Promise resolving to the response body
|
|
1727
1751
|
*/
|
|
1728
1752
|
request(options) {
|
|
1729
1753
|
return lastValueFrom(_request(this, __privateGet(this, _httpRequest2), options));
|
|
1730
1754
|
}
|
|
1731
1755
|
/**
|
|
1732
|
-
*
|
|
1756
|
+
* Perform an HTTP request a `/data` sub-endpoint
|
|
1757
|
+
* NOTE: Considered internal, thus marked as deprecated. Use `request` instead.
|
|
1733
1758
|
*
|
|
1734
|
-
* @deprecated Use your own
|
|
1759
|
+
* @deprecated - Use `request()` or your own HTTP library instead
|
|
1735
1760
|
* @param endpoint - Endpoint to hit (mutate, query etc)
|
|
1736
1761
|
* @param body - Request body
|
|
1737
1762
|
* @param options - Request options
|
|
1763
|
+
* @internal
|
|
1738
1764
|
*/
|
|
1739
1765
|
dataRequest(endpoint, body, options) {
|
|
1740
1766
|
return lastValueFrom(_dataRequest(this, __privateGet(this, _httpRequest2), endpoint, body, options));
|
|
@@ -1761,9 +1787,12 @@ const _SanityClient = class {
|
|
|
1761
1787
|
let SanityClient = _SanityClient;
|
|
1762
1788
|
_clientConfig2 = new WeakMap();
|
|
1763
1789
|
_httpRequest2 = new WeakMap();
|
|
1764
|
-
const httpRequest = defineHttpRequest(envMiddleware);
|
|
1790
|
+
const httpRequest = defineHttpRequest(envMiddleware, {});
|
|
1765
1791
|
const requester = httpRequest.defaultRequester;
|
|
1766
|
-
const createClient = config => new SanityClient(
|
|
1792
|
+
const createClient = config => new SanityClient(defineHttpRequest(envMiddleware, {
|
|
1793
|
+
maxRetries: config.maxRetries,
|
|
1794
|
+
retryDelay: config.retryDelay
|
|
1795
|
+
}), config);
|
|
1767
1796
|
function deprecatedCreateClient(config) {
|
|
1768
1797
|
printNoDefaultExport();
|
|
1769
1798
|
return new SanityClient(httpRequest, config);
|