@sanity/codegen 4.21.0-next.9 → 5.0.0-next-major.14
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 +144 -90
- package/lib/index.js +365 -203
- package/lib/index.js.map +1 -1
- package/package.json +9 -7
package/lib/index.d.ts
CHANGED
|
@@ -1,7 +1,9 @@
|
|
|
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 {
|
|
5
|
+
import {WorkerChannel} from '@sanity/worker-channels'
|
|
6
|
+
import {WorkerChannelReporter} from '@sanity/worker-channels'
|
|
5
7
|
import * as z from 'zod'
|
|
6
8
|
|
|
7
9
|
/**
|
|
@@ -38,6 +40,48 @@ export declare const configDefinition: z.ZodObject<
|
|
|
38
40
|
}
|
|
39
41
|
>
|
|
40
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
|
+
|
|
41
85
|
/**
|
|
42
86
|
* findQueriesInPath takes a path or array of paths and returns all GROQ queries in the files.
|
|
43
87
|
* @param path - The path or array of paths to search for queries
|
|
@@ -51,11 +95,16 @@ export declare function findQueriesInPath({
|
|
|
51
95
|
path,
|
|
52
96
|
babelOptions,
|
|
53
97
|
resolver,
|
|
54
|
-
}: {
|
|
98
|
+
}: FindQueriesInPathOptions): {
|
|
99
|
+
files: string[]
|
|
100
|
+
queries: AsyncIterable<ExtractedModule>
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
declare interface FindQueriesInPathOptions {
|
|
55
104
|
path: string | string[]
|
|
56
105
|
babelOptions?: TransformOptions
|
|
57
106
|
resolver?: NodeJS.RequireResolve
|
|
58
|
-
}
|
|
107
|
+
}
|
|
59
108
|
|
|
60
109
|
/**
|
|
61
110
|
* findQueriesInSource takes a source string and returns all GROQ queries in it.
|
|
@@ -72,7 +121,16 @@ export declare function findQueriesInSource(
|
|
|
72
121
|
filename: string,
|
|
73
122
|
babelConfig?: TransformOptions,
|
|
74
123
|
resolver?: NodeJS.RequireResolve,
|
|
75
|
-
):
|
|
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
|
+
}
|
|
76
134
|
|
|
77
135
|
/**
|
|
78
136
|
* This is a custom implementation of require.resolve that takes into account the paths
|
|
@@ -84,38 +142,48 @@ export declare function findQueriesInSource(
|
|
|
84
142
|
export declare function getResolver(cwd?: string): NodeJS.RequireResolve
|
|
85
143
|
|
|
86
144
|
/**
|
|
87
|
-
*
|
|
145
|
+
* An error that occurred during query evaluation.
|
|
146
|
+
* @public
|
|
88
147
|
*/
|
|
89
|
-
declare
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
result: resolveExpressionReturnType
|
|
94
|
-
/** location is the location of the query in the source */
|
|
95
|
-
location: {
|
|
96
|
-
start?: {
|
|
97
|
-
line: number
|
|
98
|
-
column: number
|
|
99
|
-
index: number
|
|
100
|
-
}
|
|
101
|
-
end?: {
|
|
102
|
-
line: number
|
|
103
|
-
column: number
|
|
104
|
-
index: number
|
|
105
|
-
}
|
|
106
|
-
}
|
|
148
|
+
declare class QueryEvaluationError extends Error {
|
|
149
|
+
variable?: QueryVariable
|
|
150
|
+
filename: string
|
|
151
|
+
constructor({variable, cause, filename}: QueryEvaluationErrorOptions)
|
|
107
152
|
}
|
|
108
153
|
|
|
109
|
-
declare
|
|
110
|
-
|
|
111
|
-
|
|
154
|
+
declare interface QueryEvaluationErrorOptions {
|
|
155
|
+
variable?: QueryVariable
|
|
156
|
+
cause: unknown
|
|
157
|
+
filename: string
|
|
158
|
+
}
|
|
159
|
+
|
|
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
|
|
112
180
|
}
|
|
113
181
|
|
|
114
182
|
/**
|
|
115
183
|
* Read, parse and process a config file
|
|
116
184
|
* @internal
|
|
117
185
|
*/
|
|
118
|
-
export declare function readConfig(path: string): Promise<
|
|
186
|
+
export declare function readConfig(path: string): Promise<TypeGenConfig>
|
|
119
187
|
|
|
120
188
|
/**
|
|
121
189
|
* Read a schema from a given path
|
|
@@ -134,20 +202,6 @@ export declare function readSchema(path: string): Promise<SchemaType>
|
|
|
134
202
|
*/
|
|
135
203
|
export declare function registerBabel(babelOptions?: TransformOptions): void
|
|
136
204
|
|
|
137
|
-
declare type resolveExpressionReturnType = string
|
|
138
|
-
|
|
139
|
-
declare type ResultError = {
|
|
140
|
-
type: 'error'
|
|
141
|
-
error: Error
|
|
142
|
-
filename: string
|
|
143
|
-
}
|
|
144
|
-
|
|
145
|
-
declare type ResultQueries = {
|
|
146
|
-
type: 'queries'
|
|
147
|
-
filename: string
|
|
148
|
-
queries: NamedQueryResult[]
|
|
149
|
-
}
|
|
150
|
-
|
|
151
205
|
/**
|
|
152
206
|
* safeParseQuery parses a GROQ query string, but first attempts to extract any parameters used in slices. This method is _only_
|
|
153
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.
|
|
@@ -155,62 +209,62 @@ declare type ResultQueries = {
|
|
|
155
209
|
*/
|
|
156
210
|
export declare function safeParseQuery(query: string): ExprNode
|
|
157
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
|
+
|
|
158
222
|
export declare type TypeGenConfig = z.infer<typeof configDefinition>
|
|
159
223
|
|
|
160
224
|
/**
|
|
161
225
|
* A class used to generate TypeScript types from a given schema
|
|
162
|
-
* @internal
|
|
163
226
|
* @beta
|
|
164
227
|
*/
|
|
165
228
|
export declare class TypeGenerator {
|
|
166
|
-
private
|
|
167
|
-
private
|
|
168
|
-
private
|
|
169
|
-
private
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
*/
|
|
177
|
-
generateSchemaTypes(): string
|
|
178
|
-
/**
|
|
179
|
-
* Takes a identifier and a type node and generates a type alias for the type node.
|
|
180
|
-
* @param identifierName - The name of the type to generated
|
|
181
|
-
* @param typeNode - The type node to generate the type for
|
|
182
|
-
* @returns
|
|
183
|
-
* @internal
|
|
184
|
-
* @beta
|
|
185
|
-
*/
|
|
186
|
-
generateTypeNodeTypes(identifierName: string, typeNode: TypeNode): string
|
|
187
|
-
static generateKnownTypes(): string
|
|
188
|
-
/**
|
|
189
|
-
* Takes a list of queries from the codebase and generates a type declaration
|
|
190
|
-
* for SanityClient to consume.
|
|
191
|
-
*
|
|
192
|
-
* Note: only types that have previously been generated with `generateTypeNodeTypes`
|
|
193
|
-
* will be included in the query map.
|
|
194
|
-
*
|
|
195
|
-
* @param queries - A list of queries to generate a type declaration for
|
|
196
|
-
* @returns
|
|
197
|
-
* @internal
|
|
198
|
-
* @beta
|
|
199
|
-
*/
|
|
200
|
-
generateQueryMap(queries: QueryWithTypeNode[]): string
|
|
201
|
-
/**
|
|
202
|
-
* Since we are sanitizing identifiers we migt end up with collisions. Ie there might be a type mux.video and muxVideo, both these
|
|
203
|
-
* types would be sanityized into MuxVideo. To avoid this we keep track of the generated type names and add a index to the name.
|
|
204
|
-
* When we reference a type we also keep track of the original name so we can reference the correct type later.
|
|
205
|
-
*/
|
|
206
|
-
private getTypeName
|
|
207
|
-
private getTypeNodeType
|
|
208
|
-
private generateArrayTsType
|
|
209
|
-
private generateObjectProperty
|
|
210
|
-
private generateObjectTsType
|
|
211
|
-
private generateInlineTsType
|
|
212
|
-
private generateUnionTsType
|
|
213
|
-
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
|
+
}>
|
|
214
239
|
}
|
|
215
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
|
+
|
|
216
270
|
export {}
|