nestjs-openapi-next 1.0.0
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/LICENSE +21 -0
- package/README.md +211 -0
- package/bun.lock +2154 -0
- package/dist/constants.d.ts +23 -0
- package/dist/constants.js +26 -0
- package/dist/decorators/api-basic.decorator.d.ts +1 -0
- package/dist/decorators/api-basic.decorator.js +7 -0
- package/dist/decorators/api-bearer.decorator.d.ts +1 -0
- package/dist/decorators/api-bearer.decorator.js +7 -0
- package/dist/decorators/api-body.decorator.d.ts +16 -0
- package/dist/decorators/api-body.decorator.js +22 -0
- package/dist/decorators/api-callbacks.decorator.d.ts +2 -0
- package/dist/decorators/api-callbacks.decorator.js +8 -0
- package/dist/decorators/api-consumes.decorator.d.ts +1 -0
- package/dist/decorators/api-consumes.decorator.js +8 -0
- package/dist/decorators/api-cookie.decorator.d.ts +1 -0
- package/dist/decorators/api-cookie.decorator.js +7 -0
- package/dist/decorators/api-default-getter.decorator.d.ts +2 -0
- package/dist/decorators/api-default-getter.decorator.js +12 -0
- package/dist/decorators/api-exclude-controller.decorator.d.ts +1 -0
- package/dist/decorators/api-exclude-controller.decorator.js +8 -0
- package/dist/decorators/api-exclude-endpoint.decorator.d.ts +1 -0
- package/dist/decorators/api-exclude-endpoint.decorator.js +10 -0
- package/dist/decorators/api-extension.decorator.d.ts +1 -0
- package/dist/decorators/api-extension.decorator.js +38 -0
- package/dist/decorators/api-extra-models.decorator.d.ts +1 -0
- package/dist/decorators/api-extra-models.decorator.js +17 -0
- package/dist/decorators/api-header.decorator.d.ts +7 -0
- package/dist/decorators/api-header.decorator.js +37 -0
- package/dist/decorators/api-hide-property.decorator.d.ts +1 -0
- package/dist/decorators/api-hide-property.decorator.js +6 -0
- package/dist/decorators/api-link.decorator.d.ts +7 -0
- package/dist/decorators/api-link.decorator.js +21 -0
- package/dist/decorators/api-oauth2.decorator.d.ts +1 -0
- package/dist/decorators/api-oauth2.decorator.js +7 -0
- package/dist/decorators/api-operation.decorator.d.ts +5 -0
- package/dist/decorators/api-operation.decorator.js +12 -0
- package/dist/decorators/api-param.decorator.d.ts +22 -0
- package/dist/decorators/api-param.decorator.js +17 -0
- package/dist/decorators/api-produces.decorator.d.ts +1 -0
- package/dist/decorators/api-produces.decorator.js +8 -0
- package/dist/decorators/api-property.decorator.d.ts +15 -0
- package/dist/decorators/api-property.decorator.js +48 -0
- package/dist/decorators/api-query-method.decorator.d.ts +1 -0
- package/dist/decorators/api-query-method.decorator.js +8 -0
- package/dist/decorators/api-query.decorator.d.ts +24 -0
- package/dist/decorators/api-query.decorator.js +25 -0
- package/dist/decorators/api-response.decorator.d.ts +39 -0
- package/dist/decorators/api-response.decorator.js +55 -0
- package/dist/decorators/api-schema.decorator.d.ts +6 -0
- package/dist/decorators/api-schema.decorator.js +8 -0
- package/dist/decorators/api-security-device-flow.decorator.d.ts +1 -0
- package/dist/decorators/api-security-device-flow.decorator.js +7 -0
- package/dist/decorators/api-security.decorator.d.ts +2 -0
- package/dist/decorators/api-security.decorator.js +25 -0
- package/dist/decorators/api-streaming-response.decorator.d.ts +7 -0
- package/dist/decorators/api-streaming-response.decorator.js +9 -0
- package/dist/decorators/api-tag-group.decorator.d.ts +11 -0
- package/dist/decorators/api-tag-group.decorator.js +13 -0
- package/dist/decorators/api-use-tags.decorator.d.ts +1 -0
- package/dist/decorators/api-use-tags.decorator.js +8 -0
- package/dist/decorators/helpers.d.ts +8 -0
- package/dist/decorators/helpers.js +123 -0
- package/dist/decorators/index.d.ts +28 -0
- package/dist/decorators/index.js +48 -0
- package/dist/document-builder.d.ts +29 -0
- package/dist/document-builder.js +142 -0
- package/dist/explorers/api-callbacks.explorer.d.ts +2 -0
- package/dist/explorers/api-callbacks.explorer.js +36 -0
- package/dist/explorers/api-consumes.explorer.d.ts +5 -0
- package/dist/explorers/api-consumes.explorer.js +11 -0
- package/dist/explorers/api-exclude-controller.explorer.d.ts +2 -0
- package/dist/explorers/api-exclude-controller.explorer.js +10 -0
- package/dist/explorers/api-exclude-endpoint.explorer.d.ts +2 -0
- package/dist/explorers/api-exclude-endpoint.explorer.js +6 -0
- package/dist/explorers/api-extra-models.explorer.d.ts +3 -0
- package/dist/explorers/api-extra-models.explorer.js +11 -0
- package/dist/explorers/api-headers.explorer.d.ts +7 -0
- package/dist/explorers/api-headers.explorer.js +9 -0
- package/dist/explorers/api-operation.explorer.d.ts +2 -0
- package/dist/explorers/api-operation.explorer.js +43 -0
- package/dist/explorers/api-parameters.explorer.d.ts +71 -0
- package/dist/explorers/api-parameters.explorer.js +47 -0
- package/dist/explorers/api-produces.explorer.d.ts +5 -0
- package/dist/explorers/api-produces.explorer.js +11 -0
- package/dist/explorers/api-response.explorer.d.ts +13 -0
- package/dist/explorers/api-response.explorer.js +103 -0
- package/dist/explorers/api-security.explorer.d.ts +5 -0
- package/dist/explorers/api-security.explorer.js +13 -0
- package/dist/explorers/api-use-tags.explorer.d.ts +5 -0
- package/dist/explorers/api-use-tags.explorer.js +18 -0
- package/dist/extra/swagger-shim.d.ts +100 -0
- package/dist/extra/swagger-shim.js +367 -0
- package/dist/fixtures/document.base.d.ts +2 -0
- package/dist/fixtures/document.base.js +16 -0
- package/dist/index.d.ts +7 -0
- package/dist/index.js +23 -0
- package/dist/interfaces/callback-object.interface.d.ts +12 -0
- package/dist/interfaces/callback-object.interface.js +2 -0
- package/dist/interfaces/denormalized-doc-resolvers.interface.d.ts +7 -0
- package/dist/interfaces/denormalized-doc-resolvers.interface.js +2 -0
- package/dist/interfaces/denormalized-doc.interface.d.ts +8 -0
- package/dist/interfaces/denormalized-doc.interface.js +2 -0
- package/dist/interfaces/enum-schema-attributes.interface.d.ts +2 -0
- package/dist/interfaces/enum-schema-attributes.interface.js +2 -0
- package/dist/interfaces/index.d.ts +3 -0
- package/dist/interfaces/index.js +18 -0
- package/dist/interfaces/module-route.interface.d.ts +2 -0
- package/dist/interfaces/module-route.interface.js +2 -0
- package/dist/interfaces/open-api-spec.interface.d.ts +252 -0
- package/dist/interfaces/open-api-spec.interface.js +2 -0
- package/dist/interfaces/schema-object-metadata.interface.d.ts +30 -0
- package/dist/interfaces/schema-object-metadata.interface.js +2 -0
- package/dist/interfaces/swagger-custom-options.interface.d.ts +24 -0
- package/dist/interfaces/swagger-custom-options.interface.js +2 -0
- package/dist/interfaces/swagger-document-options.interface.d.ts +10 -0
- package/dist/interfaces/swagger-document-options.interface.js +2 -0
- package/dist/interfaces/swagger-ui-init-options.interface.d.ts +7 -0
- package/dist/interfaces/swagger-ui-init-options.interface.js +2 -0
- package/dist/interfaces/swagger-ui-options.interface.d.ts +15 -0
- package/dist/interfaces/swagger-ui-options.interface.js +2 -0
- package/dist/plugin/compiler-plugin.d.ts +2 -0
- package/dist/plugin/compiler-plugin.js +30 -0
- package/dist/plugin/index.d.ts +2 -0
- package/dist/plugin/index.js +18 -0
- package/dist/plugin/merge-options.d.ts +17 -0
- package/dist/plugin/merge-options.js +37 -0
- package/dist/plugin/metadata-loader.d.ts +7 -0
- package/dist/plugin/metadata-loader.js +51 -0
- package/dist/plugin/plugin-constants.d.ts +3 -0
- package/dist/plugin/plugin-constants.js +6 -0
- package/dist/plugin/plugin-debug-logger.d.ts +5 -0
- package/dist/plugin/plugin-debug-logger.js +7 -0
- package/dist/plugin/utils/ast-utils.d.ts +30 -0
- package/dist/plugin/utils/ast-utils.js +273 -0
- package/dist/plugin/utils/is-filename-matched.util.d.ts +1 -0
- package/dist/plugin/utils/is-filename-matched.util.js +5 -0
- package/dist/plugin/utils/plugin-utils.d.ts +30 -0
- package/dist/plugin/utils/plugin-utils.js +307 -0
- package/dist/plugin/utils/type-reference-to-identifier.util.d.ts +7 -0
- package/dist/plugin/utils/type-reference-to-identifier.util.js +52 -0
- package/dist/plugin/visitors/abstract.visitor.d.ts +4 -0
- package/dist/plugin/visitors/abstract.visitor.js +30 -0
- package/dist/plugin/visitors/controller-class.visitor.d.ts +20 -0
- package/dist/plugin/visitors/controller-class.visitor.js +265 -0
- package/dist/plugin/visitors/model-class.visitor.d.ts +35 -0
- package/dist/plugin/visitors/model-class.visitor.js +514 -0
- package/dist/plugin/visitors/readonly.visitor.d.ts +21 -0
- package/dist/plugin/visitors/readonly.visitor.js +40 -0
- package/dist/services/constants.d.ts +1 -0
- package/dist/services/constants.js +4 -0
- package/dist/services/decorators-properties.d.ts +21 -0
- package/dist/services/decorators-properties.js +149 -0
- package/dist/services/mimetype-content-wrapper.d.ts +4 -0
- package/dist/services/mimetype-content-wrapper.js +11 -0
- package/dist/services/model-properties-accessor.d.ts +6 -0
- package/dist/services/model-properties-accessor.js +37 -0
- package/dist/services/parameter-metadata-accessor.d.ts +22 -0
- package/dist/services/parameter-metadata-accessor.js +41 -0
- package/dist/services/parameters-metadata-mapper.d.ts +9 -0
- package/dist/services/parameters-metadata-mapper.js +38 -0
- package/dist/services/response-object-factory.d.ts +28 -0
- package/dist/services/response-object-factory.js +120 -0
- package/dist/services/response-object-mapper.d.ts +39 -0
- package/dist/services/response-object-mapper.js +38 -0
- package/dist/services/schema-object-factory.d.ts +156 -0
- package/dist/services/schema-object-factory.js +471 -0
- package/dist/services/swagger-types-mapper.d.ts +260 -0
- package/dist/services/swagger-types-mapper.js +107 -0
- package/dist/storages/global-parameters.storage.d.ts +8 -0
- package/dist/storages/global-parameters.storage.js +22 -0
- package/dist/storages/global-responses.storage.d.ts +10 -0
- package/dist/storages/global-responses.storage.js +21 -0
- package/dist/swagger-explorer.d.ts +41 -0
- package/dist/swagger-explorer.js +326 -0
- package/dist/swagger-module.d.ts +27 -0
- package/dist/swagger-module.js +223 -0
- package/dist/swagger-scanner.d.ts +25 -0
- package/dist/swagger-scanner.js +116 -0
- package/dist/swagger-transformer.d.ts +4 -0
- package/dist/swagger-transformer.js +22 -0
- package/dist/swagger-ui/constants.d.ts +3 -0
- package/dist/swagger-ui/constants.js +132 -0
- package/dist/swagger-ui/helpers.d.ts +2 -0
- package/dist/swagger-ui/helpers.js +16 -0
- package/dist/swagger-ui/index.d.ts +1 -0
- package/dist/swagger-ui/index.js +17 -0
- package/dist/swagger-ui/swagger-ui.d.ts +4 -0
- package/dist/swagger-ui/swagger-ui.js +62 -0
- package/dist/type-helpers/index.d.ts +4 -0
- package/dist/type-helpers/index.js +20 -0
- package/dist/type-helpers/intersection-type.helper.d.ts +8 -0
- package/dist/type-helpers/intersection-type.helper.js +42 -0
- package/dist/type-helpers/mapped-types.utils.d.ts +2 -0
- package/dist/type-helpers/mapped-types.utils.js +31 -0
- package/dist/type-helpers/omit-type.helper.d.ts +2 -0
- package/dist/type-helpers/omit-type.helper.js +40 -0
- package/dist/type-helpers/partial-type.helper.d.ts +4 -0
- package/dist/type-helpers/partial-type.helper.js +49 -0
- package/dist/type-helpers/pick-type.helper.d.ts +2 -0
- package/dist/type-helpers/pick-type.helper.js +40 -0
- package/dist/types/swagger-enum.type.d.ts +1 -0
- package/dist/types/swagger-enum.type.js +2 -0
- package/dist/utils/assign-two-levels-deep.d.ts +1 -0
- package/dist/utils/assign-two-levels-deep.js +12 -0
- package/dist/utils/enum.utils.d.ts +9 -0
- package/dist/utils/enum.utils.js +65 -0
- package/dist/utils/extend-metadata.util.d.ts +2 -0
- package/dist/utils/extend-metadata.util.js +11 -0
- package/dist/utils/get-global-prefix.d.ts +2 -0
- package/dist/utils/get-global-prefix.js +7 -0
- package/dist/utils/get-schema-path.util.d.ts +4 -0
- package/dist/utils/get-schema-path.util.js +26 -0
- package/dist/utils/index.d.ts +1 -0
- package/dist/utils/index.js +17 -0
- package/dist/utils/is-body-parameter.util.d.ts +2 -0
- package/dist/utils/is-body-parameter.util.js +6 -0
- package/dist/utils/is-built-in-type.util.d.ts +2 -0
- package/dist/utils/is-built-in-type.util.js +8 -0
- package/dist/utils/is-date-ctor.util.d.ts +2 -0
- package/dist/utils/is-date-ctor.util.js +6 -0
- package/dist/utils/merge-and-uniq.util.d.ts +1 -0
- package/dist/utils/merge-and-uniq.util.js +7 -0
- package/dist/utils/normalize-rel-path.d.ts +1 -0
- package/dist/utils/normalize-rel-path.js +7 -0
- package/dist/utils/remove-undefined-keys.d.ts +5 -0
- package/dist/utils/remove-undefined-keys.js +11 -0
- package/dist/utils/resolve-path.util.d.ts +1 -0
- package/dist/utils/resolve-path.util.js +7 -0
- package/dist/utils/reverse-object-keys.util.d.ts +1 -0
- package/dist/utils/reverse-object-keys.util.js +11 -0
- package/dist/utils/sort-object-lexicographically.d.ts +5 -0
- package/dist/utils/sort-object-lexicographically.js +11 -0
- package/dist/utils/strip-last-slash.util.d.ts +1 -0
- package/dist/utils/strip-last-slash.util.js +8 -0
- package/dist/utils/validate-global-prefix.util.d.ts +1 -0
- package/dist/utils/validate-global-prefix.util.js +5 -0
- package/dist/utils/validate-path.util.d.ts +1 -0
- package/dist/utils/validate-path.util.js +5 -0
- package/eslint.config.mjs +47 -0
- package/nest-cli.json +6 -0
- package/package.json +116 -0
- package/plugin.js +10 -0
- package/tsconfig.build.json +21 -0
|
@@ -0,0 +1,514 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.ModelClassVisitor = void 0;
|
|
4
|
+
const lodash_1 = require("lodash");
|
|
5
|
+
const path_1 = require("path");
|
|
6
|
+
const ts = require("typescript");
|
|
7
|
+
const typescript_1 = require("typescript");
|
|
8
|
+
const decorators_1 = require("../../decorators");
|
|
9
|
+
const decorators_properties_1 = require("../../services/decorators-properties");
|
|
10
|
+
const plugin_constants_1 = require("../plugin-constants");
|
|
11
|
+
const plugin_debug_logger_1 = require("../plugin-debug-logger");
|
|
12
|
+
const ast_utils_1 = require("../utils/ast-utils");
|
|
13
|
+
const plugin_utils_1 = require("../utils/plugin-utils");
|
|
14
|
+
const type_reference_to_identifier_util_1 = require("../utils/type-reference-to-identifier.util");
|
|
15
|
+
const abstract_visitor_1 = require("./abstract.visitor");
|
|
16
|
+
class ModelClassVisitor extends abstract_visitor_1.AbstractFileVisitor {
|
|
17
|
+
constructor() {
|
|
18
|
+
super(...arguments);
|
|
19
|
+
this._typeImports = {};
|
|
20
|
+
this._collectedMetadata = {};
|
|
21
|
+
}
|
|
22
|
+
get typeImports() {
|
|
23
|
+
return this._typeImports;
|
|
24
|
+
}
|
|
25
|
+
collectedMetadata(options) {
|
|
26
|
+
const metadataWithImports = [];
|
|
27
|
+
Object.keys(this._collectedMetadata).forEach((filePath) => {
|
|
28
|
+
const metadata = this._collectedMetadata[filePath];
|
|
29
|
+
const fileExt = options.esmCompatible ? (0, plugin_utils_1.getOutputExtension)(filePath) : '';
|
|
30
|
+
const path = filePath.replace(/\.[jt]s$/, fileExt);
|
|
31
|
+
const importExpr = ts.factory.createCallExpression(ts.factory.createToken(ts.SyntaxKind.ImportKeyword), undefined, [ts.factory.createStringLiteral(path)]);
|
|
32
|
+
metadataWithImports.push([importExpr, metadata]);
|
|
33
|
+
});
|
|
34
|
+
return metadataWithImports;
|
|
35
|
+
}
|
|
36
|
+
visit(sourceFile, ctx, program, options) {
|
|
37
|
+
const typeChecker = program.getTypeChecker();
|
|
38
|
+
sourceFile = this.updateImports(sourceFile, ctx.factory, program);
|
|
39
|
+
const propertyNodeVisitorFactory = (metadata) => (node) => {
|
|
40
|
+
const visit = () => {
|
|
41
|
+
if (ts.isPropertyDeclaration(node)) {
|
|
42
|
+
this.visitPropertyNodeDeclaration(node, ctx, typeChecker, options, sourceFile, metadata);
|
|
43
|
+
}
|
|
44
|
+
else if (options.parameterProperties &&
|
|
45
|
+
ts.isConstructorDeclaration(node)) {
|
|
46
|
+
this.visitConstructorDeclarationNode(node, typeChecker, options, sourceFile, metadata);
|
|
47
|
+
}
|
|
48
|
+
return node;
|
|
49
|
+
};
|
|
50
|
+
const visitedNode = visit();
|
|
51
|
+
if (!options.readonly) {
|
|
52
|
+
return visitedNode;
|
|
53
|
+
}
|
|
54
|
+
};
|
|
55
|
+
const visitClassNode = (node) => {
|
|
56
|
+
var _a;
|
|
57
|
+
if (ts.isClassDeclaration(node)) {
|
|
58
|
+
const metadata = {};
|
|
59
|
+
const isExported = (_a = node.modifiers) === null || _a === void 0 ? void 0 : _a.some((modifier) => modifier.kind === ts.SyntaxKind.ExportKeyword);
|
|
60
|
+
if (options.readonly) {
|
|
61
|
+
if (isExported) {
|
|
62
|
+
ts.forEachChild(node, propertyNodeVisitorFactory(metadata));
|
|
63
|
+
}
|
|
64
|
+
else {
|
|
65
|
+
if (options.debug) {
|
|
66
|
+
plugin_debug_logger_1.pluginDebugLogger.debug(`Skipping class "${node.name.getText()}" because it's not exported.`);
|
|
67
|
+
}
|
|
68
|
+
}
|
|
69
|
+
}
|
|
70
|
+
else {
|
|
71
|
+
node = ts.visitEachChild(node, propertyNodeVisitorFactory(metadata), ctx);
|
|
72
|
+
}
|
|
73
|
+
if ((isExported && options.readonly) || !options.readonly) {
|
|
74
|
+
const declaration = this.addMetadataFactory(ctx.factory, node, metadata, sourceFile, options);
|
|
75
|
+
if (!options.readonly) {
|
|
76
|
+
return declaration;
|
|
77
|
+
}
|
|
78
|
+
}
|
|
79
|
+
}
|
|
80
|
+
if (options.readonly) {
|
|
81
|
+
ts.forEachChild(node, visitClassNode);
|
|
82
|
+
}
|
|
83
|
+
else {
|
|
84
|
+
return ts.visitEachChild(node, visitClassNode, ctx);
|
|
85
|
+
}
|
|
86
|
+
};
|
|
87
|
+
return ts.visitNode(sourceFile, visitClassNode);
|
|
88
|
+
}
|
|
89
|
+
visitPropertyNodeDeclaration(node, ctx, typeChecker, options, sourceFile, metadata) {
|
|
90
|
+
const isPropertyStatic = (node.modifiers || []).some((modifier) => modifier.kind === ts.SyntaxKind.StaticKeyword);
|
|
91
|
+
if (isPropertyStatic) {
|
|
92
|
+
return node;
|
|
93
|
+
}
|
|
94
|
+
const isPrivateProperty = ts.isPrivateIdentifier(node.name);
|
|
95
|
+
if (isPrivateProperty) {
|
|
96
|
+
return node;
|
|
97
|
+
}
|
|
98
|
+
const decorators = ts.canHaveDecorators(node) && ts.getDecorators(node);
|
|
99
|
+
const classTransformerShim = options.classTransformerShim;
|
|
100
|
+
const hidePropertyDecoratorExists = (0, plugin_utils_1.getDecoratorOrUndefinedByNames)(classTransformerShim
|
|
101
|
+
? [decorators_1.ApiHideProperty.name, 'Exclude']
|
|
102
|
+
: [decorators_1.ApiHideProperty.name], decorators, typescript_1.factory);
|
|
103
|
+
const annotatePropertyDecoratorExists = (0, plugin_utils_1.getDecoratorOrUndefinedByNames)(classTransformerShim ? [decorators_1.ApiProperty.name, 'Expose'] : [decorators_1.ApiProperty.name], decorators, typescript_1.factory);
|
|
104
|
+
if (!annotatePropertyDecoratorExists &&
|
|
105
|
+
(hidePropertyDecoratorExists || classTransformerShim === 'exclusive')) {
|
|
106
|
+
return node;
|
|
107
|
+
}
|
|
108
|
+
else if (annotatePropertyDecoratorExists && hidePropertyDecoratorExists) {
|
|
109
|
+
plugin_debug_logger_1.pluginDebugLogger.debug(`"${node.parent.name.getText()}->${node.name.getText()}" has conflicting decorators, excluding as @ApiHideProperty() takes priority.`);
|
|
110
|
+
return node;
|
|
111
|
+
}
|
|
112
|
+
try {
|
|
113
|
+
this.inspectPropertyDeclaration(ctx.factory, node, typeChecker, options, sourceFile.fileName, sourceFile, metadata);
|
|
114
|
+
}
|
|
115
|
+
catch (err) {
|
|
116
|
+
return node;
|
|
117
|
+
}
|
|
118
|
+
}
|
|
119
|
+
visitConstructorDeclarationNode(constructorNode, typeChecker, options, sourceFile, metadata) {
|
|
120
|
+
constructorNode.forEachChild((node) => {
|
|
121
|
+
if (ts.isParameter(node) &&
|
|
122
|
+
node.modifiers != null &&
|
|
123
|
+
node.modifiers.some((modifier) => modifier.kind === ts.SyntaxKind.ReadonlyKeyword ||
|
|
124
|
+
modifier.kind === ts.SyntaxKind.PrivateKeyword ||
|
|
125
|
+
modifier.kind === ts.SyntaxKind.PublicKeyword ||
|
|
126
|
+
modifier.kind === ts.SyntaxKind.ProtectedKeyword)) {
|
|
127
|
+
const objectLiteralExpr = this.createDecoratorObjectLiteralExpr(typescript_1.factory, node, typeChecker, typescript_1.factory.createNodeArray(), options, sourceFile.fileName, sourceFile);
|
|
128
|
+
const propertyName = node.name.getText();
|
|
129
|
+
metadata[propertyName] = objectLiteralExpr;
|
|
130
|
+
}
|
|
131
|
+
});
|
|
132
|
+
}
|
|
133
|
+
addMetadataFactory(factory, node, classMetadata, sourceFile, options) {
|
|
134
|
+
const returnValue = factory.createObjectLiteralExpression(Object.keys(classMetadata).map((key) => factory.createPropertyAssignment(factory.createIdentifier(key), classMetadata[key])));
|
|
135
|
+
if (options.readonly) {
|
|
136
|
+
const filePath = this.normalizeImportPath(options.pathToSource, sourceFile.fileName);
|
|
137
|
+
if (!this._collectedMetadata[filePath]) {
|
|
138
|
+
this._collectedMetadata[filePath] = {};
|
|
139
|
+
}
|
|
140
|
+
const attributeKey = node.name.getText();
|
|
141
|
+
this._collectedMetadata[filePath][attributeKey] = returnValue;
|
|
142
|
+
return;
|
|
143
|
+
}
|
|
144
|
+
const method = factory.createMethodDeclaration([factory.createModifier(ts.SyntaxKind.StaticKeyword)], undefined, factory.createIdentifier(plugin_constants_1.METADATA_FACTORY_NAME), undefined, undefined, [], undefined, factory.createBlock([factory.createReturnStatement(returnValue)], true));
|
|
145
|
+
return factory.updateClassDeclaration(node, node.modifiers, node.name, node.typeParameters, node.heritageClauses, [...node.members, method]);
|
|
146
|
+
}
|
|
147
|
+
inspectPropertyDeclaration(factory, compilerNode, typeChecker, options, hostFilename, sourceFile, metadata) {
|
|
148
|
+
const objectLiteralExpr = this.createDecoratorObjectLiteralExpr(factory, compilerNode, typeChecker, factory.createNodeArray(), options, hostFilename, sourceFile);
|
|
149
|
+
this.addClassMetadata(compilerNode, objectLiteralExpr, sourceFile, metadata);
|
|
150
|
+
}
|
|
151
|
+
createDecoratorObjectLiteralExpr(factory, node, typeChecker, existingProperties = factory.createNodeArray(), options = {}, hostFilename = '', sourceFile) {
|
|
152
|
+
const isRequired = !node.questionToken;
|
|
153
|
+
const properties = [
|
|
154
|
+
...existingProperties,
|
|
155
|
+
!(0, plugin_utils_1.hasPropertyKey)('required', existingProperties) &&
|
|
156
|
+
factory.createPropertyAssignment('required', (0, ast_utils_1.createBooleanLiteral)(factory, isRequired)),
|
|
157
|
+
...this.createTypePropertyAssignments(factory, node.type, typeChecker, existingProperties, hostFilename, options),
|
|
158
|
+
...this.createDescriptionAndTsDocTagPropertyAssignments(factory, node, typeChecker, existingProperties, options, sourceFile),
|
|
159
|
+
this.createDefaultPropertyAssignment(factory, node, existingProperties, options),
|
|
160
|
+
this.createEnumPropertyAssignment(factory, node, typeChecker, existingProperties, hostFilename, options)
|
|
161
|
+
];
|
|
162
|
+
if ((ts.isPropertyDeclaration(node) || ts.isPropertySignature(node)) &&
|
|
163
|
+
options.classValidatorShim) {
|
|
164
|
+
properties.push(this.createValidationPropertyAssignments(factory, node, options));
|
|
165
|
+
}
|
|
166
|
+
return factory.createObjectLiteralExpression((0, lodash_1.compact)((0, lodash_1.flatten)(properties)));
|
|
167
|
+
}
|
|
168
|
+
createTypePropertyAssignments(factory, node, typeChecker, existingProperties, hostFilename, options) {
|
|
169
|
+
const key = 'type';
|
|
170
|
+
if ((0, plugin_utils_1.hasPropertyKey)(key, existingProperties)) {
|
|
171
|
+
return [];
|
|
172
|
+
}
|
|
173
|
+
if (node) {
|
|
174
|
+
if (ts.isArrayTypeNode(node) && ts.isTypeLiteralNode(node.elementType)) {
|
|
175
|
+
const initializer = this.createInitializerForArrayLiteralTypeNode(node, factory, typeChecker, existingProperties, hostFilename, options);
|
|
176
|
+
return [factory.createPropertyAssignment(key, initializer)];
|
|
177
|
+
}
|
|
178
|
+
if (ts.isTypeLiteralNode(node)) {
|
|
179
|
+
const initializer = this.createInitializerForTypeLiteralNode(node, factory, typeChecker, existingProperties, hostFilename, options);
|
|
180
|
+
return [factory.createPropertyAssignment(key, initializer)];
|
|
181
|
+
}
|
|
182
|
+
if (ts.isUnionTypeNode(node)) {
|
|
183
|
+
const { nullableType, isNullable } = this.isNullableUnion(node);
|
|
184
|
+
const remainingTypes = node.types.filter((t) => t !== nullableType);
|
|
185
|
+
if (remainingTypes.length === 1) {
|
|
186
|
+
const nonNullishNode = remainingTypes[0];
|
|
187
|
+
const resolved = typeChecker.getTypeAtLocation(nonNullishNode);
|
|
188
|
+
let candidateType = resolved;
|
|
189
|
+
const arrayTuple = (0, plugin_utils_1.extractTypeArgumentIfArray)(candidateType);
|
|
190
|
+
if (arrayTuple) {
|
|
191
|
+
candidateType = arrayTuple.type;
|
|
192
|
+
}
|
|
193
|
+
let isEnumType = false;
|
|
194
|
+
if (candidateType) {
|
|
195
|
+
if ((0, ast_utils_1.isEnum)(candidateType)) {
|
|
196
|
+
isEnumType = true;
|
|
197
|
+
}
|
|
198
|
+
else {
|
|
199
|
+
const maybeEnum = (0, plugin_utils_1.isAutoGeneratedEnumUnion)(candidateType, typeChecker);
|
|
200
|
+
if (maybeEnum) {
|
|
201
|
+
isEnumType = true;
|
|
202
|
+
}
|
|
203
|
+
}
|
|
204
|
+
}
|
|
205
|
+
if (isEnumType) {
|
|
206
|
+
return isNullable
|
|
207
|
+
? [
|
|
208
|
+
factory.createPropertyAssignment('nullable', (0, ast_utils_1.createBooleanLiteral)(factory, true))
|
|
209
|
+
]
|
|
210
|
+
: [];
|
|
211
|
+
}
|
|
212
|
+
const propertyAssignments = this.createTypePropertyAssignments(factory, nonNullishNode, typeChecker, existingProperties, hostFilename, options);
|
|
213
|
+
if (!isNullable) {
|
|
214
|
+
return propertyAssignments;
|
|
215
|
+
}
|
|
216
|
+
return [
|
|
217
|
+
...propertyAssignments,
|
|
218
|
+
factory.createPropertyAssignment('nullable', (0, ast_utils_1.createBooleanLiteral)(factory, true))
|
|
219
|
+
];
|
|
220
|
+
}
|
|
221
|
+
}
|
|
222
|
+
}
|
|
223
|
+
const type = typeChecker.getTypeAtLocation(node);
|
|
224
|
+
if (!type) {
|
|
225
|
+
return [];
|
|
226
|
+
}
|
|
227
|
+
const typeReferenceDescriptor = (0, plugin_utils_1.getTypeReferenceAsString)(type, typeChecker);
|
|
228
|
+
if (!typeReferenceDescriptor.typeName) {
|
|
229
|
+
return [];
|
|
230
|
+
}
|
|
231
|
+
const identifier = (0, type_reference_to_identifier_util_1.typeReferenceToIdentifier)(typeReferenceDescriptor, hostFilename, options, factory, type, this._typeImports);
|
|
232
|
+
const initializer = factory.createArrowFunction(undefined, undefined, [], undefined, undefined, identifier);
|
|
233
|
+
return [factory.createPropertyAssignment(key, initializer)];
|
|
234
|
+
}
|
|
235
|
+
createInitializerForArrayLiteralTypeNode(node, factory, typeChecker, existingProperties, hostFilename, options) {
|
|
236
|
+
const elementType = node.elementType;
|
|
237
|
+
const propertyAssignments = Array.from(elementType.members || []).map((member) => {
|
|
238
|
+
const literalExpr = this.createDecoratorObjectLiteralExpr(factory, member, typeChecker, existingProperties, options, hostFilename);
|
|
239
|
+
return factory.createPropertyAssignment(factory.createIdentifier(member.name.getText()), literalExpr);
|
|
240
|
+
});
|
|
241
|
+
const initializer = factory.createArrowFunction(undefined, undefined, [], undefined, undefined, factory.createArrayLiteralExpression([
|
|
242
|
+
factory.createParenthesizedExpression(factory.createObjectLiteralExpression(propertyAssignments))
|
|
243
|
+
]));
|
|
244
|
+
return initializer;
|
|
245
|
+
}
|
|
246
|
+
createInitializerForTypeLiteralNode(node, factory, typeChecker, existingProperties, hostFilename, options) {
|
|
247
|
+
const propertyAssignments = Array.from(node.members || []).map((member) => {
|
|
248
|
+
const literalExpr = this.createDecoratorObjectLiteralExpr(factory, member, typeChecker, existingProperties, options, hostFilename);
|
|
249
|
+
return factory.createPropertyAssignment(factory.createIdentifier(member.name.getText()), literalExpr);
|
|
250
|
+
});
|
|
251
|
+
const initializer = factory.createArrowFunction(undefined, undefined, [], undefined, undefined, factory.createParenthesizedExpression(factory.createObjectLiteralExpression(propertyAssignments)));
|
|
252
|
+
return initializer;
|
|
253
|
+
}
|
|
254
|
+
isNullableUnion(node) {
|
|
255
|
+
const nullableType = node.types.find((type) => type.kind === ts.SyntaxKind.NullKeyword ||
|
|
256
|
+
(ts.SyntaxKind.LiteralType && type.getText() === 'null'));
|
|
257
|
+
const isNullable = !!nullableType;
|
|
258
|
+
return { nullableType, isNullable };
|
|
259
|
+
}
|
|
260
|
+
createEnumPropertyAssignment(factory, node, typeChecker, existingProperties, hostFilename, options) {
|
|
261
|
+
const key = 'enum';
|
|
262
|
+
if ((0, plugin_utils_1.hasPropertyKey)(key, existingProperties)) {
|
|
263
|
+
return undefined;
|
|
264
|
+
}
|
|
265
|
+
let type;
|
|
266
|
+
try {
|
|
267
|
+
if (node.type) {
|
|
268
|
+
type = typeChecker.getTypeFromTypeNode(node.type);
|
|
269
|
+
}
|
|
270
|
+
}
|
|
271
|
+
catch (e) {
|
|
272
|
+
}
|
|
273
|
+
if (!type) {
|
|
274
|
+
type = typeChecker.getTypeAtLocation(node);
|
|
275
|
+
}
|
|
276
|
+
if (!type) {
|
|
277
|
+
return undefined;
|
|
278
|
+
}
|
|
279
|
+
if ((type.flags & ts.TypeFlags.Union) !== 0) {
|
|
280
|
+
const union = type;
|
|
281
|
+
const nonNullish = union.types.filter((t) => (t.flags & (ts.TypeFlags.Null | ts.TypeFlags.Undefined)) === 0);
|
|
282
|
+
if (nonNullish.length === 1) {
|
|
283
|
+
type = nonNullish[0];
|
|
284
|
+
}
|
|
285
|
+
}
|
|
286
|
+
if ((0, plugin_utils_1.isAutoGeneratedTypeUnion)(type)) {
|
|
287
|
+
const types = type.types;
|
|
288
|
+
const nonUndefined = types.find((t) => t.intrinsicName !== 'undefined');
|
|
289
|
+
if (nonUndefined) {
|
|
290
|
+
type = nonUndefined;
|
|
291
|
+
}
|
|
292
|
+
}
|
|
293
|
+
if ((0, plugin_utils_1.isAutoGeneratedTypeUnion)(type)) {
|
|
294
|
+
const types = type.types;
|
|
295
|
+
type = types[types.length - 1];
|
|
296
|
+
}
|
|
297
|
+
const typeIsArrayTuple = (0, plugin_utils_1.extractTypeArgumentIfArray)(type);
|
|
298
|
+
if (!typeIsArrayTuple) {
|
|
299
|
+
return undefined;
|
|
300
|
+
}
|
|
301
|
+
const isArrayType = typeIsArrayTuple.isArray;
|
|
302
|
+
type = typeIsArrayTuple.type;
|
|
303
|
+
const isEnumMember = type.symbol && type.symbol.flags === ts.SymbolFlags.EnumMember;
|
|
304
|
+
if (!(0, ast_utils_1.isEnum)(type) || isEnumMember) {
|
|
305
|
+
if (!isEnumMember) {
|
|
306
|
+
type = (0, plugin_utils_1.isAutoGeneratedEnumUnion)(type, typeChecker);
|
|
307
|
+
}
|
|
308
|
+
if (!type) {
|
|
309
|
+
return undefined;
|
|
310
|
+
}
|
|
311
|
+
const typeIsArrayTuple = (0, plugin_utils_1.extractTypeArgumentIfArray)(type);
|
|
312
|
+
if (!typeIsArrayTuple) {
|
|
313
|
+
return undefined;
|
|
314
|
+
}
|
|
315
|
+
type = typeIsArrayTuple.type;
|
|
316
|
+
}
|
|
317
|
+
const typeReferenceDescriptor = { typeName: (0, ast_utils_1.getText)(type, typeChecker) };
|
|
318
|
+
const enumIdentifier = (0, type_reference_to_identifier_util_1.typeReferenceToIdentifier)(typeReferenceDescriptor, hostFilename, options, factory, type, this._typeImports);
|
|
319
|
+
const enumProperty = factory.createPropertyAssignment(key, enumIdentifier);
|
|
320
|
+
if (isArrayType) {
|
|
321
|
+
const isArrayKey = 'isArray';
|
|
322
|
+
const isArrayProperty = factory.createPropertyAssignment(isArrayKey, factory.createIdentifier('true'));
|
|
323
|
+
return [enumProperty, isArrayProperty];
|
|
324
|
+
}
|
|
325
|
+
return enumProperty;
|
|
326
|
+
}
|
|
327
|
+
createDefaultPropertyAssignment(factory, node, existingProperties, options) {
|
|
328
|
+
var _a;
|
|
329
|
+
const key = 'default';
|
|
330
|
+
if (options.skipDefaultValues) {
|
|
331
|
+
return undefined;
|
|
332
|
+
}
|
|
333
|
+
if ((0, plugin_utils_1.hasPropertyKey)(key, existingProperties)) {
|
|
334
|
+
return undefined;
|
|
335
|
+
}
|
|
336
|
+
if (ts.isPropertySignature(node)) {
|
|
337
|
+
return undefined;
|
|
338
|
+
}
|
|
339
|
+
if (node.initializer == null) {
|
|
340
|
+
return undefined;
|
|
341
|
+
}
|
|
342
|
+
let initializer = node.initializer;
|
|
343
|
+
if (ts.isAsExpression(initializer)) {
|
|
344
|
+
initializer = initializer.expression;
|
|
345
|
+
}
|
|
346
|
+
initializer =
|
|
347
|
+
(_a = this.clonePrimitiveLiteral(factory, initializer)) !== null && _a !== void 0 ? _a : initializer;
|
|
348
|
+
if (!(0, plugin_utils_1.canReferenceNode)(initializer, options)) {
|
|
349
|
+
const parentFilePath = node.getSourceFile().fileName;
|
|
350
|
+
const propertyName = node.name.getText();
|
|
351
|
+
plugin_debug_logger_1.pluginDebugLogger.debug(`Skipping registering default value for "${propertyName}" property in "${parentFilePath}" file because it is not a referenceable value ("${initializer.getText()}").`);
|
|
352
|
+
return undefined;
|
|
353
|
+
}
|
|
354
|
+
return factory.createPropertyAssignment(key, initializer);
|
|
355
|
+
}
|
|
356
|
+
createValidationPropertyAssignments(factory, node, options) {
|
|
357
|
+
const assignments = [];
|
|
358
|
+
const decorators = ts.canHaveDecorators(node) && ts.getDecorators(node);
|
|
359
|
+
if (!options.readonly) {
|
|
360
|
+
this.addPropertyByValidationDecorator(factory, 'IsIn', 'enum', decorators, assignments, options);
|
|
361
|
+
}
|
|
362
|
+
decorators_properties_1.decoratorsProperties.forEach((decoratorProperty) => {
|
|
363
|
+
if (decoratorProperty.mappingType === decorators_properties_1.decoratorsPropertiesMappingType.DIRECT) {
|
|
364
|
+
this.addPropertyByValidationDecorator(factory, decoratorProperty.decorator, decoratorProperty.property, decorators, assignments, options);
|
|
365
|
+
}
|
|
366
|
+
else if (decoratorProperty.mappingType ===
|
|
367
|
+
decorators_properties_1.decoratorsPropertiesMappingType.INDIRECT_VALUE) {
|
|
368
|
+
this.addPropertiesByValidationDecorator(factory, decoratorProperty.decorator, decorators, assignments, () => {
|
|
369
|
+
return [
|
|
370
|
+
factory.createPropertyAssignment(decoratorProperty.property, (0, ast_utils_1.createPrimitiveLiteral)(factory, decoratorProperty.value))
|
|
371
|
+
];
|
|
372
|
+
});
|
|
373
|
+
}
|
|
374
|
+
else if (decoratorProperty.mappingType ===
|
|
375
|
+
decorators_properties_1.decoratorsPropertiesMappingType.INDIRECT_ARGUMENT) {
|
|
376
|
+
this.addPropertiesByValidationDecorator(factory, decoratorProperty.decorator, decorators, assignments, (decoratorRef) => {
|
|
377
|
+
const decoratorArguments = (0, ast_utils_1.getDecoratorArguments)(decoratorRef);
|
|
378
|
+
const result = [];
|
|
379
|
+
const argumentValue = (0, lodash_1.head)(decoratorArguments);
|
|
380
|
+
if (!(0, plugin_utils_1.canReferenceNode)(argumentValue, options)) {
|
|
381
|
+
return result;
|
|
382
|
+
}
|
|
383
|
+
const clonedArgumentValue = this.clonePrimitiveLiteral(factory, argumentValue);
|
|
384
|
+
if (clonedArgumentValue) {
|
|
385
|
+
result.push(factory.createPropertyAssignment(decoratorProperty.property, clonedArgumentValue));
|
|
386
|
+
}
|
|
387
|
+
return result;
|
|
388
|
+
});
|
|
389
|
+
}
|
|
390
|
+
});
|
|
391
|
+
this.addPropertiesByValidationDecorator(factory, 'Length', decorators, assignments, (decoratorRef) => {
|
|
392
|
+
var _a, _b;
|
|
393
|
+
const decoratorArguments = (0, ast_utils_1.getDecoratorArguments)(decoratorRef);
|
|
394
|
+
const result = [];
|
|
395
|
+
const minLength = (0, lodash_1.head)(decoratorArguments);
|
|
396
|
+
if (!(0, plugin_utils_1.canReferenceNode)(minLength, options)) {
|
|
397
|
+
return result;
|
|
398
|
+
}
|
|
399
|
+
const clonedMinLength = (_a = this.clonePrimitiveLiteral(factory, minLength)) !== null && _a !== void 0 ? _a : minLength;
|
|
400
|
+
if (clonedMinLength) {
|
|
401
|
+
result.push(factory.createPropertyAssignment('minLength', clonedMinLength));
|
|
402
|
+
}
|
|
403
|
+
if (decoratorArguments.length > 1) {
|
|
404
|
+
const maxLength = decoratorArguments[1];
|
|
405
|
+
if (!(0, plugin_utils_1.canReferenceNode)(maxLength, options)) {
|
|
406
|
+
return result;
|
|
407
|
+
}
|
|
408
|
+
const clonedMaxLength = (_b = this.clonePrimitiveLiteral(factory, maxLength)) !== null && _b !== void 0 ? _b : maxLength;
|
|
409
|
+
if (clonedMaxLength) {
|
|
410
|
+
result.push(factory.createPropertyAssignment('maxLength', clonedMaxLength));
|
|
411
|
+
}
|
|
412
|
+
}
|
|
413
|
+
return result;
|
|
414
|
+
});
|
|
415
|
+
this.addPropertiesByValidationDecorator(factory, 'Matches', decorators, assignments, (decoratorRef) => {
|
|
416
|
+
const decoratorArguments = (0, ast_utils_1.getDecoratorArguments)(decoratorRef);
|
|
417
|
+
return [
|
|
418
|
+
factory.createPropertyAssignment('pattern', (0, ast_utils_1.createPrimitiveLiteral)(factory, (0, lodash_1.head)(decoratorArguments).text))
|
|
419
|
+
];
|
|
420
|
+
});
|
|
421
|
+
return assignments;
|
|
422
|
+
}
|
|
423
|
+
addPropertyByValidationDecorator(factory, decoratorName, propertyKey, decorators, assignments, options) {
|
|
424
|
+
this.addPropertiesByValidationDecorator(factory, decoratorName, decorators, assignments, (decoratorRef) => {
|
|
425
|
+
var _a;
|
|
426
|
+
const argument = (0, lodash_1.head)((0, ast_utils_1.getDecoratorArguments)(decoratorRef));
|
|
427
|
+
const assignment = (_a = this.clonePrimitiveLiteral(factory, argument)) !== null && _a !== void 0 ? _a : argument;
|
|
428
|
+
if (!(0, plugin_utils_1.canReferenceNode)(assignment, options)) {
|
|
429
|
+
return [];
|
|
430
|
+
}
|
|
431
|
+
return [factory.createPropertyAssignment(propertyKey, assignment)];
|
|
432
|
+
});
|
|
433
|
+
}
|
|
434
|
+
addPropertiesByValidationDecorator(factory, decoratorName, decorators, assignments, addPropertyAssignments) {
|
|
435
|
+
const decoratorRef = (0, plugin_utils_1.getDecoratorOrUndefinedByNames)([decoratorName], decorators, factory);
|
|
436
|
+
if (!decoratorRef) {
|
|
437
|
+
return;
|
|
438
|
+
}
|
|
439
|
+
assignments.push(...addPropertyAssignments(decoratorRef));
|
|
440
|
+
}
|
|
441
|
+
addClassMetadata(node, objectLiteral, sourceFile, metadata) {
|
|
442
|
+
const hostClass = node.parent;
|
|
443
|
+
const className = hostClass.name && hostClass.name.getText();
|
|
444
|
+
if (!className) {
|
|
445
|
+
return;
|
|
446
|
+
}
|
|
447
|
+
const propertyName = node.name && node.name.getText(sourceFile);
|
|
448
|
+
if (!propertyName ||
|
|
449
|
+
(node.name && node.name.kind === ts.SyntaxKind.ComputedPropertyName)) {
|
|
450
|
+
return;
|
|
451
|
+
}
|
|
452
|
+
metadata[propertyName] = objectLiteral;
|
|
453
|
+
}
|
|
454
|
+
createDescriptionAndTsDocTagPropertyAssignments(factory, node, typeChecker, existingProperties = factory.createNodeArray(), options = {}, sourceFile) {
|
|
455
|
+
var _a;
|
|
456
|
+
if (!options.introspectComments || !sourceFile) {
|
|
457
|
+
return [];
|
|
458
|
+
}
|
|
459
|
+
const propertyAssignments = [];
|
|
460
|
+
const comments = (0, ast_utils_1.getMainCommentOfNode)(node);
|
|
461
|
+
const tags = (0, ast_utils_1.getTsDocTagsOfNode)(node, typeChecker);
|
|
462
|
+
const keyOfComment = options.dtoKeyOfComment;
|
|
463
|
+
if (!(0, plugin_utils_1.hasPropertyKey)(keyOfComment, existingProperties) && comments) {
|
|
464
|
+
const descriptionPropertyAssignment = factory.createPropertyAssignment(keyOfComment, factory.createStringLiteral(comments));
|
|
465
|
+
propertyAssignments.push(descriptionPropertyAssignment);
|
|
466
|
+
}
|
|
467
|
+
const hasExampleOrExamplesKey = (0, plugin_utils_1.hasPropertyKey)('example', existingProperties) ||
|
|
468
|
+
(0, plugin_utils_1.hasPropertyKey)('examples', existingProperties);
|
|
469
|
+
if (!hasExampleOrExamplesKey && ((_a = tags.example) === null || _a === void 0 ? void 0 : _a.length)) {
|
|
470
|
+
if (tags.example.length === 1) {
|
|
471
|
+
const examplePropertyAssignment = factory.createPropertyAssignment('example', (0, ast_utils_1.createLiteralFromAnyValue)(factory, tags.example[0]));
|
|
472
|
+
propertyAssignments.push(examplePropertyAssignment);
|
|
473
|
+
}
|
|
474
|
+
else {
|
|
475
|
+
const examplesPropertyAssignment = factory.createPropertyAssignment('examples', (0, ast_utils_1.createLiteralFromAnyValue)(factory, tags.example));
|
|
476
|
+
propertyAssignments.push(examplesPropertyAssignment);
|
|
477
|
+
}
|
|
478
|
+
}
|
|
479
|
+
const hasDeprecatedKey = (0, plugin_utils_1.hasPropertyKey)('deprecated', existingProperties);
|
|
480
|
+
if (!hasDeprecatedKey && tags.deprecated) {
|
|
481
|
+
const deprecatedPropertyAssignment = factory.createPropertyAssignment('deprecated', (0, ast_utils_1.createLiteralFromAnyValue)(factory, tags.deprecated));
|
|
482
|
+
propertyAssignments.push(deprecatedPropertyAssignment);
|
|
483
|
+
}
|
|
484
|
+
return propertyAssignments;
|
|
485
|
+
}
|
|
486
|
+
normalizeImportPath(pathToSource, path) {
|
|
487
|
+
let relativePath = path_1.posix.relative((0, plugin_utils_1.convertPath)(pathToSource), (0, plugin_utils_1.convertPath)(path));
|
|
488
|
+
relativePath = relativePath[0] !== '.' ? './' + relativePath : relativePath;
|
|
489
|
+
return relativePath;
|
|
490
|
+
}
|
|
491
|
+
clonePrimitiveLiteral(factory, node) {
|
|
492
|
+
var _a;
|
|
493
|
+
const primitiveTypeName = this.getInitializerPrimitiveTypeName(node);
|
|
494
|
+
if (!primitiveTypeName) {
|
|
495
|
+
return undefined;
|
|
496
|
+
}
|
|
497
|
+
const text = (_a = node.text) !== null && _a !== void 0 ? _a : node.getText();
|
|
498
|
+
return (0, ast_utils_1.createPrimitiveLiteral)(factory, text, primitiveTypeName);
|
|
499
|
+
}
|
|
500
|
+
getInitializerPrimitiveTypeName(node) {
|
|
501
|
+
if (ts.isIdentifier(node) &&
|
|
502
|
+
(node.text === 'true' || node.text === 'false')) {
|
|
503
|
+
return 'boolean';
|
|
504
|
+
}
|
|
505
|
+
if (ts.isNumericLiteral(node) || ts.isPrefixUnaryExpression(node)) {
|
|
506
|
+
return 'number';
|
|
507
|
+
}
|
|
508
|
+
if (ts.isStringLiteral(node)) {
|
|
509
|
+
return 'string';
|
|
510
|
+
}
|
|
511
|
+
return undefined;
|
|
512
|
+
}
|
|
513
|
+
}
|
|
514
|
+
exports.ModelClassVisitor = ModelClassVisitor;
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
import * as ts from 'typescript';
|
|
2
|
+
import { PluginOptions } from '../merge-options';
|
|
3
|
+
export declare class ReadonlyVisitor {
|
|
4
|
+
private readonly options;
|
|
5
|
+
readonly key = "@nestjs/swagger";
|
|
6
|
+
private readonly modelClassVisitor;
|
|
7
|
+
private readonly controllerClassVisitor;
|
|
8
|
+
get typeImports(): {
|
|
9
|
+
[x: string]: string;
|
|
10
|
+
};
|
|
11
|
+
constructor(options: PluginOptions);
|
|
12
|
+
visit(program: ts.Program, sf: ts.SourceFile): ts.Node;
|
|
13
|
+
collect(): {
|
|
14
|
+
models: [ts.CallExpression, Record<string, {
|
|
15
|
+
[x: string]: ts.ObjectLiteralExpression;
|
|
16
|
+
}>][];
|
|
17
|
+
controllers: [ts.CallExpression, Record<string, {
|
|
18
|
+
[x: string]: ts.ObjectLiteralExpression;
|
|
19
|
+
}>][];
|
|
20
|
+
};
|
|
21
|
+
}
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.ReadonlyVisitor = void 0;
|
|
4
|
+
const ts = require("typescript");
|
|
5
|
+
const merge_options_1 = require("../merge-options");
|
|
6
|
+
const is_filename_matched_util_1 = require("../utils/is-filename-matched.util");
|
|
7
|
+
const controller_class_visitor_1 = require("./controller-class.visitor");
|
|
8
|
+
const model_class_visitor_1 = require("./model-class.visitor");
|
|
9
|
+
class ReadonlyVisitor {
|
|
10
|
+
get typeImports() {
|
|
11
|
+
return Object.assign(Object.assign({}, this.modelClassVisitor.typeImports), this.controllerClassVisitor.typeImports);
|
|
12
|
+
}
|
|
13
|
+
constructor(options) {
|
|
14
|
+
this.options = options;
|
|
15
|
+
this.key = '@nestjs/swagger';
|
|
16
|
+
this.modelClassVisitor = new model_class_visitor_1.ModelClassVisitor();
|
|
17
|
+
this.controllerClassVisitor = new controller_class_visitor_1.ControllerClassVisitor();
|
|
18
|
+
options.readonly = true;
|
|
19
|
+
if (!options.pathToSource) {
|
|
20
|
+
throw new Error(`"pathToSource" must be defined in plugin options`);
|
|
21
|
+
}
|
|
22
|
+
}
|
|
23
|
+
visit(program, sf) {
|
|
24
|
+
const factoryHost = { factory: ts.factory };
|
|
25
|
+
const parsedOptions = (0, merge_options_1.mergePluginOptions)(this.options);
|
|
26
|
+
if ((0, is_filename_matched_util_1.isFilenameMatched)(parsedOptions.dtoFileNameSuffix, sf.fileName)) {
|
|
27
|
+
return this.modelClassVisitor.visit(sf, factoryHost, program, parsedOptions);
|
|
28
|
+
}
|
|
29
|
+
if ((0, is_filename_matched_util_1.isFilenameMatched)(parsedOptions.controllerFileNameSuffix, sf.fileName)) {
|
|
30
|
+
return this.controllerClassVisitor.visit(sf, factoryHost, program, parsedOptions);
|
|
31
|
+
}
|
|
32
|
+
}
|
|
33
|
+
collect() {
|
|
34
|
+
return {
|
|
35
|
+
models: this.modelClassVisitor.collectedMetadata(this.options),
|
|
36
|
+
controllers: this.controllerClassVisitor.collectedMetadata(this.options)
|
|
37
|
+
};
|
|
38
|
+
}
|
|
39
|
+
}
|
|
40
|
+
exports.ReadonlyVisitor = ReadonlyVisitor;
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export declare const BUILT_IN_TYPES: (ArrayConstructor | ObjectConstructor | NumberConstructor | StringConstructor | BooleanConstructor)[];
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
export declare enum decoratorsPropertiesMappingType {
|
|
2
|
+
DIRECT = 0,
|
|
3
|
+
INDIRECT_VALUE = 1,
|
|
4
|
+
INDIRECT_ARGUMENT = 2
|
|
5
|
+
}
|
|
6
|
+
export declare const decoratorsProperties: ({
|
|
7
|
+
mappingType: decoratorsPropertiesMappingType;
|
|
8
|
+
decorator: string;
|
|
9
|
+
property: string;
|
|
10
|
+
value: number;
|
|
11
|
+
} | {
|
|
12
|
+
mappingType: decoratorsPropertiesMappingType;
|
|
13
|
+
decorator: string;
|
|
14
|
+
property: string;
|
|
15
|
+
value: boolean;
|
|
16
|
+
} | {
|
|
17
|
+
mappingType: decoratorsPropertiesMappingType;
|
|
18
|
+
decorator: string;
|
|
19
|
+
property: string;
|
|
20
|
+
value: string;
|
|
21
|
+
})[];
|