@sanity/client 4.0.1 → 5.0.0-esm.1

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