Skip to content

Commit 775043c

Browse files
committed
feat: implement getTypeSchema function to generate schema from TypeScript type strings
1 parent 69d59ec commit 775043c

3 files changed

Lines changed: 57 additions & 17 deletions

File tree

src/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,3 +3,4 @@ export * from './extraction/getValueFromType';
33
export * from './validation/getFlowValidation';
44
export * from './validation/getValueValidation';
55
export * from './schema/getSignatureSchema';
6+
export * from './schema/getTypeSchema';

src/schema/getTypeSchema.ts

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
import {DataType} from "@code0-tech/sagittarius-graphql-types"
2+
import ts from "typescript"
3+
import {getSchema, Schema} from "../util/schema.util"
4+
import {createCompilerHost, getSharedTypeDeclarations} from "../utils"
5+
6+
/**
7+
* Generates a schema for a given TypeScript type string.
8+
*
9+
* This function creates a virtual TypeScript environment, parses the provided type string
10+
* (along with any provided data type definitions), and extracts the schema for the last
11+
* type-related statement (TypeAlias, Interface, or Class) found in the string.
12+
*
13+
* @param typeString - The TypeScript code containing the type definition (e.g., "type MyType = { a: string }").
14+
* @param dataTypes - An optional array of additional data type definitions to be included in the environment.
15+
* @returns The generated Schema for the identified type, or undefined if no valid type was found or an error occurred.
16+
*/
17+
export const getTypeSchema = (
18+
typeString: string,
19+
dataTypes: DataType[] = [],
20+
): Schema | undefined => {
21+
const fileName = "index.ts"
22+
const typeDefs = getSharedTypeDeclarations(dataTypes)
23+
const sourceCode = `${typeDefs}\n${typeString}`
24+
25+
const host = createCompilerHost(fileName, sourceCode)
26+
const program = host.languageService.getProgram()
27+
const sourceFile = program?.getSourceFile(fileName)
28+
const checker = program?.getTypeChecker()
29+
30+
if (!sourceFile || !checker) return undefined
31+
32+
const targetStatement = sourceFile.statements
33+
.filter((stmt): stmt is ts.TypeAliasDeclaration | ts.InterfaceDeclaration | ts.ClassDeclaration =>
34+
ts.isTypeAliasDeclaration(stmt) || ts.isInterfaceDeclaration(stmt) || ts.isClassDeclaration(stmt)
35+
).pop()
36+
if (!targetStatement) return undefined
37+
38+
const targetType = checker.getTypeAtLocation(targetStatement)
39+
if (!targetType) return undefined
40+
41+
return getSchema(checker, undefined, targetType, [], [], false)
42+
}

src/util/schema.util.ts

Lines changed: 14 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -113,31 +113,28 @@ export type Schema =
113113
*/
114114
export const getSchema = (
115115
checker: ts.TypeChecker,
116-
node: ts.VariableDeclaration,
116+
node: ts.VariableDeclaration | undefined,
117117
parameterType: ts.Type,
118118
functionDeclarations: FunctionDeclaration[],
119119
functions: FunctionDefinition[],
120120
suggestions: boolean = true
121121
): Schema => {
122122
// Collect all available suggestions for this parameter
123-
const literalValueSuggestions = getValues(parameterType);
124-
const referenceSuggestions = getReferences(
125-
checker,
126-
node!,
127-
parameterType,
128-
checker.getSymbolsInScope(node!, ts.SymbolFlags.Variable)
129-
);
130-
const nodeSuggestions = getNodes(
131-
checker,
132-
functionDeclarations,
133-
functions,
134-
parameterType
135-
);
136123
const combinedSuggestions = suggestions ? {
137124
suggestions: [
138-
...literalValueSuggestions,
139-
...referenceSuggestions,
140-
...nodeSuggestions,
125+
...getValues(parameterType),
126+
...(node ? getReferences(
127+
checker,
128+
node,
129+
parameterType,
130+
checker.getSymbolsInScope(node, ts.SymbolFlags.Variable)
131+
) : []),
132+
...getNodes(
133+
checker,
134+
functionDeclarations,
135+
functions,
136+
parameterType
137+
),
141138
],
142139
} : {};
143140

0 commit comments

Comments
 (0)