@sanity/codegen 3.88.1-typegen-experimental.0 → 3.88.2
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 +85 -282
- package/lib/index.d.ts +85 -282
- package/lib/index.js +172 -537
- package/lib/index.js.map +1 -1
- package/lib/index.mjs +173 -538
- package/lib/index.mjs.map +1 -1
- package/package.json +17 -15
- package/src/_exports/index.ts +3 -14
- package/src/readConfig.ts +23 -71
- package/src/readSchema.ts +1 -0
- package/src/typescript/expressionResolvers.ts +3 -4
- package/src/typescript/findQueriesInPath.ts +2 -9
- package/src/typescript/findQueriesInSource.ts +6 -22
- package/src/typescript/typeGenerator.ts +306 -445
- package/src/typescript/asyncQueue.ts +0 -100
- package/src/typescript/schemaTypeGenerator.ts +0 -381
|
@@ -11,10 +11,6 @@ import {getResolver} from './moduleResolver'
|
|
|
11
11
|
|
|
12
12
|
const debug = createDebug('sanity:codegen:findQueries:debug')
|
|
13
13
|
|
|
14
|
-
type ResultFiles = {
|
|
15
|
-
type: 'files'
|
|
16
|
-
fileCount: number
|
|
17
|
-
}
|
|
18
14
|
type ResultQueries = {
|
|
19
15
|
type: 'queries'
|
|
20
16
|
filename: string
|
|
@@ -26,14 +22,13 @@ type ResultError = {
|
|
|
26
22
|
filename: string
|
|
27
23
|
}
|
|
28
24
|
|
|
29
|
-
export type QueryExtractionResult = ResultFiles | ResultQueries | ResultError
|
|
30
|
-
|
|
31
25
|
/**
|
|
32
26
|
* findQueriesInPath takes a path or array of paths and returns all GROQ queries in the files.
|
|
33
27
|
* @param path - The path or array of paths to search for queries
|
|
34
28
|
* @param babelOptions - The babel configuration to use when parsing the source
|
|
35
29
|
* @param resolver - A resolver function to use when resolving module imports
|
|
36
30
|
* @returns An async generator that yields the results of the search
|
|
31
|
+
* @beta
|
|
37
32
|
* @internal
|
|
38
33
|
*/
|
|
39
34
|
export async function* findQueriesInPath({
|
|
@@ -44,7 +39,7 @@ export async function* findQueriesInPath({
|
|
|
44
39
|
path: string | string[]
|
|
45
40
|
babelOptions?: TransformOptions
|
|
46
41
|
resolver?: NodeJS.RequireResolve
|
|
47
|
-
}): AsyncGenerator<
|
|
42
|
+
}): AsyncGenerator<ResultQueries | ResultError> {
|
|
48
43
|
const queryNames = new Set()
|
|
49
44
|
// Holds all query names found in the source files
|
|
50
45
|
debug(`Globing ${path}`)
|
|
@@ -57,8 +52,6 @@ export async function* findQueriesInPath({
|
|
|
57
52
|
})
|
|
58
53
|
.sort()
|
|
59
54
|
|
|
60
|
-
yield {type: 'files', fileCount: files.length}
|
|
61
|
-
|
|
62
55
|
for (const filename of files) {
|
|
63
56
|
if (typeof filename !== 'string') {
|
|
64
57
|
continue
|
|
@@ -12,7 +12,6 @@ const require = createRequire(__filename)
|
|
|
12
12
|
|
|
13
13
|
const groqTagName = 'groq'
|
|
14
14
|
const defineQueryFunctionName = 'defineQuery'
|
|
15
|
-
const defineProjectionFunctionName = 'defineProjection'
|
|
16
15
|
const groqModuleName = 'groq'
|
|
17
16
|
const nextSanityModuleName = 'next-sanity'
|
|
18
17
|
|
|
@@ -25,6 +24,7 @@ const ignoreValue = '@sanity-typegen-ignore'
|
|
|
25
24
|
* @param babelConfig - The babel configuration to use when parsing the source
|
|
26
25
|
* @param resolver - A resolver function to use when resolving module imports
|
|
27
26
|
* @returns
|
|
27
|
+
* @beta
|
|
28
28
|
* @internal
|
|
29
29
|
*/
|
|
30
30
|
export function findQueriesInSource(
|
|
@@ -50,14 +50,13 @@ export function findQueriesInSource(
|
|
|
50
50
|
babelTypes.isIdentifier(init.tag) &&
|
|
51
51
|
init.tag.name === groqTagName
|
|
52
52
|
|
|
53
|
-
// Look for strings wrapped in a defineQuery
|
|
54
|
-
const
|
|
53
|
+
// Look for strings wrapped in a defineQuery function call
|
|
54
|
+
const isDefineQueryCall =
|
|
55
55
|
babelTypes.isCallExpression(init) &&
|
|
56
56
|
(isImportFrom(groqModuleName, defineQueryFunctionName, scope, init.callee) ||
|
|
57
|
-
isImportFrom(groqModuleName, defineProjectionFunctionName, scope, init.callee) ||
|
|
58
57
|
isImportFrom(nextSanityModuleName, defineQueryFunctionName, scope, init.callee))
|
|
59
58
|
|
|
60
|
-
if (babelTypes.isIdentifier(node.id) && (isGroqTemplateTag ||
|
|
59
|
+
if (babelTypes.isIdentifier(node.id) && (isGroqTemplateTag || isDefineQueryCall)) {
|
|
61
60
|
// If we find a comment leading the decleration which macthes with ignoreValue we don't add
|
|
62
61
|
// the query
|
|
63
62
|
if (declarationLeadingCommentContains(path, ignoreValue)) {
|
|
@@ -65,23 +64,8 @@ export function findQueriesInSource(
|
|
|
65
64
|
}
|
|
66
65
|
|
|
67
66
|
const queryName = `${node.id.name}`
|
|
68
|
-
|
|
69
|
-
let resultType: 'query' | 'projection' = 'query'
|
|
70
|
-
let expressionToResolve: babelTypes.Expression = init
|
|
71
|
-
|
|
72
|
-
if (isDefineCall) {
|
|
73
|
-
// If it's a define call, resolve the first argument instead of the whole call
|
|
74
|
-
if (init.arguments.length > 0 && babelTypes.isExpression(init.arguments[0])) {
|
|
75
|
-
expressionToResolve = init.arguments[0]
|
|
76
|
-
}
|
|
77
|
-
// Check specifically if it's a defineProjection call from groq
|
|
78
|
-
if (isImportFrom(groqModuleName, defineProjectionFunctionName, scope, init.callee)) {
|
|
79
|
-
resultType = 'projection'
|
|
80
|
-
}
|
|
81
|
-
}
|
|
82
|
-
|
|
83
67
|
const queryResult = resolveExpression({
|
|
84
|
-
node:
|
|
68
|
+
node: init,
|
|
85
69
|
file,
|
|
86
70
|
scope,
|
|
87
71
|
babelConfig,
|
|
@@ -100,7 +84,7 @@ export function findQueriesInSource(
|
|
|
100
84
|
}
|
|
101
85
|
: {}
|
|
102
86
|
|
|
103
|
-
queries.push({name: queryName, result: queryResult,
|
|
87
|
+
queries.push({name: queryName, result: queryResult, location})
|
|
104
88
|
}
|
|
105
89
|
},
|
|
106
90
|
})
|