ts-class-to-openapi 1.0.3 → 1.0.4
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/README.md +238 -21
- package/dist/__test__/entities/complex-generics.entity.d.ts +33 -0
- package/dist/__test__/entities/comprehensive-enum.entity.d.ts +23 -0
- package/dist/__test__/entities/enum.entity.d.ts +29 -0
- package/dist/__test__/entities/user-role-generic.entity.d.ts +13 -0
- package/dist/__test__/enum.test.d.ts +1 -0
- package/dist/__test__/generic-types.test.d.ts +1 -0
- package/dist/__test__/index.d.ts +2 -0
- package/dist/index.esm.js +500 -10
- package/dist/index.js +500 -10
- package/dist/run.js +500 -10
- package/dist/transformer.d.ts +137 -0
- package/dist/transformer.fixtures.d.ts +4 -0
- package/package.json +1 -1
package/dist/transformer.d.ts
CHANGED
|
@@ -94,14 +94,26 @@ declare class SchemaTransformer {
|
|
|
94
94
|
private getRelevantSourceFiles;
|
|
95
95
|
/**
|
|
96
96
|
* Transforms a class by its name into an OpenAPI schema object.
|
|
97
|
+
* Now considers the context of the calling file to resolve ambiguous class names.
|
|
97
98
|
*
|
|
98
99
|
* @param className - The name of the class to transform
|
|
99
100
|
* @param filePath - Optional path to the file containing the class
|
|
101
|
+
* @param contextFile - Optional context file for resolving class ambiguity
|
|
100
102
|
* @returns Object containing the class name and its corresponding JSON schema
|
|
101
103
|
* @throws {Error} When the specified class cannot be found
|
|
102
104
|
* @private
|
|
103
105
|
*/
|
|
104
106
|
private transformByName;
|
|
107
|
+
/**
|
|
108
|
+
* Prioritizes source files based on context to resolve class name conflicts.
|
|
109
|
+
* Gives priority to files in the same directory or with similar names.
|
|
110
|
+
*
|
|
111
|
+
* @param sourceFiles - Array of source files to prioritize
|
|
112
|
+
* @param contextFile - Optional context file for prioritization
|
|
113
|
+
* @returns Prioritized array of source files
|
|
114
|
+
* @private
|
|
115
|
+
*/
|
|
116
|
+
private prioritizeSourceFiles;
|
|
105
117
|
/**
|
|
106
118
|
* Gets the singleton instance of SchemaTransformer.
|
|
107
119
|
*
|
|
@@ -195,6 +207,7 @@ declare class SchemaTransformer {
|
|
|
195
207
|
* Transforms a TypeScript class declaration into a schema object.
|
|
196
208
|
*
|
|
197
209
|
* @param classNode - The TypeScript class declaration node
|
|
210
|
+
* @param sourceFile - The source file containing the class (for context)
|
|
198
211
|
* @returns Object containing class name and generated schema
|
|
199
212
|
* @private
|
|
200
213
|
*/
|
|
@@ -215,6 +228,82 @@ declare class SchemaTransformer {
|
|
|
215
228
|
* @private
|
|
216
229
|
*/
|
|
217
230
|
private getPropertyType;
|
|
231
|
+
/**
|
|
232
|
+
* Resolves generic types by analyzing the type alias and its arguments.
|
|
233
|
+
* For example, User<Role> where User is a type alias will be resolved to its structure.
|
|
234
|
+
*
|
|
235
|
+
* @param typeNode - The TypeScript type reference node with generic arguments
|
|
236
|
+
* @returns String representation of the resolved type or schema
|
|
237
|
+
* @private
|
|
238
|
+
*/
|
|
239
|
+
private resolveGenericType;
|
|
240
|
+
/**
|
|
241
|
+
* Checks if a type string represents a resolved generic type.
|
|
242
|
+
*
|
|
243
|
+
* @param type - The type string to check
|
|
244
|
+
* @returns True if it's a resolved generic type
|
|
245
|
+
* @private
|
|
246
|
+
*/
|
|
247
|
+
private isResolvedGenericType;
|
|
248
|
+
/**
|
|
249
|
+
* Checks if a type is a known class or interface.
|
|
250
|
+
*
|
|
251
|
+
* @param typeName - The type name to check
|
|
252
|
+
* @returns True if it's a known type
|
|
253
|
+
* @private
|
|
254
|
+
*/
|
|
255
|
+
private isKnownType;
|
|
256
|
+
/**
|
|
257
|
+
* Finds a class by name in the project without transforming it.
|
|
258
|
+
*
|
|
259
|
+
* @param className - The class name to find
|
|
260
|
+
* @returns True if found, false otherwise
|
|
261
|
+
* @private
|
|
262
|
+
*/
|
|
263
|
+
private findClassInProject;
|
|
264
|
+
/**
|
|
265
|
+
* Checks if a type is a primitive type.
|
|
266
|
+
*
|
|
267
|
+
* @param typeName - The type name to check
|
|
268
|
+
* @returns True if it's a primitive type
|
|
269
|
+
* @private
|
|
270
|
+
*/
|
|
271
|
+
private isPrimitiveType;
|
|
272
|
+
/**
|
|
273
|
+
* Resolves a generic type schema by analyzing the type alias structure.
|
|
274
|
+
*
|
|
275
|
+
* @param resolvedTypeName - The resolved generic type name (e.g., User_Role)
|
|
276
|
+
* @returns OpenAPI schema for the resolved generic type
|
|
277
|
+
* @private
|
|
278
|
+
*/
|
|
279
|
+
private resolveGenericTypeSchema;
|
|
280
|
+
/**
|
|
281
|
+
* Finds a type alias declaration by name.
|
|
282
|
+
*
|
|
283
|
+
* @param typeName - The type alias name to find
|
|
284
|
+
* @returns The type alias declaration node or null
|
|
285
|
+
* @private
|
|
286
|
+
*/
|
|
287
|
+
private findTypeAliasDeclaration;
|
|
288
|
+
/**
|
|
289
|
+
* Creates a schema from a type alias declaration, substituting type parameters.
|
|
290
|
+
*
|
|
291
|
+
* @param typeAlias - The type alias declaration
|
|
292
|
+
* @param typeArgNames - The concrete type arguments
|
|
293
|
+
* @returns OpenAPI schema for the type alias
|
|
294
|
+
* @private
|
|
295
|
+
*/
|
|
296
|
+
private createSchemaFromTypeAlias;
|
|
297
|
+
/**
|
|
298
|
+
* Resolves type parameters in a type alias to concrete types.
|
|
299
|
+
*
|
|
300
|
+
* @param typeNode - The type node to resolve
|
|
301
|
+
* @param typeParameters - The type parameters of the type alias
|
|
302
|
+
* @param typeArgNames - The concrete type arguments
|
|
303
|
+
* @returns The resolved type string
|
|
304
|
+
* @private
|
|
305
|
+
*/
|
|
306
|
+
private resolveTypeParameterInTypeAlias;
|
|
218
307
|
/**
|
|
219
308
|
* Converts a TypeScript type node to its string representation.
|
|
220
309
|
*
|
|
@@ -251,6 +340,7 @@ declare class SchemaTransformer {
|
|
|
251
340
|
* Generates an OpenAPI schema from extracted property information.
|
|
252
341
|
*
|
|
253
342
|
* @param properties - Array of property information to process
|
|
343
|
+
* @param contextFile - Optional context file path for resolving class references
|
|
254
344
|
* @returns Complete OpenAPI schema object with properties and validation rules
|
|
255
345
|
* @private
|
|
256
346
|
*/
|
|
@@ -260,6 +350,7 @@ declare class SchemaTransformer {
|
|
|
260
350
|
* Handles primitive types, arrays, and nested objects recursively.
|
|
261
351
|
*
|
|
262
352
|
* @param type - The TypeScript type string to map
|
|
353
|
+
* @param contextFile - Optional context file path for resolving class references
|
|
263
354
|
* @returns Object containing OpenAPI type, optional format, and nested schema
|
|
264
355
|
* @private
|
|
265
356
|
*/
|
|
@@ -274,6 +365,52 @@ declare class SchemaTransformer {
|
|
|
274
365
|
* @private
|
|
275
366
|
*/
|
|
276
367
|
private applyDecorators;
|
|
368
|
+
/**
|
|
369
|
+
* Applies the @IsEnum decorator to a property, handling both primitive values and object enums.
|
|
370
|
+
* Supports arrays of enum values as well.
|
|
371
|
+
*
|
|
372
|
+
* @param decorator - The IsEnum decorator information
|
|
373
|
+
* @param schema - The schema object to modify
|
|
374
|
+
* @param propertyName - The name of the property
|
|
375
|
+
* @param isArrayType - Whether the property is an array type
|
|
376
|
+
* @private
|
|
377
|
+
*/
|
|
378
|
+
private applyEnumDecorator;
|
|
379
|
+
/**
|
|
380
|
+
* Attempts to resolve enum values from an enum type name.
|
|
381
|
+
* This searches through the TypeScript AST to find the enum declaration
|
|
382
|
+
* and extract its values.
|
|
383
|
+
*
|
|
384
|
+
* @param enumTypeName - The name of the enum type
|
|
385
|
+
* @returns Array of enum values if found, empty array otherwise
|
|
386
|
+
* @private
|
|
387
|
+
*/
|
|
388
|
+
private resolveEnumValues;
|
|
389
|
+
/**
|
|
390
|
+
* Finds enum values in a specific source file.
|
|
391
|
+
*
|
|
392
|
+
* @param sourceFile - The source file to search
|
|
393
|
+
* @param enumTypeName - The name of the enum to find
|
|
394
|
+
* @returns Array of enum values if found, empty array otherwise
|
|
395
|
+
* @private
|
|
396
|
+
*/
|
|
397
|
+
private findEnumValues;
|
|
398
|
+
/**
|
|
399
|
+
* Extracts values from a TypeScript enum declaration.
|
|
400
|
+
*
|
|
401
|
+
* @param enumNode - The enum declaration node
|
|
402
|
+
* @returns Array of enum values
|
|
403
|
+
* @private
|
|
404
|
+
*/
|
|
405
|
+
private extractEnumValues;
|
|
406
|
+
/**
|
|
407
|
+
* Extracts values from object literal enum (const object as const).
|
|
408
|
+
*
|
|
409
|
+
* @param initializer - The object literal initializer
|
|
410
|
+
* @returns Array of enum values
|
|
411
|
+
* @private
|
|
412
|
+
*/
|
|
413
|
+
private extractObjectEnumValues;
|
|
277
414
|
/**
|
|
278
415
|
* Applies sensible default behaviors for properties without class-validator decorators.
|
|
279
416
|
* This allows the schema generator to work with plain TypeScript classes.
|
package/package.json
CHANGED