@sanity/sdk-react 3.10.0-next.20261027125037 → 3.10.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 +83 -4
- package/dist/index.js +1 -1
- package/package.json +2 -2
package/README.md
CHANGED
|
@@ -48,6 +48,10 @@ export function App() {
|
|
|
48
48
|
|
|
49
49
|
**Auth is automatic** — Dashboard injects an auth token via iframe. No custom login flow is needed for your application.
|
|
50
50
|
|
|
51
|
+
### 3. Generate types (beta)
|
|
52
|
+
|
|
53
|
+
Configure TypeGen before writing components so hooks return typed data. Add `typegen.resources` to `sanity.cli.ts` and run `npx sanity typegen generate --watch`. See [TypeScript & TypeGen](#typescript--typegen) for the steps.
|
|
54
|
+
|
|
51
55
|
---
|
|
52
56
|
|
|
53
57
|
## Guide
|
|
@@ -536,18 +540,93 @@ If you need to pass an explicit config, replace `studioMode` with `studio`:
|
|
|
536
540
|
|
|
537
541
|
### TypeScript & TypeGen
|
|
538
542
|
|
|
543
|
+
Set up TypeGen when you create an app. Hooks then infer document, field, and query result types from the handle, so you don't write or pass result types yourself.
|
|
544
|
+
|
|
545
|
+
This workflow is in beta. It requires `@sanity/sdk-react` 3.9.0 or later and `@sanity/cli` 8.16.0 or later. See the [TypeScript guide](https://www.sanity.io/docs/app-sdk/sdk-typegen) for every option and for troubleshooting.
|
|
546
|
+
|
|
547
|
+
**1. Extract each dataset's schema.** Run this in the Studio that defines the schema, then copy the file into your app:
|
|
548
|
+
|
|
549
|
+
```bash
|
|
550
|
+
npx sanity schema extract --path schema.production.json
|
|
551
|
+
```
|
|
552
|
+
|
|
553
|
+
**2. Declare your datasets in a module that the app and the CLI both import.** `as const` keeps the project ID and dataset as literal types, which inference needs. The CLI loads this file in Node, so it must not use browser globals.
|
|
554
|
+
|
|
555
|
+
```ts
|
|
556
|
+
// src/resources.ts
|
|
557
|
+
import type {DocumentResource} from '@sanity/sdk-react'
|
|
558
|
+
|
|
559
|
+
export const resources = {
|
|
560
|
+
production: {projectId: 'abc123', dataset: 'production'},
|
|
561
|
+
} as const satisfies Record<string, DocumentResource>
|
|
562
|
+
```
|
|
563
|
+
|
|
564
|
+
**3. Add `typegen.resources` to `sanity.cli.ts`.** Each entry writes one types file for one dataset:
|
|
565
|
+
|
|
566
|
+
```ts
|
|
567
|
+
// sanity.cli.ts
|
|
568
|
+
import {defineCliConfig} from 'sanity/cli'
|
|
569
|
+
|
|
570
|
+
import {resources} from './src/resources'
|
|
571
|
+
|
|
572
|
+
export default defineCliConfig({
|
|
573
|
+
app: {organizationId: 'your-org-id', entry: './src/App.tsx'},
|
|
574
|
+
typegen: {
|
|
575
|
+
resources: [
|
|
576
|
+
{
|
|
577
|
+
...resources.production,
|
|
578
|
+
schema: './schema.production.json',
|
|
579
|
+
generates: './src/sanity.types.production.ts',
|
|
580
|
+
},
|
|
581
|
+
],
|
|
582
|
+
},
|
|
583
|
+
})
|
|
584
|
+
```
|
|
585
|
+
|
|
586
|
+
**4. Generate types.** Run it again after changing a schema or query, or add `--watch` (`@sanity/cli` 8.22.0 or later) to regenerate on every change. `sanity dev` does not generate types for `typegen.resources`, so run the watcher alongside it.
|
|
587
|
+
|
|
539
588
|
```bash
|
|
540
|
-
# Generate types from your schema
|
|
541
589
|
npx sanity typegen generate
|
|
590
|
+
npx sanity typegen generate --watch
|
|
542
591
|
```
|
|
543
592
|
|
|
593
|
+
**5. Use hooks without type parameters.** Build handles and options from your declared resources:
|
|
594
|
+
|
|
544
595
|
```tsx
|
|
545
|
-
import
|
|
596
|
+
import {createDocumentHandle, useDocument, useQuery} from '@sanity/sdk-react'
|
|
597
|
+
import {defineQuery} from 'groq'
|
|
598
|
+
|
|
599
|
+
import {resources} from './resources'
|
|
600
|
+
|
|
601
|
+
const book = createDocumentHandle({
|
|
602
|
+
...resources.production,
|
|
603
|
+
documentId: 'book-1',
|
|
604
|
+
documentType: 'book',
|
|
605
|
+
})
|
|
606
|
+
|
|
607
|
+
const bookTitlesQuery = defineQuery('*[_type == "book"]{_id, title}')
|
|
546
608
|
|
|
547
|
-
|
|
548
|
-
//
|
|
609
|
+
export function BookTitle() {
|
|
610
|
+
// `Book` from sanity.types.production.ts, or null
|
|
611
|
+
const {data: doc} = useDocument(book)
|
|
612
|
+
return <h1>{doc?.title}</h1>
|
|
613
|
+
}
|
|
614
|
+
|
|
615
|
+
export function BookTitles() {
|
|
616
|
+
// the generated result type for bookTitlesQuery
|
|
617
|
+
const {data} = useQuery({...resources.production, query: bookTitlesQuery})
|
|
618
|
+
return (
|
|
619
|
+
<ul>
|
|
620
|
+
{data.map((b) => (
|
|
621
|
+
<li key={b._id}>{b.title}</li>
|
|
622
|
+
))}
|
|
623
|
+
</ul>
|
|
624
|
+
)
|
|
625
|
+
}
|
|
549
626
|
```
|
|
550
627
|
|
|
628
|
+
`defineQuery` comes from the `groq` package (6.12.0 or later). Apps created from the `app-quickstart` template don't include it, so run `npm install groq` first. Assign each query to a variable so TypeGen can find it.
|
|
629
|
+
|
|
551
630
|
---
|
|
552
631
|
|
|
553
632
|
### Deployment
|
package/dist/index.js
CHANGED
|
@@ -4371,7 +4371,7 @@ function useUsers(options) {
|
|
|
4371
4371
|
loadMore
|
|
4372
4372
|
}, $[20] = data, $[21] = hasMore, $[22] = isPending, $[23] = loadMore, $[24] = totalCount, $[25] = t8) : t8 = $[25], t8;
|
|
4373
4373
|
}
|
|
4374
|
-
var version = "3.10.0
|
|
4374
|
+
var version = "3.10.0";
|
|
4375
4375
|
function getEnv(key) {
|
|
4376
4376
|
if (import.meta.env) return import.meta.env[key];
|
|
4377
4377
|
if (typeof process < "u" && process.env) return process.env[key];
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@sanity/sdk-react",
|
|
3
|
-
"version": "3.10.0
|
|
3
|
+
"version": "3.10.0",
|
|
4
4
|
"private": false,
|
|
5
5
|
"description": "Sanity SDK React toolkit for Content OS",
|
|
6
6
|
"keywords": [
|
|
@@ -45,7 +45,7 @@
|
|
|
45
45
|
"@module-federation/runtime": "^2.9.2",
|
|
46
46
|
"@sanity/client": "^8.11.0",
|
|
47
47
|
"@sanity/message-protocol": "^0.24.0",
|
|
48
|
-
"@sanity/sdk": "3.10.0
|
|
48
|
+
"@sanity/sdk": "3.10.0",
|
|
49
49
|
"@sanity/types": "^6.20.0",
|
|
50
50
|
"react-compiler-runtime": "^1.0.0",
|
|
51
51
|
"react-error-boundary": "^6.1.6",
|