@sanity/client 6.21.3 → 6.21.4-canary.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.
@@ -944,14 +944,22 @@ class LiveClient {
944
944
  /**
945
945
  * Requires `apiVersion` to be `2021-03-26` or later.
946
946
  */
947
- events() {
948
- const apiVersion = this.#client.config().apiVersion.replace(/^v/, "");
947
+ events({ includeDrafts = !1 } = {}) {
948
+ const { apiVersion: _apiVersion, token } = this.#client.config(), apiVersion = _apiVersion.replace(/^v/, "");
949
949
  if (apiVersion !== "X" && apiVersion < requiredApiVersion)
950
950
  throw new Error(
951
951
  `The live events API requires API version ${requiredApiVersion} or later. The current API version is ${apiVersion}. Please update your API version to use this feature.`
952
952
  );
953
- const path = _getDataUrl(this.#client, "live/events"), url = new URL(this.#client.getUrl(path, !1)), listenFor = ["restart", "message"];
954
- return new rxjs.Observable((observer) => {
953
+ if (includeDrafts && !token)
954
+ throw new Error(
955
+ "The live events API requires a token when 'includeDrafts: true'. Please update your client configuration. The token should have the lowest possible access role."
956
+ );
957
+ const path = _getDataUrl(this.#client, "live/events"), url = new URL(this.#client.getUrl(path, !1));
958
+ includeDrafts && url.searchParams.set("includeDrafts", "true");
959
+ const listenFor = ["restart", "message"], esOptions = {};
960
+ return token && (esOptions.headers = {
961
+ Authorization: `Bearer ${token}`
962
+ }), new rxjs.Observable((observer) => {
955
963
  let es, reconnectTimer, stopped = !1, unsubscribed = !1;
956
964
  open();
957
965
  function onError(evt) {
@@ -976,10 +984,10 @@ class LiveClient {
976
984
  }
977
985
  }
978
986
  async function getEventSource() {
979
- const EventSourceImplementation = typeof EventSource > "u" ? (await import("@sanity/eventsource")).default : EventSource;
987
+ const EventSourceImplementation = typeof EventSource > "u" || token ? (await import("@sanity/eventsource")).default : EventSource;
980
988
  if (unsubscribed)
981
989
  return;
982
- const evs = new EventSourceImplementation(url.toString());
990
+ const evs = new EventSourceImplementation(url.toString(), esOptions);
983
991
  evs.addEventListener("error", onError);
984
992
  for (const type of listenFor)
985
993
  evs.addEventListener(type, onMessage);