@sanity/codegen 3.86.2-experimental.0 → 3.87.1-canary.4

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 CHANGED
@@ -1,6 +1,36 @@
1
1
  import {ExprNode} from 'groq-js'
2
2
  import {SchemaType} from 'groq-js'
3
3
  import {TransformOptions} from '@babel/core'
4
+ import {TypeNode} from 'groq-js'
5
+ import * as z from 'zod'
6
+
7
+ export declare type CodegenConfig = z.infer<typeof configDefintion>
8
+
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
+ >
4
34
 
5
35
  /**
6
36
  * findQueriesInPath takes a path or array of paths and returns all GROQ queries in the files.
@@ -38,54 +68,6 @@ export declare function findQueriesInSource(
38
68
  resolver?: NodeJS.RequireResolve,
39
69
  ): NamedQueryResult[]
40
70
 
41
- export declare interface GeneratedQueries {
42
- filename: string
43
- results: GeneratedQuery[]
44
- error?: unknown
45
- }
46
-
47
- export declare interface GeneratedQuery {
48
- name: string
49
- query: string
50
- code: string
51
- type: 'query' | 'projection'
52
- stats: TypeEvaluationStats
53
- }
54
-
55
- export declare interface GeneratedSchema {
56
- code: string
57
- schemas: {
58
- filename: string
59
- projectId: string | 'default'
60
- dataset: string | 'default'
61
- typeCount: number
62
- }[]
63
- }
64
-
65
- export declare interface GeneratedTypemap {
66
- code: string
67
- stats: TypeEvaluationStats
68
- }
69
-
70
- export declare function generateTypes({
71
- schemas,
72
- queriesByFile,
73
- augmentGroqModule,
74
- overloadClientMethods,
75
- }: GenerateTypesOptions): TypegenResults
76
-
77
- export declare interface GenerateTypesOptions {
78
- schemas: {
79
- projectId: string
80
- dataset: string
81
- filename: string
82
- schema: SchemaType
83
- }[]
84
- queriesByFile: AsyncIterable<ResultQueries | ResultError>
85
- augmentGroqModule: boolean
86
- overloadClientMethods: boolean
87
- }
88
-
89
71
  /**
90
72
  * This is a custom implementation of require.resolve that takes into account the paths
91
73
  * configuration in tsconfig.json. This is necessary if we want to resolve paths that are
@@ -103,10 +85,8 @@ declare interface NamedQueryResult {
103
85
  name: string
104
86
  /** result is a groq query */
105
87
  result: resolveExpressionReturnType
106
- /** type indicates whether this is a query or projection */
107
- type: 'query' | 'projection'
108
88
  /** location is the location of the query in the source */
109
- location?: {
89
+ location: {
110
90
  start?: {
111
91
  line: number
112
92
  column: number
@@ -120,23 +100,12 @@ declare interface NamedQueryResult {
120
100
  }
121
101
  }
122
102
 
123
- /**
124
- * Normalized config type always containing a "schemas" array and no "schema" property
125
- */
126
- export declare type NormalizedTypegenConfig = {
127
- path: string[]
128
- generates: string
129
- formatGeneratedCode: boolean
130
- overloadClientMethods: boolean
131
- augmentGroqModule: boolean
132
- schemas: Array<{
133
- projectId: string
134
- dataset: string
135
- schemaPath: string
136
- }>
103
+ declare type QueryWithTypeNode = {
104
+ query: string
105
+ typeNode: TypeNode
137
106
  }
138
107
 
139
- export declare function readConfig(path: string): Promise<NormalizedTypegenConfig>
108
+ export declare function readConfig(path: string): Promise<CodegenConfig>
140
109
 
141
110
  /**
142
111
  * Read a schema from a given path
@@ -157,13 +126,13 @@ export declare function registerBabel(babelOptions?: TransformOptions): void
157
126
 
158
127
  declare type resolveExpressionReturnType = string
159
128
 
160
- export declare type ResultError = {
129
+ declare type ResultError = {
161
130
  type: 'error'
162
131
  error: Error
163
132
  filename: string
164
133
  }
165
134
 
166
- export declare type ResultQueries = {
135
+ declare type ResultQueries = {
167
136
  type: 'queries'
168
137
  filename: string
169
138
  queries: NamedQueryResult[]
@@ -176,17 +145,60 @@ export declare type ResultQueries = {
176
145
  */
177
146
  export declare function safeParseQuery(query: string): ExprNode
178
147
 
179
- /** @internal */
180
- export declare interface TypeEvaluationStats {
181
- totalTypeNodes: number
182
- unknownTypeCount: number
183
- emptyUnionCount: number
184
- }
185
-
186
- export declare interface TypegenResults {
187
- generatedSchema: () => Promise<GeneratedSchema>
188
- generatedQueries: () => AsyncIterable<GeneratedQueries>
189
- generatedTypemap: () => Promise<GeneratedTypemap>
148
+ /**
149
+ * A class used to generate TypeScript types from a given schema
150
+ * @internal
151
+ * @beta
152
+ */
153
+ 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
190
202
  }
191
203
 
192
204
  export {}
package/lib/index.d.ts CHANGED
@@ -1,6 +1,36 @@
1
1
  import {ExprNode} from 'groq-js'
2
2
  import {SchemaType} from 'groq-js'
3
3
  import {TransformOptions} from '@babel/core'
4
+ import {TypeNode} from 'groq-js'
5
+ import * as z from 'zod'
6
+
7
+ export declare type CodegenConfig = z.infer<typeof configDefintion>
8
+
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
+ >
4
34
 
5
35
  /**
6
36
  * findQueriesInPath takes a path or array of paths and returns all GROQ queries in the files.
@@ -38,54 +68,6 @@ export declare function findQueriesInSource(
38
68
  resolver?: NodeJS.RequireResolve,
39
69
  ): NamedQueryResult[]
40
70
 
41
- export declare interface GeneratedQueries {
42
- filename: string
43
- results: GeneratedQuery[]
44
- error?: unknown
45
- }
46
-
47
- export declare interface GeneratedQuery {
48
- name: string
49
- query: string
50
- code: string
51
- type: 'query' | 'projection'
52
- stats: TypeEvaluationStats
53
- }
54
-
55
- export declare interface GeneratedSchema {
56
- code: string
57
- schemas: {
58
- filename: string
59
- projectId: string | 'default'
60
- dataset: string | 'default'
61
- typeCount: number
62
- }[]
63
- }
64
-
65
- export declare interface GeneratedTypemap {
66
- code: string
67
- stats: TypeEvaluationStats
68
- }
69
-
70
- export declare function generateTypes({
71
- schemas,
72
- queriesByFile,
73
- augmentGroqModule,
74
- overloadClientMethods,
75
- }: GenerateTypesOptions): TypegenResults
76
-
77
- export declare interface GenerateTypesOptions {
78
- schemas: {
79
- projectId: string
80
- dataset: string
81
- filename: string
82
- schema: SchemaType
83
- }[]
84
- queriesByFile: AsyncIterable<ResultQueries | ResultError>
85
- augmentGroqModule: boolean
86
- overloadClientMethods: boolean
87
- }
88
-
89
71
  /**
90
72
  * This is a custom implementation of require.resolve that takes into account the paths
91
73
  * configuration in tsconfig.json. This is necessary if we want to resolve paths that are
@@ -103,10 +85,8 @@ declare interface NamedQueryResult {
103
85
  name: string
104
86
  /** result is a groq query */
105
87
  result: resolveExpressionReturnType
106
- /** type indicates whether this is a query or projection */
107
- type: 'query' | 'projection'
108
88
  /** location is the location of the query in the source */
109
- location?: {
89
+ location: {
110
90
  start?: {
111
91
  line: number
112
92
  column: number
@@ -120,23 +100,12 @@ declare interface NamedQueryResult {
120
100
  }
121
101
  }
122
102
 
123
- /**
124
- * Normalized config type always containing a "schemas" array and no "schema" property
125
- */
126
- export declare type NormalizedTypegenConfig = {
127
- path: string[]
128
- generates: string
129
- formatGeneratedCode: boolean
130
- overloadClientMethods: boolean
131
- augmentGroqModule: boolean
132
- schemas: Array<{
133
- projectId: string
134
- dataset: string
135
- schemaPath: string
136
- }>
103
+ declare type QueryWithTypeNode = {
104
+ query: string
105
+ typeNode: TypeNode
137
106
  }
138
107
 
139
- export declare function readConfig(path: string): Promise<NormalizedTypegenConfig>
108
+ export declare function readConfig(path: string): Promise<CodegenConfig>
140
109
 
141
110
  /**
142
111
  * Read a schema from a given path
@@ -157,13 +126,13 @@ export declare function registerBabel(babelOptions?: TransformOptions): void
157
126
 
158
127
  declare type resolveExpressionReturnType = string
159
128
 
160
- export declare type ResultError = {
129
+ declare type ResultError = {
161
130
  type: 'error'
162
131
  error: Error
163
132
  filename: string
164
133
  }
165
134
 
166
- export declare type ResultQueries = {
135
+ declare type ResultQueries = {
167
136
  type: 'queries'
168
137
  filename: string
169
138
  queries: NamedQueryResult[]
@@ -176,17 +145,60 @@ export declare type ResultQueries = {
176
145
  */
177
146
  export declare function safeParseQuery(query: string): ExprNode
178
147
 
179
- /** @internal */
180
- export declare interface TypeEvaluationStats {
181
- totalTypeNodes: number
182
- unknownTypeCount: number
183
- emptyUnionCount: number
184
- }
185
-
186
- export declare interface TypegenResults {
187
- generatedSchema: () => Promise<GeneratedSchema>
188
- generatedQueries: () => AsyncIterable<GeneratedQueries>
189
- generatedTypemap: () => Promise<GeneratedTypemap>
148
+ /**
149
+ * A class used to generate TypeScript types from a given schema
150
+ * @internal
151
+ * @beta
152
+ */
153
+ 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
190
202
  }
191
203
 
192
204
  export {}