@sanity/client 5.0.0 → 5.1.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 +45 -0
- package/dist/index.browser.cjs +35 -2
- package/dist/index.browser.cjs.map +1 -1
- package/dist/index.browser.js +35 -3
- package/dist/index.browser.js.map +1 -1
- package/dist/index.cjs +36 -3
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.ts +8 -0
- package/dist/index.js +36 -4
- package/dist/index.js.map +1 -1
- package/package.json +2 -2
- package/src/data/dataMethods.ts +52 -2
- package/src/index.browser.ts +2 -0
- package/src/index.ts +2 -0
- package/src/migrationNotice.ts +9 -0
- package/src/types.ts +1 -0
- package/umd/sanityClient.js +37 -2
- package/umd/sanityClient.min.js +3 -3
package/README.md
CHANGED
|
@@ -846,6 +846,49 @@ The following options are available for mutations, and can be applied either as
|
|
|
846
846
|
- `dryRun` (`true|false`) - default `false`. If true, the mutation will be a dry run - the response will be identical to the one returned had this property been omitted or false (including error responses) but no documents will be affected.
|
|
847
847
|
- `autoGenerateArrayKeys` (`true|false`) - default `false`. If true, the mutation API will automatically add `_key` attributes to objects in arrays that is missing them. This makes array operations more robust by having a unique key within the array available for selections, which helps prevent race conditions in real-time, collaborative editing.
|
|
848
848
|
|
|
849
|
+
### Aborting a request
|
|
850
|
+
|
|
851
|
+
Requests can be aborted (or cancelled) in two ways:
|
|
852
|
+
|
|
853
|
+
#### 1. Abort a request by passing an [AbortSignal] with the request options
|
|
854
|
+
|
|
855
|
+
Sanity Client supports the [AbortController] API and supports receiving an abort signal that can be used to cancel the request. Here's an example that will abort the request if it takes more than 200ms to complete:
|
|
856
|
+
|
|
857
|
+
```js
|
|
858
|
+
const abortController = new AbortController()
|
|
859
|
+
|
|
860
|
+
// note the lack of await here
|
|
861
|
+
const request = getClient().fetch('*[_type == "movie"]', {}, {signal: abortController.signal})
|
|
862
|
+
|
|
863
|
+
// this will abort the request after 200ms
|
|
864
|
+
setTimeout(() => abortController.abort(), 200)
|
|
865
|
+
|
|
866
|
+
try {
|
|
867
|
+
const response = await request
|
|
868
|
+
//…
|
|
869
|
+
} catch (error) {
|
|
870
|
+
if (error.name === 'AbortError') {
|
|
871
|
+
console.log('Request was aborted')
|
|
872
|
+
} else {
|
|
873
|
+
// rethrow in case of other errors
|
|
874
|
+
throw error
|
|
875
|
+
}
|
|
876
|
+
}
|
|
877
|
+
```
|
|
878
|
+
|
|
879
|
+
#### 2. Cancel a request by unsubscribing from the Observable
|
|
880
|
+
|
|
881
|
+
When using the Observable API (e.g. `client.observable.fetch()`), you can cancel the request by simply `unsubscribe` from the returned observable:
|
|
882
|
+
|
|
883
|
+
```js
|
|
884
|
+
const subscription = client.observable.fetch('*[_type == "movie"]').subscribe((result) => {
|
|
885
|
+
/* do something with the result */
|
|
886
|
+
})
|
|
887
|
+
|
|
888
|
+
// this will cancel the request
|
|
889
|
+
subscription.unsubscribe()
|
|
890
|
+
```
|
|
891
|
+
|
|
849
892
|
### Get client configuration
|
|
850
893
|
|
|
851
894
|
```js
|
|
@@ -1234,3 +1277,5 @@ await client.request<void>({uri: '/auth/logout', method: 'POST'})
|
|
|
1234
1277
|
[api-versioning]: http://sanity.io/docs/api-versioning
|
|
1235
1278
|
[zod]: https://zod.dev/
|
|
1236
1279
|
[groqd]: https://github.com/FormidableLabs/groqd#readme
|
|
1280
|
+
[AbortSignal]: https://developer.mozilla.org/en-US/docs/Web/API/AbortSignal
|
|
1281
|
+
[AbortController]: https://developer.mozilla.org/en-US/docs/Web/API/AbortController
|
package/dist/index.browser.cjs
CHANGED
|
@@ -749,7 +749,8 @@ function _dataRequest(client, httpRequest, endpoint, body) {
|
|
|
749
749
|
headers,
|
|
750
750
|
token,
|
|
751
751
|
tag,
|
|
752
|
-
canUseCdn: isQuery
|
|
752
|
+
canUseCdn: isQuery,
|
|
753
|
+
signal: options.signal
|
|
753
754
|
};
|
|
754
755
|
return _requestObservable(client, httpRequest, reqOptions).pipe(operators.filter(isResponse), operators.map(getBody), operators.map(res => {
|
|
755
756
|
if (!isMutation) {
|
|
@@ -796,9 +797,10 @@ function _requestObservable(client, httpRequest, options) {
|
|
|
796
797
|
const reqOptions = getRequestOptions(config, Object.assign({}, options, {
|
|
797
798
|
url: _getUrl(client, uri, useCdn)
|
|
798
799
|
}));
|
|
799
|
-
|
|
800
|
+
const request = new rxjs.Observable(subscriber =>
|
|
800
801
|
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion -- the typings thinks it's optional because it's not required to specify it when calling createClient, but it's always defined in practice since SanityClient provides a default
|
|
801
802
|
httpRequest(reqOptions, config.requester).subscribe(subscriber));
|
|
803
|
+
return options.signal ? request.pipe(_withAbortSignal(options.signal)) : request;
|
|
802
804
|
}
|
|
803
805
|
function _request(client, httpRequest, options) {
|
|
804
806
|
const observable = _requestObservable(client, httpRequest, options).pipe(operators.filter(event => event.type === "response"), operators.map(event => event.body));
|
|
@@ -820,6 +822,33 @@ function _getUrl(client, uri) {
|
|
|
820
822
|
const base = canUseCdn ? cdnUrl : url;
|
|
821
823
|
return "".concat(base, "/").concat(uri.replace(/^\//, ""));
|
|
822
824
|
}
|
|
825
|
+
function _withAbortSignal(signal) {
|
|
826
|
+
return input => {
|
|
827
|
+
return new rxjs.Observable(observer => {
|
|
828
|
+
const abort = () => observer.error(_createAbortError(signal));
|
|
829
|
+
if (signal && signal.aborted) {
|
|
830
|
+
abort();
|
|
831
|
+
return;
|
|
832
|
+
}
|
|
833
|
+
const subscription = input.subscribe(observer);
|
|
834
|
+
signal.addEventListener("abort", abort);
|
|
835
|
+
return () => {
|
|
836
|
+
signal.removeEventListener("abort", abort);
|
|
837
|
+
subscription.unsubscribe();
|
|
838
|
+
};
|
|
839
|
+
});
|
|
840
|
+
};
|
|
841
|
+
}
|
|
842
|
+
const isDomExceptionSupported = Boolean(globalThis.DOMException);
|
|
843
|
+
function _createAbortError(signal) {
|
|
844
|
+
var _a, _b;
|
|
845
|
+
if (isDomExceptionSupported) {
|
|
846
|
+
return new DOMException((_a = signal == null ? void 0 : signal.reason) != null ? _a : "The operation was aborted.", "AbortError");
|
|
847
|
+
}
|
|
848
|
+
const error = new Error((_b = signal == null ? void 0 : signal.reason) != null ? _b : "The operation was aborted.");
|
|
849
|
+
error.name = "AbortError";
|
|
850
|
+
return error;
|
|
851
|
+
}
|
|
823
852
|
var __accessCheck$4 = (obj, member, msg) => {
|
|
824
853
|
if (!member.has(obj)) throw TypeError("Cannot " + msg);
|
|
825
854
|
};
|
|
@@ -1662,6 +1691,9 @@ const _SanityClient = class {
|
|
|
1662
1691
|
let SanityClient = _SanityClient;
|
|
1663
1692
|
_clientConfig2 = new WeakMap();
|
|
1664
1693
|
_httpRequest2 = new WeakMap();
|
|
1694
|
+
function migrationNotice() {
|
|
1695
|
+
throw new TypeError("The default export of @sanity/client has been deprecated. Use the named export `createClient` instead");
|
|
1696
|
+
}
|
|
1665
1697
|
const httpRequest = defineHttpRequest(envMiddleware);
|
|
1666
1698
|
const requester = httpRequest.defaultRequester;
|
|
1667
1699
|
const createClient = config => new SanityClient(httpRequest, config);
|
|
@@ -1676,5 +1708,6 @@ exports.SanityClient = SanityClient;
|
|
|
1676
1708
|
exports.ServerError = ServerError;
|
|
1677
1709
|
exports.Transaction = Transaction;
|
|
1678
1710
|
exports.createClient = createClient;
|
|
1711
|
+
exports.default = migrationNotice;
|
|
1679
1712
|
exports.requester = requester;
|
|
1680
1713
|
//# sourceMappingURL=index.browser.cjs.map
|