@sanity/client 6.17.1-canary.0 → 6.17.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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@sanity/client",
3
- "version": "6.17.1-canary.0",
3
+ "version": "6.17.1",
4
4
  "description": "Client for retrieving, creating and patching data from Sanity.io",
5
5
  "keywords": [
6
6
  "sanity",
@@ -118,7 +118,7 @@
118
118
  },
119
119
  "dependencies": {
120
120
  "@sanity/eventsource": "^5.0.2",
121
- "get-it": "^8.4.27",
121
+ "get-it": "^8.4.28",
122
122
  "rxjs": "^7.0.0"
123
123
  },
124
124
  "devDependencies": {
@@ -131,7 +131,7 @@
131
131
  "@types/node": "^20.8.8",
132
132
  "@typescript-eslint/eslint-plugin": "^7.8.0",
133
133
  "@typescript-eslint/parser": "^7.8.0",
134
- "@vercel/stega": "0.1.1",
134
+ "@vercel/stega": "0.1.2",
135
135
  "@vitest/coverage-v8": "1.5.3",
136
136
  "eslint": "^8.57.0",
137
137
  "eslint-config-prettier": "^9.1.0",
@@ -5,7 +5,6 @@ import {defaultConfig, initConfig} from './config'
5
5
  import * as dataMethods from './data/dataMethods'
6
6
  import {_listen} from './data/listen'
7
7
  import {ObservablePatch, Patch} from './data/patch'
8
- import {_syncTags} from './data/syncTags'
9
8
  import {ObservableTransaction, Transaction} from './data/transaction'
10
9
  import {DatasetsClient, ObservableDatasetsClient} from './datasets/DatasetsClient'
11
10
  import {ObservableProjectsClient, ProjectsClient} from './projects/ProjectsClient'
@@ -42,7 +41,6 @@ import {ObservableUsersClient, UsersClient} from './users/UsersClient'
42
41
 
43
42
  export type {
44
43
  _listen,
45
- _syncTags,
46
44
  AssetsClient,
47
45
  DatasetsClient,
48
46
  ObservableAssetsClient,
@@ -70,7 +68,6 @@ export class ObservableSanityClient {
70
68
  * Instance properties
71
69
  */
72
70
  listen = _listen
73
- syncTags = _syncTags
74
71
 
75
72
  constructor(httpRequest: HttpRequest, config: ClientConfig = defaultConfig) {
76
73
  this.config(config)
@@ -716,7 +713,6 @@ export class SanityClient {
716
713
  * Instance properties
717
714
  */
718
715
  listen = _listen
719
- syncTags = _syncTags
720
716
 
721
717
  constructor(httpRequest: HttpRequest, config: ClientConfig = defaultConfig) {
722
718
  this.config(config)
@@ -85,16 +85,13 @@ export function _listen<R extends Record<string, Any> = Record<string, Any>>(
85
85
 
86
86
  return new Observable((observer) => {
87
87
  let es: InstanceType<typeof import('@sanity/eventsource')>
88
- getEventSource()
89
- .then((eventSource) => {
90
- es = eventSource
91
- })
92
- .catch((reason) => {
93
- observer.error(reason)
94
- stop()
95
- })
96
88
  let reconnectTimer: NodeJS.Timeout
97
89
  let stopped = false
90
+ // Unsubscribe differs from stopped in that we will never reopen.
91
+ // Once it is`true`, it will never be `false` again.
92
+ let unsubscribed = false
93
+
94
+ open()
98
95
 
99
96
  function onError() {
100
97
  if (stopped) {
@@ -150,8 +147,17 @@ export function _listen<R extends Record<string, Any> = Record<string, Any>>(
150
147
  }
151
148
  }
152
149
 
153
- async function getEventSource(): Promise<InstanceType<typeof import('@sanity/eventsource')>> {
150
+ async function getEventSource(): Promise<InstanceType<
151
+ typeof import('@sanity/eventsource')
152
+ > | void> {
154
153
  const {default: EventSource} = await import('@sanity/eventsource')
154
+
155
+ // If the listener has been unsubscribed from before we managed to load the module,
156
+ // do not set up the EventSource.
157
+ if (unsubscribed) {
158
+ return
159
+ }
160
+
155
161
  const evs = new EventSource(uri, esOptions)
156
162
  evs.addEventListener('error', onError)
157
163
  evs.addEventListener('channelError', onChannelError)
@@ -163,7 +169,9 @@ export function _listen<R extends Record<string, Any> = Record<string, Any>>(
163
169
  function open() {
164
170
  getEventSource()
165
171
  .then((eventSource) => {
166
- es = eventSource
172
+ if (eventSource) {
173
+ es = eventSource
174
+ }
167
175
  })
168
176
  .catch((reason) => {
169
177
  observer.error(reason)
@@ -174,6 +182,7 @@ export function _listen<R extends Record<string, Any> = Record<string, Any>>(
174
182
  function stop() {
175
183
  stopped = true
176
184
  unsubscribe()
185
+ unsubscribed = true
177
186
  }
178
187
 
179
188
  return stop
package/src/types.ts CHANGED
@@ -785,7 +785,6 @@ export interface RawQueryResponse<R> {
785
785
  ms: number
786
786
  result: R
787
787
  resultSourceMap?: ContentSourceMap
788
- syncTags?: `s1:${string}`[]
789
788
  }
790
789
 
791
790
  /** @public */
@@ -1000,26 +999,6 @@ export interface ContentSourceMap {
1000
999
  paths: ContentSourceMapPaths
1001
1000
  }
1002
1001
 
1003
- /** @public */
1004
- export interface SyncTagsOptions {
1005
- pos?: string
1006
- }
1007
- /** @public */
1008
- export interface SyncTagsErrorEvent {
1009
- type: 'error'
1010
- status: number
1011
- message: string
1012
- }
1013
- /** @public */
1014
- export interface SyncTagsRestartEvent {
1015
- type: 'restart'
1016
- }
1017
- /** @public */
1018
- export interface SyncTagsChangeEvent {
1019
- tags: `s1:${string}`[]
1020
- pos: string
1021
- }
1022
-
1023
1002
  export type {
1024
1003
  ContentSourceMapParsedPath,
1025
1004
  ContentSourceMapParsedPathKeyedSegment,
@@ -28,9 +28,9 @@
28
28
  const qIndex = url.indexOf("?");
29
29
  if (qIndex === -1)
30
30
  return { url, searchParams: new URLSearchParams() };
31
- const base = url.slice(0, qIndex), qs = url.slice(qIndex + 1), searchParams = new URLSearchParams(qs);
32
- if (typeof searchParams.set == "function")
33
- return { url: base, searchParams };
31
+ const base = url.slice(0, qIndex), qs = url.slice(qIndex + 1);
32
+ if (!isReactNative)
33
+ return { url: base, searchParams: new URLSearchParams(qs) };
34
34
  if (typeof decodeURIComponent != "function")
35
35
  throw new Error(
36
36
  "Broken `URLSearchParams` implementation, and `decodeURIComponent` is not defined"
@@ -1778,10 +1778,10 @@
1778
1778
  return combineLatest.apply(void 0, __spreadArray([], __read(otherSources)));
1779
1779
  }
1780
1780
 
1781
- var s = { 0: 8203, 1: 8204, 2: 8205, 3: 8290, 4: 8291, 5: 8288, 6: 65279, 7: 8289, 8: 119155, 9: 119156, a: 119157, b: 119158, c: 119159, d: 119160, e: 119161, f: 119162 }, c = { 0: 8203, 1: 8204, 2: 8205, 3: 65279 }, d = new Array(4).fill(String.fromCodePoint(c[0])).join("");
1781
+ var s = { 0: 8203, 1: 8204, 2: 8205, 3: 8290, 4: 8291, 5: 8288, 6: 65279, 7: 8289, 8: 119155, 9: 119156, a: 119157, b: 119158, c: 119159, d: 119160, e: 119161, f: 119162 }, c = { 0: 8203, 1: 8204, 2: 8205, 3: 65279 }, u = new Array(4).fill(String.fromCodePoint(c[0])).join("");
1782
1782
  function E(t) {
1783
1783
  let e = JSON.stringify(t);
1784
- return `${d}${Array.from(e).map((r) => {
1784
+ return `${u}${Array.from(e).map((r) => {
1785
1785
  let n = r.charCodeAt(0);
1786
1786
  if (n > 255)
1787
1787
  throw new Error(`Only ASCII edit info can be encoded. Error attempting to encode ${e} on character ${r} (${n})`);
@@ -1789,9 +1789,9 @@
1789
1789
  }).join("")}`;
1790
1790
  }
1791
1791
  function I(t) {
1792
- return Number.isNaN(Number(t)) ? !!Date.parse(t) : !1;
1792
+ return !Number.isNaN(Number(t)) || /[a-z]/i.test(t) && !/\d+(?:[-:\/]\d+){2}(?:T\d+(?:[-:\/]\d+){1,2}(\.\d+)?Z?)?/.test(t) ? !1 : !!Date.parse(t);
1793
1793
  }
1794
- function x(t) {
1794
+ function T(t) {
1795
1795
  try {
1796
1796
  new URL(t, t.startsWith("/") ? "https://acme.com" : void 0);
1797
1797
  } catch {
@@ -1800,17 +1800,17 @@
1800
1800
  return !0;
1801
1801
  }
1802
1802
  function C(t, e, r = "auto") {
1803
- return r === !0 || r === "auto" && (I(t) || x(t)) ? t : `${t}${E(e)}`;
1803
+ return r === !0 || r === "auto" && (I(t) || T(t)) ? t : `${t}${E(e)}`;
1804
1804
  }
1805
1805
  Object.fromEntries(Object.entries(c).map((t) => t.reverse()));
1806
1806
  Object.fromEntries(Object.entries(s).map((t) => t.reverse()));
1807
- var T = `${Object.values(s).map((t) => `\\u{${t.toString(16)}}`).join("")}`, f = new RegExp(`[${T}]{4,}`, "gu");
1807
+ var S = `${Object.values(s).map((t) => `\\u{${t.toString(16)}}`).join("")}`, f = new RegExp(`[${S}]{4,}`, "gu");
1808
1808
  function _(t) {
1809
1809
  var e;
1810
1810
  return { cleaned: t.replace(f, ""), encoded: ((e = t.match(f)) == null ? void 0 : e[0]) || "" };
1811
1811
  }
1812
1812
  function O(t) {
1813
- return JSON.parse(_(JSON.stringify(t)).cleaned);
1813
+ return t && JSON.parse(_(JSON.stringify(t)).cleaned);
1814
1814
  }
1815
1815
  function stegaClean(result) {
1816
1816
  try {
@@ -2672,13 +2672,8 @@ ${selectionOpts}`);
2672
2672
  return (token || withCredentials) && (esOptions.withCredentials = !0), token && (esOptions.headers = {
2673
2673
  Authorization: `Bearer ${token}`
2674
2674
  }), new Observable((observer) => {
2675
- let es;
2676
- getEventSource().then((eventSource) => {
2677
- es = eventSource;
2678
- }).catch((reason) => {
2679
- observer.error(reason), stop();
2680
- });
2681
- let reconnectTimer, stopped = !1;
2675
+ let es, reconnectTimer, stopped = !1, unsubscribed = !1;
2676
+ open();
2682
2677
  function onError() {
2683
2678
  stopped || (emitReconnect(), !stopped && es.readyState === es.CLOSED && (unsubscribe(), clearTimeout(reconnectTimer), reconnectTimer = setTimeout(open, 100)));
2684
2679
  }
@@ -2699,18 +2694,21 @@ ${selectionOpts}`);
2699
2694
  shouldEmitReconnect && observer.next({ type: "reconnect" });
2700
2695
  }
2701
2696
  async function getEventSource() {
2702
- const { default: EventSource } = await Promise.resolve().then(function () { return browser$2; }), evs = new EventSource(uri, esOptions);
2697
+ const { default: EventSource } = await Promise.resolve().then(function () { return browser$2; });
2698
+ if (unsubscribed)
2699
+ return;
2700
+ const evs = new EventSource(uri, esOptions);
2703
2701
  return evs.addEventListener("error", onError), evs.addEventListener("channelError", onChannelError), evs.addEventListener("disconnect", onDisconnect), listenFor.forEach((type) => evs.addEventListener(type, onMessage)), evs;
2704
2702
  }
2705
2703
  function open() {
2706
2704
  getEventSource().then((eventSource) => {
2707
- es = eventSource;
2705
+ eventSource && (es = eventSource);
2708
2706
  }).catch((reason) => {
2709
2707
  observer.error(reason), stop();
2710
2708
  });
2711
2709
  }
2712
2710
  function stop() {
2713
- stopped = !0, unsubscribe();
2711
+ stopped = !0, unsubscribe(), unsubscribed = !0;
2714
2712
  }
2715
2713
  return stop;
2716
2714
  });
@@ -2732,57 +2730,6 @@ ${selectionOpts}`);
2732
2730
  function extractErrorMessage(err) {
2733
2731
  return err.error ? err.error.description ? err.error.description : typeof err.error == "string" ? err.error : JSON.stringify(err.error, null, 2) : err.message || "Unknown listener error";
2734
2732
  }
2735
- function _syncTags(opts = {}) {
2736
- const path = _getDataUrl(this, "sync-tags"), url = new URL(this.getUrl(path, !0));
2737
- return opts.pos && url.searchParams.append("start", opts.pos), new Observable((observer) => {
2738
- const controller = new AbortController(), { signal } = controller;
2739
- return fetch(url, { signal }).then(async (res) => {
2740
- if (!res.body)
2741
- throw new TypeError("Response body is not readable");
2742
- const reader = res.body.pipeThrough(new TextDecoderStream()).pipeThrough(splitStream(`
2743
- `)).pipeThrough(parseJSON()).getReader(), results = {
2744
- [Symbol.asyncIterator]() {
2745
- return {
2746
- next: () => reader.read()
2747
- };
2748
- }
2749
- };
2750
- for await (const chunk of results) {
2751
- if (signal.aborted)
2752
- break;
2753
- if (chunk.type === "error") {
2754
- observer.error(chunk);
2755
- break;
2756
- }
2757
- observer.next(chunk);
2758
- }
2759
- }).catch((err) => {
2760
- (err == null ? void 0 : err.name) !== "TimeoutError" && (err == null ? void 0 : err.name) !== "AbortError" && observer.error(err);
2761
- }), () => {
2762
- controller.abort();
2763
- };
2764
- });
2765
- }
2766
- function splitStream(splitOn) {
2767
- let buffer = "";
2768
- return new TransformStream({
2769
- transform(chunk, controller) {
2770
- buffer += chunk;
2771
- const parts = buffer.split(splitOn);
2772
- parts.slice(0, -1).forEach((part) => controller.enqueue(part)), buffer = parts[parts.length - 1];
2773
- },
2774
- flush(controller) {
2775
- buffer && controller.enqueue(buffer);
2776
- }
2777
- });
2778
- }
2779
- function parseJSON() {
2780
- return new TransformStream({
2781
- transform(chunk, controller) {
2782
- chunk && controller.enqueue(JSON.parse(chunk));
2783
- }
2784
- });
2785
- }
2786
2733
  var __accessCheck$3 = (obj, member, msg) => {
2787
2734
  if (!member.has(obj))
2788
2735
  throw TypeError("Cannot " + msg);
@@ -2987,7 +2934,7 @@ ${selectionOpts}`);
2987
2934
  }, __privateSet = (obj, member, value, setter) => (__accessCheck(obj, member, "write to private field"), member.set(obj, value), value), _clientConfig, _httpRequest;
2988
2935
  const _ObservableSanityClient = class _ObservableSanityClient2 {
2989
2936
  constructor(httpRequest, config = defaultConfig) {
2990
- __publicField(this, "assets"), __publicField(this, "datasets"), __publicField(this, "projects"), __publicField(this, "users"), __privateAdd(this, _clientConfig, void 0), __privateAdd(this, _httpRequest, void 0), __publicField(this, "listen", _listen), __publicField(this, "syncTags", _syncTags), this.config(config), __privateSet(this, _httpRequest, httpRequest), this.assets = new ObservableAssetsClient(this, __privateGet(this, _httpRequest)), this.datasets = new ObservableDatasetsClient(this, __privateGet(this, _httpRequest)), this.projects = new ObservableProjectsClient(this, __privateGet(this, _httpRequest)), this.users = new ObservableUsersClient(this, __privateGet(this, _httpRequest));
2937
+ __publicField(this, "assets"), __publicField(this, "datasets"), __publicField(this, "projects"), __publicField(this, "users"), __privateAdd(this, _clientConfig, void 0), __privateAdd(this, _httpRequest, void 0), __publicField(this, "listen", _listen), this.config(config), __privateSet(this, _httpRequest, httpRequest), this.assets = new ObservableAssetsClient(this, __privateGet(this, _httpRequest)), this.datasets = new ObservableDatasetsClient(this, __privateGet(this, _httpRequest)), this.projects = new ObservableProjectsClient(this, __privateGet(this, _httpRequest)), this.users = new ObservableUsersClient(this, __privateGet(this, _httpRequest));
2991
2938
  }
2992
2939
  /**
2993
2940
  * Clone the client - returns a new instance
@@ -3116,7 +3063,7 @@ ${selectionOpts}`);
3116
3063
  var _clientConfig2, _httpRequest2;
3117
3064
  const _SanityClient = class _SanityClient2 {
3118
3065
  constructor(httpRequest, config = defaultConfig) {
3119
- __publicField(this, "assets"), __publicField(this, "datasets"), __publicField(this, "projects"), __publicField(this, "users"), __publicField(this, "observable"), __privateAdd(this, _clientConfig2, void 0), __privateAdd(this, _httpRequest2, void 0), __publicField(this, "listen", _listen), __publicField(this, "syncTags", _syncTags), this.config(config), __privateSet(this, _httpRequest2, httpRequest), this.assets = new AssetsClient(this, __privateGet(this, _httpRequest2)), this.datasets = new DatasetsClient(this, __privateGet(this, _httpRequest2)), this.projects = new ProjectsClient(this, __privateGet(this, _httpRequest2)), this.users = new UsersClient(this, __privateGet(this, _httpRequest2)), this.observable = new ObservableSanityClient(httpRequest, config);
3066
+ __publicField(this, "assets"), __publicField(this, "datasets"), __publicField(this, "projects"), __publicField(this, "users"), __publicField(this, "observable"), __privateAdd(this, _clientConfig2, void 0), __privateAdd(this, _httpRequest2, void 0), __publicField(this, "listen", _listen), this.config(config), __privateSet(this, _httpRequest2, httpRequest), this.assets = new AssetsClient(this, __privateGet(this, _httpRequest2)), this.datasets = new DatasetsClient(this, __privateGet(this, _httpRequest2)), this.projects = new ProjectsClient(this, __privateGet(this, _httpRequest2)), this.users = new UsersClient(this, __privateGet(this, _httpRequest2)), this.observable = new ObservableSanityClient(httpRequest, config);
3120
3067
  }
3121
3068
  /**
3122
3069
  * Clone the client - returns a new instance