@sanity/client 7.14.1 → 7.16.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.
@@ -11,6 +11,10 @@ import {
11
11
  type MutationEvent,
12
12
  type OpenEvent,
13
13
  type ReconnectEvent,
14
+ type ResetEvent,
15
+ type ResumableListenEventNames,
16
+ type ResumableListenOptions,
17
+ type WelcomeBackEvent,
14
18
  type WelcomeEvent,
15
19
  } from '../types'
16
20
  import defaults from '../util/defaults'
@@ -33,6 +37,7 @@ const possibleOptions = [
33
37
  'includeAllVersions',
34
38
  'visibility',
35
39
  'effectFormat',
40
+ 'enableResume',
36
41
  'tag',
37
42
  ]
38
43
 
@@ -51,7 +56,10 @@ const defaultOptions = {
51
56
  */
52
57
  export type MapListenEventNamesToListenEvents<
53
58
  R extends Record<string, Any> = Record<string, Any>,
54
- Events extends ListenEventName[] = ListenEventName[],
59
+ Events extends (ResumableListenEventNames | ListenEventName)[] = (
60
+ | ResumableListenEventNames
61
+ | ListenEventName
62
+ )[],
55
63
  > = Events extends (infer E)[]
56
64
  ? E extends 'welcome'
57
65
  ? WelcomeEvent
@@ -59,9 +67,13 @@ export type MapListenEventNamesToListenEvents<
59
67
  ? MutationEvent<R>
60
68
  : E extends 'reconnect'
61
69
  ? ReconnectEvent
62
- : E extends 'open'
63
- ? OpenEvent
64
- : never
70
+ : E extends 'welcomeback'
71
+ ? WelcomeBackEvent
72
+ : E extends 'reset'
73
+ ? ResetEvent
74
+ : E extends 'open'
75
+ ? OpenEvent
76
+ : never
65
77
  : never
66
78
 
67
79
  /**
@@ -75,9 +87,9 @@ export type MapListenEventNamesToListenEvents<
75
87
  */
76
88
  export type ListenEventFromOptions<
77
89
  R extends Record<string, Any> = Record<string, Any>,
78
- Opts extends ListenOptions | undefined = undefined,
79
- > = Opts extends ListenOptions
80
- ? Opts['events'] extends ListenEventName[]
90
+ Opts extends ListenOptions | ResumableListenOptions | undefined = undefined,
91
+ > = Opts extends ListenOptions | ResumableListenOptions
92
+ ? Opts['events'] extends (ResumableListenEventNames | ListenEventName)[]
81
93
  ? MapListenEventNamesToListenEvents<R, Opts['events']>
82
94
  : // fall back to ListenEvent if opts events is present, but we can't infer the literal event names
83
95
  ListenEvent<R>
@@ -106,7 +118,7 @@ export function _listen<R extends Record<string, Any> = Record<string, Any>>(
106
118
  */
107
119
  export function _listen<
108
120
  R extends Record<string, Any> = Record<string, Any>,
109
- Opts extends ListenOptions = ListenOptions,
121
+ Opts extends ListenOptions | ResumableListenOptions = ListenOptions | ResumableListenOptions,
110
122
  >(
111
123
  this: SanityClient | ObservableSanityClient,
112
124
  query: string,
@@ -116,7 +128,7 @@ export function _listen<
116
128
  /** @public */
117
129
  export function _listen<
118
130
  R extends Record<string, Any> = Record<string, Any>,
119
- Opts extends ListenOptions = ListenOptions,
131
+ Opts extends ListenOptions | ResumableListenOptions = ListenOptions | ResumableListenOptions,
120
132
  >(
121
133
  this: SanityClient | ObservableSanityClient,
122
134
  query: string,
@@ -2,7 +2,15 @@ import {lastValueFrom, type Observable} from 'rxjs'
2
2
 
3
3
  import {_request} from '../data/dataMethods'
4
4
  import type {ObservableSanityClient, SanityClient} from '../SanityClient'
5
- import type {DatasetAclMode, DatasetResponse, DatasetsResponse, HttpRequest} from '../types'
5
+ import type {
6
+ DatasetCreateOptions,
7
+ DatasetEditOptions,
8
+ DatasetResponse,
9
+ DatasetsResponse,
10
+ EmbeddingsSettings,
11
+ EmbeddingsSettingsBody,
12
+ HttpRequest,
13
+ } from '../types'
6
14
  import * as validate from '../validators'
7
15
 
8
16
  /** @internal */
@@ -18,9 +26,9 @@ export class ObservableDatasetsClient {
18
26
  * Create a new dataset with the given name
19
27
  *
20
28
  * @param name - Name of the dataset to create
21
- * @param options - Options for the dataset
29
+ * @param options - Options for the dataset, including optional embeddings configuration
22
30
  */
23
- create(name: string, options?: {aclMode?: DatasetAclMode}): Observable<DatasetResponse> {
31
+ create(name: string, options?: DatasetCreateOptions): Observable<DatasetResponse> {
24
32
  return _modify<DatasetResponse>(this.#client, this.#httpRequest, 'PUT', name, options)
25
33
  }
26
34
 
@@ -30,7 +38,7 @@ export class ObservableDatasetsClient {
30
38
  * @param name - Name of the dataset to edit
31
39
  * @param options - New options for the dataset
32
40
  */
33
- edit(name: string, options?: {aclMode?: DatasetAclMode}): Observable<DatasetResponse> {
41
+ edit(name: string, options?: DatasetEditOptions): Observable<DatasetResponse> {
34
42
  return _modify<DatasetResponse>(this.#client, this.#httpRequest, 'PATCH', name, options)
35
43
  }
36
44
 
@@ -60,6 +68,37 @@ export class ObservableDatasetsClient {
60
68
  tag: null,
61
69
  })
62
70
  }
71
+
72
+ /**
73
+ * Get embeddings settings for a dataset
74
+ *
75
+ * @param name - Name of the dataset
76
+ */
77
+ getEmbeddingsSettings(name: string): Observable<EmbeddingsSettings> {
78
+ validate.resourceGuard('dataset', this.#client.config())
79
+ validate.dataset(name)
80
+ return _request<EmbeddingsSettings>(this.#client, this.#httpRequest, {
81
+ uri: _embeddingsSettingsUri(this.#client, name),
82
+ tag: null,
83
+ })
84
+ }
85
+
86
+ /**
87
+ * Edit embeddings settings for a dataset
88
+ *
89
+ * @param name - Name of the dataset
90
+ * @param settings - Embeddings settings to apply
91
+ */
92
+ editEmbeddingsSettings(name: string, settings: EmbeddingsSettingsBody): Observable<void> {
93
+ validate.resourceGuard('dataset', this.#client.config())
94
+ validate.dataset(name)
95
+ return _request<void>(this.#client, this.#httpRequest, {
96
+ method: 'PUT',
97
+ uri: _embeddingsSettingsUri(this.#client, name),
98
+ body: settings,
99
+ tag: null,
100
+ })
101
+ }
63
102
  }
64
103
 
65
104
  /** @internal */
@@ -75,9 +114,9 @@ export class DatasetsClient {
75
114
  * Create a new dataset with the given name
76
115
  *
77
116
  * @param name - Name of the dataset to create
78
- * @param options - Options for the dataset
117
+ * @param options - Options for the dataset, including optional embeddings configuration
79
118
  */
80
- create(name: string, options?: {aclMode?: DatasetAclMode}): Promise<DatasetResponse> {
119
+ create(name: string, options?: DatasetCreateOptions): Promise<DatasetResponse> {
81
120
  validate.resourceGuard('dataset', this.#client.config())
82
121
  return lastValueFrom(
83
122
  _modify<DatasetResponse>(this.#client, this.#httpRequest, 'PUT', name, options),
@@ -90,7 +129,7 @@ export class DatasetsClient {
90
129
  * @param name - Name of the dataset to edit
91
130
  * @param options - New options for the dataset
92
131
  */
93
- edit(name: string, options?: {aclMode?: DatasetAclMode}): Promise<DatasetResponse> {
132
+ edit(name: string, options?: DatasetEditOptions): Promise<DatasetResponse> {
94
133
  validate.resourceGuard('dataset', this.#client.config())
95
134
  return lastValueFrom(
96
135
  _modify<DatasetResponse>(this.#client, this.#httpRequest, 'PATCH', name, options),
@@ -123,6 +162,52 @@ export class DatasetsClient {
123
162
  _request<DatasetsResponse>(this.#client, this.#httpRequest, {uri, tag: null}),
124
163
  )
125
164
  }
165
+
166
+ /**
167
+ * Get embeddings settings for a dataset
168
+ *
169
+ * @param name - Name of the dataset
170
+ */
171
+ getEmbeddingsSettings(name: string): Promise<EmbeddingsSettings> {
172
+ validate.resourceGuard('dataset', this.#client.config())
173
+ validate.dataset(name)
174
+ return lastValueFrom(
175
+ _request<EmbeddingsSettings>(this.#client, this.#httpRequest, {
176
+ uri: _embeddingsSettingsUri(this.#client, name),
177
+ tag: null,
178
+ }),
179
+ )
180
+ }
181
+
182
+ /**
183
+ * Edit embeddings settings for a dataset
184
+ *
185
+ * @param name - Name of the dataset
186
+ * @param settings - Embeddings settings to apply
187
+ */
188
+ editEmbeddingsSettings(name: string, settings: EmbeddingsSettingsBody): Promise<void> {
189
+ validate.resourceGuard('dataset', this.#client.config())
190
+ validate.dataset(name)
191
+ return lastValueFrom(
192
+ _request<void>(this.#client, this.#httpRequest, {
193
+ method: 'PUT',
194
+ uri: _embeddingsSettingsUri(this.#client, name),
195
+ body: settings,
196
+ tag: null,
197
+ }),
198
+ )
199
+ }
200
+ }
201
+
202
+ function _embeddingsSettingsUri(
203
+ client: SanityClient | ObservableSanityClient,
204
+ name: string,
205
+ ): string {
206
+ const config = client.config()
207
+ if (config.useProjectHostname === false) {
208
+ return `/projects/${config.projectId}/datasets/${name}/settings/embeddings`
209
+ }
210
+ return `/datasets/${name}/settings/embeddings`
126
211
  }
127
212
 
128
213
  function _modify<R = unknown>(
@@ -130,7 +215,7 @@ function _modify<R = unknown>(
130
215
  httpRequest: HttpRequest,
131
216
  method: 'DELETE' | 'PATCH' | 'PUT',
132
217
  name: string,
133
- options?: {aclMode?: DatasetAclMode},
218
+ options?: DatasetCreateOptions | DatasetEditOptions,
134
219
  ) {
135
220
  validate.resourceGuard('dataset', client.config())
136
221
  validate.dataset(name)
package/src/types.ts CHANGED
@@ -463,6 +463,33 @@ export type AuthProviderResponse = {providers: AuthProvider[]}
463
463
  /** @public */
464
464
  export type DatasetAclMode = 'public' | 'private' | 'custom'
465
465
 
466
+ /** @public */
467
+ export type DatasetCreateOptions = {
468
+ aclMode?: DatasetAclMode
469
+ embeddings?: {
470
+ enabled: boolean
471
+ projection?: string
472
+ }
473
+ }
474
+
475
+ /** @public */
476
+ export type DatasetEditOptions = {
477
+ aclMode?: DatasetAclMode
478
+ }
479
+
480
+ /** @public */
481
+ export type EmbeddingsSettings = {
482
+ enabled: boolean
483
+ projection?: string
484
+ status: string
485
+ }
486
+
487
+ /** @public */
488
+ export type EmbeddingsSettingsBody = {
489
+ enabled: boolean
490
+ projection?: string
491
+ }
492
+
466
493
  /** @public */
467
494
  export type DatasetResponse = {datasetName: string; aclMode: DatasetAclMode}
468
495
  /** @public */
@@ -1151,8 +1178,14 @@ export type OpenEvent = {
1151
1178
  }
1152
1179
 
1153
1180
  /**
1154
- * The listener has been established, and will start receiving events.
1155
- * Note that this is also emitted upon _reconnection_.
1181
+ * Emitted when the listener connection has been successfully established
1182
+ * and is ready to receive events.
1183
+ *
1184
+ * If the listener was created with `enableResume: true` and resume support
1185
+ * is available, the `welcome` event will only be emitted on the initial
1186
+ * connection. On subsequent reconnects, a `welcomeback` event will be
1187
+ * emitted instead, followed by any events that were missed while the
1188
+ * connection was disconnected.
1156
1189
  *
1157
1190
  * @public
1158
1191
  */
@@ -1161,10 +1194,44 @@ export type WelcomeEvent = {
1161
1194
  listenerName: string
1162
1195
  }
1163
1196
 
1197
+ /**
1198
+ * Emitted when the listener reconnects and successfully resumes from
1199
+ * its previous position.
1200
+ *
1201
+ * Even if the listener is created with `enableResume: true`, resume support
1202
+ * may not be available. In that case, a reconnect will emit `welcome`
1203
+ * instead of `welcomeback`.
1204
+ *
1205
+ * If resumability is unavailable, even listeners created with `enableResume: true` may still
1206
+ * emit `welcome` when reconnected. Subscribers should therefore treat `welcome` after a reconnect
1207
+ * the same way they would otherwise treat a `reset` event.
1208
+ *
1209
+ * @public
1210
+ */
1211
+ export type WelcomeBackEvent = {
1212
+ type: 'welcomeback'
1213
+ listenerName: string
1214
+ }
1215
+
1216
+ /**
1217
+ * The listener can't be resumed or otherwise need to reset its local state
1218
+ *
1219
+ * If resumability is unavailable, even listeners created with `enableResume: true` may still
1220
+ * emit `welcome` when reconnected. Subscribers should therefore treat `welcome` after a reconnect
1221
+ * the same way they would otherwise treat a `reset` event.
1222
+ *
1223
+ * @public
1224
+ */
1225
+ export type ResetEvent = {
1226
+ type: 'reset'
1227
+ }
1228
+
1164
1229
  /** @public */
1165
- export type ListenEvent<R extends Record<string, Any>> =
1230
+ export type ListenEvent<R extends Record<string, Any> = Record<string, Any>> =
1166
1231
  | MutationEvent<R>
1167
1232
  | ReconnectEvent
1233
+ | WelcomeBackEvent
1234
+ | ResetEvent
1168
1235
  | WelcomeEvent
1169
1236
  | OpenEvent
1170
1237
 
@@ -1182,6 +1249,14 @@ export type ListenEventName =
1182
1249
  */
1183
1250
  | 'open'
1184
1251
 
1252
+ /** @public */
1253
+ export type ResumableListenEventNames =
1254
+ | ListenEventName
1255
+ /** The listener has reconnected and successfully resumed from where it left off */
1256
+ | 'welcomeback'
1257
+ /** The listener can't be resumed or otherwise need to reset its local state */
1258
+ | 'reset'
1259
+
1185
1260
  /** @public */
1186
1261
  export type ListenParams = {[key: string]: Any}
1187
1262
 
@@ -1233,7 +1308,7 @@ export interface ListenOptions {
1233
1308
 
1234
1309
  /**
1235
1310
  * Array of event names to include in the observable. By default, only mutation events are included.
1236
- *
1311
+ * Note: `welcomeback` and `reset` events requires `enableResume: true`
1237
1312
  * @defaultValue `['mutation']`
1238
1313
  */
1239
1314
  events?: ListenEventName[]
@@ -1255,6 +1330,32 @@ export interface ListenOptions {
1255
1330
  * @defaultValue `undefined`
1256
1331
  */
1257
1332
  tag?: string
1333
+
1334
+ /**
1335
+ * If this is enabled, the client will normally resume events upon reconnect
1336
+ * When if enabling this, you should also add the `reset` to the events array and handle the case where the backend is unable to resume.
1337
+ * @beta
1338
+ * @defaultValue `false`
1339
+ */
1340
+ enableResume?: boolean
1341
+ }
1342
+
1343
+ /** @public */
1344
+ export interface ResumableListenOptions extends Omit<ListenOptions, 'events' | 'enableResume'> {
1345
+ /**
1346
+ * If this is enabled, the client will normally resume events upon reconnect
1347
+ * Note that you should also subscribe to `reset`-events and handle the case where the backend is unable to resume
1348
+ * @beta
1349
+ * @defaultValue `false`
1350
+ */
1351
+ enableResume: true
1352
+
1353
+ /**
1354
+ * Array of event names to include in the observable. By default, only mutation events are included.
1355
+ *
1356
+ * @defaultValue `['mutation']`
1357
+ */
1358
+ events?: ResumableListenEventNames[]
1258
1359
  }
1259
1360
 
1260
1361
  /** @public */
@@ -3346,6 +3346,7 @@ ${selectionOpts}`);
3346
3346
  "includeAllVersions",
3347
3347
  "visibility",
3348
3348
  "effectFormat",
3349
+ "enableResume",
3349
3350
  "tag"
3350
3351
  ], defaultOptions = {
3351
3352
  includeResult: true
@@ -3494,7 +3495,7 @@ ${selectionOpts}`);
3494
3495
  * Create a new dataset with the given name
3495
3496
  *
3496
3497
  * @param name - Name of the dataset to create
3497
- * @param options - Options for the dataset
3498
+ * @param options - Options for the dataset, including optional embeddings configuration
3498
3499
  */
3499
3500
  create(name, options) {
3500
3501
  return _modify(this.#client, this.#httpRequest, "PUT", name, options);
@@ -3528,6 +3529,31 @@ ${selectionOpts}`);
3528
3529
  tag: null
3529
3530
  });
3530
3531
  }
3532
+ /**
3533
+ * Get embeddings settings for a dataset
3534
+ *
3535
+ * @param name - Name of the dataset
3536
+ */
3537
+ getEmbeddingsSettings(name) {
3538
+ return resourceGuard("dataset", this.#client.config()), dataset(name), _request(this.#client, this.#httpRequest, {
3539
+ uri: _embeddingsSettingsUri(this.#client, name),
3540
+ tag: null
3541
+ });
3542
+ }
3543
+ /**
3544
+ * Edit embeddings settings for a dataset
3545
+ *
3546
+ * @param name - Name of the dataset
3547
+ * @param settings - Embeddings settings to apply
3548
+ */
3549
+ editEmbeddingsSettings(name, settings) {
3550
+ return resourceGuard("dataset", this.#client.config()), dataset(name), _request(this.#client, this.#httpRequest, {
3551
+ method: "PUT",
3552
+ uri: _embeddingsSettingsUri(this.#client, name),
3553
+ body: settings,
3554
+ tag: null
3555
+ });
3556
+ }
3531
3557
  }
3532
3558
  class DatasetsClient {
3533
3559
  #client;
@@ -3539,7 +3565,7 @@ ${selectionOpts}`);
3539
3565
  * Create a new dataset with the given name
3540
3566
  *
3541
3567
  * @param name - Name of the dataset to create
3542
- * @param options - Options for the dataset
3568
+ * @param options - Options for the dataset, including optional embeddings configuration
3543
3569
  */
3544
3570
  create(name, options) {
3545
3571
  return resourceGuard("dataset", this.#client.config()), lastValueFrom(
@@ -3576,6 +3602,39 @@ ${selectionOpts}`);
3576
3602
  _request(this.#client, this.#httpRequest, { uri, tag: null })
3577
3603
  );
3578
3604
  }
3605
+ /**
3606
+ * Get embeddings settings for a dataset
3607
+ *
3608
+ * @param name - Name of the dataset
3609
+ */
3610
+ getEmbeddingsSettings(name) {
3611
+ return resourceGuard("dataset", this.#client.config()), dataset(name), lastValueFrom(
3612
+ _request(this.#client, this.#httpRequest, {
3613
+ uri: _embeddingsSettingsUri(this.#client, name),
3614
+ tag: null
3615
+ })
3616
+ );
3617
+ }
3618
+ /**
3619
+ * Edit embeddings settings for a dataset
3620
+ *
3621
+ * @param name - Name of the dataset
3622
+ * @param settings - Embeddings settings to apply
3623
+ */
3624
+ editEmbeddingsSettings(name, settings) {
3625
+ return resourceGuard("dataset", this.#client.config()), dataset(name), lastValueFrom(
3626
+ _request(this.#client, this.#httpRequest, {
3627
+ method: "PUT",
3628
+ uri: _embeddingsSettingsUri(this.#client, name),
3629
+ body: settings,
3630
+ tag: null
3631
+ })
3632
+ );
3633
+ }
3634
+ }
3635
+ function _embeddingsSettingsUri(client, name) {
3636
+ const config = client.config();
3637
+ return config.useProjectHostname === false ? `/projects/${config.projectId}/datasets/${name}/settings/embeddings` : `/datasets/${name}/settings/embeddings`;
3579
3638
  }
3580
3639
  function _modify(client, httpRequest, method, name, options) {
3581
3640
  return resourceGuard("dataset", client.config()), dataset(name), _request(client, httpRequest, {