@sanity/client 7.23.0 → 7.23.2

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.
@@ -914,11 +914,26 @@ export declare function connectEventSource<EventName extends string>(
914
914
 
915
915
  /**
916
916
  * @public
917
- * Thrown if the EventSource connection could not be established.
918
- * Note that ConnectionFailedErrors are rare, and disconnects will normally be handled by the EventSource instance itself and emitted as `reconnect` events.
917
+ * Thrown when the EventSource connection could not be established, or was rejected by the server.
918
+ * Transient failures (network drops, 5xx, 408, 429) are reconnected internally and emitted as
919
+ * `reconnect` events; a permanent rejection (any other 4xx, eg an expired token) errors the
920
+ * stream with this class so consumers can react — check `status` for the rejection code.
919
921
  */
920
922
  export declare class ConnectionFailedError extends Error {
921
923
  readonly name = 'ConnectionFailedError'
924
+ /**
925
+ * HTTP status code of the rejected connection attempt, if known.
926
+ * Only set when the EventSource implementation exposes it — the polyfill used in
927
+ * Node.js and when custom headers (eg authorization) are required does, while
928
+ * native EventSource implementations (browser and Node.js) do not.
929
+ */
930
+ readonly status?: number
931
+ constructor(
932
+ message?: string,
933
+ options?: ErrorOptions & {
934
+ status?: number
935
+ },
936
+ )
922
937
  }
923
938
 
924
939
  /**
@@ -914,11 +914,26 @@ export declare function connectEventSource<EventName extends string>(
914
914
 
915
915
  /**
916
916
  * @public
917
- * Thrown if the EventSource connection could not be established.
918
- * Note that ConnectionFailedErrors are rare, and disconnects will normally be handled by the EventSource instance itself and emitted as `reconnect` events.
917
+ * Thrown when the EventSource connection could not be established, or was rejected by the server.
918
+ * Transient failures (network drops, 5xx, 408, 429) are reconnected internally and emitted as
919
+ * `reconnect` events; a permanent rejection (any other 4xx, eg an expired token) errors the
920
+ * stream with this class so consumers can react — check `status` for the rejection code.
919
921
  */
920
922
  export declare class ConnectionFailedError extends Error {
921
923
  readonly name = 'ConnectionFailedError'
924
+ /**
925
+ * HTTP status code of the rejected connection attempt, if known.
926
+ * Only set when the EventSource implementation exposes it — the polyfill used in
927
+ * Node.js and when custom headers (eg authorization) are required does, while
928
+ * native EventSource implementations (browser and Node.js) do not.
929
+ */
930
+ readonly status?: number
931
+ constructor(
932
+ message?: string,
933
+ options?: ErrorOptions & {
934
+ status?: number
935
+ },
936
+ )
922
937
  }
923
938
 
924
939
  /**
@@ -405,6 +405,17 @@ const initConfig = (config, prevConfig) => {
405
405
  };
406
406
  class ConnectionFailedError extends Error {
407
407
  name = "ConnectionFailedError";
408
+ /**
409
+ * HTTP status code of the rejected connection attempt, if known.
410
+ * Only set when the EventSource implementation exposes it — the polyfill used in
411
+ * Node.js and when custom headers (eg authorization) are required does, while
412
+ * native EventSource implementations (browser and Node.js) do not.
413
+ */
414
+ status;
415
+ constructor(message, options = {}) {
416
+ const { status, ...errorOptions } = options;
417
+ super(message, errorOptions), this.status = status;
418
+ }
408
419
  }
409
420
  class DisconnectError extends Error {
410
421
  name = "DisconnectError";
@@ -448,6 +459,11 @@ function connectWithESInstance(es, events) {
448
459
  );
449
460
  return;
450
461
  }
462
+ const rawStatus = evt.status, status = typeof rawStatus == "number" ? rawStatus : void 0;
463
+ if (status !== void 0) {
464
+ observer.error(new ConnectionFailedError("EventSource connection failed", { status }));
465
+ return;
466
+ }
451
467
  es.readyState === es.CLOSED ? observer.error(new ConnectionFailedError("EventSource connection failed")) : emitReconnect && observer.next({ type: "reconnect" });
452
468
  }
453
469
  function onOpen() {
@@ -1417,11 +1433,11 @@ var defaults = (obj, defaults2) => Object.keys(defaults2).concat(Object.keys(obj
1417
1433
  const pick = (obj, props) => props.reduce((selection, prop) => (typeof obj[prop] > "u" || (selection[prop] = obj[prop]), selection), {}), eventSourcePolyfill = defer(() => import("@sanity/eventsource")).pipe(
1418
1434
  map(({ default: EventSource2 }) => EventSource2),
1419
1435
  shareReplay(1)
1420
- );
1436
+ ), RETRYABLE_STATUSES = /* @__PURE__ */ new Set([408, 429]);
1421
1437
  function reconnectOnConnectionFailure() {
1422
1438
  return function(source) {
1423
1439
  return source.pipe(
1424
- catchError((err, caught) => err instanceof ConnectionFailedError ? concat(of({ type: "reconnect" }), timer(1e3).pipe(mergeMap(() => caught))) : throwError(() => err))
1440
+ catchError((err, caught) => err instanceof ConnectionFailedError && (typeof err.status != "number" || err.status < 400 || err.status >= 500 || RETRYABLE_STATUSES.has(err.status)) ? concat(of({ type: "reconnect" }), timer(1e3).pipe(mergeMap(() => caught))) : throwError(() => err))
1425
1441
  );
1426
1442
  };
1427
1443
  }