@sanity/codegen 3.86.1 → 3.86.2-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 +79 -91
- package/lib/index.d.ts +79 -91
- package/lib/index.js +492 -151
- package/lib/index.js.map +1 -1
- package/lib/index.mjs +493 -152
- package/lib/index.mjs.map +1 -1
- package/package.json +15 -17
- package/src/_exports/index.ts +16 -3
- package/src/readConfig.ts +77 -19
- package/src/typescript/asyncQueue.ts +100 -0
- package/src/typescript/expressionResolvers.ts +4 -3
- package/src/typescript/findQueriesInPath.ts +2 -2
- package/src/typescript/findQueriesInSource.ts +25 -5
- package/src/typescript/generateTypes.ts +425 -0
- package/src/typescript/schemaTypeGenerator.ts +415 -0
- package/src/typescript/typeGenerator.ts +0 -374
package/lib/index.d.mts
CHANGED
|
@@ -1,36 +1,6 @@
|
|
|
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
|
-
>
|
|
34
4
|
|
|
35
5
|
/**
|
|
36
6
|
* findQueriesInPath takes a path or array of paths and returns all GROQ queries in the files.
|
|
@@ -68,6 +38,54 @@ export declare function findQueriesInSource(
|
|
|
68
38
|
resolver?: NodeJS.RequireResolve,
|
|
69
39
|
): NamedQueryResult[]
|
|
70
40
|
|
|
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
|
+
|
|
71
89
|
/**
|
|
72
90
|
* This is a custom implementation of require.resolve that takes into account the paths
|
|
73
91
|
* configuration in tsconfig.json. This is necessary if we want to resolve paths that are
|
|
@@ -85,8 +103,10 @@ declare interface NamedQueryResult {
|
|
|
85
103
|
name: string
|
|
86
104
|
/** result is a groq query */
|
|
87
105
|
result: resolveExpressionReturnType
|
|
106
|
+
/** type indicates whether this is a query or projection */
|
|
107
|
+
type: 'query' | 'projection'
|
|
88
108
|
/** location is the location of the query in the source */
|
|
89
|
-
location
|
|
109
|
+
location?: {
|
|
90
110
|
start?: {
|
|
91
111
|
line: number
|
|
92
112
|
column: number
|
|
@@ -100,12 +120,23 @@ declare interface NamedQueryResult {
|
|
|
100
120
|
}
|
|
101
121
|
}
|
|
102
122
|
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
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
|
+
}>
|
|
106
137
|
}
|
|
107
138
|
|
|
108
|
-
export declare function readConfig(path: string): Promise<
|
|
139
|
+
export declare function readConfig(path: string): Promise<NormalizedTypegenConfig>
|
|
109
140
|
|
|
110
141
|
/**
|
|
111
142
|
* Read a schema from a given path
|
|
@@ -126,13 +157,13 @@ export declare function registerBabel(babelOptions?: TransformOptions): void
|
|
|
126
157
|
|
|
127
158
|
declare type resolveExpressionReturnType = string
|
|
128
159
|
|
|
129
|
-
declare type ResultError = {
|
|
160
|
+
export declare type ResultError = {
|
|
130
161
|
type: 'error'
|
|
131
162
|
error: Error
|
|
132
163
|
filename: string
|
|
133
164
|
}
|
|
134
165
|
|
|
135
|
-
declare type ResultQueries = {
|
|
166
|
+
export declare type ResultQueries = {
|
|
136
167
|
type: 'queries'
|
|
137
168
|
filename: string
|
|
138
169
|
queries: NamedQueryResult[]
|
|
@@ -145,60 +176,17 @@ declare type ResultQueries = {
|
|
|
145
176
|
*/
|
|
146
177
|
export declare function safeParseQuery(query: string): ExprNode
|
|
147
178
|
|
|
148
|
-
/**
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
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
|
|
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>
|
|
202
190
|
}
|
|
203
191
|
|
|
204
192
|
export {}
|
package/lib/index.d.ts
CHANGED
|
@@ -1,36 +1,6 @@
|
|
|
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
|
-
>
|
|
34
4
|
|
|
35
5
|
/**
|
|
36
6
|
* findQueriesInPath takes a path or array of paths and returns all GROQ queries in the files.
|
|
@@ -68,6 +38,54 @@ export declare function findQueriesInSource(
|
|
|
68
38
|
resolver?: NodeJS.RequireResolve,
|
|
69
39
|
): NamedQueryResult[]
|
|
70
40
|
|
|
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
|
+
|
|
71
89
|
/**
|
|
72
90
|
* This is a custom implementation of require.resolve that takes into account the paths
|
|
73
91
|
* configuration in tsconfig.json. This is necessary if we want to resolve paths that are
|
|
@@ -85,8 +103,10 @@ declare interface NamedQueryResult {
|
|
|
85
103
|
name: string
|
|
86
104
|
/** result is a groq query */
|
|
87
105
|
result: resolveExpressionReturnType
|
|
106
|
+
/** type indicates whether this is a query or projection */
|
|
107
|
+
type: 'query' | 'projection'
|
|
88
108
|
/** location is the location of the query in the source */
|
|
89
|
-
location
|
|
109
|
+
location?: {
|
|
90
110
|
start?: {
|
|
91
111
|
line: number
|
|
92
112
|
column: number
|
|
@@ -100,12 +120,23 @@ declare interface NamedQueryResult {
|
|
|
100
120
|
}
|
|
101
121
|
}
|
|
102
122
|
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
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
|
+
}>
|
|
106
137
|
}
|
|
107
138
|
|
|
108
|
-
export declare function readConfig(path: string): Promise<
|
|
139
|
+
export declare function readConfig(path: string): Promise<NormalizedTypegenConfig>
|
|
109
140
|
|
|
110
141
|
/**
|
|
111
142
|
* Read a schema from a given path
|
|
@@ -126,13 +157,13 @@ export declare function registerBabel(babelOptions?: TransformOptions): void
|
|
|
126
157
|
|
|
127
158
|
declare type resolveExpressionReturnType = string
|
|
128
159
|
|
|
129
|
-
declare type ResultError = {
|
|
160
|
+
export declare type ResultError = {
|
|
130
161
|
type: 'error'
|
|
131
162
|
error: Error
|
|
132
163
|
filename: string
|
|
133
164
|
}
|
|
134
165
|
|
|
135
|
-
declare type ResultQueries = {
|
|
166
|
+
export declare type ResultQueries = {
|
|
136
167
|
type: 'queries'
|
|
137
168
|
filename: string
|
|
138
169
|
queries: NamedQueryResult[]
|
|
@@ -145,60 +176,17 @@ declare type ResultQueries = {
|
|
|
145
176
|
*/
|
|
146
177
|
export declare function safeParseQuery(query: string): ExprNode
|
|
147
178
|
|
|
148
|
-
/**
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
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
|
|
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>
|
|
202
190
|
}
|
|
203
191
|
|
|
204
192
|
export {}
|