@sanity/client 0.0.0-dev.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.
Files changed (42) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +1225 -0
  3. package/dist/index.browser.cjs +1760 -0
  4. package/dist/index.browser.cjs.map +1 -0
  5. package/dist/index.browser.js +1737 -0
  6. package/dist/index.browser.js.map +1 -0
  7. package/dist/index.cjs +1769 -0
  8. package/dist/index.cjs.js +16 -0
  9. package/dist/index.cjs.map +1 -0
  10. package/dist/index.d.ts +2234 -0
  11. package/dist/index.js +1746 -0
  12. package/dist/index.js.map +1 -0
  13. package/package.json +127 -0
  14. package/src/SanityClient.ts +1253 -0
  15. package/src/assets/AssetsClient.ts +191 -0
  16. package/src/config.ts +96 -0
  17. package/src/data/dataMethods.ts +416 -0
  18. package/src/data/encodeQueryString.ts +29 -0
  19. package/src/data/listen.ts +196 -0
  20. package/src/data/patch.ts +345 -0
  21. package/src/data/transaction.ts +358 -0
  22. package/src/datasets/DatasetsClient.ts +115 -0
  23. package/src/generateHelpUrl.ts +5 -0
  24. package/src/http/browserMiddleware.ts +1 -0
  25. package/src/http/errors.ts +68 -0
  26. package/src/http/nodeMiddleware.ts +11 -0
  27. package/src/http/request.ts +48 -0
  28. package/src/http/requestOptions.ts +33 -0
  29. package/src/index.browser.ts +28 -0
  30. package/src/index.ts +28 -0
  31. package/src/projects/ProjectsClient.ts +61 -0
  32. package/src/types.ts +575 -0
  33. package/src/users/UsersClient.ts +55 -0
  34. package/src/util/defaults.ts +10 -0
  35. package/src/util/getSelection.ts +21 -0
  36. package/src/util/once.ts +14 -0
  37. package/src/util/pick.ts +11 -0
  38. package/src/validators.ts +78 -0
  39. package/src/warnings.ts +30 -0
  40. package/umd/.gitkeep +1 -0
  41. package/umd/sanityClient.js +4840 -0
  42. package/umd/sanityClient.min.js +14 -0
@@ -0,0 +1,1253 @@
1
+ import {lastValueFrom, Observable} from 'rxjs'
2
+
3
+ import {AssetsClient, ObservableAssetsClient} from './assets/AssetsClient'
4
+ import {defaultConfig, initConfig} from './config'
5
+ import * as dataMethods from './data/dataMethods'
6
+ import {_listen} from './data/listen'
7
+ import {ObservablePatch, Patch} from './data/patch'
8
+ import {ObservableTransaction, Transaction} from './data/transaction'
9
+ import {DatasetsClient, ObservableDatasetsClient} from './datasets/DatasetsClient'
10
+ import {ObservableProjectsClient, ProjectsClient} from './projects/ProjectsClient'
11
+ import type {
12
+ AllDocumentIdsMutationOptions,
13
+ AllDocumentsMutationOptions,
14
+ Any,
15
+ BaseMutationOptions,
16
+ ClientConfig,
17
+ FilteredResponseQueryOptions,
18
+ FirstDocumentIdMutationOptions,
19
+ FirstDocumentMutationOptions,
20
+ HttpRequest,
21
+ IdentifiedSanityDocumentStub,
22
+ InitializedClientConfig,
23
+ MultipleMutationResult,
24
+ Mutation,
25
+ MutationSelection,
26
+ PatchOperations,
27
+ PatchSelection,
28
+ QueryParams,
29
+ RawQueryResponse,
30
+ RawRequestOptions,
31
+ SanityDocument,
32
+ SanityDocumentStub,
33
+ SingleMutationResult,
34
+ UnfilteredResponseQueryOptions,
35
+ } from './types'
36
+ import {ObservableUsersClient, UsersClient} from './users/UsersClient'
37
+
38
+ export type {
39
+ _listen,
40
+ AssetsClient,
41
+ DatasetsClient,
42
+ ObservableAssetsClient,
43
+ ObservableDatasetsClient,
44
+ ObservableProjectsClient,
45
+ ObservableUsersClient,
46
+ ProjectsClient,
47
+ UsersClient,
48
+ }
49
+
50
+ /** @public */
51
+ export class ObservableSanityClient {
52
+ assets: ObservableAssetsClient
53
+ datasets: ObservableDatasetsClient
54
+ projects: ObservableProjectsClient
55
+ users: ObservableUsersClient
56
+
57
+ /**
58
+ * Private properties
59
+ */
60
+ #clientConfig: InitializedClientConfig
61
+ #httpRequest: HttpRequest
62
+
63
+ /**
64
+ * Instance properties
65
+ */
66
+ listen = _listen
67
+
68
+ constructor(httpRequest: HttpRequest, config: ClientConfig = defaultConfig) {
69
+ this.config(config)
70
+
71
+ this.#httpRequest = httpRequest
72
+
73
+ this.assets = new ObservableAssetsClient(this, this.#httpRequest)
74
+ this.datasets = new ObservableDatasetsClient(this, this.#httpRequest)
75
+ this.projects = new ObservableProjectsClient(this, this.#httpRequest)
76
+ this.users = new ObservableUsersClient(this, this.#httpRequest)
77
+ }
78
+
79
+ /**
80
+ * Clone the client - returns a new instance
81
+ */
82
+ clone(): ObservableSanityClient {
83
+ return new ObservableSanityClient(this.#httpRequest, this.config())
84
+ }
85
+
86
+ /**
87
+ * Returns the current client configuration
88
+ */
89
+ config(): InitializedClientConfig
90
+ /**
91
+ * Reconfigure the client. Note that this _mutates_ the current client.
92
+ */
93
+ config(newConfig?: Partial<ClientConfig>): this
94
+ config(newConfig?: Partial<ClientConfig>): ClientConfig | this {
95
+ if (newConfig === undefined) {
96
+ return {...this.#clientConfig}
97
+ }
98
+
99
+ if (this.#clientConfig && this.#clientConfig.allowReconfigure === false) {
100
+ throw new Error(
101
+ 'Existing client instance cannot be reconfigured - use `withConfig(newConfig)` to return a new client'
102
+ )
103
+ }
104
+
105
+ this.#clientConfig = initConfig(newConfig, this.#clientConfig || {})
106
+ return this
107
+ }
108
+
109
+ /**
110
+ * Clone the client with a new (partial) configuration.
111
+ *
112
+ * @param newConfig - New client configuration properties, shallowly merged with existing configuration
113
+ */
114
+ withConfig(newConfig?: Partial<ClientConfig>): ObservableSanityClient {
115
+ return new ObservableSanityClient(this.#httpRequest, {...this.config(), ...newConfig})
116
+ }
117
+
118
+ /**
119
+ * Perform a GROQ-query against the configured dataset.
120
+ *
121
+ * @param query - GROQ-query to perform
122
+ */
123
+ fetch<R = Any>(query: string): Observable<R>
124
+ /**
125
+ * Perform a GROQ-query against the configured dataset.
126
+ *
127
+ * @param query - GROQ-query to perform
128
+ * @param params - Query parameters
129
+ */
130
+ fetch<R = Any, Q = QueryParams>(query: string, params: Q): Observable<R>
131
+ /**
132
+ * Perform a GROQ-query against the configured dataset.
133
+ *
134
+ * @param query - GROQ-query to perform
135
+ * @param params - Query parameters
136
+ * @param options - Request options
137
+ */
138
+ fetch<R = Any, Q = QueryParams>(
139
+ query: string,
140
+ params: Q | undefined,
141
+ options: FilteredResponseQueryOptions
142
+ ): Observable<R>
143
+ /**
144
+ * Perform a GROQ-query against the configured dataset.
145
+ *
146
+ * @param query - GROQ-query to perform
147
+ * @param params - Query parameters
148
+ * @param options - Request options
149
+ */
150
+ fetch<R = Any, Q = QueryParams>(
151
+ query: string,
152
+ params: Q | undefined,
153
+ options: UnfilteredResponseQueryOptions
154
+ ): Observable<RawQueryResponse<R>>
155
+ fetch<R, Q extends QueryParams>(
156
+ query: string,
157
+ params?: Q,
158
+ options: FilteredResponseQueryOptions | UnfilteredResponseQueryOptions = {}
159
+ ): Observable<RawQueryResponse<R> | R> {
160
+ return dataMethods._fetch<R, Q>(this, this.#httpRequest, query, params, options)
161
+ }
162
+
163
+ /**
164
+ * Fetch a single document with the given ID.
165
+ *
166
+ * @param id - Document ID to fetch
167
+ * @param options - Request options
168
+ */
169
+ getDocument<R extends Record<string, Any> = Record<string, Any>>(
170
+ id: string,
171
+ options?: {tag?: string}
172
+ ): Observable<SanityDocument<R> | undefined> {
173
+ return dataMethods._getDocument<R>(this, this.#httpRequest, id, options)
174
+ }
175
+
176
+ /**
177
+ * Fetch multiple documents in one request.
178
+ * Should be used sparingly - performing a query is usually a better option.
179
+ * The order/position of documents is preserved based on the original array of IDs.
180
+ * If any of the documents are missing, they will be replaced by a `null` entry in the returned array
181
+ *
182
+ * @param ids - Document IDs to fetch
183
+ * @param options - Request options
184
+ */
185
+ getDocuments<R extends Record<string, Any> = Record<string, Any>>(
186
+ ids: string[],
187
+ options?: {tag?: string}
188
+ ): Observable<(SanityDocument<R> | null)[]> {
189
+ return dataMethods._getDocuments<R>(this, this.#httpRequest, ids, options)
190
+ }
191
+
192
+ /**
193
+ * Create a document. Requires a `_type` property. If no `_id` is provided, it will be generated by the database.
194
+ * Returns an observable that resolves to the created document.
195
+ *
196
+ * @param document - Document to create
197
+ * @param options - Mutation options
198
+ */
199
+ create<R extends Record<string, Any> = Record<string, Any>>(
200
+ document: SanityDocumentStub<R>,
201
+ options: FirstDocumentMutationOptions
202
+ ): Observable<SanityDocument<R>>
203
+ /**
204
+ * Create a document. Requires a `_type` property. If no `_id` is provided, it will be generated by the database.
205
+ * Returns an observable that resolves to an array containing the created document.
206
+ *
207
+ * @param document - Document to create
208
+ * @param options - Mutation options
209
+ */
210
+ create<R extends Record<string, Any> = Record<string, Any>>(
211
+ document: SanityDocumentStub<R>,
212
+ options: AllDocumentsMutationOptions
213
+ ): Observable<SanityDocument<R>[]>
214
+ /**
215
+ * Create a document. Requires a `_type` property. If no `_id` is provided, it will be generated by the database.
216
+ * Returns an observable that resolves to a mutation result object containing the ID of the created document.
217
+ *
218
+ * @param document - Document to create
219
+ * @param options - Mutation options
220
+ */
221
+ create<R extends Record<string, Any> = Record<string, Any>>(
222
+ document: SanityDocumentStub<R>,
223
+ options: FirstDocumentIdMutationOptions
224
+ ): Observable<SingleMutationResult>
225
+ /**
226
+ * Create a document. Requires a `_type` property. If no `_id` is provided, it will be generated by the database.
227
+ * Returns an observable that resolves to a mutation result object containing the ID of the created document.
228
+ *
229
+ * @param document - Document to create
230
+ * @param options - Mutation options
231
+ */
232
+ create<R extends Record<string, Any> = Record<string, Any>>(
233
+ document: SanityDocumentStub<R>,
234
+ options: AllDocumentIdsMutationOptions
235
+ ): Observable<MultipleMutationResult>
236
+ /**
237
+ * Create a document. Requires a `_type` property. If no `_id` is provided, it will be generated by the database.
238
+ * Returns an observable that resolves to the created document.
239
+ *
240
+ * @param document - Document to create
241
+ * @param options - Mutation options
242
+ */
243
+ create<R extends Record<string, Any> = Record<string, Any>>(
244
+ document: SanityDocumentStub<R>,
245
+ options?: BaseMutationOptions
246
+ ): Observable<SanityDocument<R>>
247
+ create<R extends Record<string, Any> = Record<string, Any>>(
248
+ document: SanityDocumentStub<R>,
249
+ options?:
250
+ | AllDocumentIdsMutationOptions
251
+ | AllDocumentsMutationOptions
252
+ | BaseMutationOptions
253
+ | FirstDocumentIdMutationOptions
254
+ | FirstDocumentMutationOptions
255
+ ): Observable<
256
+ SanityDocument<R> | SanityDocument<R>[] | SingleMutationResult | MultipleMutationResult
257
+ > {
258
+ return dataMethods._create<R>(this, this.#httpRequest, document, 'create', options)
259
+ }
260
+
261
+ /**
262
+ * Create a document if no document with the same ID already exists.
263
+ * Returns an observable that resolves to the created document.
264
+ *
265
+ * @param document - Document to create
266
+ * @param options - Mutation options
267
+ */
268
+ createIfNotExists<R extends Record<string, Any> = Record<string, Any>>(
269
+ document: IdentifiedSanityDocumentStub<R>,
270
+ options: FirstDocumentMutationOptions
271
+ ): Observable<SanityDocument<R>>
272
+ /**
273
+ * Create a document if no document with the same ID already exists.
274
+ * Returns an observable that resolves to an array containing the created document.
275
+ *
276
+ * @param document - Document to create
277
+ * @param options - Mutation options
278
+ */
279
+ createIfNotExists<R extends Record<string, Any> = Record<string, Any>>(
280
+ document: IdentifiedSanityDocumentStub<R>,
281
+ options: AllDocumentsMutationOptions
282
+ ): Observable<SanityDocument<R>[]>
283
+ /**
284
+ * Create a document if no document with the same ID already exists.
285
+ * Returns an observable that resolves to a mutation result object containing the ID of the created document.
286
+ *
287
+ * @param document - Document to create
288
+ * @param options - Mutation options
289
+ */
290
+ createIfNotExists<R extends Record<string, Any> = Record<string, Any>>(
291
+ document: IdentifiedSanityDocumentStub<R>,
292
+ options: FirstDocumentIdMutationOptions
293
+ ): Observable<SingleMutationResult>
294
+ /**
295
+ * Create a document if no document with the same ID already exists.
296
+ * Returns an observable that resolves to a mutation result object containing the ID of the created document.
297
+ *
298
+ * @param document - Document to create
299
+ * @param options - Mutation options
300
+ */
301
+ createIfNotExists<R extends Record<string, Any> = Record<string, Any>>(
302
+ document: IdentifiedSanityDocumentStub<R>,
303
+ options: AllDocumentIdsMutationOptions
304
+ ): Observable<MultipleMutationResult>
305
+ /**
306
+ * Create a document if no document with the same ID already exists.
307
+ * Returns an observable that resolves to the created document.
308
+ *
309
+ * @param document - Document to create
310
+ * @param options - Mutation options
311
+ */
312
+ createIfNotExists<R extends Record<string, Any> = Record<string, Any>>(
313
+ document: IdentifiedSanityDocumentStub<R>,
314
+ options?: BaseMutationOptions
315
+ ): Observable<SanityDocument<R>>
316
+ createIfNotExists<R extends Record<string, Any> = Record<string, Any>>(
317
+ document: IdentifiedSanityDocumentStub<R>,
318
+ options?:
319
+ | AllDocumentIdsMutationOptions
320
+ | AllDocumentsMutationOptions
321
+ | BaseMutationOptions
322
+ | FirstDocumentIdMutationOptions
323
+ | FirstDocumentMutationOptions
324
+ ): Observable<
325
+ SanityDocument<R> | SanityDocument<R>[] | SingleMutationResult | MultipleMutationResult
326
+ > {
327
+ return dataMethods._createIfNotExists<R>(this, this.#httpRequest, document, options)
328
+ }
329
+
330
+ /**
331
+ * Create a document if it does not exist, or replace a document with the same document ID
332
+ * Returns an observable that resolves to the created document.
333
+ *
334
+ * @param document - Document to either create or replace
335
+ * @param options - Mutation options
336
+ */
337
+ createOrReplace<R extends Record<string, Any> = Record<string, Any>>(
338
+ document: IdentifiedSanityDocumentStub<R>,
339
+ options: FirstDocumentMutationOptions
340
+ ): Observable<SanityDocument<R>>
341
+ /**
342
+ * Create a document if it does not exist, or replace a document with the same document ID
343
+ * Returns an observable that resolves to an array containing the created document.
344
+ *
345
+ * @param document - Document to either create or replace
346
+ * @param options - Mutation options
347
+ */
348
+ createOrReplace<R extends Record<string, Any> = Record<string, Any>>(
349
+ document: IdentifiedSanityDocumentStub<R>,
350
+ options: AllDocumentsMutationOptions
351
+ ): Observable<SanityDocument<R>[]>
352
+ /**
353
+ * Create a document if it does not exist, or replace a document with the same document ID
354
+ * Returns an observable that resolves to a mutation result object containing the ID of the created document.
355
+ *
356
+ * @param document - Document to either create or replace
357
+ * @param options - Mutation options
358
+ */
359
+ createOrReplace<R extends Record<string, Any> = Record<string, Any>>(
360
+ document: IdentifiedSanityDocumentStub<R>,
361
+ options: FirstDocumentIdMutationOptions
362
+ ): Observable<SingleMutationResult>
363
+ /**
364
+ * Create a document if it does not exist, or replace a document with the same document ID
365
+ * Returns an observable that resolves to a mutation result object containing the created document ID.
366
+ *
367
+ * @param document - Document to either create or replace
368
+ * @param options - Mutation options
369
+ */
370
+ createOrReplace<R extends Record<string, Any> = Record<string, Any>>(
371
+ document: IdentifiedSanityDocumentStub<R>,
372
+ options: AllDocumentIdsMutationOptions
373
+ ): Observable<MultipleMutationResult>
374
+ /**
375
+ * Create a document if it does not exist, or replace a document with the same document ID
376
+ * Returns an observable that resolves to the created document.
377
+ *
378
+ * @param document - Document to either create or replace
379
+ * @param options - Mutation options
380
+ */
381
+ createOrReplace<R extends Record<string, Any> = Record<string, Any>>(
382
+ document: IdentifiedSanityDocumentStub<R>,
383
+ options?: BaseMutationOptions
384
+ ): Observable<SanityDocument<R>>
385
+ createOrReplace<R extends Record<string, Any> = Record<string, Any>>(
386
+ document: IdentifiedSanityDocumentStub<R>,
387
+ options?:
388
+ | AllDocumentIdsMutationOptions
389
+ | AllDocumentsMutationOptions
390
+ | BaseMutationOptions
391
+ | FirstDocumentIdMutationOptions
392
+ | FirstDocumentMutationOptions
393
+ ): Observable<
394
+ SanityDocument<R> | SanityDocument<R>[] | SingleMutationResult | MultipleMutationResult
395
+ > {
396
+ return dataMethods._createOrReplace<R>(this, this.#httpRequest, document, options)
397
+ }
398
+
399
+ /**
400
+ * Deletes a document with the given document ID.
401
+ * Returns an observable that resolves to the deleted document.
402
+ *
403
+ * @param id - Document ID to delete
404
+ * @param options - Options for the mutation
405
+ */
406
+ delete<R extends Record<string, Any> = Record<string, Any>>(
407
+ id: string,
408
+ options: FirstDocumentMutationOptions
409
+ ): Observable<SanityDocument<R>>
410
+ /**
411
+ * Deletes a document with the given document ID.
412
+ * Returns an observable that resolves to an array containing the deleted document.
413
+ *
414
+ * @param id - Document ID to delete
415
+ * @param options - Options for the mutation
416
+ */
417
+ delete<R extends Record<string, Any> = Record<string, Any>>(
418
+ id: string,
419
+ options: AllDocumentsMutationOptions
420
+ ): Observable<SanityDocument<R>[]>
421
+ /**
422
+ * Deletes a document with the given document ID.
423
+ * Returns an observable that resolves to a mutation result object containing the deleted document ID.
424
+ *
425
+ * @param id - Document ID to delete
426
+ * @param options - Options for the mutation
427
+ */
428
+ delete(id: string, options: FirstDocumentIdMutationOptions): Observable<SingleMutationResult>
429
+ /**
430
+ * Deletes a document with the given document ID.
431
+ * Returns an observable that resolves to a mutation result object containing the deleted document ID.
432
+ *
433
+ * @param id - Document ID to delete
434
+ * @param options - Options for the mutation
435
+ */
436
+ delete(id: string, options: AllDocumentIdsMutationOptions): Observable<MultipleMutationResult>
437
+ /**
438
+ * Deletes a document with the given document ID.
439
+ * Returns an observable that resolves to the deleted document.
440
+ *
441
+ * @param id - Document ID to delete
442
+ * @param options - Options for the mutation
443
+ */
444
+ delete<R extends Record<string, Any> = Record<string, Any>>(
445
+ id: string,
446
+ options?: BaseMutationOptions
447
+ ): Observable<SanityDocument<R>>
448
+ /**
449
+ * Deletes one or more documents matching the given query or document ID.
450
+ * Returns an observable that resolves to first deleted document.
451
+ *
452
+ * @param selection - An object with either an `id` or `query` key defining what to delete
453
+ * @param options - Options for the mutation
454
+ */
455
+ delete<R extends Record<string, Any> = Record<string, Any>>(
456
+ selection: MutationSelection,
457
+ options: FirstDocumentMutationOptions
458
+ ): Observable<SanityDocument<R>>
459
+ /**
460
+ * Deletes one or more documents matching the given query or document ID.
461
+ * Returns an observable that resolves to an array containing the deleted documents.
462
+ *
463
+ * @param selection - An object with either an `id` or `query` key defining what to delete
464
+ * @param options - Options for the mutation
465
+ */
466
+ delete<R extends Record<string, Any> = Record<string, Any>>(
467
+ selection: MutationSelection,
468
+ options: AllDocumentsMutationOptions
469
+ ): Observable<SanityDocument<R>[]>
470
+ /**
471
+ * Deletes one or more documents matching the given query or document ID.
472
+ * Returns an observable that resolves to a mutation result object containing the ID of the first deleted document.
473
+ *
474
+ * @param selection - An object with either an `id` or `query` key defining what to delete
475
+ * @param options - Options for the mutation
476
+ */
477
+ delete(
478
+ selection: MutationSelection,
479
+ options: FirstDocumentIdMutationOptions
480
+ ): Observable<SingleMutationResult>
481
+ /**
482
+ * Deletes one or more documents matching the given query or document ID.
483
+ * Returns an observable that resolves to a mutation result object containing the document IDs that were deleted.
484
+ *
485
+ * @param selection - An object with either an `id` or `query` key defining what to delete
486
+ * @param options - Options for the mutation
487
+ */
488
+ delete(
489
+ selection: MutationSelection,
490
+ options: AllDocumentIdsMutationOptions
491
+ ): Observable<MultipleMutationResult>
492
+ /**
493
+ * Deletes one or more documents matching the given query or document ID.
494
+ * Returns an observable that resolves to first deleted document.
495
+ *
496
+ * @param selection - An object with either an `id` or `query` key defining what to delete
497
+ * @param options - Options for the mutation
498
+ */
499
+ delete<R extends Record<string, Any> = Record<string, Any>>(
500
+ selection: MutationSelection,
501
+ options?: BaseMutationOptions
502
+ ): Observable<SanityDocument<R>>
503
+ delete<R extends Record<string, Any> = Record<string, Any>>(
504
+ selection: string | MutationSelection,
505
+ options?:
506
+ | AllDocumentIdsMutationOptions
507
+ | AllDocumentsMutationOptions
508
+ | BaseMutationOptions
509
+ | FirstDocumentIdMutationOptions
510
+ | FirstDocumentMutationOptions
511
+ ): Observable<
512
+ SanityDocument<R> | SanityDocument<R>[] | SingleMutationResult | MultipleMutationResult
513
+ > {
514
+ return dataMethods._delete<R>(this, this.#httpRequest, selection, options)
515
+ }
516
+
517
+ /**
518
+ * Perform mutation operations against the configured dataset
519
+ * Returns an observable that resolves to the first mutated document.
520
+ *
521
+ * @param operations - Mutation operations to execute
522
+ * @param options - Mutation options
523
+ */
524
+ mutate<R extends Record<string, Any> = Record<string, Any>>(
525
+ operations: Mutation<R>[] | ObservablePatch | ObservableTransaction,
526
+ options: FirstDocumentMutationOptions
527
+ ): Observable<SanityDocument<R>>
528
+ /**
529
+ * Perform mutation operations against the configured dataset.
530
+ * Returns an observable that resolves to an array of the mutated documents.
531
+ *
532
+ * @param operations - Mutation operations to execute
533
+ * @param options - Mutation options
534
+ */
535
+ mutate<R extends Record<string, Any> = Record<string, Any>>(
536
+ operations: Mutation<R>[] | ObservablePatch | ObservableTransaction,
537
+ options: AllDocumentsMutationOptions
538
+ ): Observable<SanityDocument<R>[]>
539
+ /**
540
+ * Perform mutation operations against the configured dataset
541
+ * Returns an observable that resolves to a mutation result object containing the document ID of the first mutated document.
542
+ *
543
+ * @param operations - Mutation operations to execute
544
+ * @param options - Mutation options
545
+ */
546
+ mutate<R extends Record<string, Any> = Record<string, Any>>(
547
+ operations: Mutation<R>[] | ObservablePatch | ObservableTransaction,
548
+ options: FirstDocumentIdMutationOptions
549
+ ): Observable<SingleMutationResult>
550
+ /**
551
+ * Perform mutation operations against the configured dataset
552
+ * Returns an observable that resolves to a mutation result object containing the mutated document IDs.
553
+ *
554
+ * @param operations - Mutation operations to execute
555
+ * @param options - Mutation options
556
+ */
557
+ mutate<R extends Record<string, Any> = Record<string, Any>>(
558
+ operations: Mutation<R>[] | ObservablePatch | ObservableTransaction,
559
+ options: AllDocumentIdsMutationOptions
560
+ ): Observable<MultipleMutationResult>
561
+ /**
562
+ * Perform mutation operations against the configured dataset
563
+ * Returns an observable that resolves to the first mutated document.
564
+ *
565
+ * @param operations - Mutation operations to execute
566
+ * @param options - Mutation options
567
+ */
568
+ mutate<R extends Record<string, Any> = Record<string, Any>>(
569
+ operations: Mutation<R>[] | ObservablePatch | ObservableTransaction,
570
+ options?: BaseMutationOptions
571
+ ): Observable<SanityDocument<R>>
572
+ mutate<R extends Record<string, Any> = Record<string, Any>>(
573
+ operations: Mutation<R>[] | ObservablePatch | ObservableTransaction,
574
+ options?:
575
+ | FirstDocumentMutationOptions
576
+ | AllDocumentsMutationOptions
577
+ | FirstDocumentIdMutationOptions
578
+ | AllDocumentIdsMutationOptions
579
+ | BaseMutationOptions
580
+ ): Observable<
581
+ SanityDocument<R> | SanityDocument<R>[] | SingleMutationResult | MultipleMutationResult
582
+ > {
583
+ return dataMethods._mutate<R>(this, this.#httpRequest, operations, options)
584
+ }
585
+
586
+ /**
587
+ * Create a new buildable patch of operations to perform
588
+ *
589
+ * @param documentId - Document ID(s) to patch
590
+ * @param operations - Optional object of patch operations to initialize the patch instance with
591
+ */
592
+ patch(documentId: PatchSelection, operations?: PatchOperations): ObservablePatch {
593
+ return new ObservablePatch(documentId, operations, this)
594
+ }
595
+
596
+ /**
597
+ * Create a new transaction of mutations
598
+ *
599
+ * @param operations - Optional array of mutation operations to initialize the transaction instance with
600
+ */
601
+ transaction<R extends Record<string, Any> = Record<string, Any>>(
602
+ operations?: Mutation<R>[]
603
+ ): ObservableTransaction {
604
+ return new ObservableTransaction(operations, this)
605
+ }
606
+
607
+ /**
608
+ * DEPRECATED: Perform an HTTP request against the Sanity API
609
+ *
610
+ * @deprecated Use your own request library!
611
+ * @param options - Request options
612
+ */
613
+ request<R = Any>(options: RawRequestOptions): Observable<R> {
614
+ return dataMethods._request(this, this.#httpRequest, options)
615
+ }
616
+
617
+ /**
618
+ * Get a Sanity API URL for the URI provided
619
+ *
620
+ * @param uri - URI/path to build URL for
621
+ * @param canUseCdn - Whether or not to allow using the API CDN for this route
622
+ */
623
+ getUrl(uri: string, canUseCdn?: boolean): string {
624
+ return dataMethods._getUrl(this, uri, canUseCdn)
625
+ }
626
+
627
+ /**
628
+ * Get a Sanity API URL for the data operation and path provided
629
+ *
630
+ * @param operation - Data operation (eg `query`, `mutate`, `listen` or similar)
631
+ * @param path - Path to append after the operation
632
+ */
633
+ getDataUrl(operation: string, path?: string): string {
634
+ return dataMethods._getDataUrl(this, operation, path)
635
+ }
636
+ }
637
+
638
+ /** @public */
639
+ export class SanityClient {
640
+ assets: AssetsClient
641
+ datasets: DatasetsClient
642
+ projects: ProjectsClient
643
+ users: UsersClient
644
+
645
+ /**
646
+ * Observable version of the Sanity client, with the same configuration as the promise-based one
647
+ */
648
+ observable: ObservableSanityClient
649
+
650
+ /**
651
+ * Private properties
652
+ */
653
+ #clientConfig: InitializedClientConfig
654
+ #httpRequest: HttpRequest
655
+
656
+ /**
657
+ * Instance properties
658
+ */
659
+ listen = _listen
660
+
661
+ constructor(httpRequest: HttpRequest, config: ClientConfig = defaultConfig) {
662
+ this.config(config)
663
+
664
+ this.#httpRequest = httpRequest
665
+
666
+ this.assets = new AssetsClient(this, this.#httpRequest)
667
+ this.datasets = new DatasetsClient(this, this.#httpRequest)
668
+ this.projects = new ProjectsClient(this, this.#httpRequest)
669
+ this.users = new UsersClient(this, this.#httpRequest)
670
+
671
+ this.observable = new ObservableSanityClient(httpRequest, config)
672
+ }
673
+
674
+ /**
675
+ * Clone the client - returns a new instance
676
+ */
677
+ clone(): SanityClient {
678
+ return new SanityClient(this.#httpRequest, this.config())
679
+ }
680
+
681
+ /**
682
+ * Returns the current client configuration
683
+ */
684
+ config(): InitializedClientConfig
685
+ /**
686
+ * Reconfigure the client. Note that this _mutates_ the current client.
687
+ */
688
+ config(newConfig?: Partial<ClientConfig>): this
689
+ config(newConfig?: Partial<ClientConfig>): ClientConfig | this {
690
+ if (newConfig === undefined) {
691
+ return {...this.#clientConfig}
692
+ }
693
+
694
+ if (this.#clientConfig && this.#clientConfig.allowReconfigure === false) {
695
+ throw new Error(
696
+ 'Existing client instance cannot be reconfigured - use `withConfig(newConfig)` to return a new client'
697
+ )
698
+ }
699
+
700
+ if (this.observable) {
701
+ this.observable.config(newConfig)
702
+ }
703
+
704
+ this.#clientConfig = initConfig(newConfig, this.#clientConfig || {})
705
+ return this
706
+ }
707
+
708
+ /**
709
+ * Clone the client with a new (partial) configuration.
710
+ *
711
+ * @param newConfig - New client configuration properties, shallowly merged with existing configuration
712
+ */
713
+ withConfig(newConfig?: Partial<ClientConfig>): SanityClient {
714
+ return new SanityClient(this.#httpRequest, {...this.config(), ...newConfig})
715
+ }
716
+
717
+ /**
718
+ * Perform a GROQ-query against the configured dataset.
719
+ *
720
+ * @param query - GROQ-query to perform
721
+ */
722
+ fetch<R = Any>(query: string): Promise<R>
723
+ /**
724
+ * Perform a GROQ-query against the configured dataset.
725
+ *
726
+ * @param query - GROQ-query to perform
727
+ * @param params - Optional query parameters
728
+ */
729
+ fetch<R = Any, Q = QueryParams>(query: string, params: Q): Promise<R>
730
+ /**
731
+ * Perform a GROQ-query against the configured dataset.
732
+ *
733
+ * @param query - GROQ-query to perform
734
+ * @param params - Optional query parameters
735
+ * @param options - Request options
736
+ */
737
+ fetch<R = Any, Q = QueryParams>(
738
+ query: string,
739
+ params: Q | undefined,
740
+ options: FilteredResponseQueryOptions
741
+ ): Promise<R>
742
+ /**
743
+ * Perform a GROQ-query against the configured dataset.
744
+ *
745
+ * @param query - GROQ-query to perform
746
+ * @param params - Optional query parameters
747
+ * @param options - Request options
748
+ */
749
+ fetch<R = Any, Q = QueryParams>(
750
+ query: string,
751
+ params: Q | undefined,
752
+ options: UnfilteredResponseQueryOptions
753
+ ): Promise<RawQueryResponse<R>>
754
+ fetch<R, Q extends QueryParams>(
755
+ query: string,
756
+ params?: Q,
757
+ options: FilteredResponseQueryOptions | UnfilteredResponseQueryOptions = {}
758
+ ): Promise<RawQueryResponse<R> | R> {
759
+ return lastValueFrom(dataMethods._fetch<R, Q>(this, this.#httpRequest, query, params, options))
760
+ }
761
+
762
+ /**
763
+ * Fetch a single document with the given ID.
764
+ *
765
+ * @param id - Document ID to fetch
766
+ * @param options - Request options
767
+ */
768
+ getDocument<R extends Record<string, Any> = Record<string, Any>>(
769
+ id: string,
770
+ options?: {tag?: string}
771
+ ): Promise<SanityDocument<R> | undefined> {
772
+ return lastValueFrom(dataMethods._getDocument<R>(this, this.#httpRequest, id, options))
773
+ }
774
+
775
+ /**
776
+ * Fetch multiple documents in one request.
777
+ * Should be used sparingly - performing a query is usually a better option.
778
+ * The order/position of documents is preserved based on the original array of IDs.
779
+ * If any of the documents are missing, they will be replaced by a `null` entry in the returned array
780
+ *
781
+ * @param ids - Document IDs to fetch
782
+ * @param options - Request options
783
+ */
784
+ getDocuments<R extends Record<string, Any> = Record<string, Any>>(
785
+ ids: string[],
786
+ options?: {tag?: string}
787
+ ): Promise<(SanityDocument<R> | null)[]> {
788
+ return lastValueFrom(dataMethods._getDocuments<R>(this, this.#httpRequest, ids, options))
789
+ }
790
+
791
+ /**
792
+ * Create a document. Requires a `_type` property. If no `_id` is provided, it will be generated by the database.
793
+ * Returns a promise that resolves to the created document.
794
+ *
795
+ * @param document - Document to create
796
+ * @param options - Mutation options
797
+ */
798
+ create<R extends Record<string, Any> = Record<string, Any>>(
799
+ document: SanityDocumentStub<R>,
800
+ options: FirstDocumentMutationOptions
801
+ ): Promise<SanityDocument<R>>
802
+ /**
803
+ * Create a document. Requires a `_type` property. If no `_id` is provided, it will be generated by the database.
804
+ * Returns a promise that resolves to an array containing the created document.
805
+ *
806
+ * @param document - Document to create
807
+ * @param options - Mutation options
808
+ */
809
+ create<R extends Record<string, Any> = Record<string, Any>>(
810
+ document: SanityDocumentStub<R>,
811
+ options: AllDocumentsMutationOptions
812
+ ): Promise<SanityDocument<R>[]>
813
+ /**
814
+ * Create a document. Requires a `_type` property. If no `_id` is provided, it will be generated by the database.
815
+ * Returns a promise that resolves to a mutation result object containing the ID of the created document.
816
+ *
817
+ * @param document - Document to create
818
+ * @param options - Mutation options
819
+ */
820
+ create<R extends Record<string, Any> = Record<string, Any>>(
821
+ document: SanityDocumentStub<R>,
822
+ options: FirstDocumentIdMutationOptions
823
+ ): Promise<SingleMutationResult>
824
+ /**
825
+ * Create a document. Requires a `_type` property. If no `_id` is provided, it will be generated by the database.
826
+ * Returns a promise that resolves to a mutation result object containing the ID of the created document.
827
+ *
828
+ * @param document - Document to create
829
+ * @param options - Mutation options
830
+ */
831
+ create<R extends Record<string, Any> = Record<string, Any>>(
832
+ document: SanityDocumentStub<R>,
833
+ options: AllDocumentIdsMutationOptions
834
+ ): Promise<MultipleMutationResult>
835
+ /**
836
+ * Create a document. Requires a `_type` property. If no `_id` is provided, it will be generated by the database.
837
+ * Returns a promise that resolves to the created document.
838
+ *
839
+ * @param document - Document to create
840
+ * @param options - Mutation options
841
+ */
842
+ create<R extends Record<string, Any> = Record<string, Any>>(
843
+ document: SanityDocumentStub<R>,
844
+ options?: BaseMutationOptions
845
+ ): Promise<SanityDocument<R>>
846
+ create<R extends Record<string, Any> = Record<string, Any>>(
847
+ document: SanityDocumentStub<R>,
848
+ options?:
849
+ | AllDocumentIdsMutationOptions
850
+ | AllDocumentsMutationOptions
851
+ | BaseMutationOptions
852
+ | FirstDocumentIdMutationOptions
853
+ | FirstDocumentMutationOptions
854
+ ): Promise<
855
+ SanityDocument<R> | SanityDocument<R>[] | SingleMutationResult | MultipleMutationResult
856
+ > {
857
+ return lastValueFrom(
858
+ dataMethods._create<R>(this, this.#httpRequest, document, 'create', options)
859
+ )
860
+ }
861
+
862
+ /**
863
+ * Create a document if no document with the same ID already exists.
864
+ * Returns a promise that resolves to the created document.
865
+ *
866
+ * @param document - Document to create
867
+ * @param options - Mutation options
868
+ */
869
+ createIfNotExists<R extends Record<string, Any> = Record<string, Any>>(
870
+ document: IdentifiedSanityDocumentStub<R>,
871
+ options: FirstDocumentMutationOptions
872
+ ): Promise<SanityDocument<R>>
873
+ /**
874
+ * Create a document if no document with the same ID already exists.
875
+ * Returns a promise that resolves to an array containing the created document.
876
+ *
877
+ * @param document - Document to create
878
+ * @param options - Mutation options
879
+ */
880
+ createIfNotExists<R extends Record<string, Any> = Record<string, Any>>(
881
+ document: IdentifiedSanityDocumentStub<R>,
882
+ options: AllDocumentsMutationOptions
883
+ ): Promise<SanityDocument<R>[]>
884
+ /**
885
+ * Create a document if no document with the same ID already exists.
886
+ * Returns a promise that resolves to a mutation result object containing the ID of the created document.
887
+ *
888
+ * @param document - Document to create
889
+ * @param options - Mutation options
890
+ */
891
+ createIfNotExists<R extends Record<string, Any> = Record<string, Any>>(
892
+ document: IdentifiedSanityDocumentStub<R>,
893
+ options: FirstDocumentIdMutationOptions
894
+ ): Promise<SingleMutationResult>
895
+ /**
896
+ * Create a document if no document with the same ID already exists.
897
+ * Returns a promise that resolves to a mutation result object containing the ID of the created document.
898
+ *
899
+ * @param document - Document to create
900
+ * @param options - Mutation options
901
+ */
902
+ createIfNotExists<R extends Record<string, Any> = Record<string, Any>>(
903
+ document: IdentifiedSanityDocumentStub<R>,
904
+ options: AllDocumentIdsMutationOptions
905
+ ): Promise<MultipleMutationResult>
906
+ /**
907
+ * Create a document if no document with the same ID already exists.
908
+ * Returns a promise that resolves to the created document.
909
+ *
910
+ * @param document - Document to create
911
+ * @param options - Mutation options
912
+ */
913
+ createIfNotExists<R extends Record<string, Any> = Record<string, Any>>(
914
+ document: IdentifiedSanityDocumentStub<R>,
915
+ options?: BaseMutationOptions
916
+ ): Promise<SanityDocument<R>>
917
+ createIfNotExists<R extends Record<string, Any> = Record<string, Any>>(
918
+ document: IdentifiedSanityDocumentStub<R>,
919
+ options?:
920
+ | AllDocumentIdsMutationOptions
921
+ | AllDocumentsMutationOptions
922
+ | BaseMutationOptions
923
+ | FirstDocumentIdMutationOptions
924
+ | FirstDocumentMutationOptions
925
+ ): Promise<
926
+ SanityDocument<R> | SanityDocument<R>[] | SingleMutationResult | MultipleMutationResult
927
+ > {
928
+ return lastValueFrom(
929
+ dataMethods._createIfNotExists<R>(this, this.#httpRequest, document, options)
930
+ )
931
+ }
932
+
933
+ /**
934
+ * Create a document if it does not exist, or replace a document with the same document ID
935
+ * Returns a promise that resolves to the created document.
936
+ *
937
+ * @param document - Document to either create or replace
938
+ * @param options - Mutation options
939
+ */
940
+ createOrReplace<R extends Record<string, Any> = Record<string, Any>>(
941
+ document: IdentifiedSanityDocumentStub<R>,
942
+ options: FirstDocumentMutationOptions
943
+ ): Promise<SanityDocument<R>>
944
+ /**
945
+ * Create a document if it does not exist, or replace a document with the same document ID
946
+ * Returns a promise that resolves to an array containing the created document.
947
+ *
948
+ * @param document - Document to either create or replace
949
+ * @param options - Mutation options
950
+ */
951
+ createOrReplace<R extends Record<string, Any> = Record<string, Any>>(
952
+ document: IdentifiedSanityDocumentStub<R>,
953
+ options: AllDocumentsMutationOptions
954
+ ): Promise<SanityDocument<R>[]>
955
+ /**
956
+ * Create a document if it does not exist, or replace a document with the same document ID
957
+ * Returns a promise that resolves to a mutation result object containing the ID of the created document.
958
+ *
959
+ * @param document - Document to either create or replace
960
+ * @param options - Mutation options
961
+ */
962
+ createOrReplace<R extends Record<string, Any> = Record<string, Any>>(
963
+ document: IdentifiedSanityDocumentStub<R>,
964
+ options: FirstDocumentIdMutationOptions
965
+ ): Promise<SingleMutationResult>
966
+ /**
967
+ * Create a document if it does not exist, or replace a document with the same document ID
968
+ * Returns a promise that resolves to a mutation result object containing the created document ID.
969
+ *
970
+ * @param document - Document to either create or replace
971
+ * @param options - Mutation options
972
+ */
973
+ createOrReplace<R extends Record<string, Any> = Record<string, Any>>(
974
+ document: IdentifiedSanityDocumentStub<R>,
975
+ options: AllDocumentIdsMutationOptions
976
+ ): Promise<MultipleMutationResult>
977
+ /**
978
+ * Create a document if it does not exist, or replace a document with the same document ID
979
+ * Returns a promise that resolves to the created document.
980
+ *
981
+ * @param document - Document to either create or replace
982
+ * @param options - Mutation options
983
+ */
984
+ createOrReplace<R extends Record<string, Any> = Record<string, Any>>(
985
+ document: IdentifiedSanityDocumentStub<R>,
986
+ options?: BaseMutationOptions
987
+ ): Promise<SanityDocument<R>>
988
+ createOrReplace<R extends Record<string, Any> = Record<string, Any>>(
989
+ document: IdentifiedSanityDocumentStub<R>,
990
+ options?:
991
+ | AllDocumentIdsMutationOptions
992
+ | AllDocumentsMutationOptions
993
+ | BaseMutationOptions
994
+ | FirstDocumentIdMutationOptions
995
+ | FirstDocumentMutationOptions
996
+ ): Promise<
997
+ SanityDocument<R> | SanityDocument<R>[] | SingleMutationResult | MultipleMutationResult
998
+ > {
999
+ return lastValueFrom(
1000
+ dataMethods._createOrReplace<R>(this, this.#httpRequest, document, options)
1001
+ )
1002
+ }
1003
+
1004
+ /**
1005
+ * Deletes a document with the given document ID.
1006
+ * Returns a promise that resolves to the deleted document.
1007
+ *
1008
+ * @param id - Document ID to delete
1009
+ * @param options - Options for the mutation
1010
+ */
1011
+ delete<R extends Record<string, Any> = Record<string, Any>>(
1012
+ id: string,
1013
+ options: FirstDocumentMutationOptions
1014
+ ): Promise<SanityDocument<R>>
1015
+ /**
1016
+ * Deletes a document with the given document ID.
1017
+ * Returns a promise that resolves to an array containing the deleted document.
1018
+ *
1019
+ * @param id - Document ID to delete
1020
+ * @param options - Options for the mutation
1021
+ */
1022
+ delete<R extends Record<string, Any> = Record<string, Any>>(
1023
+ id: string,
1024
+ options: AllDocumentsMutationOptions
1025
+ ): Promise<SanityDocument<R>[]>
1026
+ /**
1027
+ * Deletes a document with the given document ID.
1028
+ * Returns a promise that resolves to a mutation result object containing the deleted document ID.
1029
+ *
1030
+ * @param id - Document ID to delete
1031
+ * @param options - Options for the mutation
1032
+ */
1033
+ delete(id: string, options: FirstDocumentIdMutationOptions): Promise<SingleMutationResult>
1034
+ /**
1035
+ * Deletes a document with the given document ID.
1036
+ * Returns a promise that resolves to a mutation result object containing the deleted document ID.
1037
+ *
1038
+ * @param id - Document ID to delete
1039
+ * @param options - Options for the mutation
1040
+ */
1041
+ delete(id: string, options: AllDocumentIdsMutationOptions): Promise<MultipleMutationResult>
1042
+ /**
1043
+ * Deletes a document with the given document ID.
1044
+ * Returns a promise that resolves to the deleted document.
1045
+ *
1046
+ * @param id - Document ID to delete
1047
+ * @param options - Options for the mutation
1048
+ */
1049
+ delete<R extends Record<string, Any> = Record<string, Any>>(
1050
+ id: string,
1051
+ options?: BaseMutationOptions
1052
+ ): Promise<SanityDocument<R>>
1053
+ /**
1054
+ * Deletes one or more documents matching the given query or document ID.
1055
+ * Returns a promise that resolves to first deleted document.
1056
+ *
1057
+ * @param selection - An object with either an `id` or `query` key defining what to delete
1058
+ * @param options - Options for the mutation
1059
+ */
1060
+ delete<R extends Record<string, Any> = Record<string, Any>>(
1061
+ selection: MutationSelection,
1062
+ options: FirstDocumentMutationOptions
1063
+ ): Promise<SanityDocument<R>>
1064
+ /**
1065
+ * Deletes one or more documents matching the given query or document ID.
1066
+ * Returns a promise that resolves to an array containing the deleted documents.
1067
+ *
1068
+ * @param selection - An object with either an `id` or `query` key defining what to delete
1069
+ * @param options - Options for the mutation
1070
+ */
1071
+ delete<R extends Record<string, Any> = Record<string, Any>>(
1072
+ selection: MutationSelection,
1073
+ options: AllDocumentsMutationOptions
1074
+ ): Promise<SanityDocument<R>[]>
1075
+ /**
1076
+ * Deletes one or more documents matching the given query or document ID.
1077
+ * Returns a promise that resolves to a mutation result object containing the ID of the first deleted document.
1078
+ *
1079
+ * @param selection - An object with either an `id` or `query` key defining what to delete
1080
+ * @param options - Options for the mutation
1081
+ */
1082
+ delete(
1083
+ selection: MutationSelection,
1084
+ options: FirstDocumentIdMutationOptions
1085
+ ): Promise<SingleMutationResult>
1086
+ /**
1087
+ * Deletes one or more documents matching the given query or document ID.
1088
+ * Returns a promise that resolves to a mutation result object containing the document IDs that were deleted.
1089
+ *
1090
+ * @param selection - An object with either an `id` or `query` key defining what to delete
1091
+ * @param options - Options for the mutation
1092
+ */
1093
+ delete(
1094
+ selection: MutationSelection,
1095
+ options: AllDocumentIdsMutationOptions
1096
+ ): Promise<MultipleMutationResult>
1097
+ /**
1098
+ * Deletes one or more documents matching the given query or document ID.
1099
+ * Returns a promise that resolves to first deleted document.
1100
+ *
1101
+ * @param selection - An object with either an `id` or `query` key defining what to delete
1102
+ * @param options - Options for the mutation
1103
+ */
1104
+ delete<R extends Record<string, Any> = Record<string, Any>>(
1105
+ selection: MutationSelection,
1106
+ options?: BaseMutationOptions
1107
+ ): Promise<SanityDocument<R>>
1108
+ delete<R extends Record<string, Any> = Record<string, Any>>(
1109
+ selection: string | MutationSelection,
1110
+ options?:
1111
+ | AllDocumentIdsMutationOptions
1112
+ | AllDocumentsMutationOptions
1113
+ | BaseMutationOptions
1114
+ | FirstDocumentIdMutationOptions
1115
+ | FirstDocumentMutationOptions
1116
+ ): Promise<
1117
+ SanityDocument<R> | SanityDocument<R>[] | SingleMutationResult | MultipleMutationResult
1118
+ > {
1119
+ return lastValueFrom(dataMethods._delete<R>(this, this.#httpRequest, selection, options))
1120
+ }
1121
+
1122
+ /**
1123
+ * Perform mutation operations against the configured dataset
1124
+ * Returns a promise that resolves to the first mutated document.
1125
+ *
1126
+ * @param operations - Mutation operations to execute
1127
+ * @param options - Mutation options
1128
+ */
1129
+ mutate<R extends Record<string, Any> = Record<string, Any>>(
1130
+ operations: Mutation<R>[] | Patch | Transaction,
1131
+ options: FirstDocumentMutationOptions
1132
+ ): Promise<SanityDocument<R>>
1133
+ /**
1134
+ * Perform mutation operations against the configured dataset.
1135
+ * Returns a promise that resolves to an array of the mutated documents.
1136
+ *
1137
+ * @param operations - Mutation operations to execute
1138
+ * @param options - Mutation options
1139
+ */
1140
+ mutate<R extends Record<string, Any> = Record<string, Any>>(
1141
+ operations: Mutation<R>[] | Patch | Transaction,
1142
+ options: AllDocumentsMutationOptions
1143
+ ): Promise<SanityDocument<R>[]>
1144
+ /**
1145
+ * Perform mutation operations against the configured dataset
1146
+ * Returns a promise that resolves to a mutation result object containing the document ID of the first mutated document.
1147
+ *
1148
+ * @param operations - Mutation operations to execute
1149
+ * @param options - Mutation options
1150
+ */
1151
+ mutate<R extends Record<string, Any> = Record<string, Any>>(
1152
+ operations: Mutation<R>[] | Patch | Transaction,
1153
+ options: FirstDocumentIdMutationOptions
1154
+ ): Promise<SingleMutationResult>
1155
+ /**
1156
+ * Perform mutation operations against the configured dataset
1157
+ * Returns a promise that resolves to a mutation result object containing the mutated document IDs.
1158
+ *
1159
+ * @param operations - Mutation operations to execute
1160
+ * @param options - Mutation options
1161
+ */
1162
+ mutate<R extends Record<string, Any>>(
1163
+ operations: Mutation<R>[] | Patch | Transaction,
1164
+ options: AllDocumentIdsMutationOptions
1165
+ ): Promise<MultipleMutationResult>
1166
+ /**
1167
+ * Perform mutation operations against the configured dataset
1168
+ * Returns a promise that resolves to the first mutated document.
1169
+ *
1170
+ * @param operations - Mutation operations to execute
1171
+ * @param options - Mutation options
1172
+ */
1173
+ mutate<R extends Record<string, Any> = Record<string, Any>>(
1174
+ operations: Mutation<R>[] | Patch | Transaction,
1175
+ options?: BaseMutationOptions
1176
+ ): Promise<SanityDocument<R>>
1177
+ mutate<R extends Record<string, Any> = Record<string, Any>>(
1178
+ operations: Mutation<R>[] | Patch | Transaction,
1179
+ options?:
1180
+ | FirstDocumentMutationOptions
1181
+ | AllDocumentsMutationOptions
1182
+ | FirstDocumentIdMutationOptions
1183
+ | AllDocumentIdsMutationOptions
1184
+ | BaseMutationOptions
1185
+ ): Promise<
1186
+ SanityDocument<R> | SanityDocument<R>[] | SingleMutationResult | MultipleMutationResult
1187
+ > {
1188
+ return lastValueFrom(dataMethods._mutate<R>(this, this.#httpRequest, operations, options))
1189
+ }
1190
+
1191
+ /**
1192
+ * Create a new buildable patch of operations to perform
1193
+ *
1194
+ * @param documentId - Document ID(s)to patch
1195
+ * @param operations - Optional object of patch operations to initialize the patch instance with
1196
+ */
1197
+ patch(documentId: PatchSelection, operations?: PatchOperations): Patch {
1198
+ return new Patch(documentId, operations, this)
1199
+ }
1200
+
1201
+ /**
1202
+ * Create a new transaction of mutations
1203
+ *
1204
+ * @param operations - Optional array of mutation operations to initialize the transaction instance with
1205
+ */
1206
+ transaction<R extends Record<string, Any> = Record<string, Any>>(
1207
+ operations?: Mutation<R>[]
1208
+ ): Transaction {
1209
+ return new Transaction(operations, this)
1210
+ }
1211
+
1212
+ /**
1213
+ * DEPRECATED: Perform an HTTP request against the Sanity API
1214
+ *
1215
+ * @deprecated Use your own request library!
1216
+ * @param options - Request options
1217
+ */
1218
+ request<R = Any>(options: RawRequestOptions): Promise<R> {
1219
+ return lastValueFrom(dataMethods._request<R>(this, this.#httpRequest, options))
1220
+ }
1221
+
1222
+ /**
1223
+ * DEPRECATED: Perform an HTTP request a `/data` sub-endpoint
1224
+ *
1225
+ * @deprecated Use your own request library!
1226
+ * @param endpoint - Endpoint to hit (mutate, query etc)
1227
+ * @param body - Request body
1228
+ * @param options - Request options
1229
+ */
1230
+ dataRequest(endpoint: string, body: unknown, options?: BaseMutationOptions): Promise<Any> {
1231
+ return lastValueFrom(dataMethods._dataRequest(this, this.#httpRequest, endpoint, body, options))
1232
+ }
1233
+
1234
+ /**
1235
+ * Get a Sanity API URL for the URI provided
1236
+ *
1237
+ * @param uri - URI/path to build URL for
1238
+ * @param canUseCdn - Whether or not to allow using the API CDN for this route
1239
+ */
1240
+ getUrl(uri: string, canUseCdn?: boolean): string {
1241
+ return dataMethods._getUrl(this, uri, canUseCdn)
1242
+ }
1243
+
1244
+ /**
1245
+ * Get a Sanity API URL for the data operation and path provided
1246
+ *
1247
+ * @param operation - Data operation (eg `query`, `mutate`, `listen` or similar)
1248
+ * @param path - Path to append after the operation
1249
+ */
1250
+ getDataUrl(operation: string, path?: string): string {
1251
+ return dataMethods._getDataUrl(this, operation, path)
1252
+ }
1253
+ }