@sanity/codegen 3.88.1-typegen-experimental.0 → 3.88.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.
package/lib/index.d.ts CHANGED
@@ -1,12 +1,36 @@
1
1
  import {ExprNode} from 'groq-js'
2
2
  import {SchemaType} from 'groq-js'
3
- import * as t from '@babel/types'
4
3
  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 rawConfigSchema>
7
+ export declare type CodegenConfig = z.infer<typeof configDefintion>
8
8
 
9
- export declare const DEFAULT_CONFIG: NormalizedCodegenConfig
9
+ declare const configDefintion: z.ZodObject<
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
+ >
10
34
 
11
35
  /**
12
36
  * findQueriesInPath takes a path or array of paths and returns all GROQ queries in the files.
@@ -14,6 +38,7 @@ export declare const DEFAULT_CONFIG: NormalizedCodegenConfig
14
38
  * @param babelOptions - The babel configuration to use when parsing the source
15
39
  * @param resolver - A resolver function to use when resolving module imports
16
40
  * @returns An async generator that yields the results of the search
41
+ * @beta
17
42
  * @internal
18
43
  */
19
44
  export declare function findQueriesInPath({
@@ -24,7 +49,7 @@ export declare function findQueriesInPath({
24
49
  path: string | string[]
25
50
  babelOptions?: TransformOptions
26
51
  resolver?: NodeJS.RequireResolve
27
- }): AsyncGenerator<QueryExtractionResult>
52
+ }): AsyncGenerator<ResultQueries | ResultError>
28
53
 
29
54
  /**
30
55
  * findQueriesInSource takes a source string and returns all GROQ queries in it.
@@ -33,6 +58,7 @@ export declare function findQueriesInPath({
33
58
  * @param babelConfig - The babel configuration to use when parsing the source
34
59
  * @param resolver - A resolver function to use when resolving module imports
35
60
  * @returns
61
+ * @beta
36
62
  * @internal
37
63
  */
38
64
  export declare function findQueriesInSource(
@@ -59,10 +85,8 @@ declare interface NamedQueryResult {
59
85
  name: string
60
86
  /** result is a groq query */
61
87
  result: resolveExpressionReturnType
62
- /** type indicates whether this is a query or projection */
63
- type: 'query' | 'projection'
64
88
  /** location is the location of the query in the source */
65
- location?: {
89
+ location: {
66
90
  start?: {
67
91
  line: number
68
92
  column: number
@@ -76,181 +100,19 @@ declare interface NamedQueryResult {
76
100
  }
77
101
  }
78
102
 
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
103
+ declare type QueryWithTypeNode = {
100
104
  query: string
101
- code: string
102
- type: 'query' | 'projection'
103
- stats: TypeEvaluationStats
105
+ typeNode: TypeNode
104
106
  }
105
107
 
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>
108
+ export declare function readConfig(path: string): Promise<CodegenConfig>
248
109
 
249
110
  /**
250
111
  * Read a schema from a given path
251
112
  * @param path - The path to the schema
252
113
  * @returns The schema
253
114
  * @internal
115
+ * @beta
254
116
  **/
255
117
  export declare function readSchema(path: string): Promise<SchemaType>
256
118
 
@@ -270,11 +132,6 @@ declare type ResultError = {
270
132
  filename: string
271
133
  }
272
134
 
273
- declare type ResultFiles = {
274
- type: 'files'
275
- fileCount: number
276
- }
277
-
278
135
  declare type ResultQueries = {
279
136
  type: 'queries'
280
137
  filename: string
@@ -288,114 +145,60 @@ declare type ResultQueries = {
288
145
  */
289
146
  export declare function safeParseQuery(query: string): ExprNode
290
147
 
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
306
- private generateArrayTsType
307
- private generateTsObjectProperty
308
- private generateObjectTsType
309
- private generateInlineTsType
310
- private generateUnionTsType
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
148
  /**
149
+ * A class used to generate TypeScript types from a given schema
337
150
  * @internal
151
+ * @beta
338
152
  */
339
153
  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
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
399
202
  }
400
203
 
401
204
  export {}