@sanity/client 6.4.10-dev.0 → 6.4.11
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/dist/index.browser.cjs +7 -6
- package/dist/index.browser.cjs.map +1 -1
- package/dist/index.browser.js +7 -6
- package/dist/index.browser.js.map +1 -1
- package/dist/index.cjs +8 -7
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.ts +192 -92
- package/dist/index.js +8 -7
- package/dist/index.js.map +1 -1
- package/package.json +12 -14
- package/src/SanityClient.ts +61 -6
- package/src/data/listen.ts +5 -5
- package/src/types.ts +169 -80
- package/umd/sanityClient.js +27 -429
- package/umd/sanityClient.min.js +3 -3
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@sanity/client",
|
|
3
|
-
"version": "6.4.
|
|
3
|
+
"version": "6.4.11",
|
|
4
4
|
"description": "Client for retrieving, creating and patching data from Sanity.io",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"sanity",
|
|
@@ -40,10 +40,8 @@
|
|
|
40
40
|
"source": "./src/index.ts",
|
|
41
41
|
"require": "./dist/index.cjs",
|
|
42
42
|
"node": {
|
|
43
|
-
"module": "./dist/index.js",
|
|
44
43
|
"import": "./dist/index.cjs.js"
|
|
45
44
|
},
|
|
46
|
-
"import": "./dist/index.js",
|
|
47
45
|
"default": "./dist/index.js"
|
|
48
46
|
},
|
|
49
47
|
"./package.json": "./package.json"
|
|
@@ -72,7 +70,7 @@
|
|
|
72
70
|
"prepublishOnly": "npm run build",
|
|
73
71
|
"rollup": "NODE_ENV=production rollup -c rollup.config.cjs",
|
|
74
72
|
"test": "vitest",
|
|
75
|
-
"type-check": "tsc --noEmit
|
|
73
|
+
"type-check": "tsc --noEmit",
|
|
76
74
|
"test:browser": "npm test -- --config ./vitest.browser.config.ts",
|
|
77
75
|
"test:bun": "(cd runtimes/bun && bun wiptest)",
|
|
78
76
|
"test:deno": "deno test --allow-read --allow-net --allow-env --fail-fast --import-map=runtimes/deno/import_map.json runtimes/deno",
|
|
@@ -97,16 +95,16 @@
|
|
|
97
95
|
"rxjs": "^7.0.0"
|
|
98
96
|
},
|
|
99
97
|
"devDependencies": {
|
|
100
|
-
"@edge-runtime/types": "^2.2.
|
|
101
|
-
"@edge-runtime/vm": "^3.1.
|
|
98
|
+
"@edge-runtime/types": "^2.2.1",
|
|
99
|
+
"@edge-runtime/vm": "^3.1.1",
|
|
102
100
|
"@rollup/plugin-commonjs": "^25.0.4",
|
|
103
101
|
"@rollup/plugin-node-resolve": "^15.2.1",
|
|
104
102
|
"@sanity/pkg-utils": "^2.4.8",
|
|
105
103
|
"@sanity/semantic-release-preset": "^4.1.4",
|
|
106
104
|
"@types/node": "^20.5.9",
|
|
107
|
-
"@typescript-eslint/eslint-plugin": "^6.
|
|
108
|
-
"@typescript-eslint/parser": "^6.
|
|
109
|
-
"@vitest/coverage-v8": "0.34.3",
|
|
105
|
+
"@typescript-eslint/eslint-plugin": "^6.6.0",
|
|
106
|
+
"@typescript-eslint/parser": "^6.6.0",
|
|
107
|
+
"@vitest/coverage-v8": "^0.34.3",
|
|
110
108
|
"eslint": "^8.48.0",
|
|
111
109
|
"eslint-config-prettier": "^9.0.0",
|
|
112
110
|
"eslint-plugin-prettier": "^5.0.0",
|
|
@@ -118,18 +116,18 @@
|
|
|
118
116
|
"prettier": "^3.0.3",
|
|
119
117
|
"prettier-plugin-packagejson": "^2.4.5",
|
|
120
118
|
"rimraf": "^5.0.1",
|
|
121
|
-
"rollup": "^3.
|
|
119
|
+
"rollup": "^3.29.0",
|
|
122
120
|
"sse-channel": "^4.0.0",
|
|
123
|
-
"terser": "^5.19.
|
|
121
|
+
"terser": "^5.19.4",
|
|
124
122
|
"typescript": "^5.2.2",
|
|
125
|
-
"vitest": "0.34.3",
|
|
126
|
-
"vitest-github-actions-reporter": "0.10.0"
|
|
123
|
+
"vitest": "^0.34.3",
|
|
124
|
+
"vitest-github-actions-reporter": "^0.10.0"
|
|
127
125
|
},
|
|
128
126
|
"engines": {
|
|
129
127
|
"node": ">=14.18"
|
|
130
128
|
},
|
|
131
129
|
"publishConfig": {
|
|
132
130
|
"access": "public",
|
|
133
|
-
"provenance":
|
|
131
|
+
"provenance": true
|
|
134
132
|
}
|
|
135
133
|
}
|
package/src/SanityClient.ts
CHANGED
|
@@ -586,11 +586,39 @@ export class ObservableSanityClient {
|
|
|
586
586
|
/**
|
|
587
587
|
* Create a new buildable patch of operations to perform
|
|
588
588
|
*
|
|
589
|
-
* @param documentId - Document ID
|
|
589
|
+
* @param documentId - Document ID to patch
|
|
590
590
|
* @param operations - Optional object of patch operations to initialize the patch instance with
|
|
591
|
+
* @returns Patch instance - call `.commit()` to perform the operations defined
|
|
591
592
|
*/
|
|
592
|
-
patch(documentId:
|
|
593
|
-
|
|
593
|
+
patch(documentId: string, operations?: PatchOperations): ObservablePatch
|
|
594
|
+
|
|
595
|
+
/**
|
|
596
|
+
* Create a new buildable patch of operations to perform
|
|
597
|
+
*
|
|
598
|
+
* @param documentIds - Array of document IDs to patch
|
|
599
|
+
* @param operations - Optional object of patch operations to initialize the patch instance with
|
|
600
|
+
* @returns Patch instance - call `.commit()` to perform the operations defined
|
|
601
|
+
*/
|
|
602
|
+
patch(documentIds: string[], operations?: PatchOperations): ObservablePatch
|
|
603
|
+
|
|
604
|
+
/**
|
|
605
|
+
* Create a new buildable patch of operations to perform
|
|
606
|
+
*
|
|
607
|
+
* @param selection - An object with `query` and optional `params`, defining which document(s) to patch
|
|
608
|
+
* @param operations - Optional object of patch operations to initialize the patch instance with
|
|
609
|
+
* @returns Patch instance - call `.commit()` to perform the operations defined
|
|
610
|
+
*/
|
|
611
|
+
patch(selection: MutationSelection, operations?: PatchOperations): ObservablePatch
|
|
612
|
+
|
|
613
|
+
/**
|
|
614
|
+
* Create a new buildable patch of operations to perform
|
|
615
|
+
*
|
|
616
|
+
* @param selection - Document ID, an array of document IDs, or an object with `query` and optional `params`, defining which document(s) to patch
|
|
617
|
+
* @param operations - Optional object of patch operations to initialize the patch instance with
|
|
618
|
+
* @returns Patch instance - call `.commit()` to perform the operations defined
|
|
619
|
+
*/
|
|
620
|
+
patch(selection: PatchSelection, operations?: PatchOperations): ObservablePatch {
|
|
621
|
+
return new ObservablePatch(selection, operations, this)
|
|
594
622
|
}
|
|
595
623
|
|
|
596
624
|
/**
|
|
@@ -605,9 +633,8 @@ export class ObservableSanityClient {
|
|
|
605
633
|
}
|
|
606
634
|
|
|
607
635
|
/**
|
|
608
|
-
*
|
|
636
|
+
* Perform an HTTP request against the Sanity API
|
|
609
637
|
*
|
|
610
|
-
* @deprecated Use your own request library!
|
|
611
638
|
* @param options - Request options
|
|
612
639
|
*/
|
|
613
640
|
request<R = Any>(options: RawRequestOptions): Observable<R> {
|
|
@@ -1191,8 +1218,36 @@ export class SanityClient {
|
|
|
1191
1218
|
/**
|
|
1192
1219
|
* Create a new buildable patch of operations to perform
|
|
1193
1220
|
*
|
|
1194
|
-
* @param documentId - Document ID
|
|
1221
|
+
* @param documentId - Document ID to patch
|
|
1222
|
+
* @param operations - Optional object of patch operations to initialize the patch instance with
|
|
1223
|
+
* @returns Patch instance - call `.commit()` to perform the operations defined
|
|
1224
|
+
*/
|
|
1225
|
+
patch(documentId: string, operations?: PatchOperations): Patch
|
|
1226
|
+
|
|
1227
|
+
/**
|
|
1228
|
+
* Create a new buildable patch of operations to perform
|
|
1229
|
+
*
|
|
1230
|
+
* @param documentIds - Array of document IDs to patch
|
|
1231
|
+
* @param operations - Optional object of patch operations to initialize the patch instance with
|
|
1232
|
+
* @returns Patch instance - call `.commit()` to perform the operations defined
|
|
1233
|
+
*/
|
|
1234
|
+
patch(documentIds: string[], operations?: PatchOperations): Patch
|
|
1235
|
+
|
|
1236
|
+
/**
|
|
1237
|
+
* Create a new buildable patch of operations to perform
|
|
1238
|
+
*
|
|
1239
|
+
* @param selection - An object with `query` and optional `params`, defining which document(s) to patch
|
|
1240
|
+
* @param operations - Optional object of patch operations to initialize the patch instance with
|
|
1241
|
+
* @returns Patch instance - call `.commit()` to perform the operations defined
|
|
1242
|
+
*/
|
|
1243
|
+
patch(selection: MutationSelection, operations?: PatchOperations): Patch
|
|
1244
|
+
|
|
1245
|
+
/**
|
|
1246
|
+
* Create a new buildable patch of operations to perform
|
|
1247
|
+
*
|
|
1248
|
+
* @param selection - Document ID, an array of document IDs, or an object with `query` and optional `params`, defining which document(s) to patch
|
|
1195
1249
|
* @param operations - Optional object of patch operations to initialize the patch instance with
|
|
1250
|
+
* @returns Patch instance - call `.commit()` to perform the operations defined
|
|
1196
1251
|
*/
|
|
1197
1252
|
patch(documentId: PatchSelection, operations?: PatchOperations): Patch {
|
|
1198
1253
|
return new Patch(documentId, operations, this)
|
package/src/data/listen.ts
CHANGED
|
@@ -29,8 +29,8 @@ const defaultOptions = {
|
|
|
29
29
|
*
|
|
30
30
|
* @param query - GROQ-filter to listen to changes for
|
|
31
31
|
* @param params - Optional query parameters
|
|
32
|
-
* @param options -
|
|
33
|
-
* @
|
|
32
|
+
* @param options - Optional listener options
|
|
33
|
+
* @public
|
|
34
34
|
*/
|
|
35
35
|
export function _listen<R extends Record<string, Any> = Record<string, Any>>(
|
|
36
36
|
this: SanityClient | ObservableSanityClient,
|
|
@@ -42,8 +42,8 @@ export function _listen<R extends Record<string, Any> = Record<string, Any>>(
|
|
|
42
42
|
*
|
|
43
43
|
* @param query - GROQ-filter to listen to changes for
|
|
44
44
|
* @param params - Optional query parameters
|
|
45
|
-
* @param options -
|
|
46
|
-
* @
|
|
45
|
+
* @param options - Optional listener options
|
|
46
|
+
* @public
|
|
47
47
|
*/
|
|
48
48
|
export function _listen<R extends Record<string, Any> = Record<string, Any>>(
|
|
49
49
|
this: SanityClient | ObservableSanityClient,
|
|
@@ -51,7 +51,7 @@ export function _listen<R extends Record<string, Any> = Record<string, Any>>(
|
|
|
51
51
|
params?: QueryParams,
|
|
52
52
|
options?: ListenOptions,
|
|
53
53
|
): Observable<ListenEvent<R>>
|
|
54
|
-
/** @
|
|
54
|
+
/** @public */
|
|
55
55
|
export function _listen<R extends Record<string, Any> = Record<string, Any>>(
|
|
56
56
|
this: SanityClient | ObservableSanityClient,
|
|
57
57
|
query: string,
|
package/src/types.ts
CHANGED
|
@@ -439,42 +439,154 @@ export type Mutation<R extends Record<string, Any> = Record<string, Any>> =
|
|
|
439
439
|
| {delete: MutationSelection}
|
|
440
440
|
| {patch: PatchMutationOperation}
|
|
441
441
|
|
|
442
|
-
/**
|
|
442
|
+
/**
|
|
443
|
+
* A mutation was performed. Note that when updating multiple documents in a transaction,
|
|
444
|
+
* each document affected will get a separate mutation event.
|
|
445
|
+
*
|
|
446
|
+
* @public
|
|
447
|
+
*/
|
|
443
448
|
export type MutationEvent<R extends Record<string, Any> = Record<string, Any>> = {
|
|
444
449
|
type: 'mutation'
|
|
450
|
+
|
|
451
|
+
/**
|
|
452
|
+
* The ID of the document that was affected
|
|
453
|
+
*/
|
|
445
454
|
documentId: string
|
|
455
|
+
|
|
456
|
+
/**
|
|
457
|
+
* A unique ID for this event
|
|
458
|
+
*/
|
|
446
459
|
eventId: string
|
|
460
|
+
|
|
461
|
+
/**
|
|
462
|
+
* The user ID of the user that performed the mutation
|
|
463
|
+
*/
|
|
447
464
|
identity: string
|
|
465
|
+
|
|
466
|
+
/**
|
|
467
|
+
* An array of mutations that were performed. Note that this can differ slightly from the
|
|
468
|
+
* mutations sent to the server, as the server may perform some mutations automatically.
|
|
469
|
+
*/
|
|
448
470
|
mutations: Mutation[]
|
|
471
|
+
|
|
472
|
+
/**
|
|
473
|
+
* The revision ID of the document before the mutation was performed
|
|
474
|
+
*/
|
|
449
475
|
previousRev?: string
|
|
476
|
+
|
|
477
|
+
/**
|
|
478
|
+
* The revision ID of the document after the mutation was performed
|
|
479
|
+
*/
|
|
450
480
|
resultRev?: string
|
|
481
|
+
|
|
482
|
+
/**
|
|
483
|
+
* The document as it looked after the mutation was performed. This is only included if
|
|
484
|
+
* the listener was configured with `includeResult: true`.
|
|
485
|
+
*/
|
|
451
486
|
result?: SanityDocument<R>
|
|
487
|
+
|
|
488
|
+
/**
|
|
489
|
+
* The document as it looked before the mutation was performed. This is only included if
|
|
490
|
+
* the listener was configured with `includePreviousRevision: true`.
|
|
491
|
+
*/
|
|
452
492
|
previous?: SanityDocument<R> | null
|
|
493
|
+
|
|
494
|
+
/**
|
|
495
|
+
* The effects of the mutation, if the listener was configured with `effectFormat: 'mendoza'`.
|
|
496
|
+
* Object with `apply` and `revert` arrays, see {@link https://github.com/sanity-io/mendoza}.
|
|
497
|
+
*/
|
|
453
498
|
effects?: {apply: unknown[]; revert: unknown[]}
|
|
499
|
+
|
|
500
|
+
/**
|
|
501
|
+
* A timestamp for when the mutation was performed
|
|
502
|
+
*/
|
|
454
503
|
timestamp: string
|
|
504
|
+
|
|
505
|
+
/**
|
|
506
|
+
* The transaction ID for the mutation
|
|
507
|
+
*/
|
|
455
508
|
transactionId: string
|
|
509
|
+
|
|
510
|
+
/**
|
|
511
|
+
* The type of transition the document went through.
|
|
512
|
+
*
|
|
513
|
+
* - `update` means the document was previously part of the subscribed set of documents,
|
|
514
|
+
* and still is.
|
|
515
|
+
* - `appear` means the document was not previously part of the subscribed set of documents,
|
|
516
|
+
* but is now. This can happen both on create or if updating to a state where it now matches
|
|
517
|
+
* the filter provided to the listener.
|
|
518
|
+
* - `disappear` means the document was previously part of the subscribed set of documents,
|
|
519
|
+
* but is no longer. This can happen both on delete or if updating to a state where it no
|
|
520
|
+
* longer matches the filter provided to the listener.
|
|
521
|
+
*/
|
|
456
522
|
transition: 'update' | 'appear' | 'disappear'
|
|
523
|
+
|
|
524
|
+
/**
|
|
525
|
+
* Whether the change that triggered this event is visible to queries (query) or only to
|
|
526
|
+
* subsequent transactions (transaction). The listener client can specify a preferred visibility
|
|
527
|
+
* through the `visibility` parameter on the listener, but this is only on a best-effort basis,
|
|
528
|
+
* and may yet not be accurate.
|
|
529
|
+
*/
|
|
457
530
|
visibility: 'query' | 'transaction'
|
|
531
|
+
|
|
532
|
+
/**
|
|
533
|
+
* The total number of events that will be sent for this transaction.
|
|
534
|
+
* Note that this may differ from the amount of _documents_ affected by the transaction, as this
|
|
535
|
+
* number only includes the documents that matches the given filter.
|
|
536
|
+
*
|
|
537
|
+
* This can be useful if you need to perform changes to all matched documents atomically,
|
|
538
|
+
* eg you would wait for `transactionTotalEvents` events with the same `transactionId` before
|
|
539
|
+
* applying the changes locally.
|
|
540
|
+
*/
|
|
541
|
+
transactionTotalEvents: number
|
|
542
|
+
|
|
543
|
+
/**
|
|
544
|
+
* The index of this event within the transaction. Note that events may be delivered out of order,
|
|
545
|
+
* and that the index is zero-based.
|
|
546
|
+
*/
|
|
547
|
+
transactionCurrentEvent: number
|
|
458
548
|
}
|
|
459
549
|
|
|
460
|
-
/**
|
|
550
|
+
/**
|
|
551
|
+
* An error occurred. This is different from a network-level error (which will be emitted as 'error').
|
|
552
|
+
* Possible causes are things such as malformed filters, non-existant datasets or similar.
|
|
553
|
+
*
|
|
554
|
+
* @public
|
|
555
|
+
*/
|
|
461
556
|
export type ChannelErrorEvent = {
|
|
462
557
|
type: 'channelError'
|
|
463
558
|
message: string
|
|
464
559
|
}
|
|
465
560
|
|
|
466
|
-
/**
|
|
561
|
+
/**
|
|
562
|
+
* The listener has been told to explicitly disconnect and not reconnect.
|
|
563
|
+
* This is a rare situation, but may occur if the API knows reconnect attempts will fail,
|
|
564
|
+
* eg in the case of a deleted dataset, a blocked project or similar events.
|
|
565
|
+
*
|
|
566
|
+
* Note that this is not treated as an error on the observable, but will complete the observable.
|
|
567
|
+
*
|
|
568
|
+
* @public
|
|
569
|
+
*/
|
|
467
570
|
export type DisconnectEvent = {
|
|
468
571
|
type: 'disconnect'
|
|
469
572
|
reason: string
|
|
470
573
|
}
|
|
471
574
|
|
|
472
|
-
/**
|
|
575
|
+
/**
|
|
576
|
+
* The listener has been disconnected, and a reconnect attempt is scheduled.
|
|
577
|
+
*
|
|
578
|
+
* @public
|
|
579
|
+
*/
|
|
473
580
|
export type ReconnectEvent = {
|
|
474
581
|
type: 'reconnect'
|
|
475
582
|
}
|
|
476
583
|
|
|
477
|
-
/**
|
|
584
|
+
/**
|
|
585
|
+
* The listener has been established, and will start receiving events.
|
|
586
|
+
* Note that this is also emitted upon _reconnection_.
|
|
587
|
+
*
|
|
588
|
+
* @public
|
|
589
|
+
*/
|
|
478
590
|
export type WelcomeEvent = {
|
|
479
591
|
type: 'welcome'
|
|
480
592
|
}
|
|
@@ -488,15 +600,65 @@ export type ListenEvent<R extends Record<string, Any>> =
|
|
|
488
600
|
| WelcomeEvent
|
|
489
601
|
|
|
490
602
|
/** @public */
|
|
491
|
-
export type ListenEventName =
|
|
603
|
+
export type ListenEventName =
|
|
604
|
+
/** A mutation was performed */
|
|
605
|
+
| 'mutation'
|
|
606
|
+
/** The listener has been (re)established */
|
|
607
|
+
| 'welcome'
|
|
608
|
+
/** The listener has been disconnected, and a reconnect attempt is scheduled */
|
|
609
|
+
| 'reconnect'
|
|
492
610
|
|
|
493
611
|
/** @public */
|
|
494
612
|
export interface ListenOptions {
|
|
613
|
+
/**
|
|
614
|
+
* Whether or not to include the resulting document in addition to the mutations performed.
|
|
615
|
+
* If you do not need the actual document, set this to `false` to reduce bandwidth usage.
|
|
616
|
+
* The result will be available on the `.result` property of the events.
|
|
617
|
+
* @defaultValue `true`
|
|
618
|
+
*/
|
|
495
619
|
includeResult?: boolean
|
|
620
|
+
|
|
621
|
+
/**
|
|
622
|
+
* Whether or not to include the document as it looked before the mutation event.
|
|
623
|
+
* The previous revision will be available on the `.previous` property of the events,
|
|
624
|
+
* and may be `null` in the case of a new document.
|
|
625
|
+
* @defaultValue `false`
|
|
626
|
+
*/
|
|
496
627
|
includePreviousRevision?: boolean
|
|
497
|
-
|
|
628
|
+
|
|
629
|
+
/**
|
|
630
|
+
* Whether events should be sent as soon as a transaction has been committed (`transaction`, default),
|
|
631
|
+
* or only after they are available for queries (query). Note that this is on a best-effort basis,
|
|
632
|
+
* and listeners with `query` may in certain cases (notably with deferred transactions) receive events
|
|
633
|
+
* that are not yet visible to queries.
|
|
634
|
+
*
|
|
635
|
+
* @defaultValue `'transaction'`
|
|
636
|
+
*/
|
|
637
|
+
visibility?: 'transaction' | 'query'
|
|
638
|
+
|
|
639
|
+
/**
|
|
640
|
+
* Array of event names to include in the observable. By default, only mutation events are included.
|
|
641
|
+
*
|
|
642
|
+
* @defaultValue `['mutation']`
|
|
643
|
+
*/
|
|
498
644
|
events?: ListenEventName[]
|
|
645
|
+
|
|
646
|
+
/**
|
|
647
|
+
* Format of "effects", eg the resulting changes of a mutation.
|
|
648
|
+
* Currently only `mendoza` is supported, and (if set) will include `apply` and `revert` arrays
|
|
649
|
+
* in the mutation events under the `effects` property.
|
|
650
|
+
*
|
|
651
|
+
* See {@link https://github.com/sanity-io/mendoza | The mendoza docs} for more info
|
|
652
|
+
*
|
|
653
|
+
* @defaultValue `undefined`
|
|
654
|
+
*/
|
|
499
655
|
effectFormat?: 'mendoza'
|
|
656
|
+
|
|
657
|
+
/**
|
|
658
|
+
* Optional request tag for the listener. Use to identify the request in logs.
|
|
659
|
+
*
|
|
660
|
+
* @defaultValue `undefined`
|
|
661
|
+
*/
|
|
500
662
|
tag?: string
|
|
501
663
|
}
|
|
502
664
|
|
|
@@ -518,79 +680,6 @@ export type UnfilteredResponseQueryOptions = ResponseQueryOptions & {
|
|
|
518
680
|
filterResponse: false
|
|
519
681
|
}
|
|
520
682
|
|
|
521
|
-
/**
|
|
522
|
-
* Stricter typing of the response from `client.fetch`, intended to be used by type inference libraries and tooling that
|
|
523
|
-
* delivers runtime safety in production.
|
|
524
|
-
*
|
|
525
|
-
* @example
|
|
526
|
-
* It's not designed to be used directly in application code, in fact that would be the most annoying way to query Content Lake in a runtime safe way:
|
|
527
|
-
* ```ts
|
|
528
|
-
* import type {StrictUnknownQueryResponseResult} from '@sanity/client'
|
|
529
|
-
* const query = '*[_type == "product"]'
|
|
530
|
-
* const response = client.fetch<StrictUnknownQueryResponseResult>(query)
|
|
531
|
-
* // Response is now strictly typed, without any knowledge of the dataset or what the GROQ query might return.
|
|
532
|
-
* // The GROQ query is asking for an array of documents that have `_type` = "product", but TypeScript doesn't know that,
|
|
533
|
-
* // `query` is `string` and so the type accounts for anything that GROQ supports, which means you might have any JSON valid data in the response.
|
|
534
|
-
* // \@ts-expect-error -- TS doesn't know the api will always return an array, so it errors
|
|
535
|
-
* console.log(response.length)
|
|
536
|
-
* // It needs a runtime check to ensure it's an array
|
|
537
|
-
* if (Array.isArray(response)) console.log(response.length)
|
|
538
|
-
* ```
|
|
539
|
-
*
|
|
540
|
-
* @example
|
|
541
|
-
* How's it intended to be used? By libraries that delivers end-to-end GROQ runtime type safety.
|
|
542
|
-
* For example in an imaginary TypeScript language service plugin that can look at your `sanity.config.ts`:
|
|
543
|
-
* ```ts
|
|
544
|
-
* import { defineConfig } from 'sanity'
|
|
545
|
-
|
|
546
|
-
* export default defineConfig({
|
|
547
|
-
* projectId: '...',
|
|
548
|
-
* dataset: '...',
|
|
549
|
-
* schema: {
|
|
550
|
-
* types: [
|
|
551
|
-
* {
|
|
552
|
-
* name: 'page',
|
|
553
|
-
* title: 'Page',
|
|
554
|
-
* type: 'document',
|
|
555
|
-
* fields: [
|
|
556
|
-
* {
|
|
557
|
-
* name: 'title',
|
|
558
|
-
* title: 'Title',
|
|
559
|
-
* type: 'string',
|
|
560
|
-
* },
|
|
561
|
-
* ],
|
|
562
|
-
* },
|
|
563
|
-
* ],
|
|
564
|
-
* },
|
|
565
|
-
* })
|
|
566
|
-
* ```
|
|
567
|
-
* And use that to infer typings:
|
|
568
|
-
* ```ts
|
|
569
|
-
* import {createClient} from '@sanity/client'
|
|
570
|
-
*
|
|
571
|
-
* const client = createClient()
|
|
572
|
-
* const response = client.fetch('*[_type == "page"]')
|
|
573
|
-
* // all is well, if title exists the schema says it's a string
|
|
574
|
-
* response.map(page => page.title?.toUpperCase())
|
|
575
|
-
* // IDE flags an error, the `author` property isn't defined in the schema so it could be any JSON valid value.
|
|
576
|
-
* response.map(page => page.author?.toUpperCase())
|
|
577
|
-
* // Maybe this is by design because `author` is coming from an external source, and not from entering it in the Studio interface,
|
|
578
|
-
* // the IDE plugin returns `StrictUnknownQueryResponseResult` and you have to handle it manually:
|
|
579
|
-
* response.map(page => typeof page.author === 'string' ? page.author.toUpperCase() : 'UNKNOWN')
|
|
580
|
-
* ```
|
|
581
|
-
* If the imaginary IDE extension returned `any` or `unknown` instead of `StrictUnknownQueryResponseResult` it opens up a lot of
|
|
582
|
-
* possibilities where it would fail silently and instead error at runtime.
|
|
583
|
-
*
|
|
584
|
-
* @public
|
|
585
|
-
*/
|
|
586
|
-
export type StrictUnknownQueryResponseResult =
|
|
587
|
-
| string
|
|
588
|
-
| number
|
|
589
|
-
| boolean
|
|
590
|
-
| null
|
|
591
|
-
| {[key: string]: StrictUnknownQueryResponseResult | undefined}
|
|
592
|
-
| StrictUnknownQueryResponseResult[]
|
|
593
|
-
|
|
594
683
|
/** @public */
|
|
595
684
|
export interface RawQueryResponse<R> {
|
|
596
685
|
query: string
|