@sanity/client 5.0.0-esm.2 → 5.0.0-esm.4

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/src/types.ts CHANGED
@@ -1,5 +1,6 @@
1
1
  import type {Requester} from 'get-it'
2
2
 
3
+ /** @public */
3
4
  export interface RequestOptions {
4
5
  timeout?: number
5
6
  token?: string
@@ -10,6 +11,7 @@ export interface RequestOptions {
10
11
  body?: any
11
12
  }
12
13
 
14
+ /** @public */
13
15
  export interface ClientConfig {
14
16
  projectId?: string
15
17
  dataset?: string
@@ -35,6 +37,7 @@ export interface ClientConfig {
35
37
  requester?: Requester
36
38
  }
37
39
 
40
+ /** @public */
38
41
  export interface InitializedClientConfig extends ClientConfig {
39
42
  // These are required in the initialized config
40
43
  apiHost: string
@@ -57,6 +60,7 @@ export interface InitializedClientConfig extends ClientConfig {
57
60
  cdnUrl: string
58
61
  }
59
62
 
63
+ /** @public */
60
64
  export type AssetMetadataType =
61
65
  | 'location'
62
66
  | 'exif'
@@ -66,6 +70,7 @@ export type AssetMetadataType =
66
70
  | 'blurhash'
67
71
  | 'none'
68
72
 
73
+ /** @public */
69
74
  export interface UploadClientConfig {
70
75
  /**
71
76
  * Optional request tag for the upload
@@ -138,10 +143,12 @@ export interface UploadClientConfig {
138
143
  }
139
144
  }
140
145
 
146
+ /** @internal */
141
147
  export interface SanityReference {
142
148
  _ref: string
143
149
  }
144
150
 
151
+ /** @internal */
145
152
  export type SanityDocument<T extends Record<string, any> = Record<string, any>> = {
146
153
  [P in keyof T]: T[P]
147
154
  } & {
@@ -152,6 +159,7 @@ export type SanityDocument<T extends Record<string, any> = Record<string, any>>
152
159
  _updatedAt: string
153
160
  }
154
161
 
162
+ /** @internal */
155
163
  export interface SanityAssetDocument extends SanityDocument {
156
164
  url: string
157
165
  path: string
@@ -164,6 +172,7 @@ export interface SanityAssetDocument extends SanityDocument {
164
172
  originalFilename?: string
165
173
  }
166
174
 
175
+ /** @internal */
167
176
  export interface SanityImagePalette {
168
177
  background: string
169
178
  foreground: string
@@ -171,6 +180,7 @@ export interface SanityImagePalette {
171
180
  title: string
172
181
  }
173
182
 
183
+ /** @internal */
174
184
  export interface SanityImageAssetDocument extends SanityAssetDocument {
175
185
  metadata: {
176
186
  _type: 'sanity.imageMetadata'
@@ -205,6 +215,7 @@ export interface SanityImageAssetDocument extends SanityAssetDocument {
205
215
  }
206
216
  }
207
217
 
218
+ /** @public */
208
219
  export interface ErrorProps {
209
220
  message: string
210
221
  response: any
@@ -213,11 +224,13 @@ export interface ErrorProps {
213
224
  details: any
214
225
  }
215
226
 
227
+ /** @public */
216
228
  export type HttpRequest = {
217
229
  defaultRequester: Requester
218
230
  (options: RequestOptions, requester: Requester): ReturnType<Requester>
219
231
  }
220
232
 
233
+ /** @internal */
221
234
  export interface RequestObservableOptions extends Omit<RequestOptions, 'url'> {
222
235
  url?: string
223
236
  uri?: string
@@ -225,6 +238,7 @@ export interface RequestObservableOptions extends Omit<RequestOptions, 'url'> {
225
238
  tag?: string
226
239
  }
227
240
 
241
+ /** @public */
228
242
  export interface ProgressEvent {
229
243
  type: 'progress'
230
244
  stage: 'upload' | 'download'
@@ -234,6 +248,7 @@ export interface ProgressEvent {
234
248
  lengthComputable: boolean
235
249
  }
236
250
 
251
+ /** @public */
237
252
  export interface ResponseEvent<T = unknown> {
238
253
  type: 'response'
239
254
  body: T
@@ -244,21 +259,28 @@ export interface ResponseEvent<T = unknown> {
244
259
  headers: Record<string, string>
245
260
  }
246
261
 
262
+ /** @public */
247
263
  export type HttpRequestEvent<T = unknown> = ResponseEvent<T> | ProgressEvent
248
264
 
265
+ /** @internal */
249
266
  export interface AuthProvider {
250
267
  name: string
251
268
  title: string
252
269
  url: string
253
270
  }
254
271
 
272
+ /** @internal */
255
273
  export type AuthProviderResponse = {providers: AuthProvider[]}
256
274
 
275
+ /** @internal */
257
276
  export type DatasetAclMode = 'public' | 'private' | 'custom'
258
277
 
278
+ /** @internal */
259
279
  export type DatasetResponse = {datasetName: string; aclMode: DatasetAclMode}
280
+ /** @internal */
260
281
  export type DatasetsResponse = {name: string; aclMode: DatasetAclMode}[]
261
282
 
283
+ /** @internal */
262
284
  export interface SanityProjectMember {
263
285
  id: string
264
286
  role: string
@@ -266,6 +288,7 @@ export interface SanityProjectMember {
266
288
  isCurrentUser: boolean
267
289
  }
268
290
 
291
+ /** @internal */
269
292
  export interface SanityProject {
270
293
  id: string
271
294
  displayName: string
@@ -284,6 +307,7 @@ export interface SanityProject {
284
307
  }
285
308
  }
286
309
 
310
+ /** @internal */
287
311
  export interface SanityUser {
288
312
  id: string
289
313
  projectId: string
@@ -297,6 +321,7 @@ export interface SanityUser {
297
321
  isCurrentUser: boolean
298
322
  }
299
323
 
324
+ /** @internal */
300
325
  export interface CurrentSanityUser {
301
326
  id: string
302
327
  name: string
@@ -305,24 +330,28 @@ export interface CurrentSanityUser {
305
330
  role: string
306
331
  }
307
332
 
333
+ /** @public */
308
334
  export type SanityDocumentStub<T extends Record<string, any> = Record<string, any>> = {
309
335
  [P in keyof T]: T[P]
310
336
  } & {
311
337
  _type: string
312
338
  }
313
339
 
340
+ /** @public */
314
341
  export type IdentifiedSanityDocumentStub<T extends Record<string, any> = Record<string, any>> = {
315
342
  [P in keyof T]: T[P]
316
343
  } & {
317
344
  _id: string
318
345
  } & SanityDocumentStub
319
346
 
347
+ /** @internal */
320
348
  export type InsertPatch =
321
349
  | {before: string; items: any[]}
322
350
  | {after: string; items: any[]}
323
351
  | {replace: string; items: any[]}
324
352
 
325
353
  // Note: this is actually incorrect/invalid, but implemented as-is for backwards compatibility
354
+ /** @internal */
326
355
  export interface PatchOperations {
327
356
  set?: {[key: string]: any}
328
357
  setIfMissing?: {[key: string]: any}
@@ -334,11 +363,16 @@ export interface PatchOperations {
334
363
  ifRevisionID?: string
335
364
  }
336
365
 
366
+ /** @public */
337
367
  export type QueryParams = {[key: string]: any}
368
+ /** @internal */
338
369
  export type MutationSelection = {query: string; params?: QueryParams} | {id: string | string[]}
370
+ /** @internal */
339
371
  export type PatchSelection = string | string[] | MutationSelection
372
+ /** @internal */
340
373
  export type PatchMutationOperation = PatchOperations & MutationSelection
341
374
 
375
+ /** @public */
342
376
  export type Mutation<R extends Record<string, any> = Record<string, any>> =
343
377
  | {create: SanityDocumentStub<R>}
344
378
  | {createOrReplace: IdentifiedSanityDocumentStub<R>}
@@ -346,6 +380,7 @@ export type Mutation<R extends Record<string, any> = Record<string, any>> =
346
380
  | {delete: MutationSelection}
347
381
  | {patch: PatchMutationOperation}
348
382
 
383
+ /** @public */
349
384
  export type MutationEvent<R extends Record<string, any> = Record<string, any>> = {
350
385
  type: 'mutation'
351
386
  documentId: string
@@ -363,24 +398,29 @@ export type MutationEvent<R extends Record<string, any> = Record<string, any>> =
363
398
  visibility: 'query' | 'transaction'
364
399
  }
365
400
 
401
+ /** @public */
366
402
  export type ChannelErrorEvent = {
367
403
  type: 'channelError'
368
404
  message: string
369
405
  }
370
406
 
407
+ /** @public */
371
408
  export type DisconnectEvent = {
372
409
  type: 'disconnect'
373
410
  reason: string
374
411
  }
375
412
 
413
+ /** @public */
376
414
  export type ReconnectEvent = {
377
415
  type: 'reconnect'
378
416
  }
379
417
 
418
+ /** @public */
380
419
  export type WelcomeEvent = {
381
420
  type: 'welcome'
382
421
  }
383
422
 
423
+ /** @public */
384
424
  export type ListenEvent<R extends Record<string, any>> =
385
425
  | MutationEvent<R>
386
426
  | ChannelErrorEvent
@@ -388,8 +428,10 @@ export type ListenEvent<R extends Record<string, any>> =
388
428
  | ReconnectEvent
389
429
  | WelcomeEvent
390
430
 
431
+ /** @public */
391
432
  export type ListenEventName = 'mutation' | 'welcome' | 'reconnect'
392
433
 
434
+ /** @public */
393
435
  export interface ListenOptions {
394
436
  includeResult?: boolean
395
437
  includePreviousRevision?: boolean
@@ -399,20 +441,24 @@ export interface ListenOptions {
399
441
  tag?: string
400
442
  }
401
443
 
444
+ /** @internal */
402
445
  export type FilteredResponseQueryOptions = RequestOptions & {
403
446
  filterResponse?: true
404
447
  }
405
448
 
449
+ /** @internal */
406
450
  export type UnfilteredResponseQueryOptions = RequestOptions & {
407
451
  filterResponse: false
408
452
  }
409
453
 
454
+ /** @internal */
410
455
  export interface RawQueryResponse<R> {
411
456
  q: string
412
457
  ms: number
413
458
  result: R
414
459
  }
415
460
 
461
+ /** @internal */
416
462
  export type BaseMutationOptions = RequestOptions & {
417
463
  visibility?: 'sync' | 'async' | 'deferred'
418
464
  returnDocuments?: boolean
@@ -422,68 +468,82 @@ export type BaseMutationOptions = RequestOptions & {
422
468
  skipCrossDatasetReferenceValidation?: boolean
423
469
  }
424
470
 
471
+ /** @internal */
425
472
  export type FirstDocumentMutationOptions = BaseMutationOptions & {
426
473
  returnFirst?: true
427
474
  returnDocuments?: true
428
475
  }
429
476
 
477
+ /** @internal */
430
478
  export type FirstDocumentIdMutationOptions = BaseMutationOptions & {
431
479
  returnFirst?: true
432
480
  returnDocuments: false
433
481
  }
434
482
 
483
+ /** @internal */
435
484
  export type AllDocumentsMutationOptions = BaseMutationOptions & {
436
485
  returnFirst: false
437
486
  returnDocuments?: true
438
487
  }
439
488
 
489
+ /** @internal */
440
490
  export type MutationOperation = 'create' | 'delete' | 'update' | 'none'
441
491
 
492
+ /** @internal */
442
493
  export interface SingleMutationResult {
443
494
  transactionId: string
444
495
  documentId: string
445
496
  results: {id: string; operation: MutationOperation}[]
446
497
  }
447
498
 
499
+ /** @internal */
448
500
  export interface MultipleMutationResult {
449
501
  transactionId: string
450
502
  documentIds: string[]
451
503
  results: {id: string; operation: MutationOperation}[]
452
504
  }
453
505
 
506
+ /** @internal */
454
507
  export type AllDocumentIdsMutationOptions = BaseMutationOptions & {
455
508
  returnFirst: false
456
509
  returnDocuments: false
457
510
  }
458
511
 
512
+ /** @internal */
459
513
  export type AttributeSet = {[key: string]: any}
460
514
 
515
+ /** @internal */
461
516
  export type TransactionFirstDocumentMutationOptions = BaseMutationOptions & {
462
517
  returnFirst: true
463
518
  returnDocuments: true
464
519
  }
465
520
 
521
+ /** @internal */
466
522
  export type TransactionFirstDocumentIdMutationOptions = BaseMutationOptions & {
467
523
  returnFirst: true
468
524
  returnDocuments?: false
469
525
  }
470
526
 
527
+ /** @internal */
471
528
  export type TransactionAllDocumentsMutationOptions = BaseMutationOptions & {
472
529
  returnFirst?: false
473
530
  returnDocuments: true
474
531
  }
475
532
 
533
+ /** @internal */
476
534
  export type TransactionAllDocumentIdsMutationOptions = BaseMutationOptions & {
477
535
  returnFirst?: false
478
536
  returnDocuments?: false
479
537
  }
480
538
 
539
+ /** @internal */
481
540
  export type TransactionMutationOptions =
482
541
  | TransactionFirstDocumentMutationOptions
483
542
  | TransactionFirstDocumentIdMutationOptions
484
543
  | TransactionAllDocumentsMutationOptions
485
544
  | TransactionAllDocumentIdsMutationOptions
486
545
 
546
+ /** @internal */
487
547
  export interface RawRequestOptions {
488
548
  url?: string
489
549
  uri?: string
@@ -1,10 +1,17 @@
1
- import {type Observable} from 'rxjs'
1
+ import {type Observable, lastValueFrom} from 'rxjs'
2
2
 
3
+ import {_request} from '../data/dataMethods'
3
4
  import type {ObservableSanityClient, SanityClient} from '../SanityClient'
4
- import type {CurrentSanityUser, SanityUser} from '../types'
5
+ import type {CurrentSanityUser, HttpRequest, SanityUser} from '../types'
5
6
 
6
- export class BaseUsersClient {
7
- client: SanityClient | ObservableSanityClient
7
+ /** @public */
8
+ export class ObservableUsersClient {
9
+ #client: ObservableSanityClient
10
+ #httpRequest: HttpRequest
11
+ constructor(client: ObservableSanityClient, httpRequest: HttpRequest) {
12
+ this.#client = client
13
+ this.#httpRequest = httpRequest
14
+ }
8
15
 
9
16
  /**
10
17
  * Fetch a user by user ID
@@ -12,35 +19,37 @@ export class BaseUsersClient {
12
19
  * @param id - User ID of the user to fetch. If `me` is provided, a minimal response including the users role is returned.
13
20
  */
14
21
  getById<T extends 'me' | string>(
15
- this: UsersClient,
16
- id: T
17
- ): Promise<T extends 'me' ? CurrentSanityUser : SanityUser>
18
- getById<T extends 'me' | string>(
19
- this: ObservableUsersClient,
20
22
  id: T
21
- ): Observable<T extends 'me' ? CurrentSanityUser : SanityUser>
22
- getById<T extends 'me' | string>(
23
- id: T
24
- ):
25
- | Promise<T extends 'me' ? CurrentSanityUser : SanityUser>
26
- | Observable<T extends 'me' ? CurrentSanityUser : SanityUser> {
27
- return this.client.request({uri: `/users/${id}`})
23
+ ): Observable<T extends 'me' ? CurrentSanityUser : SanityUser> {
24
+ return _request<T extends 'me' ? CurrentSanityUser : SanityUser>(
25
+ this.#client,
26
+ this.#httpRequest,
27
+ {uri: `/users/${id}`}
28
+ )
28
29
  }
29
30
  }
30
31
 
31
- export class ObservableUsersClient extends BaseUsersClient {
32
- client: ObservableSanityClient
33
-
34
- constructor(client: ObservableSanityClient) {
35
- super()
36
- this.client = client
32
+ /** @public */
33
+ export class UsersClient {
34
+ #client: SanityClient
35
+ #httpRequest: HttpRequest
36
+ constructor(client: SanityClient, httpRequest: HttpRequest) {
37
+ this.#client = client
38
+ this.#httpRequest = httpRequest
37
39
  }
38
- }
39
40
 
40
- export class UsersClient extends BaseUsersClient {
41
- client: SanityClient
42
- constructor(client: SanityClient) {
43
- super()
44
- this.client = client
41
+ /**
42
+ * Fetch a user by user ID
43
+ *
44
+ * @param id - User ID of the user to fetch. If `me` is provided, a minimal response including the users role is returned.
45
+ */
46
+ getById<T extends 'me' | string>(
47
+ id: T
48
+ ): Promise<T extends 'me' ? CurrentSanityUser : SanityUser> {
49
+ return lastValueFrom(
50
+ _request<T extends 'me' ? CurrentSanityUser : SanityUser>(this.#client, this.#httpRequest, {
51
+ uri: `/users/${id}`,
52
+ })
53
+ )
45
54
  }
46
55
  }
package/src/validators.ts CHANGED
@@ -1,3 +1,5 @@
1
+ import type {InitializedClientConfig} from './types'
2
+
1
3
  const VALID_ASSET_TYPES = ['image', 'file']
2
4
  const VALID_INSERT_LOCATIONS = ['before', 'after', 'replace']
3
5
 
@@ -9,7 +11,7 @@ export const dataset = (name: string) => {
9
11
  }
10
12
  }
11
13
 
12
- export const projectId = (id: any) => {
14
+ export const projectId = (id: string) => {
13
15
  if (!/^[-a-z0-9]+$/i.test(id)) {
14
16
  throw new Error('`projectId` can only contain only a-z, 0-9 and dashes')
15
17
  }
@@ -57,7 +59,7 @@ export const validateInsert = (at: string, selector: string, items: any[]) => {
57
59
  }
58
60
  }
59
61
 
60
- export const hasDataset = (config: any): string => {
62
+ export const hasDataset = (config: InitializedClientConfig): string => {
61
63
  if (!config.dataset) {
62
64
  throw new Error('`dataset` must be provided to perform queries')
63
65
  }