@sanity/client 6.24.2 → 6.24.4

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.
@@ -311,6 +311,16 @@ export declare class BaseTransaction {
311
311
  protected _add(mut: Mutation): this
312
312
  }
313
313
 
314
+ /**
315
+ * @public
316
+ * The server sent a `channelError` message. Usually indicative of a bad or malformed request
317
+ */
318
+ export declare class ChannelError extends Error {
319
+ readonly name = 'ChannelError'
320
+ readonly data?: unknown
321
+ constructor(message: string, data: unknown)
322
+ }
323
+
314
324
  /**
315
325
  * An error occurred. This is different from a network-level error (which will be emitted as 'error').
316
326
  * Possible causes are things such as malformed filters, non-existant datasets or similar.
@@ -403,6 +413,39 @@ export declare type ClientReturn<
403
413
  Fallback = Any,
404
414
  > = GroqString extends keyof SanityQueries ? SanityQueries[GroqString] : Fallback
405
415
 
416
+ /**
417
+ * Sanity API specific EventSource handler shared between the listen and live APIs
418
+ *
419
+ * Since the `EventSource` API is not provided by all environments, this function enables custom initialization of the EventSource instance
420
+ * for runtimes that requires polyfilling or custom setup logic (e.g. custom HTTP headers)
421
+ * via the passed `initEventSource` function which must return an EventSource instance.
422
+ *
423
+ * Possible errors to be thrown on the returned observable are:
424
+ * - {@link MessageError}
425
+ * - {@link MessageParseError}
426
+ * - {@link ChannelError}
427
+ * - {@link DisconnectError}
428
+ * - {@link ConnectionFailedError}
429
+ *
430
+ * @param initEventSource - A function that returns an EventSource instance or an Observable that resolves to an EventSource instance
431
+ * @param events - an array of named events from the API to listen for.
432
+ *
433
+ * @internal
434
+ */
435
+ export declare function connectEventSource<EventName extends string>(
436
+ initEventSource: () => EventSourceInstance | Observable<EventSourceInstance>,
437
+ events: EventName[],
438
+ ): Observable<ServerSentEvent<EventName>>
439
+
440
+ /**
441
+ * @public
442
+ * Thrown if the EventSource connection could not be established.
443
+ * Note that ConnectionFailedErrors are rare, and disconnects will normally be handled by the EventSource instance itself and emitted as `reconnect` events.
444
+ */
445
+ export declare class ConnectionFailedError extends Error {
446
+ readonly name = 'ConnectionFailedError'
447
+ }
448
+
406
449
  /** @public */
407
450
  export declare interface ContentSourceMap {
408
451
  mappings: ContentSourceMapMappings
@@ -642,6 +685,18 @@ export declare type DiscardAction = {
642
685
  purge?: boolean
643
686
  }
644
687
 
688
+ /**
689
+ * The listener has been told to explicitly disconnect.
690
+ * This is a rare situation, but may occur if the API knows reconnect attempts will fail,
691
+ * eg in the case of a deleted dataset, a blocked project or similar events.
692
+ * @public
693
+ */
694
+ export declare class DisconnectError extends Error {
695
+ readonly name = 'DisconnectError'
696
+ readonly reason?: string
697
+ constructor(message: string, reason?: string, options?: ErrorOptions)
698
+ }
699
+
645
700
  /**
646
701
  * The listener has been told to explicitly disconnect and not reconnect.
647
702
  * This is a rare situation, but may occur if the API knows reconnect attempts will fail,
@@ -688,6 +743,16 @@ export declare interface ErrorProps {
688
743
  details: Any
689
744
  }
690
745
 
746
+ /**
747
+ * @internal
748
+ */
749
+ export declare type EventSourceEvent<Name extends string> = ServerSentEvent<Name>
750
+
751
+ /**
752
+ * @internal
753
+ */
754
+ export declare type EventSourceInstance = InstanceType<typeof globalThis.EventSource>
755
+
691
756
  /** @public */
692
757
  export declare type FilterDefault = (props: {
693
758
  /**
@@ -826,7 +891,7 @@ export declare function _listen<R extends Record<string, Any> = Record<string, A
826
891
  this: SanityClient | ObservableSanityClient,
827
892
  query: string,
828
893
  params?: ListenParams,
829
- ): Observable<MutationEvent_2<R>>
894
+ ): Observable<MutationEvent<R>>
830
895
 
831
896
  /**
832
897
  * Set up a listener that will be notified when mutations occur on documents matching the provided query/filter.
@@ -845,11 +910,12 @@ export declare function _listen<R extends Record<string, Any> = Record<string, A
845
910
 
846
911
  /** @public */
847
912
  export declare type ListenEvent<R extends Record<string, Any>> =
848
- | MutationEvent_2<R>
913
+ | MutationEvent<R>
849
914
  | ChannelErrorEvent
850
915
  | DisconnectEvent
851
916
  | ReconnectEvent
852
917
  | WelcomeEvent
918
+ | OpenEvent
853
919
 
854
920
  /** @public */
855
921
  export declare type ListenEventName =
@@ -974,6 +1040,24 @@ export declare type Logger =
974
1040
  Pick<typeof console, 'debug' | 'error' | 'groupCollapsed' | 'groupEnd' | 'log' | 'table'>
975
1041
  >
976
1042
 
1043
+ /**
1044
+ * @public
1045
+ * The server sent an `error`-event to tell the client that an unexpected error has happened.
1046
+ */
1047
+ export declare class MessageError extends Error {
1048
+ readonly name = 'MessageError'
1049
+ readonly data?: unknown
1050
+ constructor(message: string, data: unknown, options?: ErrorOptions)
1051
+ }
1052
+
1053
+ /**
1054
+ * @public
1055
+ * An error occurred while parsing the message sent by the server as JSON. Should normally not happen.
1056
+ */
1057
+ export declare class MessageParseError extends Error {
1058
+ readonly name = 'MessageParseError'
1059
+ }
1060
+
977
1061
  /** @internal */
978
1062
  export declare interface MultipleActionResult {
979
1063
  transactionId: string
@@ -1031,7 +1115,7 @@ export declare interface MutationErrorItem {
1031
1115
  *
1032
1116
  * @public
1033
1117
  */
1034
- declare type MutationEvent_2<R extends Record<string, Any> = Record<string, Any>> = {
1118
+ export declare type MutationEvent<R extends Record<string, Any> = Record<string, Any>> = {
1035
1119
  type: 'mutation'
1036
1120
  /**
1037
1121
  * The ID of the document that was affected
@@ -1120,7 +1204,6 @@ declare type MutationEvent_2<R extends Record<string, Any> = Record<string, Any>
1120
1204
  */
1121
1205
  transactionCurrentEvent: number
1122
1206
  }
1123
- export {MutationEvent_2 as MutationEvent}
1124
1207
 
1125
1208
  /** @internal */
1126
1209
  export declare type MutationOperation = 'create' | 'delete' | 'update' | 'none'
@@ -1886,6 +1969,15 @@ export declare class ObservableUsersClient {
1886
1969
  ): Observable<T extends 'me' ? CurrentSanityUser : SanityUser>
1887
1970
  }
1888
1971
 
1972
+ /**
1973
+ * The listener connection has been established
1974
+ * note: it's usually a better option to use the 'welcome' event
1975
+ * @public
1976
+ */
1977
+ export declare type OpenEvent = {
1978
+ type: 'open'
1979
+ }
1980
+
1889
1981
  /** @public */
1890
1982
  export declare class Patch extends BasePatch {
1891
1983
  #private
@@ -2872,6 +2964,15 @@ export declare class ServerError extends Error {
2872
2964
  constructor(res: Any)
2873
2965
  }
2874
2966
 
2967
+ /**
2968
+ * @public
2969
+ */
2970
+ export declare interface ServerSentEvent<Name extends string> {
2971
+ type: Name
2972
+ id?: string
2973
+ data?: unknown
2974
+ }
2975
+
2875
2976
  /** @internal */
2876
2977
  export declare interface SingleActionResult {
2877
2978
  transactionId: string
@@ -2979,6 +3080,13 @@ export declare class Transaction extends BaseTransaction {
2979
3080
  * @param patchOps - Operations to perform, or a builder function
2980
3081
  */
2981
3082
  patch(documentId: string, patchOps?: PatchBuilder | PatchOperations): this
3083
+ /**
3084
+ * Performs a patch on the given selection. Can either be a builder function or an object of patch operations.
3085
+ *
3086
+ * @param selection - An object with `query` and optional `params`, defining which document(s) to patch
3087
+ * @param patchOps - Operations to perform, or a builder function
3088
+ */
3089
+ patch(patch: MutationSelection, patchOps?: PatchBuilder | PatchOperations): this
2982
3090
  /**
2983
3091
  * Adds the given patch instance to the transaction.
2984
3092
  * The operation is added to the current transaction, ready to be commited by `commit()`
@@ -311,6 +311,16 @@ export declare class BaseTransaction {
311
311
  protected _add(mut: Mutation): this
312
312
  }
313
313
 
314
+ /**
315
+ * @public
316
+ * The server sent a `channelError` message. Usually indicative of a bad or malformed request
317
+ */
318
+ export declare class ChannelError extends Error {
319
+ readonly name = 'ChannelError'
320
+ readonly data?: unknown
321
+ constructor(message: string, data: unknown)
322
+ }
323
+
314
324
  /**
315
325
  * An error occurred. This is different from a network-level error (which will be emitted as 'error').
316
326
  * Possible causes are things such as malformed filters, non-existant datasets or similar.
@@ -403,6 +413,39 @@ export declare type ClientReturn<
403
413
  Fallback = Any,
404
414
  > = GroqString extends keyof SanityQueries ? SanityQueries[GroqString] : Fallback
405
415
 
416
+ /**
417
+ * Sanity API specific EventSource handler shared between the listen and live APIs
418
+ *
419
+ * Since the `EventSource` API is not provided by all environments, this function enables custom initialization of the EventSource instance
420
+ * for runtimes that requires polyfilling or custom setup logic (e.g. custom HTTP headers)
421
+ * via the passed `initEventSource` function which must return an EventSource instance.
422
+ *
423
+ * Possible errors to be thrown on the returned observable are:
424
+ * - {@link MessageError}
425
+ * - {@link MessageParseError}
426
+ * - {@link ChannelError}
427
+ * - {@link DisconnectError}
428
+ * - {@link ConnectionFailedError}
429
+ *
430
+ * @param initEventSource - A function that returns an EventSource instance or an Observable that resolves to an EventSource instance
431
+ * @param events - an array of named events from the API to listen for.
432
+ *
433
+ * @internal
434
+ */
435
+ export declare function connectEventSource<EventName extends string>(
436
+ initEventSource: () => EventSourceInstance | Observable<EventSourceInstance>,
437
+ events: EventName[],
438
+ ): Observable<ServerSentEvent<EventName>>
439
+
440
+ /**
441
+ * @public
442
+ * Thrown if the EventSource connection could not be established.
443
+ * Note that ConnectionFailedErrors are rare, and disconnects will normally be handled by the EventSource instance itself and emitted as `reconnect` events.
444
+ */
445
+ export declare class ConnectionFailedError extends Error {
446
+ readonly name = 'ConnectionFailedError'
447
+ }
448
+
406
449
  /** @public */
407
450
  export declare interface ContentSourceMap {
408
451
  mappings: ContentSourceMapMappings
@@ -642,6 +685,18 @@ export declare type DiscardAction = {
642
685
  purge?: boolean
643
686
  }
644
687
 
688
+ /**
689
+ * The listener has been told to explicitly disconnect.
690
+ * This is a rare situation, but may occur if the API knows reconnect attempts will fail,
691
+ * eg in the case of a deleted dataset, a blocked project or similar events.
692
+ * @public
693
+ */
694
+ export declare class DisconnectError extends Error {
695
+ readonly name = 'DisconnectError'
696
+ readonly reason?: string
697
+ constructor(message: string, reason?: string, options?: ErrorOptions)
698
+ }
699
+
645
700
  /**
646
701
  * The listener has been told to explicitly disconnect and not reconnect.
647
702
  * This is a rare situation, but may occur if the API knows reconnect attempts will fail,
@@ -688,6 +743,16 @@ export declare interface ErrorProps {
688
743
  details: Any
689
744
  }
690
745
 
746
+ /**
747
+ * @internal
748
+ */
749
+ export declare type EventSourceEvent<Name extends string> = ServerSentEvent<Name>
750
+
751
+ /**
752
+ * @internal
753
+ */
754
+ export declare type EventSourceInstance = InstanceType<typeof globalThis.EventSource>
755
+
691
756
  /** @public */
692
757
  export declare type FilterDefault = (props: {
693
758
  /**
@@ -826,7 +891,7 @@ export declare function _listen<R extends Record<string, Any> = Record<string, A
826
891
  this: SanityClient | ObservableSanityClient,
827
892
  query: string,
828
893
  params?: ListenParams,
829
- ): Observable<MutationEvent_2<R>>
894
+ ): Observable<MutationEvent<R>>
830
895
 
831
896
  /**
832
897
  * Set up a listener that will be notified when mutations occur on documents matching the provided query/filter.
@@ -845,11 +910,12 @@ export declare function _listen<R extends Record<string, Any> = Record<string, A
845
910
 
846
911
  /** @public */
847
912
  export declare type ListenEvent<R extends Record<string, Any>> =
848
- | MutationEvent_2<R>
913
+ | MutationEvent<R>
849
914
  | ChannelErrorEvent
850
915
  | DisconnectEvent
851
916
  | ReconnectEvent
852
917
  | WelcomeEvent
918
+ | OpenEvent
853
919
 
854
920
  /** @public */
855
921
  export declare type ListenEventName =
@@ -974,6 +1040,24 @@ export declare type Logger =
974
1040
  Pick<typeof console, 'debug' | 'error' | 'groupCollapsed' | 'groupEnd' | 'log' | 'table'>
975
1041
  >
976
1042
 
1043
+ /**
1044
+ * @public
1045
+ * The server sent an `error`-event to tell the client that an unexpected error has happened.
1046
+ */
1047
+ export declare class MessageError extends Error {
1048
+ readonly name = 'MessageError'
1049
+ readonly data?: unknown
1050
+ constructor(message: string, data: unknown, options?: ErrorOptions)
1051
+ }
1052
+
1053
+ /**
1054
+ * @public
1055
+ * An error occurred while parsing the message sent by the server as JSON. Should normally not happen.
1056
+ */
1057
+ export declare class MessageParseError extends Error {
1058
+ readonly name = 'MessageParseError'
1059
+ }
1060
+
977
1061
  /** @internal */
978
1062
  export declare interface MultipleActionResult {
979
1063
  transactionId: string
@@ -1031,7 +1115,7 @@ export declare interface MutationErrorItem {
1031
1115
  *
1032
1116
  * @public
1033
1117
  */
1034
- declare type MutationEvent_2<R extends Record<string, Any> = Record<string, Any>> = {
1118
+ export declare type MutationEvent<R extends Record<string, Any> = Record<string, Any>> = {
1035
1119
  type: 'mutation'
1036
1120
  /**
1037
1121
  * The ID of the document that was affected
@@ -1120,7 +1204,6 @@ declare type MutationEvent_2<R extends Record<string, Any> = Record<string, Any>
1120
1204
  */
1121
1205
  transactionCurrentEvent: number
1122
1206
  }
1123
- export {MutationEvent_2 as MutationEvent}
1124
1207
 
1125
1208
  /** @internal */
1126
1209
  export declare type MutationOperation = 'create' | 'delete' | 'update' | 'none'
@@ -1886,6 +1969,15 @@ export declare class ObservableUsersClient {
1886
1969
  ): Observable<T extends 'me' ? CurrentSanityUser : SanityUser>
1887
1970
  }
1888
1971
 
1972
+ /**
1973
+ * The listener connection has been established
1974
+ * note: it's usually a better option to use the 'welcome' event
1975
+ * @public
1976
+ */
1977
+ export declare type OpenEvent = {
1978
+ type: 'open'
1979
+ }
1980
+
1889
1981
  /** @public */
1890
1982
  export declare class Patch extends BasePatch {
1891
1983
  #private
@@ -2872,6 +2964,15 @@ export declare class ServerError extends Error {
2872
2964
  constructor(res: Any)
2873
2965
  }
2874
2966
 
2967
+ /**
2968
+ * @public
2969
+ */
2970
+ export declare interface ServerSentEvent<Name extends string> {
2971
+ type: Name
2972
+ id?: string
2973
+ data?: unknown
2974
+ }
2975
+
2875
2976
  /** @internal */
2876
2977
  export declare interface SingleActionResult {
2877
2978
  transactionId: string
@@ -2979,6 +3080,13 @@ export declare class Transaction extends BaseTransaction {
2979
3080
  * @param patchOps - Operations to perform, or a builder function
2980
3081
  */
2981
3082
  patch(documentId: string, patchOps?: PatchBuilder | PatchOperations): this
3083
+ /**
3084
+ * Performs a patch on the given selection. Can either be a builder function or an object of patch operations.
3085
+ *
3086
+ * @param selection - An object with `query` and optional `params`, defining which document(s) to patch
3087
+ * @param patchOps - Operations to perform, or a builder function
3088
+ */
3089
+ patch(patch: MutationSelection, patchOps?: PatchBuilder | PatchOperations): this
2982
3090
  /**
2983
3091
  * Adds the given patch instance to the transaction.
2984
3092
  * The operation is added to the current transaction, ready to be commited by `commit()`