Skip to content

Commit e417e58

Browse files
authored
Merge pull request #91 from code0-tech/feat/#90
Schema extraction based on type
2 parents 69d59ec + 433991c commit e417e58

4 files changed

Lines changed: 63 additions & 18 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: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
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 expression string.
8+
*
9+
* This function creates a virtual TypeScript environment, wraps the provided type expression
10+
* into a type alias 'T', and extracts the schema for it.
11+
*
12+
* @param typeString - The TypeScript type expression (e.g., "string | number" or "{ a: string }").
13+
* @param dataTypes - An optional array of additional data type definitions to be included in the environment.
14+
* @returns The generated Schema for the identified type, or undefined if no valid type was found or an error occurred.
15+
*/
16+
export const getTypeSchema = (
17+
typeString: string,
18+
dataTypes: DataType[] = [],
19+
): Schema | undefined => {
20+
const fileName = "index.ts"
21+
const typeDefs = getSharedTypeDeclarations(dataTypes)
22+
const sourceCode = `${typeDefs}\ntype MyType = ${typeString}`
23+
24+
const host = createCompilerHost(fileName, sourceCode)
25+
const program = host.languageService.getProgram()
26+
const sourceFile = program?.getSourceFile(fileName)
27+
const checker = program?.getTypeChecker()
28+
29+
if (!sourceFile || !checker) return undefined
30+
31+
const targetStatement = sourceFile.statements
32+
.filter((stmt): stmt is ts.TypeAliasDeclaration | ts.InterfaceDeclaration | ts.ClassDeclaration =>
33+
ts.isTypeAliasDeclaration(stmt) || ts.isInterfaceDeclaration(stmt) || ts.isClassDeclaration(stmt)
34+
).pop()
35+
if (!targetStatement) return undefined
36+
37+
const targetType = checker.getTypeAtLocation(targetStatement)
38+
if (!targetType) return undefined
39+
40+
return getSchema(checker, undefined, targetType, [], [], false)
41+
}

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

test/schema/schema.test.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import {describe, expect, it} from "vitest";
22
import {Flow} from "@code0-tech/sagittarius-graphql-types";
3-
import {getFlowValidation, getSignatureSchema} from "../../src";
3+
import {getFlowValidation, getSignatureSchema, getTypeSchema} from "../../src";
44
import {DATA_TYPES, FUNCTION_SIGNATURES} from "../data";
55

66
describe("Schema", () => {
@@ -116,4 +116,10 @@ describe("Schema", () => {
116116

117117
});
118118

119+
it('3', () => {
120+
const result = getTypeSchema("{text: NUMBER, bla?: LIST<TEXT>}", DATA_TYPES);
121+
122+
//console.dir(result, {depth: null})
123+
});
124+
119125
})

0 commit comments

Comments
 (0)