@sanity/cli 8.27.0 → 8.28.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 (138) hide show
  1. package/README.md +1515 -408
  2. package/dist/actions/context/commandTable.js +215 -0
  3. package/dist/actions/context/commandTable.js.map +1 -0
  4. package/dist/actions/context/contextCommand.js +246 -0
  5. package/dist/actions/context/contextCommand.js.map +1 -0
  6. package/dist/actions/context/contextSpec.js +18 -0
  7. package/dist/actions/context/contextSpec.js.map +1 -0
  8. package/dist/actions/context/distillContextOperations.js +335 -0
  9. package/dist/actions/context/distillContextOperations.js.map +1 -0
  10. package/dist/actions/context/mcpEndpoint.js.map +1 -1
  11. package/dist/actions/context/operationInput.js +226 -0
  12. package/dist/actions/context/operationInput.js.map +1 -0
  13. package/dist/actions/context/operationTypes.js +3 -0
  14. package/dist/actions/context/operationTypes.js.map +1 -0
  15. package/dist/actions/context/removedCommands.js +26 -0
  16. package/dist/actions/context/removedCommands.js.map +1 -0
  17. package/dist/actions/context/renderOperationResult.js +72 -0
  18. package/dist/actions/context/renderOperationResult.js.map +1 -0
  19. package/dist/actions/context/sourceFile.js +31 -0
  20. package/dist/actions/context/sourceFile.js.map +1 -0
  21. package/dist/actions/context/syncOutput.js +17 -0
  22. package/dist/actions/context/syncOutput.js.map +1 -0
  23. package/dist/actions/context/table.js +13 -0
  24. package/dist/actions/context/table.js.map +1 -0
  25. package/dist/actions/context/waitForJob.js.map +1 -1
  26. package/dist/actions/context/waitForSourceSync.js +48 -0
  27. package/dist/actions/context/waitForSourceSync.js.map +1 -0
  28. package/dist/actions/context/watchJob.js.map +1 -1
  29. package/dist/actions/context/watchSourceSync.js +40 -0
  30. package/dist/actions/context/watchSourceSync.js.map +1 -0
  31. package/dist/actions/dev/servers/startStudioDevServer.js +4 -1
  32. package/dist/actions/dev/servers/startStudioDevServer.js.map +1 -1
  33. package/dist/commands/context/build.js +19 -81
  34. package/dist/commands/context/build.js.map +1 -1
  35. package/dist/commands/context/create.js +3 -90
  36. package/dist/commands/context/create.js.map +1 -1
  37. package/dist/commands/context/delete.js +3 -73
  38. package/dist/commands/context/delete.js.map +1 -1
  39. package/dist/commands/context/documents/content.js +117 -0
  40. package/dist/commands/context/documents/content.js.map +1 -0
  41. package/dist/commands/context/documents/delete.js +5 -0
  42. package/dist/commands/context/documents/delete.js.map +1 -0
  43. package/dist/commands/context/documents/edit.js +5 -0
  44. package/dist/commands/context/documents/edit.js.map +1 -0
  45. package/dist/commands/context/documents/get.js +5 -0
  46. package/dist/commands/context/documents/get.js.map +1 -0
  47. package/dist/commands/context/documents/list.js +5 -0
  48. package/dist/commands/context/documents/list.js.map +1 -0
  49. package/dist/commands/context/entries/list.js +2 -2
  50. package/dist/commands/context/entries/list.js.map +1 -1
  51. package/dist/commands/context/entries/rebuild.js +3 -93
  52. package/dist/commands/context/entries/rebuild.js.map +1 -1
  53. package/dist/commands/context/get.js +3 -88
  54. package/dist/commands/context/get.js.map +1 -1
  55. package/dist/commands/context/instructions/create.js +5 -0
  56. package/dist/commands/context/instructions/create.js.map +1 -0
  57. package/dist/commands/context/instructions/delete.js +5 -0
  58. package/dist/commands/context/instructions/delete.js.map +1 -0
  59. package/dist/commands/context/instructions/edit.js +5 -0
  60. package/dist/commands/context/instructions/edit.js.map +1 -0
  61. package/dist/commands/context/issues/apply.js +3 -84
  62. package/dist/commands/context/issues/apply.js.map +1 -1
  63. package/dist/commands/context/issues/dismiss.js +3 -50
  64. package/dist/commands/context/issues/dismiss.js.map +1 -1
  65. package/dist/commands/context/issues/reopen.js +3 -51
  66. package/dist/commands/context/issues/reopen.js.map +1 -1
  67. package/dist/commands/context/issues/resolve.js +23 -92
  68. package/dist/commands/context/issues/resolve.js.map +1 -1
  69. package/dist/commands/context/jobs/get.js +3 -88
  70. package/dist/commands/context/jobs/get.js.map +1 -1
  71. package/dist/commands/context/list.js +3 -95
  72. package/dist/commands/context/list.js.map +1 -1
  73. package/dist/commands/context/mcp/initial-context.js +5 -0
  74. package/dist/commands/context/mcp/initial-context.js.map +1 -0
  75. package/dist/commands/context/mcp/list.js +2 -2
  76. package/dist/commands/context/mcp/list.js.map +1 -1
  77. package/dist/commands/context/mcp/status.js.map +1 -1
  78. package/dist/commands/context/mcp/update.js.map +1 -1
  79. package/dist/commands/context/sources/create.js +175 -0
  80. package/dist/commands/context/sources/create.js.map +1 -0
  81. package/dist/commands/context/sources/delete.js +5 -0
  82. package/dist/commands/context/sources/delete.js.map +1 -0
  83. package/dist/commands/context/sources/edit.js +29 -0
  84. package/dist/commands/context/sources/edit.js.map +1 -0
  85. package/dist/commands/context/sources/get.js +5 -0
  86. package/dist/commands/context/sources/get.js.map +1 -0
  87. package/dist/commands/context/sources/list.js +5 -0
  88. package/dist/commands/context/sources/list.js.map +1 -0
  89. package/dist/commands/context/sources/sync.js +5 -0
  90. package/dist/commands/context/sources/sync.js.map +1 -0
  91. package/dist/commands/context/sources/syncs/confirm.js +5 -0
  92. package/dist/commands/context/sources/syncs/confirm.js.map +1 -0
  93. package/dist/commands/context/sources/syncs/discard.js +5 -0
  94. package/dist/commands/context/sources/syncs/discard.js.map +1 -0
  95. package/dist/commands/context/sources/syncs/get.js +5 -0
  96. package/dist/commands/context/sources/syncs/get.js.map +1 -0
  97. package/dist/commands/context/sources/syncs/list.js +5 -0
  98. package/dist/commands/context/sources/syncs/list.js.map +1 -0
  99. package/dist/commands/context/sources/upload.js +63 -0
  100. package/dist/commands/context/sources/upload.js.map +1 -0
  101. package/dist/commands/context/sync.js +5 -0
  102. package/dist/commands/context/sync.js.map +1 -0
  103. package/dist/commands/context/update.js +3 -136
  104. package/dist/commands/context/update.js.map +1 -1
  105. package/dist/exports/invokeSanityCli/commandPolicies/mcpPolicy.js +41 -26
  106. package/dist/exports/invokeSanityCli/commandPolicies/mcpPolicy.js.map +1 -1
  107. package/dist/generated/apiRoutes.js +28 -0
  108. package/dist/generated/apiRoutes.js.map +1 -1
  109. package/dist/generated/contextOperations.js +1116 -0
  110. package/dist/generated/contextOperations.js.map +1 -0
  111. package/dist/hooks/commandNotFound/topicAliases.js +8 -2
  112. package/dist/hooks/commandNotFound/topicAliases.js.map +1 -1
  113. package/dist/services/context.js +241 -268
  114. package/dist/services/context.js.map +1 -1
  115. package/dist/util/telemetry/cliCommandIdentity.js +10 -18
  116. package/dist/util/telemetry/cliCommandIdentity.js.map +1 -1
  117. package/oclif.manifest.json +3380 -1910
  118. package/package.json +24 -23
  119. package/templates/app-quickstart/.claude/skills/sanity-app-sdk/SKILL.md +49 -0
  120. package/templates/app-quickstart/AGENTS.md +49 -0
  121. package/templates/app-sanity-ui/.claude/skills/sanity-app-sdk/SKILL.md +49 -0
  122. package/templates/app-sanity-ui/AGENTS.md +49 -0
  123. package/templates/shared/dashboard/app-quickstart/.claude/skills/sanity-app-sdk/SKILL.md +49 -0
  124. package/templates/shared/dashboard/app-quickstart/AGENTS.md +49 -0
  125. package/templates/shared/dashboard/app-sanity-ui/.claude/skills/sanity-app-sdk/SKILL.md +49 -0
  126. package/templates/shared/dashboard/app-sanity-ui/AGENTS.md +49 -0
  127. package/dist/commands/context/imports/create.js +0 -270
  128. package/dist/commands/context/imports/create.js.map +0 -1
  129. package/dist/commands/context/imports/delete.js +0 -73
  130. package/dist/commands/context/imports/delete.js.map +0 -1
  131. package/dist/commands/context/imports/download.js +0 -60
  132. package/dist/commands/context/imports/download.js.map +0 -1
  133. package/dist/commands/context/imports/get.js +0 -88
  134. package/dist/commands/context/imports/get.js.map +0 -1
  135. package/dist/commands/context/imports/list.js +0 -100
  136. package/dist/commands/context/imports/list.js.map +0 -1
  137. package/dist/commands/context/refresh.js +0 -46
  138. package/dist/commands/context/refresh.js.map +0 -1
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@sanity/cli",
3
- "version": "8.27.0",
3
+ "version": "8.28.1",
4
4
  "description": "Sanity CLI tool for managing Sanity projects and organizations",
5
5
  "keywords": [
6
6
  "cli",
@@ -62,27 +62,27 @@
62
62
  "@oclif/core": "^4.11.14",
63
63
  "@oclif/plugin-help": "^6.2.53",
64
64
  "@oclif/plugin-not-found": "^3.2.88",
65
- "@sanity/blueprints": "^0.28.0",
66
- "@sanity/cli-build": "^7.1.0",
67
- "@sanity/cli-core": "^3.14.1",
68
- "@sanity/client": "^8.13.0",
69
- "@sanity/codegen": "^8.2.0",
65
+ "@sanity/blueprints": "^0.33.0",
66
+ "@sanity/cli-build": "^7.1.2",
67
+ "@sanity/cli-core": "^3.14.2",
68
+ "@sanity/client": "^8.15.0",
69
+ "@sanity/codegen": "^8.3.0",
70
70
  "@sanity/descriptors": "^1.3.0",
71
71
  "@sanity/export": "^7.1.0",
72
72
  "@sanity/generate-help-url": "^4.0.0",
73
73
  "@sanity/id-utils": "^1.1.0",
74
74
  "@sanity/import": "^7.1.0",
75
75
  "@sanity/migrate": "^8.0.5",
76
- "@sanity/runtime-cli": "^17.16.0",
77
- "@sanity/schema": "^6.17.0",
78
- "@sanity/schema-descriptor-utils": "^1.3.3",
76
+ "@sanity/runtime-cli": "^17.21.0",
77
+ "@sanity/schema": "^6.22.0",
78
+ "@sanity/schema-descriptor-utils": "^1.4.0",
79
79
  "@sanity/telemetry": "^1.1.0",
80
80
  "@sanity/template-validator": "^3.1.1",
81
- "@sanity/types": "^6.17.0",
82
- "@sanity/workbench-cli": "^2.15.0",
81
+ "@sanity/types": "^6.22.0",
82
+ "@sanity/workbench-cli": "^2.15.2",
83
83
  "@sanity/worker-channels": "^2.0.0",
84
- "@sanity/workflow-cli": "0.36.0",
85
- "@sanity/workflow-engine": "0.36.0",
84
+ "@sanity/workflow-cli": "^0.37.0",
85
+ "@sanity/workflow-engine": "^0.37.0",
86
86
  "@vercel/frameworks": "3.29.0",
87
87
  "chokidar": "^5.0.0",
88
88
  "console-table-printer": "^2.15.0",
@@ -90,7 +90,7 @@
90
90
  "execa": "^10.0.1",
91
91
  "form-data": "^4.0.5",
92
92
  "get-latest-version": "^6.0.1",
93
- "groq-js": "^2.0.0",
93
+ "groq-js": "^2.0.1",
94
94
  "gunzip-maybe": "^1.4.2",
95
95
  "import-meta-resolve": "^4.2.0",
96
96
  "is-installed-globally": "^1.0.0",
@@ -112,22 +112,22 @@
112
112
  "smol-toml": "^1.6.1",
113
113
  "string-argv": "^0.3.2",
114
114
  "string-width": "^7.2.0",
115
- "tar": "^7.5.18",
116
- "tar-stream": "^3.2.0",
115
+ "tar": "^7.5.22",
116
+ "tar-stream": "^3.2.2",
117
117
  "tinyglobby": "^0.2.17",
118
118
  "valibot": "^1.4.2",
119
- "vite": "^8.3.3",
119
+ "vite": "^8.3.6",
120
120
  "which": "^6.0.1",
121
121
  "wrap-ansi": "^9.0.2",
122
122
  "yaml": "^2.9.0",
123
- "zod": "^4.5.2"
123
+ "zod": "^4.6.7"
124
124
  },
125
125
  "devDependencies": {
126
126
  "@eslint/compat": "^2.1.0",
127
127
  "@oxc-project/types": "0.152.0",
128
128
  "@repo/package.config": "0.0.1",
129
129
  "@repo/tsconfig": "3.70.0",
130
- "@sanity/cli-test": "24.0.1",
130
+ "@sanity/cli-test": "24.0.3",
131
131
  "@sanity/eslint-config-cli": "1.1.3",
132
132
  "@sanity/pkg-utils": "^12.3.4",
133
133
  "@swc/cli": "^0.8.1",
@@ -140,20 +140,20 @@
140
140
  "@types/semver": "^7.7.1",
141
141
  "@types/tar-stream": "^3.1.4",
142
142
  "@types/which": "^3.0.4",
143
- "@vitest/coverage-istanbul": "^4.1.11",
143
+ "@vitest/coverage-istanbul": "^5.1.0",
144
144
  "babel-plugin-react-compiler": "^1.0.0",
145
145
  "eslint": "^10.7.0",
146
146
  "get-it": "^9.5.4",
147
147
  "jsdom": "^29.1.1",
148
148
  "nock": "^14.0.15",
149
149
  "oclif": "^4.23.27",
150
- "oxc-transform-react": "^0.153.0",
150
+ "oxc-transform-react": "^0.156.0",
151
151
  "publint": "^0.3.21",
152
152
  "rimraf": "^6.0.1",
153
- "sanity": "^6.17.0",
153
+ "sanity": "^6.22.0",
154
154
  "tsx": "^4.23.12",
155
155
  "typescript": "^6.0.3",
156
- "vitest": "^4.1.11"
156
+ "vitest": "^5.1.0"
157
157
  },
158
158
  "peerDependencies": {
159
159
  "babel-plugin-react-compiler": "*",
@@ -178,6 +178,7 @@
178
178
  "check:topic-aliases": "tsx scripts/check-topic-aliases.ts",
179
179
  "check:types": "tsc --noEmit",
180
180
  "generate:api-routes": "tsx scripts/generate-api-routes.ts",
181
+ "generate:context-commands": "tsx scripts/generate-context-commands.ts && oxfmt src/commands/context",
181
182
  "lint": "eslint .",
182
183
  "manifest:generate": "oclif manifest",
183
184
  "manifest:remove": "rimraf oclif.manifest.json",
@@ -5,6 +5,55 @@ description: Build features with the Sanity App SDK (@sanity/sdk-react). Use whe
5
5
 
6
6
  # Sanity App SDK
7
7
 
8
+ ## Set up TypeGen before writing components
9
+
10
+ Set up Sanity TypeGen before you write components that read content. SDK hooks then infer document, projection and query result types from the project and dataset, so do not hand-write those types or pass them as type parameters. This workflow is in beta and needs `@sanity/cli` 8.16.0 or later. Check with `npx sanity --version`.
11
+
12
+ 1. Get a schema file for each dataset the app reads. In the Studio that defines the schema, run `npx sanity schema extract --path schema.production.json` and copy the file into this app's root. If you can't reach the Studio, ask the developer for the file. If you must continue without it, extract one from a temporary Studio config that matches the schema the developer described, and tell the developer to replace it with the real extract, because types from a stand-in schema can be wrong.
13
+ 2. Declare the datasets in `src/resources.ts`. Use the same object for the `SanityApp` config in `src/App.tsx` and in every hook call. `as const` keeps the IDs as literal types, which inference needs. The CLI loads this file in Node, so it must not use browser globals.
14
+
15
+ ```ts
16
+ import type {DocumentResource} from '@sanity/sdk-react'
17
+
18
+ export const resources = {
19
+ production: {projectId: 'your-project-id', dataset: 'production'},
20
+ } as const satisfies Record<string, DocumentResource>
21
+ ```
22
+
23
+ 3. Add `typegen.resources` to `sanity.cli.ts`, next to the existing `app` config. Each entry writes one types file for one dataset:
24
+
25
+ ```ts
26
+ import {resources} from './src/resources'
27
+
28
+ export default defineCliConfig({
29
+ // ...existing app config
30
+ typegen: {
31
+ resources: [
32
+ {
33
+ ...resources.production,
34
+ schema: './schema.production.json',
35
+ generates: './src/sanity.types.production.ts',
36
+ },
37
+ ],
38
+ },
39
+ })
40
+ ```
41
+
42
+ 4. Add `"typegen": "sanity typegen generate"` to the `package.json` scripts and run `npm run typegen`. Run it again after changing a schema, query or projection. `npm run dev` does not generate these types. While developing, run `npx sanity typegen generate --watch` (`@sanity/cli` 8.22.0 or later) alongside it.
43
+ 5. Define queries with `defineQuery` from `groq` (`npm install groq`) and projections with `defineProjection` from `@sanity/sdk-react`, each assigned to a variable. TypeGen only generates types for those. Then call hooks without type parameters:
44
+
45
+ ```tsx
46
+ export const bookRowProjection = defineProjection('book', '{title, "authorName": author->name}')
47
+
48
+ const {data: handles} = useDocuments({...resources.production, documentType: 'book'})
49
+
50
+ // in a child component receiving one handle (typed DocumentHandle<'book'>):
51
+ const {data} = useDocumentProjection({...handle, projection: bookRowProjection})
52
+ // data is typed from the generated file
53
+ ```
54
+
55
+ Guide: https://www.sanity.io/docs/app-sdk/sdk-typegen
56
+
8
57
  ## Get the maintained guide first
9
58
 
10
59
  If the Sanity MCP server is configured, call its `get_sanity_rules` tool with the `app-sdk` rule before writing SDK code. That rule is maintained by Sanity, is more detailed, and supersedes the notes below. The notes below are a fallback for when MCP is not available.
@@ -11,6 +11,55 @@ A React application built with the Sanity App SDK (`@sanity/sdk-react`). It is n
11
11
  - `src/App.tsx`: entry point. The `SanityApp` component takes a `config` array with `projectId` and `dataset`. All SDK hooks must be used inside `SanityApp`.
12
12
  - `sanity.cli.ts`: CLI config with the organization ID and app entry path.
13
13
 
14
+ ## Set up TypeGen before writing components
15
+
16
+ Set up Sanity TypeGen before you write components that read content. SDK hooks then infer document, projection and query result types from the project and dataset, so do not hand-write those types or pass them as type parameters. This workflow is in beta and needs `@sanity/cli` 8.16.0 or later. Check with `npx sanity --version`.
17
+
18
+ 1. Get a schema file for each dataset the app reads. In the Studio that defines the schema, run `npx sanity schema extract --path schema.production.json` and copy the file into this app's root. If you can't reach the Studio, ask the developer for the file. If you must continue without it, extract one from a temporary Studio config that matches the schema the developer described, and tell the developer to replace it with the real extract, because types from a stand-in schema can be wrong.
19
+ 2. Declare the datasets in `src/resources.ts`. Use the same object for the `SanityApp` config in `src/App.tsx` and in every hook call. `as const` keeps the IDs as literal types, which inference needs. The CLI loads this file in Node, so it must not use browser globals.
20
+
21
+ ```ts
22
+ import type {DocumentResource} from '@sanity/sdk-react'
23
+
24
+ export const resources = {
25
+ production: {projectId: 'your-project-id', dataset: 'production'},
26
+ } as const satisfies Record<string, DocumentResource>
27
+ ```
28
+
29
+ 3. Add `typegen.resources` to `sanity.cli.ts`, next to the existing `app` config. Each entry writes one types file for one dataset:
30
+
31
+ ```ts
32
+ import {resources} from './src/resources'
33
+
34
+ export default defineCliConfig({
35
+ // ...existing app config
36
+ typegen: {
37
+ resources: [
38
+ {
39
+ ...resources.production,
40
+ schema: './schema.production.json',
41
+ generates: './src/sanity.types.production.ts',
42
+ },
43
+ ],
44
+ },
45
+ })
46
+ ```
47
+
48
+ 4. Add `"typegen": "sanity typegen generate"` to the `package.json` scripts and run `npm run typegen`. Run it again after changing a schema, query or projection. `npm run dev` does not generate these types. While developing, run `npx sanity typegen generate --watch` (`@sanity/cli` 8.22.0 or later) alongside it.
49
+ 5. Define queries with `defineQuery` from `groq` (`npm install groq`) and projections with `defineProjection` from `@sanity/sdk-react`, each assigned to a variable. TypeGen only generates types for those. Then call hooks without type parameters:
50
+
51
+ ```tsx
52
+ export const bookRowProjection = defineProjection('book', '{title, "authorName": author->name}')
53
+
54
+ const {data: handles} = useDocuments({...resources.production, documentType: 'book'})
55
+
56
+ // in a child component receiving one handle (typed DocumentHandle<'book'>):
57
+ const {data} = useDocumentProjection({...handle, projection: bookRowProjection})
58
+ // data is typed from the generated file
59
+ ```
60
+
61
+ Guide: https://www.sanity.io/docs/app-sdk/sdk-typegen
62
+
14
63
  ## Commands
15
64
 
16
65
  - `npm run dev`: starts the dev server on port 3333, but the app only renders inside the Sanity Dashboard. The CLI prints a Dashboard URL to open. Viewing it requires a signed-in Sanity account, so a human must complete authentication in the browser.
@@ -5,6 +5,55 @@ description: Build features with the Sanity App SDK (@sanity/sdk-react) and Sani
5
5
 
6
6
  # Sanity App SDK
7
7
 
8
+ ## Set up TypeGen before writing components
9
+
10
+ Set up Sanity TypeGen before you write components that read content. SDK hooks then infer document, projection and query result types from the project and dataset, so do not hand-write those types or pass them as type parameters. This workflow is in beta and needs `@sanity/cli` 8.16.0 or later. Check with `npx sanity --version`.
11
+
12
+ 1. Get a schema file for each dataset the app reads. In the Studio that defines the schema, run `npx sanity schema extract --path schema.production.json` and copy the file into this app's root. If you can't reach the Studio, ask the developer for the file. If you must continue without it, extract one from a temporary Studio config that matches the schema the developer described, and tell the developer to replace it with the real extract, because types from a stand-in schema can be wrong.
13
+ 2. Declare the datasets in `src/resources.ts`. Use the same object for the `SanityApp` config in `src/App.tsx` and in every hook call. `as const` keeps the IDs as literal types, which inference needs. The CLI loads this file in Node, so it must not use browser globals.
14
+
15
+ ```ts
16
+ import type {DocumentResource} from '@sanity/sdk-react'
17
+
18
+ export const resources = {
19
+ production: {projectId: 'your-project-id', dataset: 'production'},
20
+ } as const satisfies Record<string, DocumentResource>
21
+ ```
22
+
23
+ 3. Add `typegen.resources` to `sanity.cli.ts`, next to the existing `app` config. Each entry writes one types file for one dataset:
24
+
25
+ ```ts
26
+ import {resources} from './src/resources'
27
+
28
+ export default defineCliConfig({
29
+ // ...existing app config
30
+ typegen: {
31
+ resources: [
32
+ {
33
+ ...resources.production,
34
+ schema: './schema.production.json',
35
+ generates: './src/sanity.types.production.ts',
36
+ },
37
+ ],
38
+ },
39
+ })
40
+ ```
41
+
42
+ 4. Add `"typegen": "sanity typegen generate"` to the `package.json` scripts and run `npm run typegen`. Run it again after changing a schema, query or projection. `npm run dev` does not generate these types. While developing, run `npx sanity typegen generate --watch` (`@sanity/cli` 8.22.0 or later) alongside it.
43
+ 5. Define queries with `defineQuery` from `groq` (`npm install groq`) and projections with `defineProjection` from `@sanity/sdk-react`, each assigned to a variable. TypeGen only generates types for those. Then call hooks without type parameters:
44
+
45
+ ```tsx
46
+ export const bookRowProjection = defineProjection('book', '{title, "authorName": author->name}')
47
+
48
+ const {data: handles} = useDocuments({...resources.production, documentType: 'book'})
49
+
50
+ // in a child component receiving one handle (typed DocumentHandle<'book'>):
51
+ const {data} = useDocumentProjection({...handle, projection: bookRowProjection})
52
+ // data is typed from the generated file
53
+ ```
54
+
55
+ Guide: https://www.sanity.io/docs/app-sdk/sdk-typegen
56
+
8
57
  ## Get the maintained guide first
9
58
 
10
59
  If the Sanity MCP server is configured, call its `get_sanity_rules` tool with the `app-sdk` rule before writing SDK code. That rule is maintained by Sanity, is more detailed, and supersedes the notes below. The notes below are a fallback for when MCP is not available.
@@ -12,6 +12,55 @@ A React application built with the Sanity App SDK (`@sanity/sdk-react`) and Sani
12
12
  - `src/SanityUI.tsx`: wraps the app in Sanity UI's `ThemeProvider` with a theme from `buildTheme()`. Sanity UI components must render inside this provider.
13
13
  - `sanity.cli.ts`: CLI config with the organization ID and app entry path.
14
14
 
15
+ ## Set up TypeGen before writing components
16
+
17
+ Set up Sanity TypeGen before you write components that read content. SDK hooks then infer document, projection and query result types from the project and dataset, so do not hand-write those types or pass them as type parameters. This workflow is in beta and needs `@sanity/cli` 8.16.0 or later. Check with `npx sanity --version`.
18
+
19
+ 1. Get a schema file for each dataset the app reads. In the Studio that defines the schema, run `npx sanity schema extract --path schema.production.json` and copy the file into this app's root. If you can't reach the Studio, ask the developer for the file. If you must continue without it, extract one from a temporary Studio config that matches the schema the developer described, and tell the developer to replace it with the real extract, because types from a stand-in schema can be wrong.
20
+ 2. Declare the datasets in `src/resources.ts`. Use the same object for the `SanityApp` config in `src/App.tsx` and in every hook call. `as const` keeps the IDs as literal types, which inference needs. The CLI loads this file in Node, so it must not use browser globals.
21
+
22
+ ```ts
23
+ import type {DocumentResource} from '@sanity/sdk-react'
24
+
25
+ export const resources = {
26
+ production: {projectId: 'your-project-id', dataset: 'production'},
27
+ } as const satisfies Record<string, DocumentResource>
28
+ ```
29
+
30
+ 3. Add `typegen.resources` to `sanity.cli.ts`, next to the existing `app` config. Each entry writes one types file for one dataset:
31
+
32
+ ```ts
33
+ import {resources} from './src/resources'
34
+
35
+ export default defineCliConfig({
36
+ // ...existing app config
37
+ typegen: {
38
+ resources: [
39
+ {
40
+ ...resources.production,
41
+ schema: './schema.production.json',
42
+ generates: './src/sanity.types.production.ts',
43
+ },
44
+ ],
45
+ },
46
+ })
47
+ ```
48
+
49
+ 4. Add `"typegen": "sanity typegen generate"` to the `package.json` scripts and run `npm run typegen`. Run it again after changing a schema, query or projection. `npm run dev` does not generate these types. While developing, run `npx sanity typegen generate --watch` (`@sanity/cli` 8.22.0 or later) alongside it.
50
+ 5. Define queries with `defineQuery` from `groq` (`npm install groq`) and projections with `defineProjection` from `@sanity/sdk-react`, each assigned to a variable. TypeGen only generates types for those. Then call hooks without type parameters:
51
+
52
+ ```tsx
53
+ export const bookRowProjection = defineProjection('book', '{title, "authorName": author->name}')
54
+
55
+ const {data: handles} = useDocuments({...resources.production, documentType: 'book'})
56
+
57
+ // in a child component receiving one handle (typed DocumentHandle<'book'>):
58
+ const {data} = useDocumentProjection({...handle, projection: bookRowProjection})
59
+ // data is typed from the generated file
60
+ ```
61
+
62
+ Guide: https://www.sanity.io/docs/app-sdk/sdk-typegen
63
+
15
64
  ## Commands
16
65
 
17
66
  - `npm run dev`: starts the dev server on port 3333, but the app only renders inside the Sanity Dashboard. The CLI prints a Dashboard URL to open. Viewing it requires a signed-in Sanity account, so a human must complete authentication in the browser.
@@ -5,6 +5,55 @@ description: Build features with the Sanity App SDK (@sanity/sdk-react). Use whe
5
5
 
6
6
  # Sanity App SDK
7
7
 
8
+ ## Set up TypeGen before writing components
9
+
10
+ Set up Sanity TypeGen before you write components that read content. SDK hooks then infer document, projection and query result types from the project and dataset, so do not hand-write those types or pass them as type parameters. This workflow is in beta and needs `@sanity/cli` 8.16.0 or later. Check with `npx sanity --version`.
11
+
12
+ 1. Get a schema file for each dataset the app reads. In the Studio that defines the schema, run `npx sanity schema extract --path schema.production.json` and copy the file into this app's root. If you can't reach the Studio, ask the developer for the file. If you must continue without it, extract one from a temporary Studio config that matches the schema the developer described, and tell the developer to replace it with the real extract, because types from a stand-in schema can be wrong.
13
+ 2. Declare the datasets in `src/resources.ts`. Use the same object for the `SanityApp` config in `src/App.tsx` and in every hook call. `as const` keeps the IDs as literal types, which inference needs. The CLI loads this file in Node, so it must not use browser globals.
14
+
15
+ ```ts
16
+ import type {DocumentResource} from '@sanity/sdk-react'
17
+
18
+ export const resources = {
19
+ production: {projectId: 'your-project-id', dataset: 'production'},
20
+ } as const satisfies Record<string, DocumentResource>
21
+ ```
22
+
23
+ 3. Add `typegen.resources` to `sanity.cli.ts`, next to the existing `app` config. Each entry writes one types file for one dataset:
24
+
25
+ ```ts
26
+ import {resources} from './src/resources'
27
+
28
+ export default defineCliConfig({
29
+ // ...existing app config
30
+ typegen: {
31
+ resources: [
32
+ {
33
+ ...resources.production,
34
+ schema: './schema.production.json',
35
+ generates: './src/sanity.types.production.ts',
36
+ },
37
+ ],
38
+ },
39
+ })
40
+ ```
41
+
42
+ 4. Add `"typegen": "sanity typegen generate"` to the `package.json` scripts and run `npm run typegen`. Run it again after changing a schema, query or projection. `npm run dev` does not generate these types. While developing, run `npx sanity typegen generate --watch` (`@sanity/cli` 8.22.0 or later) alongside it.
43
+ 5. Define queries with `defineQuery` from `groq` (`npm install groq`) and projections with `defineProjection` from `@sanity/sdk-react`, each assigned to a variable. TypeGen only generates types for those. Then call hooks without type parameters:
44
+
45
+ ```tsx
46
+ export const bookRowProjection = defineProjection('book', '{title, "authorName": author->name}')
47
+
48
+ const {data: handles} = useDocuments({...resources.production, documentType: 'book'})
49
+
50
+ // in a child component receiving one handle (typed DocumentHandle<'book'>):
51
+ const {data} = useDocumentProjection({...handle, projection: bookRowProjection})
52
+ // data is typed from the generated file
53
+ ```
54
+
55
+ Guide: https://www.sanity.io/docs/app-sdk/sdk-typegen
56
+
8
57
  ## Get the maintained guide first
9
58
 
10
59
  If the Sanity MCP server is configured, call its `get_sanity_rules` tool with the `app-sdk` rule before writing SDK code. That rule is maintained by Sanity, is more detailed, and supersedes the notes below. The notes below are a fallback for when MCP is not available.
@@ -13,6 +13,55 @@ The app is set up for the Sanity Dashboard beta: `sanity.cli.ts` declares it wit
13
13
  - `src/App.tsx`: entry point. The `SanityApp` component takes a `config` array with `projectId` and `dataset`. All SDK hooks must be used inside `SanityApp`.
14
14
  - `sanity.cli.ts`: CLI config. `app` is `defineApplication({title, slug, organizationId, entry})`. `slug` is the app's unique address in the organization, used to create the app on its first deploy: lowercase letters, numbers and hyphens, starting with a letter.
15
15
 
16
+ ## Set up TypeGen before writing components
17
+
18
+ Set up Sanity TypeGen before you write components that read content. SDK hooks then infer document, projection and query result types from the project and dataset, so do not hand-write those types or pass them as type parameters. This workflow is in beta and needs `@sanity/cli` 8.16.0 or later. Check with `npx sanity --version`.
19
+
20
+ 1. Get a schema file for each dataset the app reads. In the Studio that defines the schema, run `npx sanity schema extract --path schema.production.json` and copy the file into this app's root. If you can't reach the Studio, ask the developer for the file. If you must continue without it, extract one from a temporary Studio config that matches the schema the developer described, and tell the developer to replace it with the real extract, because types from a stand-in schema can be wrong.
21
+ 2. Declare the datasets in `src/resources.ts`. Use the same object for the `SanityApp` config in `src/App.tsx` and in every hook call. `as const` keeps the IDs as literal types, which inference needs. The CLI loads this file in Node, so it must not use browser globals.
22
+
23
+ ```ts
24
+ import type {DocumentResource} from '@sanity/sdk-react'
25
+
26
+ export const resources = {
27
+ production: {projectId: 'your-project-id', dataset: 'production'},
28
+ } as const satisfies Record<string, DocumentResource>
29
+ ```
30
+
31
+ 3. Add `typegen.resources` to `sanity.cli.ts`, next to the existing `app` config. Each entry writes one types file for one dataset:
32
+
33
+ ```ts
34
+ import {resources} from './src/resources'
35
+
36
+ export default defineCliConfig({
37
+ // ...existing app config
38
+ typegen: {
39
+ resources: [
40
+ {
41
+ ...resources.production,
42
+ schema: './schema.production.json',
43
+ generates: './src/sanity.types.production.ts',
44
+ },
45
+ ],
46
+ },
47
+ })
48
+ ```
49
+
50
+ 4. Add `"typegen": "sanity typegen generate"` to the `package.json` scripts and run `npm run typegen`. Run it again after changing a schema, query or projection. `npm run dev` does not generate these types. While developing, run `npx sanity typegen generate --watch` (`@sanity/cli` 8.22.0 or later) alongside it.
51
+ 5. Define queries with `defineQuery` from `groq` (`npm install groq`) and projections with `defineProjection` from `@sanity/sdk-react`, each assigned to a variable. TypeGen only generates types for those. Then call hooks without type parameters:
52
+
53
+ ```tsx
54
+ export const bookRowProjection = defineProjection('book', '{title, "authorName": author->name}')
55
+
56
+ const {data: handles} = useDocuments({...resources.production, documentType: 'book'})
57
+
58
+ // in a child component receiving one handle (typed DocumentHandle<'book'>):
59
+ const {data} = useDocumentProjection({...handle, projection: bookRowProjection})
60
+ // data is typed from the generated file
61
+ ```
62
+
63
+ Guide: https://www.sanity.io/docs/app-sdk/sdk-typegen
64
+
16
65
  ## Commands
17
66
 
18
67
  - `npm run dev`: starts a local Sanity Dashboard on port 3333 and serves the app on the next port (3334), loaded into that Dashboard. The CLI prints a link that opens the app in the local Dashboard. The app only renders inside the Dashboard, and viewing it requires a signed-in Sanity account, so a human must complete authentication in the browser.
@@ -5,6 +5,55 @@ description: Build features with the Sanity App SDK (@sanity/sdk-react) and Sani
5
5
 
6
6
  # Sanity App SDK
7
7
 
8
+ ## Set up TypeGen before writing components
9
+
10
+ Set up Sanity TypeGen before you write components that read content. SDK hooks then infer document, projection and query result types from the project and dataset, so do not hand-write those types or pass them as type parameters. This workflow is in beta and needs `@sanity/cli` 8.16.0 or later. Check with `npx sanity --version`.
11
+
12
+ 1. Get a schema file for each dataset the app reads. In the Studio that defines the schema, run `npx sanity schema extract --path schema.production.json` and copy the file into this app's root. If you can't reach the Studio, ask the developer for the file. If you must continue without it, extract one from a temporary Studio config that matches the schema the developer described, and tell the developer to replace it with the real extract, because types from a stand-in schema can be wrong.
13
+ 2. Declare the datasets in `src/resources.ts`. Use the same object for the `SanityApp` config in `src/App.tsx` and in every hook call. `as const` keeps the IDs as literal types, which inference needs. The CLI loads this file in Node, so it must not use browser globals.
14
+
15
+ ```ts
16
+ import type {DocumentResource} from '@sanity/sdk-react'
17
+
18
+ export const resources = {
19
+ production: {projectId: 'your-project-id', dataset: 'production'},
20
+ } as const satisfies Record<string, DocumentResource>
21
+ ```
22
+
23
+ 3. Add `typegen.resources` to `sanity.cli.ts`, next to the existing `app` config. Each entry writes one types file for one dataset:
24
+
25
+ ```ts
26
+ import {resources} from './src/resources'
27
+
28
+ export default defineCliConfig({
29
+ // ...existing app config
30
+ typegen: {
31
+ resources: [
32
+ {
33
+ ...resources.production,
34
+ schema: './schema.production.json',
35
+ generates: './src/sanity.types.production.ts',
36
+ },
37
+ ],
38
+ },
39
+ })
40
+ ```
41
+
42
+ 4. Add `"typegen": "sanity typegen generate"` to the `package.json` scripts and run `npm run typegen`. Run it again after changing a schema, query or projection. `npm run dev` does not generate these types. While developing, run `npx sanity typegen generate --watch` (`@sanity/cli` 8.22.0 or later) alongside it.
43
+ 5. Define queries with `defineQuery` from `groq` (`npm install groq`) and projections with `defineProjection` from `@sanity/sdk-react`, each assigned to a variable. TypeGen only generates types for those. Then call hooks without type parameters:
44
+
45
+ ```tsx
46
+ export const bookRowProjection = defineProjection('book', '{title, "authorName": author->name}')
47
+
48
+ const {data: handles} = useDocuments({...resources.production, documentType: 'book'})
49
+
50
+ // in a child component receiving one handle (typed DocumentHandle<'book'>):
51
+ const {data} = useDocumentProjection({...handle, projection: bookRowProjection})
52
+ // data is typed from the generated file
53
+ ```
54
+
55
+ Guide: https://www.sanity.io/docs/app-sdk/sdk-typegen
56
+
8
57
  ## Get the maintained guide first
9
58
 
10
59
  If the Sanity MCP server is configured, call its `get_sanity_rules` tool with the `app-sdk` rule before writing SDK code. That rule is maintained by Sanity, is more detailed, and supersedes the notes below. The notes below are a fallback for when MCP is not available.
@@ -14,6 +14,55 @@ The app is set up for the Sanity Dashboard beta: `sanity.cli.ts` declares it wit
14
14
  - `src/SanityUI.tsx`: wraps the app in Sanity UI's `ThemeProvider` with a theme from `buildTheme()`. Sanity UI components must render inside this provider.
15
15
  - `sanity.cli.ts`: CLI config. `app` is `defineApplication({title, slug, organizationId, entry})`. `slug` is the app's unique address in the organization, used to create the app on its first deploy: lowercase letters, numbers and hyphens, starting with a letter.
16
16
 
17
+ ## Set up TypeGen before writing components
18
+
19
+ Set up Sanity TypeGen before you write components that read content. SDK hooks then infer document, projection and query result types from the project and dataset, so do not hand-write those types or pass them as type parameters. This workflow is in beta and needs `@sanity/cli` 8.16.0 or later. Check with `npx sanity --version`.
20
+
21
+ 1. Get a schema file for each dataset the app reads. In the Studio that defines the schema, run `npx sanity schema extract --path schema.production.json` and copy the file into this app's root. If you can't reach the Studio, ask the developer for the file. If you must continue without it, extract one from a temporary Studio config that matches the schema the developer described, and tell the developer to replace it with the real extract, because types from a stand-in schema can be wrong.
22
+ 2. Declare the datasets in `src/resources.ts`. Use the same object for the `SanityApp` config in `src/App.tsx` and in every hook call. `as const` keeps the IDs as literal types, which inference needs. The CLI loads this file in Node, so it must not use browser globals.
23
+
24
+ ```ts
25
+ import type {DocumentResource} from '@sanity/sdk-react'
26
+
27
+ export const resources = {
28
+ production: {projectId: 'your-project-id', dataset: 'production'},
29
+ } as const satisfies Record<string, DocumentResource>
30
+ ```
31
+
32
+ 3. Add `typegen.resources` to `sanity.cli.ts`, next to the existing `app` config. Each entry writes one types file for one dataset:
33
+
34
+ ```ts
35
+ import {resources} from './src/resources'
36
+
37
+ export default defineCliConfig({
38
+ // ...existing app config
39
+ typegen: {
40
+ resources: [
41
+ {
42
+ ...resources.production,
43
+ schema: './schema.production.json',
44
+ generates: './src/sanity.types.production.ts',
45
+ },
46
+ ],
47
+ },
48
+ })
49
+ ```
50
+
51
+ 4. Add `"typegen": "sanity typegen generate"` to the `package.json` scripts and run `npm run typegen`. Run it again after changing a schema, query or projection. `npm run dev` does not generate these types. While developing, run `npx sanity typegen generate --watch` (`@sanity/cli` 8.22.0 or later) alongside it.
52
+ 5. Define queries with `defineQuery` from `groq` (`npm install groq`) and projections with `defineProjection` from `@sanity/sdk-react`, each assigned to a variable. TypeGen only generates types for those. Then call hooks without type parameters:
53
+
54
+ ```tsx
55
+ export const bookRowProjection = defineProjection('book', '{title, "authorName": author->name}')
56
+
57
+ const {data: handles} = useDocuments({...resources.production, documentType: 'book'})
58
+
59
+ // in a child component receiving one handle (typed DocumentHandle<'book'>):
60
+ const {data} = useDocumentProjection({...handle, projection: bookRowProjection})
61
+ // data is typed from the generated file
62
+ ```
63
+
64
+ Guide: https://www.sanity.io/docs/app-sdk/sdk-typegen
65
+
17
66
  ## Commands
18
67
 
19
68
  - `npm run dev`: starts a local Sanity Dashboard on port 3333 and serves the app on the next port (3334), loaded into that Dashboard. The CLI prints a link that opens the app in the local Dashboard. The app only renders inside the Dashboard, and viewing it requires a signed-in Sanity account, so a human must complete authentication in the browser.