@sanity/client 7.11.0 → 7.11.1-allow-live.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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@sanity/client",
3
- "version": "7.11.0",
3
+ "version": "7.11.1-allow-live.0",
4
4
  "description": "Client for retrieving, creating and patching data from Sanity.io",
5
5
  "keywords": [
6
6
  "sanity",
package/src/data/live.ts CHANGED
@@ -1,4 +1,4 @@
1
- import {catchError, concat, EMPTY, mergeMap, Observable, of} from 'rxjs'
1
+ import {catchError, mergeMap, Observable, of} from 'rxjs'
2
2
  import {finalize, map} from 'rxjs/operators'
3
3
 
4
4
  import {CorsOriginError} from '../http/errors'
@@ -13,7 +13,6 @@ import type {
13
13
  SyncTag,
14
14
  } from '../types'
15
15
  import {shareReplayLatest} from '../util/shareReplayLatest'
16
- import * as validate from '../validators'
17
16
  import {_getDataUrl} from './dataMethods'
18
17
  import {connectEventSource} from './eventsource'
19
18
  import {eventSourcePolyfill} from './eventsourcePolyfill'
@@ -45,7 +44,6 @@ export class LiveClient {
45
44
  */
46
45
  tag?: string
47
46
  } = {}): Observable<LiveEvent> {
48
- validate.resourceGuard('live', this.#client.config())
49
47
  const {
50
48
  projectId,
51
49
  apiVersion: _apiVersion,
@@ -113,37 +111,53 @@ export class LiveClient {
113
111
  'welcome',
114
112
  'reconnect',
115
113
  'goaway',
116
- ]).pipe(
117
- reconnectOnConnectionFailure(),
118
- map((event) => {
119
- if (event.type === 'message') {
120
- const {data, ...rest} = event
121
- // Splat data properties from the eventsource message onto the returned event
122
- return {...rest, tags: (data as {tags: SyncTag[]}).tags} as LiveEventMessage
123
- }
124
- return event as LiveEventRestart | LiveEventReconnect | LiveEventWelcome | LiveEventGoAway
125
- }),
126
- )
114
+ ])
127
115
 
128
- // Detect if CORS is allowed, the way the CORS is checked supports preflight caching, so when the EventSource boots up it knows it sees the preflight was already made and we're good to go
129
116
  const checkCors = fetchObservable(url, {
130
117
  method: 'OPTIONS',
131
118
  mode: 'cors',
132
119
  credentials: esOptions.withCredentials ? 'include' : 'omit',
133
120
  headers: esOptions.headers,
134
121
  }).pipe(
135
- mergeMap(() => EMPTY),
136
122
  catchError(() => {
137
123
  // If the request fails, then we assume it was due to CORS, and we rethrow a special error that allows special handling in userland
138
124
  throw new CorsOriginError({projectId: projectId!})
139
125
  }),
140
126
  )
141
- const observable = concat(checkCors, events).pipe(
142
- finalize(() => eventsCache.delete(key)),
143
- shareReplayLatest({
144
- predicate: (event) => event.type === 'welcome',
145
- }),
146
- )
127
+
128
+ const observable = events
129
+ .pipe(
130
+ reconnectOnConnectionFailure(),
131
+ mergeMap((event) => {
132
+ if (event.type === 'reconnect') {
133
+ // Check for CORS on reconnect events (which happen on 403s)
134
+ return checkCors.pipe(mergeMap(() => of(event)))
135
+ }
136
+ return of(event)
137
+ }),
138
+ catchError((err) => {
139
+ return checkCors.pipe(
140
+ mergeMap(() => {
141
+ // rethrow the original error if checkCors passed
142
+ throw err
143
+ }),
144
+ )
145
+ }),
146
+ map((event) => {
147
+ if (event.type === 'message') {
148
+ const {data, ...rest} = event
149
+ // Splat data properties from the eventsource message onto the returned event
150
+ return {...rest, tags: (data as {tags: SyncTag[]}).tags} as LiveEventMessage
151
+ }
152
+ return event as LiveEventRestart | LiveEventReconnect | LiveEventWelcome | LiveEventGoAway
153
+ }),
154
+ )
155
+ .pipe(
156
+ finalize(() => eventsCache.delete(key)),
157
+ shareReplayLatest({
158
+ predicate: (event) => event.type === 'welcome',
159
+ }),
160
+ )
147
161
  eventsCache.set(key, observable)
148
162
  return observable
149
163
  }
@@ -3406,7 +3406,6 @@ ${selectionOpts}`);
3406
3406
  includeDrafts = false,
3407
3407
  tag: _tag
3408
3408
  } = {}) {
3409
- resourceGuard("live", this.#client.config());
3410
3409
  const {
3411
3410
  projectId: projectId2,
3412
3411
  apiVersion: _apiVersion,
@@ -3439,26 +3438,31 @@ ${selectionOpts}`);
3439
3438
  "welcome",
3440
3439
  "reconnect",
3441
3440
  "goaway"
3442
- ]).pipe(
3443
- reconnectOnConnectionFailure(),
3444
- map((event) => {
3445
- if (event.type === "message") {
3446
- const { data, ...rest } = event;
3447
- return { ...rest, tags: data.tags };
3448
- }
3449
- return event;
3450
- })
3451
- ), checkCors = fetchObservable(url, {
3441
+ ]), checkCors = fetchObservable(url, {
3452
3442
  method: "OPTIONS",
3453
3443
  mode: "cors",
3454
3444
  credentials: esOptions.withCredentials ? "include" : "omit",
3455
3445
  headers: esOptions.headers
3456
3446
  }).pipe(
3457
- mergeMap(() => EMPTY),
3458
3447
  catchError(() => {
3459
3448
  throw new CorsOriginError({ projectId: projectId2 });
3460
3449
  })
3461
- ), observable2 = concat(checkCors, events).pipe(
3450
+ ), observable2 = events.pipe(
3451
+ reconnectOnConnectionFailure(),
3452
+ mergeMap((event) => event.type === "reconnect" ? checkCors.pipe(mergeMap(() => of(event))) : of(event)),
3453
+ catchError((err) => checkCors.pipe(
3454
+ mergeMap(() => {
3455
+ throw err;
3456
+ })
3457
+ )),
3458
+ map((event) => {
3459
+ if (event.type === "message") {
3460
+ const { data, ...rest } = event;
3461
+ return { ...rest, tags: data.tags };
3462
+ }
3463
+ return event;
3464
+ })
3465
+ ).pipe(
3462
3466
  finalize(() => eventsCache.delete(key)),
3463
3467
  shareReplayLatest({
3464
3468
  predicate: (event) => event.type === "welcome"