ts-class-to-openapi 1.0.3 → 1.0.5
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 +258 -33
- package/dist/__test__/circular-reference.test.d.ts +1 -0
- package/dist/__test__/entities/circular.entity.d.ts +59 -0
- 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 +5 -0
- package/dist/__test__/ref-pattern.test.d.ts +1 -0
- package/dist/__test__/singleton-behavior.test.d.ts +1 -0
- package/dist/index.d.ts +3 -3
- package/dist/index.esm.js +785 -45
- package/dist/index.js +785 -44
- package/dist/run.js +785 -44
- package/dist/transformer.d.ts +269 -28
- package/dist/transformer.fixtures.d.ts +4 -0
- package/dist/types.d.ts +17 -3
- package/package.json +1 -1
package/dist/transformer.d.ts
CHANGED
|
@@ -1,13 +1,4 @@
|
|
|
1
|
-
import { type SchemaType } from './types.js';
|
|
2
|
-
/**
|
|
3
|
-
* Configuration options for SchemaTransformer memory management
|
|
4
|
-
*/
|
|
5
|
-
interface TransformerOptions {
|
|
6
|
-
/** Maximum number of schemas to cache before cleanup (default: 100) */
|
|
7
|
-
maxCacheSize?: number;
|
|
8
|
-
/** Whether to automatically clean up cache (default: true) */
|
|
9
|
-
autoCleanup?: boolean;
|
|
10
|
-
}
|
|
1
|
+
import { type SchemaType, type TransformerOptions } from './types.js';
|
|
11
2
|
/**
|
|
12
3
|
* Transforms class-validator decorated classes into OpenAPI schema objects.
|
|
13
4
|
* Analyzes TypeScript source files directly using the TypeScript compiler API.
|
|
@@ -59,6 +50,12 @@ declare class SchemaTransformer {
|
|
|
59
50
|
* @private
|
|
60
51
|
*/
|
|
61
52
|
private loadedFiles;
|
|
53
|
+
/**
|
|
54
|
+
* Set of class names currently being processed to prevent circular references
|
|
55
|
+
* Key format: "fileName:className" for uniqueness across different files
|
|
56
|
+
* @private
|
|
57
|
+
*/
|
|
58
|
+
private processingClasses;
|
|
62
59
|
/**
|
|
63
60
|
* Private constructor for singleton pattern.
|
|
64
61
|
*
|
|
@@ -83,25 +80,28 @@ declare class SchemaTransformer {
|
|
|
83
80
|
* @private
|
|
84
81
|
*/
|
|
85
82
|
private cleanupCache;
|
|
86
|
-
/**
|
|
87
|
-
* Gets relevant source files for a class, filtering out unnecessary files to save memory.
|
|
88
|
-
*
|
|
89
|
-
* @param className - The name of the class to find files for
|
|
90
|
-
* @param filePath - Optional specific file path
|
|
91
|
-
* @returns Array of relevant source files
|
|
92
|
-
* @private
|
|
93
|
-
*/
|
|
94
|
-
private getRelevantSourceFiles;
|
|
95
83
|
/**
|
|
96
84
|
* Transforms a class by its name into an OpenAPI schema object.
|
|
85
|
+
* Considers the context of the calling file to resolve ambiguous class names.
|
|
86
|
+
* Includes circular reference detection to prevent infinite recursion.
|
|
97
87
|
*
|
|
98
88
|
* @param className - The name of the class to transform
|
|
99
|
-
* @param
|
|
89
|
+
* @param contextFilePath - Optional path to context file for resolving class ambiguity
|
|
100
90
|
* @returns Object containing the class name and its corresponding JSON schema
|
|
101
91
|
* @throws {Error} When the specified class cannot be found
|
|
102
92
|
* @private
|
|
103
93
|
*/
|
|
104
94
|
private transformByName;
|
|
95
|
+
/**
|
|
96
|
+
* Prioritizes source files based on context to resolve class name conflicts.
|
|
97
|
+
* Gives priority to files in the same directory or with similar names.
|
|
98
|
+
*
|
|
99
|
+
* @param sourceFiles - Array of source files to prioritize
|
|
100
|
+
* @param contextFilePath - Optional path to context file for prioritization
|
|
101
|
+
* @returns Prioritized array of source files
|
|
102
|
+
* @private
|
|
103
|
+
*/
|
|
104
|
+
private prioritizeSourceFiles;
|
|
105
105
|
/**
|
|
106
106
|
* Gets the singleton instance of SchemaTransformer.
|
|
107
107
|
*
|
|
@@ -128,9 +128,50 @@ declare class SchemaTransformer {
|
|
|
128
128
|
/**
|
|
129
129
|
* Clears the current singleton instance. Useful for testing or when you need
|
|
130
130
|
* to create a new instance with different configuration.
|
|
131
|
+
* @private
|
|
132
|
+
*/
|
|
133
|
+
private static clearInstance;
|
|
134
|
+
/**
|
|
135
|
+
* Flag to prevent recursive disposal calls
|
|
136
|
+
* @private
|
|
131
137
|
*/
|
|
132
|
-
static
|
|
138
|
+
private static disposingInProgress;
|
|
139
|
+
/**
|
|
140
|
+
* Completely disposes of the current singleton instance and releases all resources.
|
|
141
|
+
* This is a static method that can be called without having an instance reference.
|
|
142
|
+
* Ensures complete memory cleanup regardless of the current state.
|
|
143
|
+
*
|
|
144
|
+
* @example
|
|
145
|
+
* ```typescript
|
|
146
|
+
* SchemaTransformer.disposeInstance();
|
|
147
|
+
* // All resources released, next getInstance() will create fresh instance
|
|
148
|
+
* ```
|
|
149
|
+
*
|
|
150
|
+
* @public
|
|
151
|
+
*/
|
|
152
|
+
static disposeInstance(): void;
|
|
153
|
+
/**
|
|
154
|
+
* @deprecated Use disposeInstance() instead for better clarity
|
|
155
|
+
* @private
|
|
156
|
+
*/
|
|
157
|
+
private static dispose;
|
|
133
158
|
static getInstance(tsConfigPath?: string, options?: TransformerOptions): SchemaTransformer;
|
|
159
|
+
/**
|
|
160
|
+
* Internal method to check if current instance is disposed
|
|
161
|
+
* @private
|
|
162
|
+
*/
|
|
163
|
+
private static isInstanceDisposed;
|
|
164
|
+
/**
|
|
165
|
+
* Transforms a class using the singleton instance
|
|
166
|
+
* @param cls - The class constructor function to transform
|
|
167
|
+
* @param options - Optional configuration for memory management (only used if no instance exists)
|
|
168
|
+
* @returns Object containing the class name and its corresponding JSON schema
|
|
169
|
+
* @public
|
|
170
|
+
*/
|
|
171
|
+
static transformClass<T>(cls: new (...args: any[]) => T, options?: TransformerOptions): {
|
|
172
|
+
name: string;
|
|
173
|
+
schema: SchemaType;
|
|
174
|
+
};
|
|
134
175
|
/**
|
|
135
176
|
* Transforms a class constructor function into an OpenAPI schema object.
|
|
136
177
|
*
|
|
@@ -164,24 +205,92 @@ declare class SchemaTransformer {
|
|
|
164
205
|
* @public
|
|
165
206
|
*/
|
|
166
207
|
clearCache(): void;
|
|
208
|
+
/**
|
|
209
|
+
* Completely disposes of the transformer instance and releases all resources.
|
|
210
|
+
* This includes clearing all caches, releasing TypeScript program resources,
|
|
211
|
+
* and resetting the singleton instance.
|
|
212
|
+
*
|
|
213
|
+
* After calling this method, you need to call getInstance() again to get a new instance.
|
|
214
|
+
*
|
|
215
|
+
* @example
|
|
216
|
+
* ```typescript
|
|
217
|
+
* const transformer = SchemaTransformer.getInstance();
|
|
218
|
+
* // ... use transformer
|
|
219
|
+
* transformer.dispose();
|
|
220
|
+
* // transformer is now unusable, need to get new instance
|
|
221
|
+
* const newTransformer = SchemaTransformer.getInstance();
|
|
222
|
+
* ```
|
|
223
|
+
*
|
|
224
|
+
* @private
|
|
225
|
+
*/
|
|
226
|
+
private dispose;
|
|
227
|
+
/**
|
|
228
|
+
* Completely resets the transformer by disposing current instance and creating a new one.
|
|
229
|
+
* This is useful when you need a fresh start with different TypeScript configuration
|
|
230
|
+
* or want to ensure all resources are properly released and recreated.
|
|
231
|
+
*
|
|
232
|
+
* @param tsConfigPath - Optional path to a specific TypeScript config file for the new instance
|
|
233
|
+
* @param options - Configuration options for memory management for the new instance
|
|
234
|
+
* @returns A fresh SchemaTransformer instance
|
|
235
|
+
*
|
|
236
|
+
* @example
|
|
237
|
+
* ```typescript
|
|
238
|
+
* const transformer = SchemaTransformer.getInstance();
|
|
239
|
+
* // ... use transformer
|
|
240
|
+
* const freshTransformer = transformer.reset('./new-tsconfig.json');
|
|
241
|
+
* ```
|
|
242
|
+
*
|
|
243
|
+
* @private
|
|
244
|
+
*/
|
|
245
|
+
private reset;
|
|
167
246
|
/**
|
|
168
247
|
* Gets memory usage statistics for monitoring and debugging.
|
|
169
248
|
*
|
|
170
|
-
* @returns Object containing cache size
|
|
249
|
+
* @returns Object containing cache size, loaded files count, and processing status
|
|
171
250
|
*
|
|
172
251
|
* @example
|
|
173
252
|
* ```typescript
|
|
174
253
|
* const transformer = SchemaTransformer.getInstance();
|
|
175
254
|
* const stats = transformer.getMemoryStats();
|
|
176
255
|
* console.log(`Cache entries: ${stats.cacheSize}, Files loaded: ${stats.loadedFiles}`);
|
|
256
|
+
* console.log(`Currently processing: ${stats.currentlyProcessing} classes`);
|
|
177
257
|
* ```
|
|
178
258
|
*
|
|
179
|
-
* @
|
|
259
|
+
* @private
|
|
180
260
|
*/
|
|
181
|
-
getMemoryStats
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
261
|
+
private getMemoryStats;
|
|
262
|
+
/**
|
|
263
|
+
* Checks if the transformer instance has been disposed and is no longer usable.
|
|
264
|
+
*
|
|
265
|
+
* @returns True if the instance has been disposed
|
|
266
|
+
*
|
|
267
|
+
* @example
|
|
268
|
+
* ```typescript
|
|
269
|
+
* const transformer = SchemaTransformer.getInstance();
|
|
270
|
+
* transformer.dispose();
|
|
271
|
+
* console.log(transformer.isDisposed()); // true
|
|
272
|
+
* ```
|
|
273
|
+
*
|
|
274
|
+
* @private
|
|
275
|
+
*/
|
|
276
|
+
private isDisposed;
|
|
277
|
+
/**
|
|
278
|
+
* Static method to check if there's an active singleton instance.
|
|
279
|
+
*
|
|
280
|
+
* @returns True if there's an active instance, false if disposed or never created
|
|
281
|
+
*
|
|
282
|
+
* @example
|
|
283
|
+
* ```typescript
|
|
284
|
+
* console.log(SchemaTransformer.hasActiveInstance()); // false
|
|
285
|
+
* const transformer = SchemaTransformer.getInstance();
|
|
286
|
+
* console.log(SchemaTransformer.hasActiveInstance()); // true
|
|
287
|
+
* SchemaTransformer.dispose();
|
|
288
|
+
* console.log(SchemaTransformer.hasActiveInstance()); // false
|
|
289
|
+
* ```
|
|
290
|
+
*
|
|
291
|
+
* @private
|
|
292
|
+
*/
|
|
293
|
+
private static hasActiveInstance;
|
|
185
294
|
/**
|
|
186
295
|
* Finds a class declaration by name within a source file.
|
|
187
296
|
*
|
|
@@ -195,6 +304,7 @@ declare class SchemaTransformer {
|
|
|
195
304
|
* Transforms a TypeScript class declaration into a schema object.
|
|
196
305
|
*
|
|
197
306
|
* @param classNode - The TypeScript class declaration node
|
|
307
|
+
* @param sourceFile - The source file containing the class (for context)
|
|
198
308
|
* @returns Object containing class name and generated schema
|
|
199
309
|
* @private
|
|
200
310
|
*/
|
|
@@ -215,6 +325,82 @@ declare class SchemaTransformer {
|
|
|
215
325
|
* @private
|
|
216
326
|
*/
|
|
217
327
|
private getPropertyType;
|
|
328
|
+
/**
|
|
329
|
+
* Resolves generic types by analyzing the type alias and its arguments.
|
|
330
|
+
* For example, User<Role> where User is a type alias will be resolved to its structure.
|
|
331
|
+
*
|
|
332
|
+
* @param typeNode - The TypeScript type reference node with generic arguments
|
|
333
|
+
* @returns String representation of the resolved type or schema
|
|
334
|
+
* @private
|
|
335
|
+
*/
|
|
336
|
+
private resolveGenericType;
|
|
337
|
+
/**
|
|
338
|
+
* Checks if a type string represents a resolved generic type.
|
|
339
|
+
*
|
|
340
|
+
* @param type - The type string to check
|
|
341
|
+
* @returns True if it's a resolved generic type
|
|
342
|
+
* @private
|
|
343
|
+
*/
|
|
344
|
+
private isResolvedGenericType;
|
|
345
|
+
/**
|
|
346
|
+
* Checks if a type is a known class or interface.
|
|
347
|
+
*
|
|
348
|
+
* @param typeName - The type name to check
|
|
349
|
+
* @returns True if it's a known type
|
|
350
|
+
* @private
|
|
351
|
+
*/
|
|
352
|
+
private isKnownType;
|
|
353
|
+
/**
|
|
354
|
+
* Finds a class by name in the project without transforming it.
|
|
355
|
+
*
|
|
356
|
+
* @param className - The class name to find
|
|
357
|
+
* @returns True if found, false otherwise
|
|
358
|
+
* @private
|
|
359
|
+
*/
|
|
360
|
+
private findClassInProject;
|
|
361
|
+
/**
|
|
362
|
+
* Checks if a type is a primitive type.
|
|
363
|
+
*
|
|
364
|
+
* @param typeName - The type name to check
|
|
365
|
+
* @returns True if it's a primitive type
|
|
366
|
+
* @private
|
|
367
|
+
*/
|
|
368
|
+
private isPrimitiveType;
|
|
369
|
+
/**
|
|
370
|
+
* Resolves a generic type schema by analyzing the type alias structure.
|
|
371
|
+
*
|
|
372
|
+
* @param resolvedTypeName - The resolved generic type name (e.g., User_Role)
|
|
373
|
+
* @returns OpenAPI schema for the resolved generic type
|
|
374
|
+
* @private
|
|
375
|
+
*/
|
|
376
|
+
private resolveGenericTypeSchema;
|
|
377
|
+
/**
|
|
378
|
+
* Finds a type alias declaration by name.
|
|
379
|
+
*
|
|
380
|
+
* @param typeName - The type alias name to find
|
|
381
|
+
* @returns The type alias declaration node or null
|
|
382
|
+
* @private
|
|
383
|
+
*/
|
|
384
|
+
private findTypeAliasDeclaration;
|
|
385
|
+
/**
|
|
386
|
+
* Creates a schema from a type alias declaration, substituting type parameters.
|
|
387
|
+
*
|
|
388
|
+
* @param typeAlias - The type alias declaration
|
|
389
|
+
* @param typeArgNames - The concrete type arguments
|
|
390
|
+
* @returns OpenAPI schema for the type alias
|
|
391
|
+
* @private
|
|
392
|
+
*/
|
|
393
|
+
private createSchemaFromTypeAlias;
|
|
394
|
+
/**
|
|
395
|
+
* Resolves type parameters in a type alias to concrete types.
|
|
396
|
+
*
|
|
397
|
+
* @param typeNode - The type node to resolve
|
|
398
|
+
* @param typeParameters - The type parameters of the type alias
|
|
399
|
+
* @param typeArgNames - The concrete type arguments
|
|
400
|
+
* @returns The resolved type string
|
|
401
|
+
* @private
|
|
402
|
+
*/
|
|
403
|
+
private resolveTypeParameterInTypeAlias;
|
|
218
404
|
/**
|
|
219
405
|
* Converts a TypeScript type node to its string representation.
|
|
220
406
|
*
|
|
@@ -251,6 +437,7 @@ declare class SchemaTransformer {
|
|
|
251
437
|
* Generates an OpenAPI schema from extracted property information.
|
|
252
438
|
*
|
|
253
439
|
* @param properties - Array of property information to process
|
|
440
|
+
* @param contextFilePath - Optional context file path for resolving class references
|
|
254
441
|
* @returns Complete OpenAPI schema object with properties and validation rules
|
|
255
442
|
* @private
|
|
256
443
|
*/
|
|
@@ -260,10 +447,19 @@ declare class SchemaTransformer {
|
|
|
260
447
|
* Handles primitive types, arrays, and nested objects recursively.
|
|
261
448
|
*
|
|
262
449
|
* @param type - The TypeScript type string to map
|
|
450
|
+
* @param contextFilePath - Optional context file path for resolving class references
|
|
263
451
|
* @returns Object containing OpenAPI type, optional format, and nested schema
|
|
264
452
|
* @private
|
|
265
453
|
*/
|
|
266
454
|
private mapTypeToSchema;
|
|
455
|
+
/**
|
|
456
|
+
* Checks if a schema is a $ref schema (circular reference).
|
|
457
|
+
*
|
|
458
|
+
* @param schema - The schema to check
|
|
459
|
+
* @returns True if it's a $ref schema
|
|
460
|
+
* @private
|
|
461
|
+
*/
|
|
462
|
+
private isRefSchema;
|
|
267
463
|
/**
|
|
268
464
|
* Applies class-validator decorators to schema properties.
|
|
269
465
|
* Maps validation decorators to their corresponding OpenAPI schema constraints.
|
|
@@ -274,6 +470,52 @@ declare class SchemaTransformer {
|
|
|
274
470
|
* @private
|
|
275
471
|
*/
|
|
276
472
|
private applyDecorators;
|
|
473
|
+
/**
|
|
474
|
+
* Applies the @IsEnum decorator to a property, handling both primitive values and object enums.
|
|
475
|
+
* Supports arrays of enum values as well.
|
|
476
|
+
*
|
|
477
|
+
* @param decorator - The IsEnum decorator information
|
|
478
|
+
* @param schema - The schema object to modify
|
|
479
|
+
* @param propertyName - The name of the property
|
|
480
|
+
* @param isArrayType - Whether the property is an array type
|
|
481
|
+
* @private
|
|
482
|
+
*/
|
|
483
|
+
private applyEnumDecorator;
|
|
484
|
+
/**
|
|
485
|
+
* Attempts to resolve enum values from an enum type name.
|
|
486
|
+
* This searches through the TypeScript AST to find the enum declaration
|
|
487
|
+
* and extract its values.
|
|
488
|
+
*
|
|
489
|
+
* @param enumTypeName - The name of the enum type
|
|
490
|
+
* @returns Array of enum values if found, empty array otherwise
|
|
491
|
+
* @private
|
|
492
|
+
*/
|
|
493
|
+
private resolveEnumValues;
|
|
494
|
+
/**
|
|
495
|
+
* Finds enum values in a specific source file.
|
|
496
|
+
*
|
|
497
|
+
* @param sourceFile - The source file to search
|
|
498
|
+
* @param enumTypeName - The name of the enum to find
|
|
499
|
+
* @returns Array of enum values if found, empty array otherwise
|
|
500
|
+
* @private
|
|
501
|
+
*/
|
|
502
|
+
private findEnumValues;
|
|
503
|
+
/**
|
|
504
|
+
* Extracts values from a TypeScript enum declaration.
|
|
505
|
+
*
|
|
506
|
+
* @param enumNode - The enum declaration node
|
|
507
|
+
* @returns Array of enum values
|
|
508
|
+
* @private
|
|
509
|
+
*/
|
|
510
|
+
private extractEnumValues;
|
|
511
|
+
/**
|
|
512
|
+
* Extracts values from object literal enum (const object as const).
|
|
513
|
+
*
|
|
514
|
+
* @param initializer - The object literal initializer
|
|
515
|
+
* @returns Array of enum values
|
|
516
|
+
* @private
|
|
517
|
+
*/
|
|
518
|
+
private extractObjectEnumValues;
|
|
277
519
|
/**
|
|
278
520
|
* Applies sensible default behaviors for properties without class-validator decorators.
|
|
279
521
|
* This allows the schema generator to work with plain TypeScript classes.
|
|
@@ -334,5 +576,4 @@ export declare function transform<T>(cls: new (...args: any[]) => T, options?: T
|
|
|
334
576
|
name: string;
|
|
335
577
|
schema: SchemaType;
|
|
336
578
|
};
|
|
337
|
-
export type { TransformerOptions };
|
|
338
579
|
export { SchemaTransformer };
|
package/dist/types.d.ts
CHANGED
|
@@ -3,7 +3,7 @@ type Property = {
|
|
|
3
3
|
} & {
|
|
4
4
|
type: string;
|
|
5
5
|
};
|
|
6
|
-
type SchemaType = {
|
|
6
|
+
type SchemaType = ({
|
|
7
7
|
[key: string]: any;
|
|
8
8
|
} & {
|
|
9
9
|
properties: {
|
|
@@ -13,7 +13,11 @@ type SchemaType = {
|
|
|
13
13
|
required: string[];
|
|
14
14
|
} & {
|
|
15
15
|
type: string;
|
|
16
|
-
}
|
|
16
|
+
}) | ({
|
|
17
|
+
[key: string]: any;
|
|
18
|
+
} & {
|
|
19
|
+
$ref: string;
|
|
20
|
+
});
|
|
17
21
|
/**
|
|
18
22
|
* Information about a class-validator decorator found on a property.
|
|
19
23
|
* @interface DecoratorInfo
|
|
@@ -38,4 +42,14 @@ interface PropertyInfo {
|
|
|
38
42
|
/** Whether the property is optional (has ? operator) */
|
|
39
43
|
isOptional: boolean;
|
|
40
44
|
}
|
|
41
|
-
|
|
45
|
+
/**
|
|
46
|
+
* Configuration options for SchemaTransformer memory management
|
|
47
|
+
* @interface TransformerOptions
|
|
48
|
+
*/
|
|
49
|
+
interface TransformerOptions {
|
|
50
|
+
/** Maximum number of schemas to cache before cleanup (default: 100) */
|
|
51
|
+
maxCacheSize?: number;
|
|
52
|
+
/** Whether to automatically clean up cache (default: true) */
|
|
53
|
+
autoCleanup?: boolean;
|
|
54
|
+
}
|
|
55
|
+
export { type SchemaType, type Property, type DecoratorInfo, type PropertyInfo, type TransformerOptions, };
|
package/package.json
CHANGED