@sanity/client 6.22.3-canary.0 → 6.22.3
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 +43 -1
- package/dist/index.browser.cjs +26 -0
- package/dist/index.browser.cjs.map +1 -1
- package/dist/index.browser.d.cts +7 -0
- package/dist/index.browser.d.ts +7 -0
- package/dist/index.browser.js +26 -0
- package/dist/index.browser.js.map +1 -1
- package/dist/index.cjs +27 -1
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +7 -0
- package/dist/index.d.ts +7 -0
- package/dist/index.js +27 -1
- package/dist/index.js.map +1 -1
- package/package.json +8 -6
- package/src/data/live.ts +18 -0
- package/src/defineCreateClient.ts +1 -1
- package/src/http/errors.ts +23 -0
- package/src/stega/types.ts +1 -0
- package/src/types.ts +1 -0
- package/umd/sanityClient.js +1091 -1052
- package/umd/sanityClient.min.js +2 -2
package/README.md
CHANGED
|
@@ -28,7 +28,7 @@ export const client = createClient({
|
|
|
28
28
|
dataset: 'your-dataset-name',
|
|
29
29
|
useCdn: true, // set to `false` to bypass the edge cache
|
|
30
30
|
apiVersion: '2023-05-03', // use current date (YYYY-MM-DD) to target the latest API version
|
|
31
|
-
// token: process.env.SANITY_SECRET_TOKEN //
|
|
31
|
+
// token: process.env.SANITY_SECRET_TOKEN // Needed for certain operations like updating content or accessing previewDrafts perspective
|
|
32
32
|
})
|
|
33
33
|
|
|
34
34
|
// uses GROQ to query content: https://www.sanity.io/docs/groq
|
|
@@ -884,6 +884,48 @@ client.getDocuments(ids).then((docs) => {
|
|
|
884
884
|
})
|
|
885
885
|
```
|
|
886
886
|
|
|
887
|
+
### Listening to live content updates
|
|
888
|
+
|
|
889
|
+
> [!NOTE]
|
|
890
|
+
>
|
|
891
|
+
> Live Content is experimental and requires your client config to be set up with `apiVersion: 'vX'`.
|
|
892
|
+
|
|
893
|
+
```ts
|
|
894
|
+
// Subscribe to live updates
|
|
895
|
+
const subscription = client.live.events().subscribe((event) => {
|
|
896
|
+
// Check if incoming tags match saved sync tags
|
|
897
|
+
if (event.type === 'message' && event.tags.some((tag) => syncTags.includes(tag))) {
|
|
898
|
+
// Refetch with ID to get latest data
|
|
899
|
+
render(event.id)
|
|
900
|
+
}
|
|
901
|
+
if (event.type === 'restart') {
|
|
902
|
+
// A restart event is sent when the `lastLiveEventId` we've been given earlier is no longer usable
|
|
903
|
+
render()
|
|
904
|
+
}
|
|
905
|
+
})
|
|
906
|
+
// Later, unsubscribe when no longer needed (such as on unmount)
|
|
907
|
+
// subscription.unsubscribe()
|
|
908
|
+
```
|
|
909
|
+
|
|
910
|
+
`client.live.events(options)`
|
|
911
|
+
|
|
912
|
+
Listen to live content updates. Returns an RxJS Observable. When calling `.subscribe()` on the returned observable, a subscription is returned, which can be used to unsubscribe from the events later on by calling `subscription.unsubscribe()`.
|
|
913
|
+
|
|
914
|
+
The `options` object can contain the following properties:
|
|
915
|
+
|
|
916
|
+
- `includeDrafts (boolean)` - Whether to include draft documents in the events. Default is false. Note: This is an experimental API and may change or be removed.
|
|
917
|
+
- `tag (string)` - Optional request tag for the listener. Use to identify the request in logs.
|
|
918
|
+
|
|
919
|
+
The method will emit different types of events:
|
|
920
|
+
|
|
921
|
+
- `message`: Regular event messages.
|
|
922
|
+
- `restart`: Emitted when the event stream restarts.
|
|
923
|
+
- `reconnect`: Emitted when the client reconnects to the event stream.
|
|
924
|
+
- `welcome`: Emitted when the client successfully connects to the event stream.
|
|
925
|
+
|
|
926
|
+
To listen to updates in draft content, set `includeDrafts` to `true`
|
|
927
|
+
and configure the client with a token or `withCredentials: true`. The token should have the lowest possible access role.
|
|
928
|
+
|
|
887
929
|
### Creating documents
|
|
888
930
|
|
|
889
931
|
```js
|
package/dist/index.browser.cjs
CHANGED
|
@@ -76,6 +76,19 @@ function httpErrorMessage(res) {
|
|
|
76
76
|
function stringifyBody(body, res) {
|
|
77
77
|
return (res.headers["content-type"] || "").toLowerCase().indexOf("application/json") !== -1 ? JSON.stringify(body, null, 2) : body;
|
|
78
78
|
}
|
|
79
|
+
class CorsOriginError extends Error {
|
|
80
|
+
projectId;
|
|
81
|
+
addOriginUrl;
|
|
82
|
+
constructor({ projectId: projectId2 }) {
|
|
83
|
+
super("CorsOriginError"), this.name = "CorsOriginError", this.projectId = projectId2;
|
|
84
|
+
const url = new URL(`https://sanity.io/manage/project/${projectId2}/api`);
|
|
85
|
+
if (typeof location < "u") {
|
|
86
|
+
const { origin } = location;
|
|
87
|
+
url.searchParams.set("cors", "add"), url.searchParams.set("origin", origin), this.addOriginUrl = url, this.message = `The current origin is not allowed to connect to the Live Content API. Add it here: ${url}`;
|
|
88
|
+
} else
|
|
89
|
+
this.message = `The current origin is not allowed to connect to the Live Content API. Change your configuration here: ${url}`;
|
|
90
|
+
}
|
|
91
|
+
}
|
|
79
92
|
const httpError = {
|
|
80
93
|
onResponse: (res) => {
|
|
81
94
|
if (res.statusCode >= 500)
|
|
@@ -949,6 +962,7 @@ class LiveClient {
|
|
|
949
962
|
tag: _tag
|
|
950
963
|
} = {}) {
|
|
951
964
|
const {
|
|
965
|
+
projectId: projectId2,
|
|
952
966
|
apiVersion: _apiVersion,
|
|
953
967
|
token,
|
|
954
968
|
withCredentials,
|
|
@@ -999,6 +1013,17 @@ class LiveClient {
|
|
|
999
1013
|
const EventSourceImplementation = typeof EventSource > "u" || esOptions.headers || esOptions.withCredentials ? (await import("@sanity/eventsource")).default : EventSource;
|
|
1000
1014
|
if (unsubscribed)
|
|
1001
1015
|
return;
|
|
1016
|
+
try {
|
|
1017
|
+
if (await fetch(url, {
|
|
1018
|
+
method: "OPTIONS",
|
|
1019
|
+
mode: "cors",
|
|
1020
|
+
credentials: esOptions.withCredentials ? "include" : "omit",
|
|
1021
|
+
headers: esOptions.headers
|
|
1022
|
+
}), unsubscribed)
|
|
1023
|
+
return;
|
|
1024
|
+
} catch {
|
|
1025
|
+
throw new CorsOriginError({ projectId: projectId2 });
|
|
1026
|
+
}
|
|
1002
1027
|
const evs = new EventSourceImplementation(url.toString(), esOptions);
|
|
1003
1028
|
evs.addEventListener("error", onError);
|
|
1004
1029
|
for (const type of listenFor)
|
|
@@ -1560,6 +1585,7 @@ Object.defineProperty(exports, "unstable__environment", {
|
|
|
1560
1585
|
exports.BasePatch = BasePatch;
|
|
1561
1586
|
exports.BaseTransaction = BaseTransaction;
|
|
1562
1587
|
exports.ClientError = ClientError;
|
|
1588
|
+
exports.CorsOriginError = CorsOriginError;
|
|
1563
1589
|
exports.ObservablePatch = ObservablePatch;
|
|
1564
1590
|
exports.ObservableSanityClient = ObservableSanityClient;
|
|
1565
1591
|
exports.ObservableTransaction = ObservableTransaction;
|