@sanity/client 7.26.2 → 7.27.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -62,6 +62,7 @@ export async function updateDocumentTitle(_id, title) {
62
62
  - [ESM](#esm)
63
63
  - [CommonJS](#commonjs)
64
64
  - [TypeScript](#typescript)
65
+ - [Typed query results with Sanity TypeGen](#typed-query-results-with-sanity-typegen)
65
66
  - [Next.js App Router](#nextjs-app-router)
66
67
  - [Bun](#bun)
67
68
  - [Deno](#deno)
@@ -266,6 +267,52 @@ console.log(`Number of documents: ${data}`)
266
267
 
267
268
  Another alternative is [groqd].
268
269
 
270
+ #### Typed query results with [Sanity TypeGen](https://www.sanity.io/docs/sanity-typegen)
271
+
272
+ `client.fetch` looks the query string up in the global `SanityQueries` interface and returns the registered result type when it finds one. `sanity typegen generate` writes these registrations for you, one per query it finds in your code, but the mechanism is plain interface merging:
273
+
274
+ ```ts
275
+ import {createClient} from '@sanity/client'
276
+
277
+ // Generated by `sanity typegen`, or written by hand
278
+ declare global {
279
+ interface SanityQueries {
280
+ '*[_type == "post"]': {_id: string; title: string}[]
281
+ }
282
+ }
283
+
284
+ const client = createClient({
285
+ projectId: 'your-project-id',
286
+ dataset: 'your-dataset-name',
287
+ apiVersion: '2025-02-06',
288
+ })
289
+
290
+ const posts = await client.fetch('*[_type == "post"]')
291
+ // posts is typed as `{_id: string; title: string}[]`, no generic needed
292
+ ```
293
+
294
+ The registry is a global interface rather than a module augmentation of `@sanity/client`, so it does not depend on module resolution: a registration is seen whether or not `@sanity/client` is a direct dependency of the file that makes it, however many copies of the client are installed (a copy nested inside another package reads the same registry), and from every entry point, `@sanity/client/stega` included.
295
+
296
+ Two more forms are accepted:
297
+
298
+ - The module augmentation that earlier TypeGen releases generated keeps working. It registers on the `SanityQueries` interface exported from `@sanity/client`, which inherits the global one:
299
+
300
+ ```ts
301
+ declare module '@sanity/client' {
302
+ interface SanityQueries {
303
+ '*[_type == "post"]': {_id: string; title: string}[]
304
+ }
305
+ }
306
+ ```
307
+
308
+ - Releases of `@sanity/client` that predate the global registry only read that exported interface. A generated file that registers queries globally can still reach them with an augmentation that adds nothing but a base type, which is what `sanity typegen` emits alongside the global registry. It is a harmless duplicate on releases that already inherit the global, so the same generated file works with either:
309
+
310
+ ```ts
311
+ declare module '@sanity/client' {
312
+ interface SanityQueries extends globalThis.SanityQueries {}
313
+ }
314
+ ```
315
+
269
316
  #### [Next.js App Router](https://nextjs.org/docs/app/building-your-application/data-fetching/fetching-caching-and-revalidating#fetching-data-on-the-server-with-fetch)
270
317
 
271
318
  ```tsx
@@ -897,7 +944,7 @@ const videoAsset = stegaClean(result.videoAsset)
897
944
 
898
945
  Strings with stega payloads contain invisible characters, so comparing them against string literals fails in surprising ways: `imageLocation === 'left'` is `false` even though `imageLocation` looks exactly like `'left'` when logged. The branded types available on `@sanity/client/stega` turn these bugs into compile errors.
899
946
 
900
- If you use [Sanity TypeGen](https://www.sanity.io/docs/sanity-typegen), pass `ClientReturnStega` as the first generic to `client.fetch` and every string in the result that may contain stega payloads is branded as `StegaString`:
947
+ If you use [Sanity TypeGen](https://www.sanity.io/docs/sanity-typegen), pass `ClientReturnStega` as the first generic to `client.fetch` and every string in the result that may contain stega payloads is branded as `StegaString`. It looks the query up in the same `SanityQueries` registry as `client.fetch` does (see [Typed query results with Sanity TypeGen](#typed-query-results-with-sanity-typegen)):
901
948
 
902
949
  ```ts
903
950
  import {createClient} from '@sanity/client'