ts-class-to-openapi 1.0.4 → 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 +39 -31
- package/dist/__test__/circular-reference.test.d.ts +1 -0
- package/dist/__test__/entities/circular.entity.d.ts +59 -0
- package/dist/__test__/index.d.ts +3 -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 +307 -57
- package/dist/index.js +307 -56
- package/dist/run.js +307 -56
- package/dist/transformer.d.ts +137 -33
- 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,22 +80,13 @@ 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.
|
|
97
|
-
*
|
|
85
|
+
* Considers the context of the calling file to resolve ambiguous class names.
|
|
86
|
+
* Includes circular reference detection to prevent infinite recursion.
|
|
98
87
|
*
|
|
99
88
|
* @param className - The name of the class to transform
|
|
100
|
-
* @param
|
|
101
|
-
* @param contextFile - Optional context file for resolving class ambiguity
|
|
89
|
+
* @param contextFilePath - Optional path to context file for resolving class ambiguity
|
|
102
90
|
* @returns Object containing the class name and its corresponding JSON schema
|
|
103
91
|
* @throws {Error} When the specified class cannot be found
|
|
104
92
|
* @private
|
|
@@ -109,7 +97,7 @@ declare class SchemaTransformer {
|
|
|
109
97
|
* Gives priority to files in the same directory or with similar names.
|
|
110
98
|
*
|
|
111
99
|
* @param sourceFiles - Array of source files to prioritize
|
|
112
|
-
* @param
|
|
100
|
+
* @param contextFilePath - Optional path to context file for prioritization
|
|
113
101
|
* @returns Prioritized array of source files
|
|
114
102
|
* @private
|
|
115
103
|
*/
|
|
@@ -140,9 +128,50 @@ declare class SchemaTransformer {
|
|
|
140
128
|
/**
|
|
141
129
|
* Clears the current singleton instance. Useful for testing or when you need
|
|
142
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
|
|
137
|
+
*/
|
|
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
|
|
143
156
|
*/
|
|
144
|
-
static
|
|
157
|
+
private static dispose;
|
|
145
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
|
+
};
|
|
146
175
|
/**
|
|
147
176
|
* Transforms a class constructor function into an OpenAPI schema object.
|
|
148
177
|
*
|
|
@@ -176,24 +205,92 @@ declare class SchemaTransformer {
|
|
|
176
205
|
* @public
|
|
177
206
|
*/
|
|
178
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;
|
|
179
246
|
/**
|
|
180
247
|
* Gets memory usage statistics for monitoring and debugging.
|
|
181
248
|
*
|
|
182
|
-
* @returns Object containing cache size
|
|
249
|
+
* @returns Object containing cache size, loaded files count, and processing status
|
|
183
250
|
*
|
|
184
251
|
* @example
|
|
185
252
|
* ```typescript
|
|
186
253
|
* const transformer = SchemaTransformer.getInstance();
|
|
187
254
|
* const stats = transformer.getMemoryStats();
|
|
188
255
|
* console.log(`Cache entries: ${stats.cacheSize}, Files loaded: ${stats.loadedFiles}`);
|
|
256
|
+
* console.log(`Currently processing: ${stats.currentlyProcessing} classes`);
|
|
189
257
|
* ```
|
|
190
258
|
*
|
|
191
|
-
* @
|
|
259
|
+
* @private
|
|
192
260
|
*/
|
|
193
|
-
getMemoryStats
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
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;
|
|
197
294
|
/**
|
|
198
295
|
* Finds a class declaration by name within a source file.
|
|
199
296
|
*
|
|
@@ -340,7 +437,7 @@ declare class SchemaTransformer {
|
|
|
340
437
|
* Generates an OpenAPI schema from extracted property information.
|
|
341
438
|
*
|
|
342
439
|
* @param properties - Array of property information to process
|
|
343
|
-
* @param
|
|
440
|
+
* @param contextFilePath - Optional context file path for resolving class references
|
|
344
441
|
* @returns Complete OpenAPI schema object with properties and validation rules
|
|
345
442
|
* @private
|
|
346
443
|
*/
|
|
@@ -350,11 +447,19 @@ declare class SchemaTransformer {
|
|
|
350
447
|
* Handles primitive types, arrays, and nested objects recursively.
|
|
351
448
|
*
|
|
352
449
|
* @param type - The TypeScript type string to map
|
|
353
|
-
* @param
|
|
450
|
+
* @param contextFilePath - Optional context file path for resolving class references
|
|
354
451
|
* @returns Object containing OpenAPI type, optional format, and nested schema
|
|
355
452
|
* @private
|
|
356
453
|
*/
|
|
357
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;
|
|
358
463
|
/**
|
|
359
464
|
* Applies class-validator decorators to schema properties.
|
|
360
465
|
* Maps validation decorators to their corresponding OpenAPI schema constraints.
|
|
@@ -471,5 +576,4 @@ export declare function transform<T>(cls: new (...args: any[]) => T, options?: T
|
|
|
471
576
|
name: string;
|
|
472
577
|
schema: SchemaType;
|
|
473
578
|
};
|
|
474
|
-
export type { TransformerOptions };
|
|
475
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