Skip to content

Commit 433991c

Browse files
committed
feat: enhance getTypeSchema to handle TypeScript type expressions and update documentation
1 parent 775043c commit 433991c

2 files changed

Lines changed: 12 additions & 7 deletions

File tree

src/schema/getTypeSchema.ts

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,12 @@ import {getSchema, Schema} from "../util/schema.util"
44
import {createCompilerHost, getSharedTypeDeclarations} from "../utils"
55

66
/**
7-
* Generates a schema for a given TypeScript type string.
7+
* Generates a schema for a given TypeScript type expression string.
88
*
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.
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.
1211
*
13-
* @param typeString - The TypeScript code containing the type definition (e.g., "type MyType = { a: string }").
12+
* @param typeString - The TypeScript type expression (e.g., "string | number" or "{ a: string }").
1413
* @param dataTypes - An optional array of additional data type definitions to be included in the environment.
1514
* @returns The generated Schema for the identified type, or undefined if no valid type was found or an error occurred.
1615
*/
@@ -20,7 +19,7 @@ export const getTypeSchema = (
2019
): Schema | undefined => {
2120
const fileName = "index.ts"
2221
const typeDefs = getSharedTypeDeclarations(dataTypes)
23-
const sourceCode = `${typeDefs}\n${typeString}`
22+
const sourceCode = `${typeDefs}\ntype MyType = ${typeString}`
2423

2524
const host = createCompilerHost(fileName, sourceCode)
2625
const program = host.languageService.getProgram()

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)