ts-class-to-openapi 1.0.5 → 1.0.6
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 +368 -882
- package/dist/__test__/entities/additional-test-classes.d.ts +12 -0
- package/dist/__test__/entities/decorated-classes.d.ts +54 -0
- package/dist/__test__/entities/nested-classes.d.ts +70 -0
- package/dist/__test__/entities/pure-classes.d.ts +37 -0
- package/dist/__test__/entities/schema-validation-classes.d.ts +35 -0
- package/dist/__test__/index.d.ts +3 -9
- package/dist/__test__/test.d.ts +4 -0
- package/dist/index.d.ts +2 -2
- package/dist/index.esm.js +475 -1324
- package/dist/index.js +474 -1324
- package/dist/run.d.ts +1 -1
- package/dist/run.js +1062 -1350
- package/dist/transformer.d.ts +1 -575
- package/dist/transformer.fixtures.d.ts +21 -0
- package/dist/types.d.ts +38 -3
- package/package.json +16 -15
- package/dist/__test__/entities/address.entity.d.ts +0 -5
- package/dist/__test__/entities/array.entity.d.ts +0 -7
- package/dist/__test__/entities/broken.entity.d.ts +0 -7
- package/dist/__test__/entities/circular.entity.d.ts +0 -59
- package/dist/__test__/entities/complete.entity.d.ts +0 -16
- package/dist/__test__/entities/complex-generics.entity.d.ts +0 -33
- package/dist/__test__/entities/comprehensive-enum.entity.d.ts +0 -23
- package/dist/__test__/entities/enum.entity.d.ts +0 -29
- package/dist/__test__/entities/generic.entity.d.ts +0 -11
- package/dist/__test__/entities/optional-properties.entity.d.ts +0 -11
- package/dist/__test__/entities/plain.entity.d.ts +0 -19
- package/dist/__test__/entities/simple.entity.d.ts +0 -5
- package/dist/__test__/entities/upload.entity.d.ts +0 -8
- package/dist/__test__/entities/user-role-generic.entity.d.ts +0 -13
- package/dist/__test__/plain.test.d.ts +0 -1
- package/dist/__test__/ref-pattern.test.d.ts +0 -1
- package/dist/__test__/singleton-behavior.test.d.ts +0 -1
- package/dist/__test__/test-entities/duplicate-name.entity.d.ts +0 -5
- package/dist/__test__/test-entities/generic.entity.d.ts +0 -11
- /package/dist/__test__/{circular-reference.test.d.ts → testCases/debug.test.d.ts} +0 -0
- /package/dist/__test__/{enum.test.d.ts → testCases/decorated-classes.test.d.ts} +0 -0
- /package/dist/__test__/{generic-types.test.d.ts → testCases/edge-cases.test.d.ts} +0 -0
- /package/dist/__test__/{integration.test.d.ts → testCases/nested-classes.test.d.ts} +0 -0
- /package/dist/__test__/{main.test.d.ts → testCases/pure-classes.test.d.ts} +0 -0
- /package/dist/__test__/{optional-properties.test.d.ts → testCases/schema-validation.test.d.ts} +0 -0
package/dist/transformer.d.ts
CHANGED
|
@@ -1,579 +1,5 @@
|
|
|
1
|
-
import {
|
|
2
|
-
/**
|
|
3
|
-
* Transforms class-validator decorated classes into OpenAPI schema objects.
|
|
4
|
-
* Analyzes TypeScript source files directly using the TypeScript compiler API.
|
|
5
|
-
* Implemented as a singleton for performance optimization.
|
|
6
|
-
*
|
|
7
|
-
* @example
|
|
8
|
-
* ```typescript
|
|
9
|
-
* const transformer = SchemaTransformer.getInstance();
|
|
10
|
-
* const schema = transformer.transform(User);
|
|
11
|
-
* console.log(schema);
|
|
12
|
-
* ```
|
|
13
|
-
*
|
|
14
|
-
* @public
|
|
15
|
-
*/
|
|
16
|
-
declare class SchemaTransformer {
|
|
17
|
-
/**
|
|
18
|
-
* Singleton instance
|
|
19
|
-
* @private
|
|
20
|
-
*/
|
|
21
|
-
private static instance;
|
|
22
|
-
/**
|
|
23
|
-
* TypeScript program instance for analyzing source files.
|
|
24
|
-
* @private
|
|
25
|
-
*/
|
|
26
|
-
private program;
|
|
27
|
-
/**
|
|
28
|
-
* TypeScript type checker for resolving types.
|
|
29
|
-
* @private
|
|
30
|
-
*/
|
|
31
|
-
private checker;
|
|
32
|
-
/**
|
|
33
|
-
* Cache for storing transformed class schemas to avoid reprocessing.
|
|
34
|
-
* Key format: "fileName:className" for uniqueness across different files.
|
|
35
|
-
* @private
|
|
36
|
-
*/
|
|
37
|
-
private classCache;
|
|
38
|
-
/**
|
|
39
|
-
* Maximum number of entries to keep in cache before cleanup
|
|
40
|
-
* @private
|
|
41
|
-
*/
|
|
42
|
-
private readonly maxCacheSize;
|
|
43
|
-
/**
|
|
44
|
-
* Whether to automatically clean up cache
|
|
45
|
-
* @private
|
|
46
|
-
*/
|
|
47
|
-
private readonly autoCleanup;
|
|
48
|
-
/**
|
|
49
|
-
* Set of file paths that have been loaded to avoid redundant processing
|
|
50
|
-
* @private
|
|
51
|
-
*/
|
|
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;
|
|
59
|
-
/**
|
|
60
|
-
* Private constructor for singleton pattern.
|
|
61
|
-
*
|
|
62
|
-
* @param tsConfigPath - Optional path to a specific TypeScript config file
|
|
63
|
-
* @param options - Configuration options for memory management
|
|
64
|
-
* @throws {Error} When TypeScript configuration cannot be loaded
|
|
65
|
-
* @private
|
|
66
|
-
*/
|
|
67
|
-
private constructor();
|
|
68
|
-
/**
|
|
69
|
-
* Generates a unique cache key using file name and class name.
|
|
70
|
-
*
|
|
71
|
-
* @param fileName - The source file name
|
|
72
|
-
* @param className - The class name
|
|
73
|
-
* @returns Unique cache key in format "fileName:className"
|
|
74
|
-
* @private
|
|
75
|
-
*/
|
|
76
|
-
private getCacheKey;
|
|
77
|
-
/**
|
|
78
|
-
* Cleans up cache when it exceeds maximum size to prevent memory leaks.
|
|
79
|
-
* Removes oldest entries using LRU strategy.
|
|
80
|
-
* @private
|
|
81
|
-
*/
|
|
82
|
-
private cleanupCache;
|
|
83
|
-
/**
|
|
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.
|
|
87
|
-
*
|
|
88
|
-
* @param className - The name of the class to transform
|
|
89
|
-
* @param contextFilePath - Optional path to context file for resolving class ambiguity
|
|
90
|
-
* @returns Object containing the class name and its corresponding JSON schema
|
|
91
|
-
* @throws {Error} When the specified class cannot be found
|
|
92
|
-
* @private
|
|
93
|
-
*/
|
|
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
|
-
/**
|
|
106
|
-
* Gets the singleton instance of SchemaTransformer.
|
|
107
|
-
*
|
|
108
|
-
* @param tsConfigPath - Optional path to a specific TypeScript config file (only used on first call)
|
|
109
|
-
* @param options - Configuration options for memory management (only used on first call)
|
|
110
|
-
* @returns The singleton instance
|
|
111
|
-
*
|
|
112
|
-
* @example
|
|
113
|
-
* ```typescript
|
|
114
|
-
* const transformer = SchemaTransformer.getInstance();
|
|
115
|
-
* ```
|
|
116
|
-
*
|
|
117
|
-
* @example
|
|
118
|
-
* ```typescript
|
|
119
|
-
* // With memory optimization options
|
|
120
|
-
* const transformer = SchemaTransformer.getInstance('./tsconfig.json', {
|
|
121
|
-
* maxCacheSize: 50,
|
|
122
|
-
* autoCleanup: true
|
|
123
|
-
* });
|
|
124
|
-
* ```
|
|
125
|
-
*
|
|
126
|
-
* @public
|
|
127
|
-
*/
|
|
128
|
-
/**
|
|
129
|
-
* Clears the current singleton instance. Useful for testing or when you need
|
|
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
|
|
156
|
-
*/
|
|
157
|
-
private static dispose;
|
|
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
|
-
};
|
|
175
|
-
/**
|
|
176
|
-
* Transforms a class constructor function into an OpenAPI schema object.
|
|
177
|
-
*
|
|
178
|
-
* @param cls - The class constructor function to transform
|
|
179
|
-
* @returns Object containing the class name and its corresponding JSON schema
|
|
180
|
-
*
|
|
181
|
-
* @example
|
|
182
|
-
* ```typescript
|
|
183
|
-
* import { User } from './entities/user.js';
|
|
184
|
-
* const transformer = SchemaTransformer.getInstance();
|
|
185
|
-
* const schema = transformer.transform(User);
|
|
186
|
-
* ```
|
|
187
|
-
*
|
|
188
|
-
* @public
|
|
189
|
-
*/
|
|
190
|
-
transform(cls: Function): {
|
|
191
|
-
name: string;
|
|
192
|
-
schema: SchemaType;
|
|
193
|
-
};
|
|
194
|
-
/**
|
|
195
|
-
* Clears all cached schemas and loaded file references to free memory.
|
|
196
|
-
* Useful for long-running applications or when processing many different classes.
|
|
197
|
-
*
|
|
198
|
-
* @example
|
|
199
|
-
* ```typescript
|
|
200
|
-
* const transformer = SchemaTransformer.getInstance();
|
|
201
|
-
* // After processing many classes...
|
|
202
|
-
* transformer.clearCache();
|
|
203
|
-
* ```
|
|
204
|
-
*
|
|
205
|
-
* @public
|
|
206
|
-
*/
|
|
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;
|
|
246
|
-
/**
|
|
247
|
-
* Gets memory usage statistics for monitoring and debugging.
|
|
248
|
-
*
|
|
249
|
-
* @returns Object containing cache size, loaded files count, and processing status
|
|
250
|
-
*
|
|
251
|
-
* @example
|
|
252
|
-
* ```typescript
|
|
253
|
-
* const transformer = SchemaTransformer.getInstance();
|
|
254
|
-
* const stats = transformer.getMemoryStats();
|
|
255
|
-
* console.log(`Cache entries: ${stats.cacheSize}, Files loaded: ${stats.loadedFiles}`);
|
|
256
|
-
* console.log(`Currently processing: ${stats.currentlyProcessing} classes`);
|
|
257
|
-
* ```
|
|
258
|
-
*
|
|
259
|
-
* @private
|
|
260
|
-
*/
|
|
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;
|
|
294
|
-
/**
|
|
295
|
-
* Finds a class declaration by name within a source file.
|
|
296
|
-
*
|
|
297
|
-
* @param sourceFile - The TypeScript source file to search in
|
|
298
|
-
* @param className - The name of the class to find
|
|
299
|
-
* @returns The class declaration node if found, undefined otherwise
|
|
300
|
-
* @private
|
|
301
|
-
*/
|
|
302
|
-
private findClassByName;
|
|
303
|
-
/**
|
|
304
|
-
* Transforms a TypeScript class declaration into a schema object.
|
|
305
|
-
*
|
|
306
|
-
* @param classNode - The TypeScript class declaration node
|
|
307
|
-
* @param sourceFile - The source file containing the class (for context)
|
|
308
|
-
* @returns Object containing class name and generated schema
|
|
309
|
-
* @private
|
|
310
|
-
*/
|
|
311
|
-
private transformClass;
|
|
312
|
-
/**
|
|
313
|
-
* Extracts property information from a class declaration.
|
|
314
|
-
*
|
|
315
|
-
* @param classNode - The TypeScript class declaration node
|
|
316
|
-
* @returns Array of property information including names, types, decorators, and optional status
|
|
317
|
-
* @private
|
|
318
|
-
*/
|
|
319
|
-
private extractProperties;
|
|
320
|
-
/**
|
|
321
|
-
* Gets the TypeScript type of a property as a string.
|
|
322
|
-
*
|
|
323
|
-
* @param property - The property declaration to analyze
|
|
324
|
-
* @returns String representation of the property's type
|
|
325
|
-
* @private
|
|
326
|
-
*/
|
|
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;
|
|
404
|
-
/**
|
|
405
|
-
* Converts a TypeScript type node to its string representation.
|
|
406
|
-
*
|
|
407
|
-
* @param typeNode - The TypeScript type node to convert
|
|
408
|
-
* @returns String representation of the type
|
|
409
|
-
* @private
|
|
410
|
-
*/
|
|
411
|
-
private getTypeNodeToString;
|
|
412
|
-
/**
|
|
413
|
-
* Extracts decorator information from a property declaration.
|
|
414
|
-
*
|
|
415
|
-
* @param member - The property declaration to analyze
|
|
416
|
-
* @returns Array of decorator information including names and arguments
|
|
417
|
-
* @private
|
|
418
|
-
*/
|
|
419
|
-
private extractDecorators;
|
|
420
|
-
/**
|
|
421
|
-
* Gets the name of a decorator from a call expression.
|
|
422
|
-
*
|
|
423
|
-
* @param callExpression - The decorator call expression
|
|
424
|
-
* @returns The decorator name or "unknown" if not identifiable
|
|
425
|
-
* @private
|
|
426
|
-
*/
|
|
427
|
-
private getDecoratorName;
|
|
428
|
-
/**
|
|
429
|
-
* Extracts arguments from a decorator call expression.
|
|
430
|
-
*
|
|
431
|
-
* @param callExpression - The decorator call expression
|
|
432
|
-
* @returns Array of parsed decorator arguments
|
|
433
|
-
* @private
|
|
434
|
-
*/
|
|
435
|
-
private getDecoratorArguments;
|
|
436
|
-
/**
|
|
437
|
-
* Generates an OpenAPI schema from extracted property information.
|
|
438
|
-
*
|
|
439
|
-
* @param properties - Array of property information to process
|
|
440
|
-
* @param contextFilePath - Optional context file path for resolving class references
|
|
441
|
-
* @returns Complete OpenAPI schema object with properties and validation rules
|
|
442
|
-
* @private
|
|
443
|
-
*/
|
|
444
|
-
private generateSchema;
|
|
445
|
-
/**
|
|
446
|
-
* Maps TypeScript types to OpenAPI schema types and formats.
|
|
447
|
-
* Handles primitive types, arrays, and nested objects recursively.
|
|
448
|
-
*
|
|
449
|
-
* @param type - The TypeScript type string to map
|
|
450
|
-
* @param contextFilePath - Optional context file path for resolving class references
|
|
451
|
-
* @returns Object containing OpenAPI type, optional format, and nested schema
|
|
452
|
-
* @private
|
|
453
|
-
*/
|
|
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;
|
|
463
|
-
/**
|
|
464
|
-
* Applies class-validator decorators to schema properties.
|
|
465
|
-
* Maps validation decorators to their corresponding OpenAPI schema constraints.
|
|
466
|
-
*
|
|
467
|
-
* @param decorators - Array of decorator information to apply
|
|
468
|
-
* @param schema - The schema object to modify
|
|
469
|
-
* @param propertyName - Name of the property being processed
|
|
470
|
-
* @private
|
|
471
|
-
*/
|
|
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;
|
|
519
|
-
/**
|
|
520
|
-
* Applies sensible default behaviors for properties without class-validator decorators.
|
|
521
|
-
* This allows the schema generator to work with plain TypeScript classes.
|
|
522
|
-
*
|
|
523
|
-
* @param property - The property information
|
|
524
|
-
* @param schema - The schema object to modify
|
|
525
|
-
* @private
|
|
526
|
-
*/
|
|
527
|
-
/**
|
|
528
|
-
* Applies OpenAPI format specifications based on TypeScript types.
|
|
529
|
-
* This method is called when no decorators are present to set appropriate
|
|
530
|
-
* format values for primitive types according to OpenAPI specification.
|
|
531
|
-
*
|
|
532
|
-
* @param property - The property information containing type details
|
|
533
|
-
* @param schema - The schema object to modify
|
|
534
|
-
* @private
|
|
535
|
-
*/
|
|
536
|
-
private applyTypeBasedFormats;
|
|
537
|
-
/**
|
|
538
|
-
* Determines if a property should be required based on decorators and optional status.
|
|
539
|
-
*
|
|
540
|
-
* Logic:
|
|
541
|
-
* - If property has IsNotEmpty or ArrayNotEmpty decorator, it's required (handled in applyDecorators)
|
|
542
|
-
* - Otherwise, the property is not required (preserving original behavior)
|
|
543
|
-
* - The isOptional information is stored for future use and documentation
|
|
544
|
-
*
|
|
545
|
-
* @param property - The property information
|
|
546
|
-
* @param schema - The schema object to modify
|
|
547
|
-
* @private
|
|
548
|
-
*/
|
|
549
|
-
private determineRequiredStatus;
|
|
550
|
-
}
|
|
551
|
-
/**
|
|
552
|
-
* Convenience function to transform a class using the singleton instance.
|
|
553
|
-
*
|
|
554
|
-
* @param cls - The class constructor function to transform
|
|
555
|
-
* @param options - Optional configuration for memory management
|
|
556
|
-
* @returns Object containing the class name and its corresponding JSON schema
|
|
557
|
-
*
|
|
558
|
-
* @example
|
|
559
|
-
* ```typescript
|
|
560
|
-
* import { transform } from 'class-validator-to-open-api'
|
|
561
|
-
* import { User } from './entities/user.js'
|
|
562
|
-
*
|
|
563
|
-
* const schema = transform(User)
|
|
564
|
-
* console.log(schema)
|
|
565
|
-
* ```
|
|
566
|
-
*
|
|
567
|
-
* @example
|
|
568
|
-
* ```typescript
|
|
569
|
-
* // With memory optimization
|
|
570
|
-
* const schema = transform(User, { maxCacheSize: 50, autoCleanup: true })
|
|
571
|
-
* ```
|
|
572
|
-
*
|
|
573
|
-
* @public
|
|
574
|
-
*/
|
|
1
|
+
import { SchemaType, TransformerOptions } from './types';
|
|
575
2
|
export declare function transform<T>(cls: new (...args: any[]) => T, options?: TransformerOptions): {
|
|
576
3
|
name: string;
|
|
577
4
|
schema: SchemaType;
|
|
578
5
|
};
|
|
579
|
-
export { SchemaTransformer };
|
|
@@ -12,6 +12,14 @@ declare const constants: {
|
|
|
12
12
|
type: string;
|
|
13
13
|
value: string;
|
|
14
14
|
};
|
|
15
|
+
Any: {
|
|
16
|
+
type: string;
|
|
17
|
+
value: string;
|
|
18
|
+
};
|
|
19
|
+
Unknown: {
|
|
20
|
+
type: string;
|
|
21
|
+
value: string;
|
|
22
|
+
};
|
|
15
23
|
Number: {
|
|
16
24
|
type: string;
|
|
17
25
|
value: string;
|
|
@@ -117,6 +125,9 @@ declare const constants: {
|
|
|
117
125
|
IsNotEmpty: {
|
|
118
126
|
name: string;
|
|
119
127
|
};
|
|
128
|
+
IsOptional: {
|
|
129
|
+
name: string;
|
|
130
|
+
};
|
|
120
131
|
IsBoolean: {
|
|
121
132
|
name: string;
|
|
122
133
|
type: string;
|
|
@@ -145,5 +156,15 @@ declare const constants: {
|
|
|
145
156
|
type: string;
|
|
146
157
|
};
|
|
147
158
|
};
|
|
159
|
+
tsUtilityTypes: {
|
|
160
|
+
Partial: {
|
|
161
|
+
type: string;
|
|
162
|
+
value: string;
|
|
163
|
+
};
|
|
164
|
+
Required: {
|
|
165
|
+
type: string;
|
|
166
|
+
value: string;
|
|
167
|
+
};
|
|
168
|
+
};
|
|
148
169
|
};
|
|
149
170
|
export { messages, constants };
|
package/dist/types.d.ts
CHANGED
|
@@ -1,22 +1,31 @@
|
|
|
1
|
-
|
|
1
|
+
import ts from 'typescript';
|
|
2
|
+
type Property = ({
|
|
2
3
|
[key: string]: any;
|
|
3
4
|
} & {
|
|
4
5
|
type: string;
|
|
5
|
-
};
|
|
6
|
+
}) | SchemaType;
|
|
6
7
|
type SchemaType = ({
|
|
7
8
|
[key: string]: any;
|
|
8
9
|
} & {
|
|
9
10
|
properties: {
|
|
10
11
|
[key: string]: any;
|
|
12
|
+
} & {
|
|
13
|
+
additionalProperties?: boolean;
|
|
11
14
|
};
|
|
12
15
|
} & {
|
|
13
|
-
required
|
|
16
|
+
required?: string[];
|
|
14
17
|
} & {
|
|
15
18
|
type: string;
|
|
16
19
|
}) | ({
|
|
17
20
|
[key: string]: any;
|
|
18
21
|
} & {
|
|
19
22
|
$ref: string;
|
|
23
|
+
}) | ({
|
|
24
|
+
[key: string]: any;
|
|
25
|
+
} & {
|
|
26
|
+
type: 'array';
|
|
27
|
+
} & {
|
|
28
|
+
items: SchemaType;
|
|
20
29
|
});
|
|
21
30
|
/**
|
|
22
31
|
* Information about a class-validator decorator found on a property.
|
|
@@ -41,6 +50,16 @@ interface PropertyInfo {
|
|
|
41
50
|
decorators: DecoratorInfo[];
|
|
42
51
|
/** Whether the property is optional (has ? operator) */
|
|
43
52
|
isOptional: boolean;
|
|
53
|
+
/** Whether the property type is a generic type */
|
|
54
|
+
isGeneric: boolean;
|
|
55
|
+
/** Whether the property type is a primitive type */
|
|
56
|
+
isPrimitive: boolean;
|
|
57
|
+
/** Whether the property type is a class type */
|
|
58
|
+
isClassType?: boolean;
|
|
59
|
+
/** Whether the property type is an array type */
|
|
60
|
+
isArray?: boolean;
|
|
61
|
+
/** The original TypeScript property declaration (optional) */
|
|
62
|
+
originalProperty: ts.PropertyDeclaration;
|
|
44
63
|
}
|
|
45
64
|
/**
|
|
46
65
|
* Configuration options for SchemaTransformer memory management
|
|
@@ -51,5 +70,21 @@ interface TransformerOptions {
|
|
|
51
70
|
maxCacheSize?: number;
|
|
52
71
|
/** Whether to automatically clean up cache (default: true) */
|
|
53
72
|
autoCleanup?: boolean;
|
|
73
|
+
/** Options related to the source of the class being transformed */
|
|
74
|
+
sourceOptions?: {
|
|
75
|
+
/** Whether the class is from an external module */
|
|
76
|
+
isExternal: true;
|
|
77
|
+
/** The package name (required when isExternal is true) */
|
|
78
|
+
packageName: string;
|
|
79
|
+
/** The file path of the class being transformed */
|
|
80
|
+
filePath?: string;
|
|
81
|
+
} | {
|
|
82
|
+
/** Whether the class is from an external module */
|
|
83
|
+
isExternal: false;
|
|
84
|
+
/** The package name (optional when isExternal is false) */
|
|
85
|
+
packageName: never;
|
|
86
|
+
/** The file path of the class being transformed */
|
|
87
|
+
filePath?: string;
|
|
88
|
+
};
|
|
54
89
|
}
|
|
55
90
|
export { type SchemaType, type Property, type DecoratorInfo, type PropertyInfo, type TransformerOptions, };
|