@sanity/client 7.12.0 → 7.13.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.
@@ -2130,22 +2130,41 @@ export declare function _listen<R extends Record<string, Any> = Record<string, A
2130
2130
  * @param options - Optional listener options
2131
2131
  * @public
2132
2132
  */
2133
- export declare function _listen<R extends Record<string, Any> = Record<string, Any>>(
2133
+ export declare function _listen<
2134
+ R extends Record<string, Any> = Record<string, Any>,
2135
+ Opts extends ListenOptions = ListenOptions,
2136
+ >(
2134
2137
  this: SanityClient | ObservableSanityClient,
2135
2138
  query: string,
2136
2139
  params?: ListenParams,
2137
- options?: ListenOptions,
2138
- ): Observable<ListenEvent<R>>
2140
+ options?: Opts,
2141
+ ): Observable<ListenEventFromOptions<R, Opts>>
2139
2142
 
2140
2143
  /** @public */
2141
2144
  export declare type ListenEvent<R extends Record<string, Any>> =
2142
2145
  | MutationEvent<R>
2143
- | ChannelErrorEvent
2144
- | DisconnectEvent
2145
2146
  | ReconnectEvent
2146
2147
  | WelcomeEvent
2147
2148
  | OpenEvent
2148
2149
 
2150
+ /**
2151
+ * Maps a ListenOptions object and returns the Listen events opted for, e.g:
2152
+ * ```
2153
+ * type Test = ListenEventFromOptions<Doc, {events: ['welcome', 'mutation']}>
2154
+ * // ^? WelcomeEvent | MutationEvent<Doc>
2155
+ * ```
2156
+ *
2157
+ * @public
2158
+ */
2159
+ declare type ListenEventFromOptions<
2160
+ R extends Record<string, Any> = Record<string, Any>,
2161
+ Opts extends ListenOptions | undefined = undefined,
2162
+ > = Opts extends ListenOptions
2163
+ ? Opts['events'] extends ListenEventName[]
2164
+ ? MapListenEventNamesToListenEvents<R, Opts['events']>
2165
+ : ListenEvent<R>
2166
+ : MutationEvent<R>
2167
+
2149
2168
  /** @public */
2150
2169
  export declare type ListenEventName =
2151
2170
  /** A mutation was performed */
@@ -2154,6 +2173,11 @@ export declare type ListenEventName =
2154
2173
  | 'welcome'
2155
2174
  /** The listener has been disconnected, and a reconnect attempt is scheduled */
2156
2175
  | 'reconnect'
2176
+ /**
2177
+ * The listener connection has been established
2178
+ * note: it's usually a better option to use the 'welcome' event
2179
+ */
2180
+ | 'open'
2157
2181
 
2158
2182
  /** @public */
2159
2183
  export declare interface ListenOptions {
@@ -2295,6 +2319,30 @@ declare type Logger_2 =
2295
2319
  Pick<typeof console, 'debug' | 'error' | 'groupCollapsed' | 'groupEnd' | 'log' | 'table'>
2296
2320
  >
2297
2321
 
2322
+ /**
2323
+ * Maps an array of listen events names to their corresponding listen event type, e.g:
2324
+ * ```
2325
+ * type Test = MapListenEventNamesToListenEvents<Doc, ['welcome']>
2326
+ * // ^? WelcomeEvent
2327
+ * ```
2328
+ *
2329
+ * @public
2330
+ */
2331
+ declare type MapListenEventNamesToListenEvents<
2332
+ R extends Record<string, Any> = Record<string, Any>,
2333
+ Events extends ListenEventName[] = ListenEventName[],
2334
+ > = Events extends (infer E)[]
2335
+ ? E extends 'welcome'
2336
+ ? WelcomeEvent
2337
+ : E extends 'mutation'
2338
+ ? MutationEvent<R>
2339
+ : E extends 'reconnect'
2340
+ ? ReconnectEvent
2341
+ : E extends 'open'
2342
+ ? OpenEvent
2343
+ : never
2344
+ : never
2345
+
2298
2346
  /** @public */
2299
2347
  export declare type MediaLibraryAssetInstanceIdentifier = string | SanityReference
2300
2348
 
@@ -3453,7 +3501,7 @@ export declare class ObservableSanityClient {
3453
3501
  createVersion(
3454
3502
  args: {
3455
3503
  baseId: string
3456
- releaseId: string
3504
+ releaseId?: string
3457
3505
  publishedId: string
3458
3506
  ifBaseRevisionId?: string
3459
3507
  },
@@ -5278,7 +5326,7 @@ export declare class SanityClient {
5278
5326
  args: {
5279
5327
  publishedId: string
5280
5328
  baseId: string
5281
- releaseId: string
5329
+ releaseId?: string
5282
5330
  ifBaseRevisionId?: string
5283
5331
  },
5284
5332
  options?: BaseActionOptions,
@@ -2130,22 +2130,41 @@ export declare function _listen<R extends Record<string, Any> = Record<string, A
2130
2130
  * @param options - Optional listener options
2131
2131
  * @public
2132
2132
  */
2133
- export declare function _listen<R extends Record<string, Any> = Record<string, Any>>(
2133
+ export declare function _listen<
2134
+ R extends Record<string, Any> = Record<string, Any>,
2135
+ Opts extends ListenOptions = ListenOptions,
2136
+ >(
2134
2137
  this: SanityClient | ObservableSanityClient,
2135
2138
  query: string,
2136
2139
  params?: ListenParams,
2137
- options?: ListenOptions,
2138
- ): Observable<ListenEvent<R>>
2140
+ options?: Opts,
2141
+ ): Observable<ListenEventFromOptions<R, Opts>>
2139
2142
 
2140
2143
  /** @public */
2141
2144
  export declare type ListenEvent<R extends Record<string, Any>> =
2142
2145
  | MutationEvent<R>
2143
- | ChannelErrorEvent
2144
- | DisconnectEvent
2145
2146
  | ReconnectEvent
2146
2147
  | WelcomeEvent
2147
2148
  | OpenEvent
2148
2149
 
2150
+ /**
2151
+ * Maps a ListenOptions object and returns the Listen events opted for, e.g:
2152
+ * ```
2153
+ * type Test = ListenEventFromOptions<Doc, {events: ['welcome', 'mutation']}>
2154
+ * // ^? WelcomeEvent | MutationEvent<Doc>
2155
+ * ```
2156
+ *
2157
+ * @public
2158
+ */
2159
+ declare type ListenEventFromOptions<
2160
+ R extends Record<string, Any> = Record<string, Any>,
2161
+ Opts extends ListenOptions | undefined = undefined,
2162
+ > = Opts extends ListenOptions
2163
+ ? Opts['events'] extends ListenEventName[]
2164
+ ? MapListenEventNamesToListenEvents<R, Opts['events']>
2165
+ : ListenEvent<R>
2166
+ : MutationEvent<R>
2167
+
2149
2168
  /** @public */
2150
2169
  export declare type ListenEventName =
2151
2170
  /** A mutation was performed */
@@ -2154,6 +2173,11 @@ export declare type ListenEventName =
2154
2173
  | 'welcome'
2155
2174
  /** The listener has been disconnected, and a reconnect attempt is scheduled */
2156
2175
  | 'reconnect'
2176
+ /**
2177
+ * The listener connection has been established
2178
+ * note: it's usually a better option to use the 'welcome' event
2179
+ */
2180
+ | 'open'
2157
2181
 
2158
2182
  /** @public */
2159
2183
  export declare interface ListenOptions {
@@ -2295,6 +2319,30 @@ declare type Logger_2 =
2295
2319
  Pick<typeof console, 'debug' | 'error' | 'groupCollapsed' | 'groupEnd' | 'log' | 'table'>
2296
2320
  >
2297
2321
 
2322
+ /**
2323
+ * Maps an array of listen events names to their corresponding listen event type, e.g:
2324
+ * ```
2325
+ * type Test = MapListenEventNamesToListenEvents<Doc, ['welcome']>
2326
+ * // ^? WelcomeEvent
2327
+ * ```
2328
+ *
2329
+ * @public
2330
+ */
2331
+ declare type MapListenEventNamesToListenEvents<
2332
+ R extends Record<string, Any> = Record<string, Any>,
2333
+ Events extends ListenEventName[] = ListenEventName[],
2334
+ > = Events extends (infer E)[]
2335
+ ? E extends 'welcome'
2336
+ ? WelcomeEvent
2337
+ : E extends 'mutation'
2338
+ ? MutationEvent<R>
2339
+ : E extends 'reconnect'
2340
+ ? ReconnectEvent
2341
+ : E extends 'open'
2342
+ ? OpenEvent
2343
+ : never
2344
+ : never
2345
+
2298
2346
  /** @public */
2299
2347
  export declare type MediaLibraryAssetInstanceIdentifier = string | SanityReference
2300
2348
 
@@ -3453,7 +3501,7 @@ export declare class ObservableSanityClient {
3453
3501
  createVersion(
3454
3502
  args: {
3455
3503
  baseId: string
3456
- releaseId: string
3504
+ releaseId?: string
3457
3505
  publishedId: string
3458
3506
  ifBaseRevisionId?: string
3459
3507
  },
@@ -5278,7 +5326,7 @@ export declare class SanityClient {
5278
5326
  args: {
5279
5327
  publishedId: string
5280
5328
  baseId: string
5281
- releaseId: string
5329
+ releaseId?: string
5282
5330
  ifBaseRevisionId?: string
5283
5331
  },
5284
5332
  options?: BaseActionOptions,
package/dist/stega.d.cts CHANGED
@@ -2130,22 +2130,41 @@ export declare function _listen<R extends Record<string, Any> = Record<string, A
2130
2130
  * @param options - Optional listener options
2131
2131
  * @public
2132
2132
  */
2133
- export declare function _listen<R extends Record<string, Any> = Record<string, Any>>(
2133
+ export declare function _listen<
2134
+ R extends Record<string, Any> = Record<string, Any>,
2135
+ Opts extends ListenOptions = ListenOptions,
2136
+ >(
2134
2137
  this: SanityClient | ObservableSanityClient,
2135
2138
  query: string,
2136
2139
  params?: ListenParams,
2137
- options?: ListenOptions,
2138
- ): Observable<ListenEvent<R>>
2140
+ options?: Opts,
2141
+ ): Observable<ListenEventFromOptions<R, Opts>>
2139
2142
 
2140
2143
  /** @public */
2141
2144
  export declare type ListenEvent<R extends Record<string, Any>> =
2142
2145
  | MutationEvent<R>
2143
- | ChannelErrorEvent
2144
- | DisconnectEvent
2145
2146
  | ReconnectEvent
2146
2147
  | WelcomeEvent
2147
2148
  | OpenEvent
2148
2149
 
2150
+ /**
2151
+ * Maps a ListenOptions object and returns the Listen events opted for, e.g:
2152
+ * ```
2153
+ * type Test = ListenEventFromOptions<Doc, {events: ['welcome', 'mutation']}>
2154
+ * // ^? WelcomeEvent | MutationEvent<Doc>
2155
+ * ```
2156
+ *
2157
+ * @public
2158
+ */
2159
+ declare type ListenEventFromOptions<
2160
+ R extends Record<string, Any> = Record<string, Any>,
2161
+ Opts extends ListenOptions | undefined = undefined,
2162
+ > = Opts extends ListenOptions
2163
+ ? Opts['events'] extends ListenEventName[]
2164
+ ? MapListenEventNamesToListenEvents<R, Opts['events']>
2165
+ : ListenEvent<R>
2166
+ : MutationEvent<R>
2167
+
2149
2168
  /** @public */
2150
2169
  export declare type ListenEventName =
2151
2170
  /** A mutation was performed */
@@ -2154,6 +2173,11 @@ export declare type ListenEventName =
2154
2173
  | 'welcome'
2155
2174
  /** The listener has been disconnected, and a reconnect attempt is scheduled */
2156
2175
  | 'reconnect'
2176
+ /**
2177
+ * The listener connection has been established
2178
+ * note: it's usually a better option to use the 'welcome' event
2179
+ */
2180
+ | 'open'
2157
2181
 
2158
2182
  /** @public */
2159
2183
  export declare interface ListenOptions {
@@ -2295,6 +2319,30 @@ declare type Logger_2 =
2295
2319
  Pick<typeof console, 'debug' | 'error' | 'groupCollapsed' | 'groupEnd' | 'log' | 'table'>
2296
2320
  >
2297
2321
 
2322
+ /**
2323
+ * Maps an array of listen events names to their corresponding listen event type, e.g:
2324
+ * ```
2325
+ * type Test = MapListenEventNamesToListenEvents<Doc, ['welcome']>
2326
+ * // ^? WelcomeEvent
2327
+ * ```
2328
+ *
2329
+ * @public
2330
+ */
2331
+ declare type MapListenEventNamesToListenEvents<
2332
+ R extends Record<string, Any> = Record<string, Any>,
2333
+ Events extends ListenEventName[] = ListenEventName[],
2334
+ > = Events extends (infer E)[]
2335
+ ? E extends 'welcome'
2336
+ ? WelcomeEvent
2337
+ : E extends 'mutation'
2338
+ ? MutationEvent<R>
2339
+ : E extends 'reconnect'
2340
+ ? ReconnectEvent
2341
+ : E extends 'open'
2342
+ ? OpenEvent
2343
+ : never
2344
+ : never
2345
+
2298
2346
  /** @public */
2299
2347
  export declare type MediaLibraryAssetInstanceIdentifier = string | SanityReference
2300
2348
 
@@ -3453,7 +3501,7 @@ export declare class ObservableSanityClient {
3453
3501
  createVersion(
3454
3502
  args: {
3455
3503
  baseId: string
3456
- releaseId: string
3504
+ releaseId?: string
3457
3505
  publishedId: string
3458
3506
  ifBaseRevisionId?: string
3459
3507
  },
@@ -5278,7 +5326,7 @@ export declare class SanityClient {
5278
5326
  args: {
5279
5327
  publishedId: string
5280
5328
  baseId: string
5281
- releaseId: string
5329
+ releaseId?: string
5282
5330
  ifBaseRevisionId?: string
5283
5331
  },
5284
5332
  options?: BaseActionOptions,
package/dist/stega.d.ts CHANGED
@@ -2130,22 +2130,41 @@ export declare function _listen<R extends Record<string, Any> = Record<string, A
2130
2130
  * @param options - Optional listener options
2131
2131
  * @public
2132
2132
  */
2133
- export declare function _listen<R extends Record<string, Any> = Record<string, Any>>(
2133
+ export declare function _listen<
2134
+ R extends Record<string, Any> = Record<string, Any>,
2135
+ Opts extends ListenOptions = ListenOptions,
2136
+ >(
2134
2137
  this: SanityClient | ObservableSanityClient,
2135
2138
  query: string,
2136
2139
  params?: ListenParams,
2137
- options?: ListenOptions,
2138
- ): Observable<ListenEvent<R>>
2140
+ options?: Opts,
2141
+ ): Observable<ListenEventFromOptions<R, Opts>>
2139
2142
 
2140
2143
  /** @public */
2141
2144
  export declare type ListenEvent<R extends Record<string, Any>> =
2142
2145
  | MutationEvent<R>
2143
- | ChannelErrorEvent
2144
- | DisconnectEvent
2145
2146
  | ReconnectEvent
2146
2147
  | WelcomeEvent
2147
2148
  | OpenEvent
2148
2149
 
2150
+ /**
2151
+ * Maps a ListenOptions object and returns the Listen events opted for, e.g:
2152
+ * ```
2153
+ * type Test = ListenEventFromOptions<Doc, {events: ['welcome', 'mutation']}>
2154
+ * // ^? WelcomeEvent | MutationEvent<Doc>
2155
+ * ```
2156
+ *
2157
+ * @public
2158
+ */
2159
+ declare type ListenEventFromOptions<
2160
+ R extends Record<string, Any> = Record<string, Any>,
2161
+ Opts extends ListenOptions | undefined = undefined,
2162
+ > = Opts extends ListenOptions
2163
+ ? Opts['events'] extends ListenEventName[]
2164
+ ? MapListenEventNamesToListenEvents<R, Opts['events']>
2165
+ : ListenEvent<R>
2166
+ : MutationEvent<R>
2167
+
2149
2168
  /** @public */
2150
2169
  export declare type ListenEventName =
2151
2170
  /** A mutation was performed */
@@ -2154,6 +2173,11 @@ export declare type ListenEventName =
2154
2173
  | 'welcome'
2155
2174
  /** The listener has been disconnected, and a reconnect attempt is scheduled */
2156
2175
  | 'reconnect'
2176
+ /**
2177
+ * The listener connection has been established
2178
+ * note: it's usually a better option to use the 'welcome' event
2179
+ */
2180
+ | 'open'
2157
2181
 
2158
2182
  /** @public */
2159
2183
  export declare interface ListenOptions {
@@ -2295,6 +2319,30 @@ declare type Logger_2 =
2295
2319
  Pick<typeof console, 'debug' | 'error' | 'groupCollapsed' | 'groupEnd' | 'log' | 'table'>
2296
2320
  >
2297
2321
 
2322
+ /**
2323
+ * Maps an array of listen events names to their corresponding listen event type, e.g:
2324
+ * ```
2325
+ * type Test = MapListenEventNamesToListenEvents<Doc, ['welcome']>
2326
+ * // ^? WelcomeEvent
2327
+ * ```
2328
+ *
2329
+ * @public
2330
+ */
2331
+ declare type MapListenEventNamesToListenEvents<
2332
+ R extends Record<string, Any> = Record<string, Any>,
2333
+ Events extends ListenEventName[] = ListenEventName[],
2334
+ > = Events extends (infer E)[]
2335
+ ? E extends 'welcome'
2336
+ ? WelcomeEvent
2337
+ : E extends 'mutation'
2338
+ ? MutationEvent<R>
2339
+ : E extends 'reconnect'
2340
+ ? ReconnectEvent
2341
+ : E extends 'open'
2342
+ ? OpenEvent
2343
+ : never
2344
+ : never
2345
+
2298
2346
  /** @public */
2299
2347
  export declare type MediaLibraryAssetInstanceIdentifier = string | SanityReference
2300
2348
 
@@ -3453,7 +3501,7 @@ export declare class ObservableSanityClient {
3453
3501
  createVersion(
3454
3502
  args: {
3455
3503
  baseId: string
3456
- releaseId: string
3504
+ releaseId?: string
3457
3505
  publishedId: string
3458
3506
  ifBaseRevisionId?: string
3459
3507
  },
@@ -5278,7 +5326,7 @@ export declare class SanityClient {
5278
5326
  args: {
5279
5327
  publishedId: string
5280
5328
  baseId: string
5281
- releaseId: string
5329
+ releaseId?: string
5282
5330
  ifBaseRevisionId?: string
5283
5331
  },
5284
5332
  options?: BaseActionOptions,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@sanity/client",
3
- "version": "7.12.0",
3
+ "version": "7.13.0",
4
4
  "description": "Client for retrieving, creating and patching data from Sanity.io",
5
5
  "keywords": [
6
6
  "sanity",
@@ -18,7 +18,7 @@
18
18
  },
19
19
  "repository": {
20
20
  "type": "git",
21
- "url": "git+ssh://git@github.com/sanity-io/client.git"
21
+ "url": "git+https://github.com/sanity-io/client.git"
22
22
  },
23
23
  "license": "MIT",
24
24
  "author": "Sanity.io <hello@sanity.io>",
@@ -116,7 +116,7 @@
116
116
  "test:deno:update_import_map": "deno run --allow-read --allow-write runtimes/deno/update_import_map.ts",
117
117
  "test:edge-runtime": "npm test -- --config vitest.edge.config.ts",
118
118
  "test:next": "npm test -- --config ./vitest.next.config.ts",
119
- "test:node-runtimes": "(cd runtimes/node && node --test | npx faucet)"
119
+ "test:node-runtimes": "(cd runtimes/node && node --test)"
120
120
  },
121
121
  "browserslist": "extends @sanity/browserslist-config",
122
122
  "prettier": {
@@ -156,7 +156,6 @@
156
156
  "eslint-plugin-simple-import-sort": "^12.1.1",
157
157
  "eslint-plugin-tsdoc": "^0.4.0",
158
158
  "eslint-plugin-unused-imports": "^4.1.4",
159
- "faucet": "^0.0.4",
160
159
  "globals": "^16.0.0",
161
160
  "happy-dom": "^12.10.3",
162
161
  "json-diff": "^1.0.6",
@@ -164,6 +163,7 @@
164
163
  "msw": "^2.7.3",
165
164
  "next": "^15.3.0",
166
165
  "nock": "^13.5.6",
166
+ "pkg-pr-new": "^0.0.60",
167
167
  "prettier": "^3.5.3",
168
168
  "prettier-plugin-packagejson": "^2.5.10",
169
169
  "rimraf": "^5.0.7",
@@ -601,7 +601,7 @@ export class ObservableSanityClient {
601
601
  createVersion(
602
602
  args: {
603
603
  baseId: string
604
- releaseId: string
604
+ releaseId?: string
605
605
  publishedId: string
606
606
  ifBaseRevisionId?: string
607
607
  },
@@ -1647,7 +1647,7 @@ export class SanityClient {
1647
1647
  args: {
1648
1648
  publishedId: string
1649
1649
  baseId: string
1650
- releaseId: string
1650
+ releaseId?: string
1651
1651
  ifBaseRevisionId?: string
1652
1652
  },
1653
1653
  options?: BaseActionOptions,
@@ -5,9 +5,13 @@ import type {ObservableSanityClient, SanityClient} from '../SanityClient'
5
5
  import {
6
6
  type Any,
7
7
  type ListenEvent,
8
+ type ListenEventName,
8
9
  type ListenOptions,
9
10
  type ListenParams,
10
11
  type MutationEvent,
12
+ type OpenEvent,
13
+ type ReconnectEvent,
14
+ type WelcomeEvent,
11
15
  } from '../types'
12
16
  import defaults from '../util/defaults'
13
17
  import {pick} from '../util/pick'
@@ -36,6 +40,49 @@ const defaultOptions = {
36
40
  includeResult: true,
37
41
  }
38
42
 
43
+ /**
44
+ * Maps an array of listen events names to their corresponding listen event type, e.g:
45
+ * ```
46
+ * type Test = MapListenEventNamesToListenEvents<Doc, ['welcome']>
47
+ * // ^? WelcomeEvent
48
+ * ```
49
+ *
50
+ * @public
51
+ */
52
+ export type MapListenEventNamesToListenEvents<
53
+ R extends Record<string, Any> = Record<string, Any>,
54
+ Events extends ListenEventName[] = ListenEventName[],
55
+ > = Events extends (infer E)[]
56
+ ? E extends 'welcome'
57
+ ? WelcomeEvent
58
+ : E extends 'mutation'
59
+ ? MutationEvent<R>
60
+ : E extends 'reconnect'
61
+ ? ReconnectEvent
62
+ : E extends 'open'
63
+ ? OpenEvent
64
+ : never
65
+ : never
66
+
67
+ /**
68
+ * Maps a ListenOptions object and returns the Listen events opted for, e.g:
69
+ * ```
70
+ * type Test = ListenEventFromOptions<Doc, {events: ['welcome', 'mutation']}>
71
+ * // ^? WelcomeEvent | MutationEvent<Doc>
72
+ * ```
73
+ *
74
+ * @public
75
+ */
76
+ export type ListenEventFromOptions<
77
+ R extends Record<string, Any> = Record<string, Any>,
78
+ Opts extends ListenOptions | undefined = undefined,
79
+ > = Opts extends ListenOptions
80
+ ? Opts['events'] extends ListenEventName[]
81
+ ? MapListenEventNamesToListenEvents<R, Opts['events']>
82
+ : // fall back to ListenEvent if opts events is present, but we can't infer the literal event names
83
+ ListenEvent<R>
84
+ : MutationEvent<R>
85
+
39
86
  /**
40
87
  * Set up a listener that will be notified when mutations occur on documents matching the provided query/filter.
41
88
  *
@@ -57,19 +104,25 @@ export function _listen<R extends Record<string, Any> = Record<string, Any>>(
57
104
  * @param options - Optional listener options
58
105
  * @public
59
106
  */
60
- export function _listen<R extends Record<string, Any> = Record<string, Any>>(
107
+ export function _listen<
108
+ R extends Record<string, Any> = Record<string, Any>,
109
+ Opts extends ListenOptions = ListenOptions,
110
+ >(
61
111
  this: SanityClient | ObservableSanityClient,
62
112
  query: string,
63
113
  params?: ListenParams,
64
- options?: ListenOptions,
65
- ): Observable<ListenEvent<R>>
114
+ options?: Opts,
115
+ ): Observable<ListenEventFromOptions<R, Opts>>
66
116
  /** @public */
67
- export function _listen<R extends Record<string, Any> = Record<string, Any>>(
117
+ export function _listen<
118
+ R extends Record<string, Any> = Record<string, Any>,
119
+ Opts extends ListenOptions = ListenOptions,
120
+ >(
68
121
  this: SanityClient | ObservableSanityClient,
69
122
  query: string,
70
123
  params?: ListenParams,
71
- opts: ListenOptions = {},
72
- ): Observable<MutationEvent<R> | ListenEvent<R>> {
124
+ opts: Opts = {} as Opts,
125
+ ): Observable<ListenEventFromOptions<R, Opts>> {
73
126
  const {url, token, withCredentials, requestTagPrefix, headers: configHeaders} = this.config()
74
127
  const tag = opts.tag && requestTagPrefix ? [requestTagPrefix, opts.tag].join('.') : opts.tag
75
128
  const options = {...defaults(opts, defaultOptions), tag}
@@ -81,7 +134,7 @@ export function _listen<R extends Record<string, Any> = Record<string, Any>>(
81
134
  return throwError(() => new Error('Query too large for listener'))
82
135
  }
83
136
 
84
- const listenFor = options.events ? options.events : ['mutation']
137
+ const listenFor = (options.events ? options.events : ['mutation']) satisfies Opts['events']
85
138
 
86
139
  const esOptions: EventSourceInit & {headers?: Record<string, string>} = {}
87
140
  if (withCredentials) {
@@ -110,12 +163,9 @@ export function _listen<R extends Record<string, Any> = Record<string, Any>>(
110
163
  return connectEventSource(initEventSource, listenFor).pipe(
111
164
  reconnectOnConnectionFailure(),
112
165
  filter((event) => listenFor.includes(event.type)),
113
- map(
114
- (event) =>
115
- ({
116
- type: event.type,
117
- ...('data' in event ? (event.data as object) : {}),
118
- }) as MutationEvent<R> | ListenEvent<R>,
119
- ),
120
- )
166
+ map((event) => ({
167
+ type: event.type,
168
+ ...('data' in event ? (event.data as object) : {}),
169
+ })),
170
+ ) as Observable<ListenEventFromOptions<R, Opts>>
121
171
  }
package/src/types.ts CHANGED
@@ -1142,8 +1142,6 @@ export type WelcomeEvent = {
1142
1142
  /** @public */
1143
1143
  export type ListenEvent<R extends Record<string, Any>> =
1144
1144
  | MutationEvent<R>
1145
- | ChannelErrorEvent
1146
- | DisconnectEvent
1147
1145
  | ReconnectEvent
1148
1146
  | WelcomeEvent
1149
1147
  | OpenEvent
@@ -1156,6 +1154,11 @@ export type ListenEventName =
1156
1154
  | 'welcome'
1157
1155
  /** The listener has been disconnected, and a reconnect attempt is scheduled */
1158
1156
  | 'reconnect'
1157
+ /**
1158
+ * The listener connection has been established
1159
+ * note: it's usually a better option to use the 'welcome' event
1160
+ */
1161
+ | 'open'
1159
1162
 
1160
1163
  /** @public */
1161
1164
  export type ListenParams = {[key: string]: Any}
@@ -3360,12 +3360,10 @@ ${selectionOpts}`);
3360
3360
  ), listenFor).pipe(
3361
3361
  reconnectOnConnectionFailure(),
3362
3362
  filter((event) => listenFor.includes(event.type)),
3363
- map(
3364
- (event) => ({
3365
- type: event.type,
3366
- ..."data" in event ? event.data : {}
3367
- })
3368
- )
3363
+ map((event) => ({
3364
+ type: event.type,
3365
+ ..."data" in event ? event.data : {}
3366
+ }))
3369
3367
  );
3370
3368
  }
3371
3369
  function shareReplayLatest(configOrPredicate, config) {