@sanity/client 7.11.0-live-cors → 7.11.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-live-cors",
3
+ "version": "7.11.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,5 +1,5 @@
1
- import {catchError, mergeMap, Observable, of} from 'rxjs'
2
- import {finalize, map, shareReplay} from 'rxjs/operators'
1
+ import {catchError, concat, EMPTY, mergeMap, Observable, of} from 'rxjs'
2
+ import {finalize, map} from 'rxjs/operators'
3
3
 
4
4
  import {CorsOriginError} from '../http/errors'
5
5
  import type {ObservableSanityClient, SanityClient} from '../SanityClient'
@@ -113,47 +113,32 @@ export class LiveClient {
113
113
  'welcome',
114
114
  'reconnect',
115
115
  'goaway',
116
- ])
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
+ )
117
127
 
118
- // Detect CORS only if the initial connection fails.
119
- const checkCors$ = fetchObservable(url, {
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
+ const checkCors = fetchObservable(url, {
120
130
  method: 'OPTIONS',
121
131
  mode: 'cors',
122
132
  credentials: esOptions.withCredentials ? 'include' : 'omit',
123
133
  headers: esOptions.headers,
124
134
  }).pipe(
135
+ mergeMap(() => EMPTY),
125
136
  catchError(() => {
126
137
  // If the request fails, then we assume it was due to CORS, and we rethrow a special error that allows special handling in userland
127
138
  throw new CorsOriginError({projectId: projectId!})
128
139
  }),
129
- shareReplay(1), // run only once
130
140
  )
131
-
132
- const observable = events.pipe(
133
- catchError((err) => {
134
- console.log('[live] err from catchError', err)
135
- return checkCors$.pipe(
136
- mergeMap(() => {
137
- console.log('[live] err from checkCors$', err)
138
- return of(err)
139
- }),
140
- mergeMap(() => {
141
- console.log('[live] throwing err from checkCors$', err)
142
- throw err
143
- })
144
- )
145
- }),
146
- reconnectOnConnectionFailure(),
147
- map((event) => {
148
- console.log('event', 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
- ).pipe(
141
+ const observable = concat(checkCors, events).pipe(
157
142
  finalize(() => eventsCache.delete(key)),
158
143
  shareReplayLatest({
159
144
  predicate: (event) => event.type === 'welcome',
@@ -3439,33 +3439,26 @@ ${selectionOpts}`);
3439
3439
  "welcome",
3440
3440
  "reconnect",
3441
3441
  "goaway"
3442
- ]), checkCors$ = fetchObservable(url, {
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, {
3443
3452
  method: "OPTIONS",
3444
3453
  mode: "cors",
3445
3454
  credentials: esOptions.withCredentials ? "include" : "omit",
3446
3455
  headers: esOptions.headers
3447
3456
  }).pipe(
3457
+ mergeMap(() => EMPTY),
3448
3458
  catchError(() => {
3449
3459
  throw new CorsOriginError({ projectId: projectId2 });
3450
- }),
3451
- shareReplay(1)
3452
- // run only once
3453
- ), observable2 = events.pipe(
3454
- catchError((err) => (console.log("[live] err from catchError", err), checkCors$.pipe(
3455
- mergeMap(() => (console.log("[live] err from checkCors$", err), of(err))),
3456
- mergeMap(() => {
3457
- throw console.log("[live] throwing err from checkCors$", err), err;
3458
- })
3459
- ))),
3460
- reconnectOnConnectionFailure(),
3461
- map((event) => {
3462
- if (console.log("event", event), event.type === "message") {
3463
- const { data, ...rest } = event;
3464
- return { ...rest, tags: data.tags };
3465
- }
3466
- return event;
3467
3460
  })
3468
- ).pipe(
3461
+ ), observable2 = concat(checkCors, events).pipe(
3469
3462
  finalize(() => eventsCache.delete(key)),
3470
3463
  shareReplayLatest({
3471
3464
  predicate: (event) => event.type === "welcome"