@sanity/codegen 3.88.0 → 3.88.1-typegen-experimental.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/lib/index.d.mts +282 -85
- package/lib/index.d.ts +282 -85
- package/lib/index.js +537 -172
- package/lib/index.js.map +1 -1
- package/lib/index.mjs +538 -173
- package/lib/index.mjs.map +1 -1
- package/package.json +15 -17
- package/src/_exports/index.ts +14 -3
- package/src/readConfig.ts +71 -23
- package/src/readSchema.ts +0 -1
- package/src/typescript/asyncQueue.ts +100 -0
- package/src/typescript/expressionResolvers.ts +4 -3
- package/src/typescript/findQueriesInPath.ts +9 -2
- package/src/typescript/findQueriesInSource.ts +22 -6
- package/src/typescript/schemaTypeGenerator.ts +381 -0
- package/src/typescript/typeGenerator.ts +445 -306
package/lib/index.d.mts
CHANGED
|
@@ -1,36 +1,12 @@
|
|
|
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
5
|
import * as z from 'zod'
|
|
6
6
|
|
|
7
|
-
export declare type CodegenConfig = z.infer<typeof
|
|
7
|
+
export declare type CodegenConfig = z.infer<typeof rawConfigSchema>
|
|
8
8
|
|
|
9
|
-
declare const
|
|
10
|
-
{
|
|
11
|
-
path: z.ZodDefault<z.ZodUnion<[z.ZodString, z.ZodArray<z.ZodString, 'many'>]>>
|
|
12
|
-
schema: z.ZodDefault<z.ZodString>
|
|
13
|
-
generates: z.ZodDefault<z.ZodString>
|
|
14
|
-
formatGeneratedCode: z.ZodDefault<z.ZodBoolean>
|
|
15
|
-
overloadClientMethods: z.ZodDefault<z.ZodBoolean>
|
|
16
|
-
},
|
|
17
|
-
'strip',
|
|
18
|
-
z.ZodTypeAny,
|
|
19
|
-
{
|
|
20
|
-
path: string | string[]
|
|
21
|
-
schema: string
|
|
22
|
-
generates: string
|
|
23
|
-
formatGeneratedCode: boolean
|
|
24
|
-
overloadClientMethods: boolean
|
|
25
|
-
},
|
|
26
|
-
{
|
|
27
|
-
path?: string | string[] | undefined
|
|
28
|
-
schema?: string | undefined
|
|
29
|
-
generates?: string | undefined
|
|
30
|
-
formatGeneratedCode?: boolean | undefined
|
|
31
|
-
overloadClientMethods?: boolean | undefined
|
|
32
|
-
}
|
|
33
|
-
>
|
|
9
|
+
export declare const DEFAULT_CONFIG: NormalizedCodegenConfig
|
|
34
10
|
|
|
35
11
|
/**
|
|
36
12
|
* findQueriesInPath takes a path or array of paths and returns all GROQ queries in the files.
|
|
@@ -38,7 +14,6 @@ declare const configDefintion: z.ZodObject<
|
|
|
38
14
|
* @param babelOptions - The babel configuration to use when parsing the source
|
|
39
15
|
* @param resolver - A resolver function to use when resolving module imports
|
|
40
16
|
* @returns An async generator that yields the results of the search
|
|
41
|
-
* @beta
|
|
42
17
|
* @internal
|
|
43
18
|
*/
|
|
44
19
|
export declare function findQueriesInPath({
|
|
@@ -49,7 +24,7 @@ export declare function findQueriesInPath({
|
|
|
49
24
|
path: string | string[]
|
|
50
25
|
babelOptions?: TransformOptions
|
|
51
26
|
resolver?: NodeJS.RequireResolve
|
|
52
|
-
}): AsyncGenerator<
|
|
27
|
+
}): AsyncGenerator<QueryExtractionResult>
|
|
53
28
|
|
|
54
29
|
/**
|
|
55
30
|
* findQueriesInSource takes a source string and returns all GROQ queries in it.
|
|
@@ -58,7 +33,6 @@ export declare function findQueriesInPath({
|
|
|
58
33
|
* @param babelConfig - The babel configuration to use when parsing the source
|
|
59
34
|
* @param resolver - A resolver function to use when resolving module imports
|
|
60
35
|
* @returns
|
|
61
|
-
* @beta
|
|
62
36
|
* @internal
|
|
63
37
|
*/
|
|
64
38
|
export declare function findQueriesInSource(
|
|
@@ -85,8 +59,10 @@ declare interface NamedQueryResult {
|
|
|
85
59
|
name: string
|
|
86
60
|
/** result is a groq query */
|
|
87
61
|
result: resolveExpressionReturnType
|
|
62
|
+
/** type indicates whether this is a query or projection */
|
|
63
|
+
type: 'query' | 'projection'
|
|
88
64
|
/** location is the location of the query in the source */
|
|
89
|
-
location
|
|
65
|
+
location?: {
|
|
90
66
|
start?: {
|
|
91
67
|
line: number
|
|
92
68
|
column: number
|
|
@@ -100,19 +76,181 @@ declare interface NamedQueryResult {
|
|
|
100
76
|
}
|
|
101
77
|
}
|
|
102
78
|
|
|
103
|
-
|
|
79
|
+
/**
|
|
80
|
+
* Normalized config type always containing a "schemas" array and no "schema" property
|
|
81
|
+
*/
|
|
82
|
+
export declare type NormalizedCodegenConfig = {
|
|
83
|
+
path: string[]
|
|
84
|
+
generates: string
|
|
85
|
+
formatGeneratedCode: boolean
|
|
86
|
+
overloadClientMethods: boolean
|
|
87
|
+
augmentGroqModule: boolean
|
|
88
|
+
schemas: Array<{
|
|
89
|
+
schemaId: string
|
|
90
|
+
schemaPath: string
|
|
91
|
+
}>
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
export declare type QueryExtractionResult = ResultFiles | ResultQueries | ResultError
|
|
95
|
+
|
|
96
|
+
export declare interface QueryResultDeclaration {
|
|
97
|
+
filename: string
|
|
98
|
+
name: string
|
|
99
|
+
id: t.Identifier
|
|
104
100
|
query: string
|
|
105
|
-
|
|
101
|
+
code: string
|
|
102
|
+
type: 'query' | 'projection'
|
|
103
|
+
stats: TypeEvaluationStats
|
|
106
104
|
}
|
|
107
105
|
|
|
108
|
-
export declare
|
|
106
|
+
export declare type QueryResultDeclarationsFileResult =
|
|
107
|
+
| {
|
|
108
|
+
type: 'queries'
|
|
109
|
+
filename: string
|
|
110
|
+
queryResultDeclarations: QueryResultDeclaration[]
|
|
111
|
+
}
|
|
112
|
+
| {
|
|
113
|
+
type: 'error'
|
|
114
|
+
filename: string
|
|
115
|
+
error: unknown
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
/**
|
|
119
|
+
* Define a raw config schema that accepts either a single 'schema' or an array 'schemas'.
|
|
120
|
+
*
|
|
121
|
+
* @remarks
|
|
122
|
+
* - Both fields are optional, but if both are provided, that is an error
|
|
123
|
+
* - If 'schemas' is provided, it cannot be an empty array
|
|
124
|
+
*/
|
|
125
|
+
declare const rawConfigSchema: z.ZodEffects<
|
|
126
|
+
z.ZodEffects<
|
|
127
|
+
z.ZodObject<
|
|
128
|
+
{
|
|
129
|
+
path: z.ZodOptional<z.ZodUnion<[z.ZodString, z.ZodArray<z.ZodString, 'many'>]>>
|
|
130
|
+
generates: z.ZodOptional<z.ZodString>
|
|
131
|
+
formatGeneratedCode: z.ZodOptional<z.ZodBoolean>
|
|
132
|
+
overloadClientMethods: z.ZodOptional<z.ZodBoolean>
|
|
133
|
+
augmentGroqModule: z.ZodOptional<z.ZodBoolean>
|
|
134
|
+
schema: z.ZodOptional<z.ZodString>
|
|
135
|
+
unstable_schemas: z.ZodOptional<
|
|
136
|
+
z.ZodArray<
|
|
137
|
+
z.ZodObject<
|
|
138
|
+
{
|
|
139
|
+
schemaPath: z.ZodString
|
|
140
|
+
schemaId: z.ZodString
|
|
141
|
+
},
|
|
142
|
+
'strip',
|
|
143
|
+
z.ZodTypeAny,
|
|
144
|
+
{
|
|
145
|
+
schemaPath: string
|
|
146
|
+
schemaId: string
|
|
147
|
+
},
|
|
148
|
+
{
|
|
149
|
+
schemaPath: string
|
|
150
|
+
schemaId: string
|
|
151
|
+
}
|
|
152
|
+
>,
|
|
153
|
+
'many'
|
|
154
|
+
>
|
|
155
|
+
>
|
|
156
|
+
},
|
|
157
|
+
'strip',
|
|
158
|
+
z.ZodTypeAny,
|
|
159
|
+
{
|
|
160
|
+
path?: string | string[] | undefined
|
|
161
|
+
generates?: string | undefined
|
|
162
|
+
formatGeneratedCode?: boolean | undefined
|
|
163
|
+
overloadClientMethods?: boolean | undefined
|
|
164
|
+
augmentGroqModule?: boolean | undefined
|
|
165
|
+
schema?: string | undefined
|
|
166
|
+
unstable_schemas?:
|
|
167
|
+
| {
|
|
168
|
+
schemaPath: string
|
|
169
|
+
schemaId: string
|
|
170
|
+
}[]
|
|
171
|
+
| undefined
|
|
172
|
+
},
|
|
173
|
+
{
|
|
174
|
+
path?: string | string[] | undefined
|
|
175
|
+
generates?: string | undefined
|
|
176
|
+
formatGeneratedCode?: boolean | undefined
|
|
177
|
+
overloadClientMethods?: boolean | undefined
|
|
178
|
+
augmentGroqModule?: boolean | undefined
|
|
179
|
+
schema?: string | undefined
|
|
180
|
+
unstable_schemas?:
|
|
181
|
+
| {
|
|
182
|
+
schemaPath: string
|
|
183
|
+
schemaId: string
|
|
184
|
+
}[]
|
|
185
|
+
| undefined
|
|
186
|
+
}
|
|
187
|
+
>,
|
|
188
|
+
{
|
|
189
|
+
path?: string | string[] | undefined
|
|
190
|
+
generates?: string | undefined
|
|
191
|
+
formatGeneratedCode?: boolean | undefined
|
|
192
|
+
overloadClientMethods?: boolean | undefined
|
|
193
|
+
augmentGroqModule?: boolean | undefined
|
|
194
|
+
schema?: string | undefined
|
|
195
|
+
unstable_schemas?:
|
|
196
|
+
| {
|
|
197
|
+
schemaPath: string
|
|
198
|
+
schemaId: string
|
|
199
|
+
}[]
|
|
200
|
+
| undefined
|
|
201
|
+
},
|
|
202
|
+
{
|
|
203
|
+
path?: string | string[] | undefined
|
|
204
|
+
generates?: string | undefined
|
|
205
|
+
formatGeneratedCode?: boolean | undefined
|
|
206
|
+
overloadClientMethods?: boolean | undefined
|
|
207
|
+
augmentGroqModule?: boolean | undefined
|
|
208
|
+
schema?: string | undefined
|
|
209
|
+
unstable_schemas?:
|
|
210
|
+
| {
|
|
211
|
+
schemaPath: string
|
|
212
|
+
schemaId: string
|
|
213
|
+
}[]
|
|
214
|
+
| undefined
|
|
215
|
+
}
|
|
216
|
+
>,
|
|
217
|
+
{
|
|
218
|
+
path?: string | string[] | undefined
|
|
219
|
+
generates?: string | undefined
|
|
220
|
+
formatGeneratedCode?: boolean | undefined
|
|
221
|
+
overloadClientMethods?: boolean | undefined
|
|
222
|
+
augmentGroqModule?: boolean | undefined
|
|
223
|
+
schema?: string | undefined
|
|
224
|
+
unstable_schemas?:
|
|
225
|
+
| {
|
|
226
|
+
schemaPath: string
|
|
227
|
+
schemaId: string
|
|
228
|
+
}[]
|
|
229
|
+
| undefined
|
|
230
|
+
},
|
|
231
|
+
{
|
|
232
|
+
path?: string | string[] | undefined
|
|
233
|
+
generates?: string | undefined
|
|
234
|
+
formatGeneratedCode?: boolean | undefined
|
|
235
|
+
overloadClientMethods?: boolean | undefined
|
|
236
|
+
augmentGroqModule?: boolean | undefined
|
|
237
|
+
schema?: string | undefined
|
|
238
|
+
unstable_schemas?:
|
|
239
|
+
| {
|
|
240
|
+
schemaPath: string
|
|
241
|
+
schemaId: string
|
|
242
|
+
}[]
|
|
243
|
+
| undefined
|
|
244
|
+
}
|
|
245
|
+
>
|
|
246
|
+
|
|
247
|
+
export declare function readConfig(path: string): Promise<NormalizedCodegenConfig>
|
|
109
248
|
|
|
110
249
|
/**
|
|
111
250
|
* Read a schema from a given path
|
|
112
251
|
* @param path - The path to the schema
|
|
113
252
|
* @returns The schema
|
|
114
253
|
* @internal
|
|
115
|
-
* @beta
|
|
116
254
|
**/
|
|
117
255
|
export declare function readSchema(path: string): Promise<SchemaType>
|
|
118
256
|
|
|
@@ -132,6 +270,11 @@ declare type ResultError = {
|
|
|
132
270
|
filename: string
|
|
133
271
|
}
|
|
134
272
|
|
|
273
|
+
declare type ResultFiles = {
|
|
274
|
+
type: 'files'
|
|
275
|
+
fileCount: number
|
|
276
|
+
}
|
|
277
|
+
|
|
135
278
|
declare type ResultQueries = {
|
|
136
279
|
type: 'queries'
|
|
137
280
|
filename: string
|
|
@@ -145,60 +288,114 @@ declare type ResultQueries = {
|
|
|
145
288
|
*/
|
|
146
289
|
export declare function safeParseQuery(query: string): ExprNode
|
|
147
290
|
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
private
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
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
|
|
291
|
+
declare class SchemaTypeGenerator {
|
|
292
|
+
static ProjectionBase: t.Identifier
|
|
293
|
+
static InternalReferenceSymbol: t.Identifier & {
|
|
294
|
+
typeAnnotation: t.TSTypeAnnotation
|
|
295
|
+
}
|
|
296
|
+
static sanitizeIdentifier(input: string): string
|
|
297
|
+
static normalizeIdentifier(input: string): string
|
|
298
|
+
readonly schema: SchemaType
|
|
299
|
+
readonly schemaId: string
|
|
300
|
+
private typeNames
|
|
301
|
+
private tsTypes
|
|
302
|
+
private identifiers
|
|
303
|
+
constructor({schema, schemaId}: SchemaTypeGeneratorOptions)
|
|
304
|
+
private shouldOmitProjection
|
|
305
|
+
private generateTsType
|
|
196
306
|
private generateArrayTsType
|
|
197
|
-
private
|
|
307
|
+
private generateTsObjectProperty
|
|
198
308
|
private generateObjectTsType
|
|
199
309
|
private generateInlineTsType
|
|
200
310
|
private generateUnionTsType
|
|
201
|
-
private
|
|
311
|
+
private generateDocumentTsType
|
|
312
|
+
getTypeNames(): string[]
|
|
313
|
+
getTsType(typeName: string): t.TSType | undefined
|
|
314
|
+
getIdentifier(typeName: string): t.Identifier | undefined
|
|
315
|
+
evaluateQuery(query: string): {
|
|
316
|
+
tsType: t.TSType
|
|
317
|
+
stats: TypeEvaluationStats
|
|
318
|
+
}
|
|
319
|
+
evaluateProjection(projection: string): {
|
|
320
|
+
tsType: t.TSType
|
|
321
|
+
stats: TypeEvaluationStats
|
|
322
|
+
} | null
|
|
323
|
+
}
|
|
324
|
+
|
|
325
|
+
declare interface SchemaTypeGeneratorOptions {
|
|
326
|
+
schema: SchemaType
|
|
327
|
+
schemaId: string
|
|
328
|
+
}
|
|
329
|
+
|
|
330
|
+
export declare interface TypeEvaluationStats {
|
|
331
|
+
allTypes: number
|
|
332
|
+
unknownTypes: number
|
|
333
|
+
emptyUnions: number
|
|
334
|
+
}
|
|
335
|
+
|
|
336
|
+
/**
|
|
337
|
+
* @internal
|
|
338
|
+
*/
|
|
339
|
+
export declare class TypeGenerator {
|
|
340
|
+
readonly schemas: SchemaTypeGenerator[]
|
|
341
|
+
readonly augmentGroqModule: boolean
|
|
342
|
+
readonly overloadClientMethods: boolean
|
|
343
|
+
private schemaFilenames
|
|
344
|
+
private globalIdentifiers
|
|
345
|
+
private identifiersByQuery
|
|
346
|
+
private identifiersByProjection
|
|
347
|
+
private queryResultsQueue
|
|
348
|
+
private augmentedQueryResultsPromise
|
|
349
|
+
private queryFileCountPromise
|
|
350
|
+
constructor({
|
|
351
|
+
schemas,
|
|
352
|
+
queriesByFile,
|
|
353
|
+
augmentGroqModule,
|
|
354
|
+
overloadClientMethods,
|
|
355
|
+
}: TypeGeneratorOptions)
|
|
356
|
+
private handleQueries
|
|
357
|
+
private generateQueryResultType
|
|
358
|
+
private generateAndRegisterExportedTypeAlias
|
|
359
|
+
getKnownTypes: (this: void) => {
|
|
360
|
+
code: string
|
|
361
|
+
}
|
|
362
|
+
getSchemaTypeDeclarations: (this: void) => {
|
|
363
|
+
id: t.Identifier
|
|
364
|
+
code: string
|
|
365
|
+
schemaIds: string[]
|
|
366
|
+
}[]
|
|
367
|
+
getAllSanitySchemaTypesDeclaration: (this: void) => {
|
|
368
|
+
code: string
|
|
369
|
+
}
|
|
370
|
+
getSchemaDeclarations: (this: void) => {
|
|
371
|
+
id: t.Identifier
|
|
372
|
+
code: string
|
|
373
|
+
schemaId: string
|
|
374
|
+
}[]
|
|
375
|
+
getAugmentedSchemasDeclarations: (this: void) => {
|
|
376
|
+
code: string
|
|
377
|
+
}
|
|
378
|
+
getQueryFileCount: () => Promise<{
|
|
379
|
+
fileCount: number
|
|
380
|
+
}>
|
|
381
|
+
getQueryResultDeclarations: () => AsyncIterable<QueryResultDeclarationsFileResult>
|
|
382
|
+
getAugmentedQueryResultsDeclarations: () => Promise<{
|
|
383
|
+
code: string
|
|
384
|
+
}>
|
|
385
|
+
}
|
|
386
|
+
|
|
387
|
+
/**
|
|
388
|
+
* @internal
|
|
389
|
+
*/
|
|
390
|
+
export declare interface TypeGeneratorOptions {
|
|
391
|
+
schemas: {
|
|
392
|
+
schema: SchemaType
|
|
393
|
+
schemaId: string
|
|
394
|
+
filename?: string
|
|
395
|
+
}[]
|
|
396
|
+
queriesByFile?: AsyncIterable<QueryExtractionResult>
|
|
397
|
+
augmentGroqModule?: boolean
|
|
398
|
+
overloadClientMethods?: boolean
|
|
202
399
|
}
|
|
203
400
|
|
|
204
401
|
export {}
|