-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathschema.util.ts
More file actions
409 lines (369 loc) · 13.6 KB
/
Copy pathschema.util.ts
File metadata and controls
409 lines (369 loc) · 13.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
import ts, {FunctionDeclaration} from "typescript";
import {getValues} from "./values.util";
import {getReferences} from "./references.util";
import {getNodes} from "./nodes.util";
import {
FunctionDefinition,
LiteralValue,
NodeFunction,
ReferenceValue,
SubFlowValue,
} from "@code0-tech/sagittarius-graphql-types";
import {getSubFlows} from "./subflows.util";
/**
* Base interface for all input types.
* Provides common properties for suggestions and input metadata.
*/
export interface Input {
/** The type of input (string representation) */
input?: string;
/** Array of suggested values (functions, references, or literals) */
suggestions?: (NodeFunction | ReferenceValue | LiteralValue | SubFlowValue)[];
}
/**
* Represents a generic input type with no specific structure.
* Used as a fallback when the type cannot be determined.
*/
export interface GenericInput extends Input {
input?: "generic";
}
/**
* Represents a sub-flow input type (callable/function type).
* Used for types that have call signatures.
*/
export interface SubFlowInput extends Input {
input?: "sub-flow";
}
/**
* Represents primitive input types: boolean, number, text, or select.
* Extends the base Input interface to include suggestions.
*/
export interface PrimitiveInput extends Input {
input?: "boolean" | "number" | "text" | "select";
}
/**
* Represents a data object input type with structured properties.
* Includes property definitions and required field tracking.
*/
export interface DataInput extends Input {
input?: "data";
/** Record mapping property names to their schemas */
properties?: Record<string, Schema | Schema[]>;
/** Array of required property names */
required?: string[];
}
/**
* Represents a list/array input type with item schemas.
* Supports homogeneous or heterogeneous arrays.
*/
export interface ListInput extends Input {
input?: "list";
/** Schema or array of schemas for list items */
items?: Schema[];
}
/**
* Represents a complex type input with properties and required fields.
* Similar to DataInput but used for type definitions.
*/
export interface TypeInput extends Input {
input?: "type";
/** Record mapping property names to their schemas */
properties?: Record<string, Schema | Schema[]>;
/** Array of required property names */
required?: string[];
}
/**
* Union type representing all possible schema input types.
* Discriminated union based on the 'input' field.
*/
export type Schema =
| PrimitiveInput
| DataInput
| ListInput
| TypeInput
| SubFlowInput
| GenericInput;
/**
* Generates a schema definition for a given TypeScript type.
*
* This function analyzes a TypeScript type and produces a structured schema
* that describes how the type should be presented and validated. It handles:
* - Primitive types (boolean, number, string)
* - Union types of primitives
* - Array/Tuple types
* - Complex object types with properties
* - Sub-flow types (callables)
*
* For each type, the function also collects suggestions from:
* - Literal values from the type definition
* - Variable references available in scope
* - Function node suggestions based on parameter type
*
* @param checker - TypeScript type checker for type analysis
* @param node - The variable declaration node being analyzed
* @param parameterType - The type to generate a schema for
* @param functionDeclarations - Array of function declaration nodes
* @param functions - Array of function definitions for matching
* @param suggestions
* @returns A Schema object describing how to handle the parameter type
*/
export const getSchema = (
checker: ts.TypeChecker,
node: ts.VariableDeclaration | undefined,
parameterType: ts.Type,
functionDeclarations: FunctionDeclaration[],
functions: FunctionDefinition[],
suggestions: boolean = true
): Schema => {
if ((parameterType.flags & ts.TypeFlags.TypeParameter) !== 0) {
const decl = parameterType.symbol?.declarations?.[0]
if (decl && ts.isTypeParameterDeclaration(decl) && decl.constraint) {
// getTypeFromTypeNode statt getBaseConstraintOfType → aliasSymbol bleibt erhalten
const constraintType = checker.getTypeFromTypeNode(decl.constraint)
return getSchema(checker, node, constraintType, functionDeclarations, functions, suggestions)
}
}
// Collect all available suggestions for this parameter
const combinedSuggestions = suggestions ? {
suggestions: [
...getValues(parameterType, checker),
...(node ? getReferences(
checker,
node,
parameterType,
checker.getSymbolsInScope(node, ts.SymbolFlags.Variable)
) : []),
...getNodes(
checker,
functionDeclarations,
functions,
parameterType
),
...getSubFlows(
checker,
functionDeclarations,
functions,
parameterType
),
],
} : {};
// Strip undefined and null from unions (e.g. string | undefined | null → string).
// Suggestions are collected above from the original type (preserving aliasSymbol literals),
// the base schema is determined from the stripped type, then both are merged.
if (parameterType.isUnion()) {
const nonNullish = parameterType.types.filter(
(t) => (t.flags & (ts.TypeFlags.Undefined | ts.TypeFlags.Null)) === 0
)
if (nonNullish.length === 1) {
const baseSchema = getSchema(checker, node, nonNullish[0], functionDeclarations, functions, false)
return {...baseSchema, ...combinedSuggestions}
}
}
// Boolean is internally represented by TypeScript as the union `true | false`,
// so it must be detected before the primitive-literal-union check below; otherwise
// `boolean` (and `true | false`) would incorrectly surface as a select.
if (isBoolean(parameterType)) {
return {input: "boolean", ...combinedSuggestions};
}
// Check primitive literal union first (e.g., "a" | "b" | "c") or a single
// string/number literal (e.g., "GET"). A bare literal has only one allowed
// value, so it should still surface as a select rather than a free-form text/number input.
if (isPrimitiveLiteralUnion(parameterType) || isStringOrNumberLiteral(parameterType)) {
return {input: "select", ...combinedSuggestions};
}
if (isNumber(parameterType)) {
return {input: "number", ...combinedSuggestions};
}
if (isString(parameterType)) {
return {input: "text", ...combinedSuggestions};
}
// Check if type has call signatures (is callable/sub-flow)
if (isSubFlow(parameterType)) {
return {input: "sub-flow", ...combinedSuggestions};
}
// Handle array and tuple types
if (isArrayType(checker, parameterType)) {
const itemTypes = checker.getTypeArguments(
parameterType as ts.TypeReference
);
const itemSchemas = itemTypes.flatMap(itemType => {
const itemTypes = itemType.isUnion() ? itemType.types : [itemType];
return itemTypes.map((itemType) =>
getSchema(checker, node, itemType, functionDeclarations, functions, suggestions)
)
})
return {
input: "list",
items: itemSchemas,
...combinedSuggestions,
};
}
// Handle complex object types with properties
if ((parameterType.flags & ts.TypeFlags.Object) !== 0) {
const properties: Record<string, Schema | Schema[]> = {};
const required: string[] = [];
// Iterate through all properties of the object type
for (const property of checker.getPropertiesOfType(parameterType)) {
const declaration =
property.valueDeclaration ?? property.declarations?.[0];
if (!declaration) continue;
const propertyType = checker.getTypeOfSymbolAtLocation(
property,
declaration
);
// Determine if the property is optional
const isOptional =
(property.flags & ts.SymbolFlags.Optional) !== 0 ||
(propertyType.isUnion() &&
propertyType.types.some(
(t) => (t.flags & (ts.TypeFlags.Undefined | ts.TypeFlags.Null)) !== 0
));
// Filter out undefined and null types from union types
const propertyTypes = propertyType.isUnion()
? propertyType.types.filter(
(t) => (t.flags & (ts.TypeFlags.Undefined | ts.TypeFlags.Null)) === 0
)
: [propertyType];
// Recursively generate schemas for property types
const propertySchemas = propertyTypes.map((type) =>
getSchema(checker, node, type, functionDeclarations, functions, suggestions)
);
properties[property.name] =
propertySchemas.length === 1 ? propertySchemas[0] : propertySchemas;
// Track required properties
if (!isOptional) {
required.push(property.name);
}
}
return {
input: "data",
properties,
required,
...combinedSuggestions,
};
}
// Fallback for unknown or generic types
return {
input: "generic",
};
};
/**
* Checks if a type is a boolean type (either boolean or boolean literal).
*
* This function checks for both the general boolean type and specific boolean
* literal types (true, false).
*
* @param type - The type to check
* @returns True if the type is a boolean or boolean literal, false otherwise
*/
function isBoolean(type: ts.Type): boolean {
if (
(type.flags & ts.TypeFlags.Boolean) !== 0 ||
(type.flags & ts.TypeFlags.BooleanLiteral) !== 0
) {
return true;
}
// A union whose only non-nullish members are boolean literals (e.g. `true | false`,
// or `boolean | undefined` after TS expands boolean to its constituents) is still a boolean.
if (type.isUnion()) {
const nonNullish = type.types.filter(
(t) => (t.flags & (ts.TypeFlags.Undefined | ts.TypeFlags.Null)) === 0
);
return (
nonNullish.length > 0 &&
nonNullish.every(
(t) =>
(t.flags & ts.TypeFlags.Boolean) !== 0 ||
(t.flags & ts.TypeFlags.BooleanLiteral) !== 0
)
);
}
return false;
}
/**
* Checks if a type is a number type (either number or number literal).
*
* This function checks for both the general number type and specific numeric
* literal types (42, 3.14, etc.).
*
* @param type - The type to check
* @returns True if the type is a number or number literal, false otherwise
*/
function isNumber(type: ts.Type): boolean {
return (
(type.flags & ts.TypeFlags.Number) !== 0 ||
(type.flags & ts.TypeFlags.NumberLiteral) !== 0
);
}
/**
* Checks if a type is a string type (either string or string literal).
*
* This function checks for both the general string type and specific string
* literal types ("hello", "world", etc.).
*
* @param type - The type to check
* @returns True if the type is a string or string literal, false otherwise
*/
function isString(type: ts.Type): boolean {
return (
(type.flags & ts.TypeFlags.String) !== 0 ||
(type.flags & ts.TypeFlags.StringLiteral) !== 0
);
}
/**
* Checks if a type is any primitive type (string, number, or boolean).
*
* @param type - The type to check
* @returns True if the type is a string, number, or boolean, false otherwise
*/
function isPrimitive(type: ts.Type): boolean {
return isString(type) || isNumber(type) || isBoolean(type);
}
/**
* Checks if a type is a single string or number literal (e.g. "GET" or 42).
* Boolean literals are excluded so that types like `true` continue to render as a boolean input.
*/
function isStringOrNumberLiteral(type: ts.Type): boolean {
return (
(type.flags & ts.TypeFlags.StringLiteral) !== 0 ||
(type.flags & ts.TypeFlags.NumberLiteral) !== 0
);
}
/**
* Checks if a type is a union of primitive types only.
*
* This function is used to identify union types that can be represented as
* a select input with predefined options (e.g., "a" | "b" | "c" or 1 | 2 | 3).
*
* @param type - The type to check
* @returns True if the type is a union where all members are primitives, false otherwise
*/
function isPrimitiveLiteralUnion(type: ts.Type): boolean {
if (!type.isUnion()) return false;
const nonNullish = type.types.filter(
(t) => (t.flags & (ts.TypeFlags.Undefined | ts.TypeFlags.Null)) === 0
);
return nonNullish.length > 0 && nonNullish.every(isPrimitive);
}
/**
* Checks if a type is an array or tuple type.
*
* @param checker - TypeScript type checker for type analysis
* @param type - The type to check
* @returns True if the type is an array or tuple, false otherwise
*/
function isArrayType(checker: ts.TypeChecker, type: ts.Type): boolean {
return checker.isArrayType(type) || checker.isTupleType(type);
}
/**
* Checks if a type is a callable type (has call signatures).
*
* A type with call signatures can be invoked like a function. This is used
* to identify sub-flow types that represent workflow steps.
*
* @param type - The type to check
* @returns True if the type has call signatures, false otherwise
*/
export function isSubFlow(type: ts.Type): boolean {
return type.getCallSignatures().length > 0;
}