@sanity/codegen 5.0.0-next.0-9b570ece82-202507150640 → 5.0.0-next.7

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/LICENSE CHANGED
@@ -1,6 +1,6 @@
1
1
  MIT License
2
2
 
3
- Copyright (c) 2016 - 2024 Sanity.io
3
+ Copyright (c) 2016 - 2025 Sanity.io
4
4
 
5
5
  Permission is hereby granted, free of charge, to any person obtaining a copy
6
6
  of this software and associated documentation files (the "Software"), to deal
package/lib/index.d.ts CHANGED
@@ -1,12 +1,20 @@
1
1
  import {ExprNode} from 'groq-js'
2
2
  import {SchemaType} from 'groq-js'
3
+ import * as t from '@babel/types'
3
4
  import {TransformOptions} from '@babel/core'
4
- import {TypeNode} from 'groq-js'
5
+ import {WorkerChannel} from '@sanity/worker-channels'
6
+ import {WorkerChannelReporter} from '@sanity/worker-channels'
5
7
  import * as z from 'zod'
6
8
 
7
- export declare type CodegenConfig = z.infer<typeof configDefintion>
9
+ /**
10
+ * @deprecated use TypeGenConfig
11
+ */
12
+ export declare type CodegenConfig = TypeGenConfig
8
13
 
9
- declare const configDefintion: z.ZodObject<
14
+ /**
15
+ * @internal
16
+ */
17
+ export declare const configDefinition: z.ZodObject<
10
18
  {
11
19
  path: z.ZodDefault<z.ZodUnion<[z.ZodString, z.ZodArray<z.ZodString, 'many'>]>>
12
20
  schema: z.ZodDefault<z.ZodString>
@@ -32,6 +40,48 @@ declare const configDefintion: z.ZodObject<
32
40
  }
33
41
  >
34
42
 
43
+ /**
44
+ * A module containing queries that have been evaluated.
45
+ * @public
46
+ */
47
+ export declare interface EvaluatedModule {
48
+ filename: string
49
+ queries: EvaluatedQuery[]
50
+ errors: (QueryExtractionError | QueryEvaluationError)[]
51
+ }
52
+
53
+ /**
54
+ * An `ExtractedQuery` that has been evaluated against a schema, yielding a TypeScript type.
55
+ * @public
56
+ */
57
+ export declare interface EvaluatedQuery extends ExtractedQuery {
58
+ id: t.Identifier
59
+ code: string
60
+ tsType: t.TSType
61
+ ast: t.ExportNamedDeclaration
62
+ stats: TypeEvaluationStats
63
+ }
64
+
65
+ /**
66
+ * A module (file) containing extracted GROQ queries.
67
+ * @public
68
+ */
69
+ export declare interface ExtractedModule {
70
+ filename: string
71
+ queries: ExtractedQuery[]
72
+ errors: QueryExtractionError[]
73
+ }
74
+
75
+ /**
76
+ * A GROQ query extracted from a source file.
77
+ * @public
78
+ */
79
+ export declare interface ExtractedQuery {
80
+ variable: QueryVariable
81
+ query: string
82
+ filename: string
83
+ }
84
+
35
85
  /**
36
86
  * findQueriesInPath takes a path or array of paths and returns all GROQ queries in the files.
37
87
  * @param path - The path or array of paths to search for queries
@@ -45,11 +95,16 @@ export declare function findQueriesInPath({
45
95
  path,
46
96
  babelOptions,
47
97
  resolver,
48
- }: {
98
+ }: FindQueriesInPathOptions): {
99
+ files: string[]
100
+ queries: AsyncIterable<ExtractedModule>
101
+ }
102
+
103
+ declare interface FindQueriesInPathOptions {
49
104
  path: string | string[]
50
105
  babelOptions?: TransformOptions
51
106
  resolver?: NodeJS.RequireResolve
52
- }): AsyncGenerator<ResultQueries | ResultError>
107
+ }
53
108
 
54
109
  /**
55
110
  * findQueriesInSource takes a source string and returns all GROQ queries in it.
@@ -66,7 +121,16 @@ export declare function findQueriesInSource(
66
121
  filename: string,
67
122
  babelConfig?: TransformOptions,
68
123
  resolver?: NodeJS.RequireResolve,
69
- ): NamedQueryResult[]
124
+ ): ExtractedModule
125
+
126
+ export declare interface GenerateTypesOptions {
127
+ schema: SchemaType
128
+ schemaPath?: string
129
+ queries?: AsyncIterable<ExtractedModule>
130
+ root?: string
131
+ overloadClientMethods?: boolean
132
+ reporter?: WorkerChannelReporter<TypegenWorkerChannel>
133
+ }
70
134
 
71
135
  /**
72
136
  * This is a custom implementation of require.resolve that takes into account the paths
@@ -78,34 +142,48 @@ export declare function findQueriesInSource(
78
142
  export declare function getResolver(cwd?: string): NodeJS.RequireResolve
79
143
 
80
144
  /**
81
- * NamedQueryResult is a result of a named query
145
+ * An error that occurred during query evaluation.
146
+ * @public
82
147
  */
83
- declare interface NamedQueryResult {
84
- /** name is the name of the query */
85
- name: string
86
- /** result is a groq query */
87
- result: resolveExpressionReturnType
88
- /** location is the location of the query in the source */
89
- location: {
90
- start?: {
91
- line: number
92
- column: number
93
- index: number
94
- }
95
- end?: {
96
- line: number
97
- column: number
98
- index: number
99
- }
100
- }
148
+ declare class QueryEvaluationError extends Error {
149
+ variable?: QueryVariable
150
+ filename: string
151
+ constructor({variable, cause, filename}: QueryEvaluationErrorOptions)
101
152
  }
102
153
 
103
- declare type QueryWithTypeNode = {
104
- query: string
105
- typeNode: TypeNode
154
+ declare interface QueryEvaluationErrorOptions {
155
+ variable?: QueryVariable
156
+ cause: unknown
157
+ filename: string
106
158
  }
107
159
 
108
- export declare function readConfig(path: string): Promise<CodegenConfig>
160
+ /**
161
+ * An error that occurred during query extraction.
162
+ * @public
163
+ */
164
+ export declare class QueryExtractionError extends Error {
165
+ variable?: QueryVariable
166
+ filename: string
167
+ constructor({variable, cause, filename}: QueryExtractionErrorOptions)
168
+ }
169
+
170
+ declare interface QueryExtractionErrorOptions {
171
+ variable?: QueryVariable
172
+ cause: unknown
173
+ filename: string
174
+ }
175
+
176
+ declare interface QueryVariable {
177
+ id: t.Identifier
178
+ start?: number
179
+ end?: number
180
+ }
181
+
182
+ /**
183
+ * Read, parse and process a config file
184
+ * @internal
185
+ */
186
+ export declare function readConfig(path: string): Promise<TypeGenConfig>
109
187
 
110
188
  /**
111
189
  * Read a schema from a given path
@@ -124,20 +202,6 @@ export declare function readSchema(path: string): Promise<SchemaType>
124
202
  */
125
203
  export declare function registerBabel(babelOptions?: TransformOptions): void
126
204
 
127
- declare type resolveExpressionReturnType = string
128
-
129
- declare type ResultError = {
130
- type: 'error'
131
- error: Error
132
- filename: string
133
- }
134
-
135
- declare type ResultQueries = {
136
- type: 'queries'
137
- filename: string
138
- queries: NamedQueryResult[]
139
- }
140
-
141
205
  /**
142
206
  * safeParseQuery parses a GROQ query string, but first attempts to extract any parameters used in slices. This method is _only_
143
207
  * intended for use in type generation where we don't actually execute the parsed AST on a dataset, and should not be used elsewhere.
@@ -145,60 +209,62 @@ declare type ResultQueries = {
145
209
  */
146
210
  export declare function safeParseQuery(query: string): ExprNode
147
211
 
212
+ /**
213
+ * Statistics from the query type evaluation process.
214
+ * @public
215
+ */
216
+ declare interface TypeEvaluationStats {
217
+ allTypes: number
218
+ unknownTypes: number
219
+ emptyUnions: number
220
+ }
221
+
222
+ export declare type TypeGenConfig = z.infer<typeof configDefinition>
223
+
148
224
  /**
149
225
  * A class used to generate TypeScript types from a given schema
150
- * @internal
151
226
  * @beta
152
227
  */
153
228
  export declare class TypeGenerator {
154
- private generatedTypeName
155
- private typeNameMap
156
- private typeNodeNameMap
157
- private readonly schema
158
- constructor(schema: SchemaType)
159
- /**
160
- * Generate TypeScript types for the given schema
161
- * @returns string
162
- * @internal
163
- * @beta
164
- */
165
- generateSchemaTypes(): string
166
- /**
167
- * Takes a identifier and a type node and generates a type alias for the type node.
168
- * @param identifierName - The name of the type to generated
169
- * @param typeNode - The type node to generate the type for
170
- * @returns
171
- * @internal
172
- * @beta
173
- */
174
- generateTypeNodeTypes(identifierName: string, typeNode: TypeNode): string
175
- static generateKnownTypes(): string
176
- /**
177
- * Takes a list of queries from the codebase and generates a type declaration
178
- * for SanityClient to consume.
179
- *
180
- * Note: only types that have previously been generated with `generateTypeNodeTypes`
181
- * will be included in the query map.
182
- *
183
- * @param queries - A list of queries to generate a type declaration for
184
- * @returns
185
- * @internal
186
- * @beta
187
- */
188
- generateQueryMap(queries: QueryWithTypeNode[]): string
189
- /**
190
- * Since we are sanitizing identifiers we migt end up with collisions. Ie there might be a type mux.video and muxVideo, both these
191
- * types would be sanityized into MuxVideo. To avoid this we keep track of the generated type names and add a index to the name.
192
- * When we reference a type we also keep track of the original name so we can reference the correct type later.
193
- */
194
- private getTypeName
195
- private getTypeNodeType
196
- private generateArrayTsType
197
- private generateObjectProperty
198
- private generateObjectTsType
199
- private generateInlineTsType
200
- private generateUnionTsType
201
- private generateDocumentType
229
+ private getInternalReferenceSymbolDeclaration
230
+ private getSchemaTypeGenerator
231
+ private getSchemaTypeDeclarations
232
+ private getAllSanitySchemaTypesDeclaration
233
+ private static getEvaluatedModules
234
+ private static getQueryMapDeclaration
235
+ generateTypes(options: GenerateTypesOptions): Promise<{
236
+ code: string
237
+ ast: t.Program
238
+ }>
202
239
  }
203
240
 
241
+ export declare type TypegenWorkerChannel = WorkerChannel.Definition<{
242
+ generatedSchemaTypes: WorkerChannel.Event<{
243
+ internalReferenceSymbol: {
244
+ id: t.Identifier
245
+ code: string
246
+ ast: t.ExportNamedDeclaration
247
+ }
248
+ schemaTypeDeclarations: {
249
+ id: t.Identifier
250
+ name: string
251
+ code: string
252
+ tsType: t.TSType
253
+ ast: t.ExportNamedDeclaration
254
+ }[]
255
+ allSanitySchemaTypesDeclaration: {
256
+ code: string
257
+ id: t.Identifier
258
+ ast: t.ExportNamedDeclaration
259
+ }
260
+ }>
261
+ evaluatedModules: WorkerChannel.Stream<EvaluatedModule>
262
+ generatedQueryTypes: WorkerChannel.Event<{
263
+ queryMapDeclaration: {
264
+ code: string
265
+ ast: t.Program
266
+ }
267
+ }>
268
+ }>
269
+
204
270
  export {}