@sanity/client 7.11.0 → 7.11.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": "7.11.0",
3
+ "version": "7.11.1",
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'
@@ -113,37 +113,53 @@ export class LiveClient {
113
113
  'welcome',
114
114
  'reconnect',
115
115
  '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
- )
116
+ ])
127
117
 
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
118
  const checkCors = fetchObservable(url, {
130
119
  method: 'OPTIONS',
131
120
  mode: 'cors',
132
121
  credentials: esOptions.withCredentials ? 'include' : 'omit',
133
122
  headers: esOptions.headers,
134
123
  }).pipe(
135
- mergeMap(() => EMPTY),
136
124
  catchError(() => {
137
125
  // 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
126
  throw new CorsOriginError({projectId: projectId!})
139
127
  }),
140
128
  )
141
- const observable = concat(checkCors, events).pipe(
142
- finalize(() => eventsCache.delete(key)),
143
- shareReplayLatest({
144
- predicate: (event) => event.type === 'welcome',
145
- }),
146
- )
129
+
130
+ const observable = events
131
+ .pipe(
132
+ reconnectOnConnectionFailure(),
133
+ mergeMap((event) => {
134
+ if (event.type === 'reconnect') {
135
+ // Check for CORS on reconnect events (which happen on 403s)
136
+ return checkCors.pipe(mergeMap(() => of(event)))
137
+ }
138
+ return of(event)
139
+ }),
140
+ catchError((err) => {
141
+ return checkCors.pipe(
142
+ mergeMap(() => {
143
+ // rethrow the original error if checkCors passed
144
+ throw err
145
+ }),
146
+ )
147
+ }),
148
+ map((event) => {
149
+ if (event.type === 'message') {
150
+ const {data, ...rest} = event
151
+ // Splat data properties from the eventsource message onto the returned event
152
+ return {...rest, tags: (data as {tags: SyncTag[]}).tags} as LiveEventMessage
153
+ }
154
+ return event as LiveEventRestart | LiveEventReconnect | LiveEventWelcome | LiveEventGoAway
155
+ }),
156
+ )
157
+ .pipe(
158
+ finalize(() => eventsCache.delete(key)),
159
+ shareReplayLatest({
160
+ predicate: (event) => event.type === 'welcome',
161
+ }),
162
+ )
147
163
  eventsCache.set(key, observable)
148
164
  return observable
149
165
  }
@@ -3439,26 +3439,31 @@ ${selectionOpts}`);
3439
3439
  "welcome",
3440
3440
  "reconnect",
3441
3441
  "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, {
3442
+ ]), checkCors = fetchObservable(url, {
3452
3443
  method: "OPTIONS",
3453
3444
  mode: "cors",
3454
3445
  credentials: esOptions.withCredentials ? "include" : "omit",
3455
3446
  headers: esOptions.headers
3456
3447
  }).pipe(
3457
- mergeMap(() => EMPTY),
3458
3448
  catchError(() => {
3459
3449
  throw new CorsOriginError({ projectId: projectId2 });
3460
3450
  })
3461
- ), observable2 = concat(checkCors, events).pipe(
3451
+ ), observable2 = events.pipe(
3452
+ reconnectOnConnectionFailure(),
3453
+ mergeMap((event) => event.type === "reconnect" ? checkCors.pipe(mergeMap(() => of(event))) : of(event)),
3454
+ catchError((err) => checkCors.pipe(
3455
+ mergeMap(() => {
3456
+ throw err;
3457
+ })
3458
+ )),
3459
+ map((event) => {
3460
+ if (event.type === "message") {
3461
+ const { data, ...rest } = event;
3462
+ return { ...rest, tags: data.tags };
3463
+ }
3464
+ return event;
3465
+ })
3466
+ ).pipe(
3462
3467
  finalize(() => eventsCache.delete(key)),
3463
3468
  shareReplayLatest({
3464
3469
  predicate: (event) => event.type === "welcome"