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,326 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.SwaggerExplorer = void 0;
|
|
4
|
+
const common_1 = require("@nestjs/common");
|
|
5
|
+
const constants_1 = require("@nestjs/common/constants");
|
|
6
|
+
const interfaces_1 = require("@nestjs/common/interfaces");
|
|
7
|
+
const shared_utils_1 = require("@nestjs/common/utils/shared.utils");
|
|
8
|
+
const core_1 = require("@nestjs/core");
|
|
9
|
+
const legacy_route_converter_1 = require("@nestjs/core/router/legacy-route-converter");
|
|
10
|
+
const route_path_factory_1 = require("@nestjs/core/router/route-path-factory");
|
|
11
|
+
const lodash_1 = require("lodash");
|
|
12
|
+
const path_to_regexp_1 = require("path-to-regexp");
|
|
13
|
+
const constants_2 = require("./constants");
|
|
14
|
+
const api_callbacks_explorer_1 = require("./explorers/api-callbacks.explorer");
|
|
15
|
+
const api_exclude_controller_explorer_1 = require("./explorers/api-exclude-controller.explorer");
|
|
16
|
+
const api_exclude_endpoint_explorer_1 = require("./explorers/api-exclude-endpoint.explorer");
|
|
17
|
+
const api_extra_models_explorer_1 = require("./explorers/api-extra-models.explorer");
|
|
18
|
+
const api_headers_explorer_1 = require("./explorers/api-headers.explorer");
|
|
19
|
+
const api_operation_explorer_1 = require("./explorers/api-operation.explorer");
|
|
20
|
+
const api_parameters_explorer_1 = require("./explorers/api-parameters.explorer");
|
|
21
|
+
const api_response_explorer_1 = require("./explorers/api-response.explorer");
|
|
22
|
+
const api_security_explorer_1 = require("./explorers/api-security.explorer");
|
|
23
|
+
const api_use_tags_explorer_1 = require("./explorers/api-use-tags.explorer");
|
|
24
|
+
const mimetype_content_wrapper_1 = require("./services/mimetype-content-wrapper");
|
|
25
|
+
const is_body_parameter_util_1 = require("./utils/is-body-parameter.util");
|
|
26
|
+
const merge_and_uniq_util_1 = require("./utils/merge-and-uniq.util");
|
|
27
|
+
class SwaggerExplorer {
|
|
28
|
+
constructor(schemaObjectFactory, options = {}) {
|
|
29
|
+
this.schemaObjectFactory = schemaObjectFactory;
|
|
30
|
+
this.options = options;
|
|
31
|
+
this.mimetypeContentWrapper = new mimetype_content_wrapper_1.MimetypeContentWrapper();
|
|
32
|
+
this.metadataScanner = new core_1.MetadataScanner();
|
|
33
|
+
this.schemas = {};
|
|
34
|
+
this.operationIdFactory = (controllerKey, methodKey, version) => version
|
|
35
|
+
? controllerKey
|
|
36
|
+
? `${controllerKey}_${methodKey}_${version}`
|
|
37
|
+
: `${methodKey}_${version}`
|
|
38
|
+
: controllerKey
|
|
39
|
+
? `${controllerKey}_${methodKey}`
|
|
40
|
+
: methodKey;
|
|
41
|
+
this.linkNameFactory = (controllerKey, methodKey, fieldKey) => controllerKey
|
|
42
|
+
? `${controllerKey}_${methodKey}_from_${fieldKey}`
|
|
43
|
+
: `${methodKey}_from_${fieldKey}`;
|
|
44
|
+
}
|
|
45
|
+
exploreController(wrapper, applicationConfig, options) {
|
|
46
|
+
const { operationIdFactory, linkNameFactory } = options;
|
|
47
|
+
this.routePathFactory = new route_path_factory_1.RoutePathFactory(applicationConfig);
|
|
48
|
+
if (operationIdFactory) {
|
|
49
|
+
this.operationIdFactory = operationIdFactory;
|
|
50
|
+
}
|
|
51
|
+
if (linkNameFactory) {
|
|
52
|
+
this.linkNameFactory = linkNameFactory;
|
|
53
|
+
}
|
|
54
|
+
const { instance, metatype } = wrapper;
|
|
55
|
+
const prototype = Object.getPrototypeOf(instance);
|
|
56
|
+
const documentResolvers = {
|
|
57
|
+
root: [
|
|
58
|
+
this.exploreRoutePathAndMethod,
|
|
59
|
+
api_operation_explorer_1.exploreApiOperationMetadata,
|
|
60
|
+
api_parameters_explorer_1.exploreApiParametersMetadata.bind(null, this.schemas)
|
|
61
|
+
],
|
|
62
|
+
security: [api_security_explorer_1.exploreApiSecurityMetadata],
|
|
63
|
+
tags: [api_use_tags_explorer_1.exploreApiTagsMetadata],
|
|
64
|
+
callbacks: [api_callbacks_explorer_1.exploreApiCallbacksMetadata],
|
|
65
|
+
responses: [
|
|
66
|
+
api_response_explorer_1.exploreApiResponseMetadata.bind(null, this.schemas, {
|
|
67
|
+
operationId: this.operationIdFactory,
|
|
68
|
+
linkName: this.linkNameFactory
|
|
69
|
+
})
|
|
70
|
+
]
|
|
71
|
+
};
|
|
72
|
+
return this.generateDenormalizedDocument(metatype, prototype, instance, documentResolvers, applicationConfig, options);
|
|
73
|
+
}
|
|
74
|
+
getSchemas() {
|
|
75
|
+
return this.schemas;
|
|
76
|
+
}
|
|
77
|
+
generateDenormalizedDocument(metatype, prototype, instance, documentResolvers, applicationConfig, options) {
|
|
78
|
+
const self = this;
|
|
79
|
+
const excludeController = (0, api_exclude_controller_explorer_1.exploreApiExcludeControllerMetadata)(metatype);
|
|
80
|
+
if (excludeController) {
|
|
81
|
+
return [];
|
|
82
|
+
}
|
|
83
|
+
const globalMetadata = this.exploreGlobalMetadata(metatype, {
|
|
84
|
+
autoTagControllers: options.autoTagControllers
|
|
85
|
+
});
|
|
86
|
+
const ctrlExtraModels = (0, api_extra_models_explorer_1.exploreGlobalApiExtraModelsMetadata)(metatype);
|
|
87
|
+
this.registerExtraModels(ctrlExtraModels);
|
|
88
|
+
const denormalizedPaths = this.metadataScanner.scanFromPrototype(instance, prototype, (name) => {
|
|
89
|
+
const targetCallback = prototype[name];
|
|
90
|
+
const excludeEndpoint = (0, api_exclude_endpoint_explorer_1.exploreApiExcludeEndpointMetadata)(instance, prototype, targetCallback);
|
|
91
|
+
if (excludeEndpoint && excludeEndpoint.disable) {
|
|
92
|
+
return;
|
|
93
|
+
}
|
|
94
|
+
const ctrlExtraModels = (0, api_extra_models_explorer_1.exploreApiExtraModelsMetadata)(instance, prototype, targetCallback);
|
|
95
|
+
this.registerExtraModels(ctrlExtraModels);
|
|
96
|
+
const methodMetadata = (0, lodash_1.mapValues)(documentResolvers, (explorers) => explorers.reduce((metadata, fn) => {
|
|
97
|
+
const exploredMetadata = fn.call(self, instance, prototype, targetCallback, metatype, options.globalPrefix, options.modulePath, applicationConfig, options.autoTagControllers);
|
|
98
|
+
if (!exploredMetadata) {
|
|
99
|
+
return metadata;
|
|
100
|
+
}
|
|
101
|
+
if (!(0, lodash_1.isArray)(exploredMetadata)) {
|
|
102
|
+
if (Array.isArray(metadata)) {
|
|
103
|
+
return metadata.map((item) => (Object.assign(Object.assign({}, item), exploredMetadata)));
|
|
104
|
+
}
|
|
105
|
+
return Object.assign(Object.assign({}, metadata), exploredMetadata);
|
|
106
|
+
}
|
|
107
|
+
return (0, lodash_1.isArray)(metadata)
|
|
108
|
+
? [...metadata, ...exploredMetadata]
|
|
109
|
+
: exploredMetadata;
|
|
110
|
+
}, {}));
|
|
111
|
+
if (Array.isArray(methodMetadata.root)) {
|
|
112
|
+
return methodMetadata.root.map((endpointMetadata) => {
|
|
113
|
+
endpointMetadata = (0, lodash_1.cloneDeep)(Object.assign(Object.assign({}, methodMetadata), { root: endpointMetadata }));
|
|
114
|
+
const mergedMethodMetadata = this.mergeMetadata(globalMetadata, (0, lodash_1.omitBy)(endpointMetadata, lodash_1.isEmpty));
|
|
115
|
+
return this.migrateOperationSchema(Object.assign(Object.assign({ responses: {} }, (0, lodash_1.omit)(globalMetadata, 'chunks')), mergedMethodMetadata), prototype, targetCallback);
|
|
116
|
+
});
|
|
117
|
+
}
|
|
118
|
+
const mergedMethodMetadata = this.mergeMetadata(globalMetadata, (0, lodash_1.omitBy)(methodMetadata, lodash_1.isEmpty));
|
|
119
|
+
return [
|
|
120
|
+
this.migrateOperationSchema(Object.assign(Object.assign({ responses: {} }, (0, lodash_1.omit)(globalMetadata, 'chunks')), mergedMethodMetadata), prototype, targetCallback)
|
|
121
|
+
];
|
|
122
|
+
});
|
|
123
|
+
return (0, lodash_1.flatten)(denormalizedPaths).filter((path) => { var _a; return (_a = path.root) === null || _a === void 0 ? void 0 : _a.path; });
|
|
124
|
+
}
|
|
125
|
+
exploreGlobalMetadata(metatype, options) {
|
|
126
|
+
const globalExplorers = [
|
|
127
|
+
(0, api_use_tags_explorer_1.exploreGlobalApiTagsMetadata)(options.autoTagControllers),
|
|
128
|
+
api_security_explorer_1.exploreGlobalApiSecurityMetadata,
|
|
129
|
+
api_response_explorer_1.exploreGlobalApiResponseMetadata.bind(null, this.schemas),
|
|
130
|
+
api_headers_explorer_1.exploreGlobalApiHeaderMetadata
|
|
131
|
+
];
|
|
132
|
+
const globalMetadata = globalExplorers
|
|
133
|
+
.map((explorer) => explorer.call(explorer, metatype))
|
|
134
|
+
.filter((val) => !(0, shared_utils_1.isUndefined)(val))
|
|
135
|
+
.reduce((curr, next) => {
|
|
136
|
+
if (next.depth) {
|
|
137
|
+
return Object.assign(Object.assign({}, curr), { chunks: (curr.chunks || []).concat(next) });
|
|
138
|
+
}
|
|
139
|
+
return Object.assign(Object.assign({}, curr), next);
|
|
140
|
+
}, {});
|
|
141
|
+
return globalMetadata;
|
|
142
|
+
}
|
|
143
|
+
exploreRoutePathAndMethod(instance, prototype, method, metatype, globalPrefix, modulePath, applicationConfig) {
|
|
144
|
+
const methodPath = Reflect.getMetadata(constants_1.PATH_METADATA, method);
|
|
145
|
+
if ((0, shared_utils_1.isUndefined)(methodPath)) {
|
|
146
|
+
return undefined;
|
|
147
|
+
}
|
|
148
|
+
const requestMethodMetadata = Reflect.getMetadata(constants_1.METHOD_METADATA, method);
|
|
149
|
+
const isQueryMethod = Reflect.getMetadata(constants_2.DECORATORS.API_QUERY_METHOD, method);
|
|
150
|
+
const requestMethodForPathFactory = typeof requestMethodMetadata === 'number'
|
|
151
|
+
? requestMethodMetadata
|
|
152
|
+
:
|
|
153
|
+
common_1.RequestMethod.GET;
|
|
154
|
+
const methodVersion = Reflect.getMetadata(constants_1.VERSION_METADATA, method);
|
|
155
|
+
const versioningOptions = applicationConfig.getVersioning();
|
|
156
|
+
const controllerVersion = this.getVersionMetadata(metatype, versioningOptions);
|
|
157
|
+
const versionOrVersions = methodVersion !== null && methodVersion !== void 0 ? methodVersion : controllerVersion;
|
|
158
|
+
const versions = this.getRoutePathVersions(versionOrVersions, versioningOptions);
|
|
159
|
+
const allRoutePaths = this.routePathFactory.create({
|
|
160
|
+
methodPath,
|
|
161
|
+
methodVersion,
|
|
162
|
+
modulePath,
|
|
163
|
+
globalPrefix,
|
|
164
|
+
controllerVersion,
|
|
165
|
+
ctrlPath: this.reflectControllerPath(metatype),
|
|
166
|
+
versioningOptions: applicationConfig.getVersioning()
|
|
167
|
+
}, requestMethodForPathFactory);
|
|
168
|
+
return (0, lodash_1.flatten)(allRoutePaths.map((routePath, index) => {
|
|
169
|
+
const fullPath = this.validateRoutePath(routePath);
|
|
170
|
+
const apiExtension = Reflect.getMetadata(constants_2.DECORATORS.API_EXTENSION, method);
|
|
171
|
+
if (!isQueryMethod &&
|
|
172
|
+
typeof requestMethodMetadata === 'number' &&
|
|
173
|
+
requestMethodMetadata === common_1.RequestMethod.ALL) {
|
|
174
|
+
const validMethods = [
|
|
175
|
+
'get',
|
|
176
|
+
'post',
|
|
177
|
+
'put',
|
|
178
|
+
'delete',
|
|
179
|
+
'patch',
|
|
180
|
+
'options',
|
|
181
|
+
'head',
|
|
182
|
+
'search'
|
|
183
|
+
];
|
|
184
|
+
return validMethods.map((requestMethod) => (Object.assign({ method: requestMethod, path: fullPath === '' ? '/' : fullPath, operationId: `${this.getOperationId(instance, method.name)}_${requestMethod.toLowerCase()}` }, apiExtension)));
|
|
185
|
+
}
|
|
186
|
+
const pathVersion = versions.find((v) => fullPath.includes(`/${v}/`) || fullPath.endsWith(`/${v}`));
|
|
187
|
+
const isAlias = allRoutePaths.length > 1 && allRoutePaths.length !== versions.length;
|
|
188
|
+
const methodKey = isAlias ? `${method.name}[${index}]` : method.name;
|
|
189
|
+
const httpMethodKey = isQueryMethod
|
|
190
|
+
? 'query'
|
|
191
|
+
: typeof requestMethodMetadata === 'string'
|
|
192
|
+
? requestMethodMetadata.toLowerCase()
|
|
193
|
+
: common_1.RequestMethod[requestMethodForPathFactory].toLowerCase();
|
|
194
|
+
return Object.assign({ method: httpMethodKey, path: fullPath === '' ? '/' : fullPath, operationId: this.getOperationId(instance, methodKey, pathVersion) }, apiExtension);
|
|
195
|
+
}));
|
|
196
|
+
}
|
|
197
|
+
getOperationId(instance, methodKey, version) {
|
|
198
|
+
var _a;
|
|
199
|
+
return this.operationIdFactory(((_a = instance.constructor) === null || _a === void 0 ? void 0 : _a.name) || '', methodKey, version);
|
|
200
|
+
}
|
|
201
|
+
getRoutePathVersions(versionValue, versioningOptions) {
|
|
202
|
+
let versions = [];
|
|
203
|
+
if (!versionValue || (versioningOptions === null || versioningOptions === void 0 ? void 0 : versioningOptions.type) !== common_1.VersioningType.URI) {
|
|
204
|
+
return versions;
|
|
205
|
+
}
|
|
206
|
+
if (Array.isArray(versionValue)) {
|
|
207
|
+
versions = versionValue.filter((v) => v !== interfaces_1.VERSION_NEUTRAL);
|
|
208
|
+
}
|
|
209
|
+
else if (versionValue !== interfaces_1.VERSION_NEUTRAL) {
|
|
210
|
+
versions = [versionValue];
|
|
211
|
+
}
|
|
212
|
+
const prefix = this.routePathFactory.getVersionPrefix(versioningOptions);
|
|
213
|
+
versions = versions.map((v) => `${prefix}${v}`);
|
|
214
|
+
return versions;
|
|
215
|
+
}
|
|
216
|
+
reflectControllerPath(metatype) {
|
|
217
|
+
return Reflect.getMetadata(constants_1.PATH_METADATA, metatype);
|
|
218
|
+
}
|
|
219
|
+
validateRoutePath(path) {
|
|
220
|
+
if ((0, shared_utils_1.isUndefined)(path)) {
|
|
221
|
+
return '';
|
|
222
|
+
}
|
|
223
|
+
if (Array.isArray(path)) {
|
|
224
|
+
path = (0, lodash_1.head)(path);
|
|
225
|
+
}
|
|
226
|
+
let pathWithParams = '';
|
|
227
|
+
try {
|
|
228
|
+
let normalizedPath = legacy_route_converter_1.LegacyRouteConverter.tryConvert(path, {
|
|
229
|
+
logs: this.options.httpAdapterType !== 'fastify'
|
|
230
|
+
});
|
|
231
|
+
normalizedPath = normalizedPath.replace(/::/g, '\\:');
|
|
232
|
+
normalizedPath = normalizedPath.replace(/\[:\]/g, '\\:');
|
|
233
|
+
normalizedPath = normalizedPath.replace(/\(\^([^)]+)\)/g, '');
|
|
234
|
+
const { tokens } = (0, path_to_regexp_1.parse)(normalizedPath);
|
|
235
|
+
for (const item of tokens) {
|
|
236
|
+
if (item.type === 'text') {
|
|
237
|
+
pathWithParams += item.value;
|
|
238
|
+
}
|
|
239
|
+
else if (item.type === 'param') {
|
|
240
|
+
pathWithParams += `{${item.name}}`;
|
|
241
|
+
}
|
|
242
|
+
else if (item.type === 'wildcard') {
|
|
243
|
+
pathWithParams += `{${item.name}}`;
|
|
244
|
+
}
|
|
245
|
+
else if (item.type === 'group') {
|
|
246
|
+
pathWithParams += item.tokens.reduce((acc, item) => acc +
|
|
247
|
+
(item.type === 'text'
|
|
248
|
+
? item.value
|
|
249
|
+
: `{${item.name}}`), '');
|
|
250
|
+
}
|
|
251
|
+
}
|
|
252
|
+
}
|
|
253
|
+
catch (err) {
|
|
254
|
+
if (err instanceof TypeError) {
|
|
255
|
+
legacy_route_converter_1.LegacyRouteConverter.printError(path);
|
|
256
|
+
}
|
|
257
|
+
throw err;
|
|
258
|
+
}
|
|
259
|
+
return pathWithParams === '/' ? '' : (0, shared_utils_1.addLeadingSlash)(pathWithParams);
|
|
260
|
+
}
|
|
261
|
+
mergeMetadata(globalMetadata, methodMetadata) {
|
|
262
|
+
if (methodMetadata.root && !methodMetadata.root.parameters) {
|
|
263
|
+
methodMetadata.root.parameters = [];
|
|
264
|
+
}
|
|
265
|
+
const deepMerge = (metadata) => (value, key) => {
|
|
266
|
+
if (!metadata[key]) {
|
|
267
|
+
return value;
|
|
268
|
+
}
|
|
269
|
+
const globalValue = metadata[key];
|
|
270
|
+
if (metadata.depth) {
|
|
271
|
+
return this.deepMergeMetadata(globalValue, value, metadata.depth);
|
|
272
|
+
}
|
|
273
|
+
return this.mergeValues(globalValue, value);
|
|
274
|
+
};
|
|
275
|
+
if (globalMetadata.chunks) {
|
|
276
|
+
const { chunks } = globalMetadata;
|
|
277
|
+
chunks.forEach((chunk) => {
|
|
278
|
+
methodMetadata = (0, lodash_1.mapValues)(methodMetadata, deepMerge(chunk));
|
|
279
|
+
});
|
|
280
|
+
}
|
|
281
|
+
return (0, lodash_1.mapValues)(methodMetadata, deepMerge(globalMetadata));
|
|
282
|
+
}
|
|
283
|
+
deepMergeMetadata(globalValue, methodValue, maxDepth, currentDepthLevel = 0) {
|
|
284
|
+
if (currentDepthLevel === maxDepth) {
|
|
285
|
+
return this.mergeValues(globalValue, methodValue);
|
|
286
|
+
}
|
|
287
|
+
return (0, lodash_1.mapValues)(methodValue, (value, key) => {
|
|
288
|
+
if (key in globalValue) {
|
|
289
|
+
return this.deepMergeMetadata(globalValue[key], methodValue[key], maxDepth, currentDepthLevel + 1);
|
|
290
|
+
}
|
|
291
|
+
return value;
|
|
292
|
+
});
|
|
293
|
+
}
|
|
294
|
+
mergeValues(globalValue, methodValue) {
|
|
295
|
+
if (!(0, lodash_1.isArray)(globalValue)) {
|
|
296
|
+
return Object.assign(Object.assign({}, globalValue), methodValue);
|
|
297
|
+
}
|
|
298
|
+
return [...globalValue, ...methodValue];
|
|
299
|
+
}
|
|
300
|
+
migrateOperationSchema(document, prototype, method) {
|
|
301
|
+
const parametersObject = (0, lodash_1.get)(document, 'root.parameters');
|
|
302
|
+
const requestBodyIndex = (parametersObject || []).findIndex(is_body_parameter_util_1.isBodyParameter);
|
|
303
|
+
if (requestBodyIndex < 0) {
|
|
304
|
+
return document;
|
|
305
|
+
}
|
|
306
|
+
const requestBody = parametersObject[requestBodyIndex];
|
|
307
|
+
parametersObject.splice(requestBodyIndex, 1);
|
|
308
|
+
const classConsumes = Reflect.getMetadata(constants_2.DECORATORS.API_CONSUMES, prototype);
|
|
309
|
+
const methodConsumes = Reflect.getMetadata(constants_2.DECORATORS.API_CONSUMES, method);
|
|
310
|
+
let consumes = (0, merge_and_uniq_util_1.mergeAndUniq)(classConsumes, methodConsumes);
|
|
311
|
+
consumes = (0, lodash_1.isEmpty)(consumes) ? ['application/json'] : consumes;
|
|
312
|
+
const keysToRemove = ['schema', 'in', 'name', 'examples'];
|
|
313
|
+
document.root.requestBody = Object.assign(Object.assign({}, (0, lodash_1.omit)(requestBody, keysToRemove)), this.mimetypeContentWrapper.wrap(consumes, (0, lodash_1.pick)(requestBody, ['schema', 'examples'])));
|
|
314
|
+
return document;
|
|
315
|
+
}
|
|
316
|
+
registerExtraModels(extraModels) {
|
|
317
|
+
extraModels.forEach((item) => this.schemaObjectFactory.exploreModelSchema(item, this.schemas));
|
|
318
|
+
}
|
|
319
|
+
getVersionMetadata(metatype, versioningOptions) {
|
|
320
|
+
var _a;
|
|
321
|
+
if ((versioningOptions === null || versioningOptions === void 0 ? void 0 : versioningOptions.type) === common_1.VersioningType.URI) {
|
|
322
|
+
return ((_a = Reflect.getMetadata(constants_1.VERSION_METADATA, metatype)) !== null && _a !== void 0 ? _a : versioningOptions.defaultVersion);
|
|
323
|
+
}
|
|
324
|
+
}
|
|
325
|
+
}
|
|
326
|
+
exports.SwaggerExplorer = SwaggerExplorer;
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
import { INestApplication } from '@nestjs/common';
|
|
2
|
+
import { HttpServer } from '@nestjs/common/interfaces/http/http-server.interface';
|
|
3
|
+
import { OpenAPIObject, SwaggerCustomOptions, SwaggerDocumentOptions } from './interfaces';
|
|
4
|
+
export declare class SwaggerModule {
|
|
5
|
+
private static readonly metadataLoader;
|
|
6
|
+
private static mergeTags;
|
|
7
|
+
static createDocument(app: INestApplication, config: Omit<OpenAPIObject, 'paths'>, options?: SwaggerDocumentOptions): OpenAPIObject;
|
|
8
|
+
static loadPluginMetadata(metadataFn: () => Promise<Record<string, any>>): Promise<void>;
|
|
9
|
+
protected static serveStatic(finalPath: string, app: INestApplication, customStaticPath?: string): void;
|
|
10
|
+
protected static serveDocuments(finalPath: string, urlLastSubdirectory: string, httpAdapter: HttpServer, documentOrFactory: OpenAPIObject | (() => OpenAPIObject), options: {
|
|
11
|
+
ui: boolean;
|
|
12
|
+
raw: boolean | Array<'json' | 'yaml'>;
|
|
13
|
+
jsonDocumentUrl: string;
|
|
14
|
+
yamlDocumentUrl: string;
|
|
15
|
+
swaggerOptions: SwaggerCustomOptions;
|
|
16
|
+
}): void;
|
|
17
|
+
protected static serveSwaggerUi(finalPath: string, urlLastSubdirectory: string, httpAdapter: HttpServer, getBuiltDocument: () => OpenAPIObject, swaggerOptions: SwaggerCustomOptions): void;
|
|
18
|
+
protected static serveDefinitions(httpAdapter: HttpServer, getBuiltDocument: () => OpenAPIObject, options: {
|
|
19
|
+
jsonDocumentUrl: string;
|
|
20
|
+
yamlDocumentUrl: string;
|
|
21
|
+
swaggerOptions: SwaggerCustomOptions;
|
|
22
|
+
}, serveOptions: {
|
|
23
|
+
serveJson: boolean;
|
|
24
|
+
serveYaml: boolean;
|
|
25
|
+
}): void;
|
|
26
|
+
static setup(path: string, app: INestApplication, documentOrFactory: OpenAPIObject | (() => OpenAPIObject), options?: SwaggerCustomOptions): void;
|
|
27
|
+
}
|
|
@@ -0,0 +1,223 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
3
|
+
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
4
|
+
return new (P || (P = Promise))(function (resolve, reject) {
|
|
5
|
+
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
6
|
+
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
7
|
+
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
8
|
+
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
9
|
+
});
|
|
10
|
+
};
|
|
11
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
12
|
+
exports.SwaggerModule = void 0;
|
|
13
|
+
const common_1 = require("@nestjs/common");
|
|
14
|
+
const jsyaml = require("js-yaml");
|
|
15
|
+
const metadata_loader_1 = require("./plugin/metadata-loader");
|
|
16
|
+
const swagger_scanner_1 = require("./swagger-scanner");
|
|
17
|
+
const swagger_ui_1 = require("./swagger-ui");
|
|
18
|
+
const assign_two_levels_deep_1 = require("./utils/assign-two-levels-deep");
|
|
19
|
+
const get_global_prefix_1 = require("./utils/get-global-prefix");
|
|
20
|
+
const normalize_rel_path_1 = require("./utils/normalize-rel-path");
|
|
21
|
+
const resolve_path_util_1 = require("./utils/resolve-path.util");
|
|
22
|
+
const validate_global_prefix_util_1 = require("./utils/validate-global-prefix.util");
|
|
23
|
+
const validate_path_util_1 = require("./utils/validate-path.util");
|
|
24
|
+
class SwaggerModule {
|
|
25
|
+
static mergeTags(configTags, scannedTags) {
|
|
26
|
+
var _a, _b, _c, _d, _e;
|
|
27
|
+
const byName = new Map();
|
|
28
|
+
for (const tag of configTags || []) {
|
|
29
|
+
if (!(tag === null || tag === void 0 ? void 0 : tag.name)) {
|
|
30
|
+
continue;
|
|
31
|
+
}
|
|
32
|
+
byName.set(tag.name, Object.assign({}, tag));
|
|
33
|
+
}
|
|
34
|
+
for (const tag of scannedTags || []) {
|
|
35
|
+
if (!(tag === null || tag === void 0 ? void 0 : tag.name)) {
|
|
36
|
+
continue;
|
|
37
|
+
}
|
|
38
|
+
const existing = byName.get(tag.name) || { name: tag.name };
|
|
39
|
+
const merged = { name: tag.name };
|
|
40
|
+
merged.summary = (_a = existing.summary) !== null && _a !== void 0 ? _a : tag.summary;
|
|
41
|
+
merged.description = (_b = existing.description) !== null && _b !== void 0 ? _b : tag.description;
|
|
42
|
+
merged.externalDocs = (_c = existing.externalDocs) !== null && _c !== void 0 ? _c : tag.externalDocs;
|
|
43
|
+
merged.parent = (_d = existing.parent) !== null && _d !== void 0 ? _d : tag.parent;
|
|
44
|
+
merged.kind = (_e = existing.kind) !== null && _e !== void 0 ? _e : tag.kind;
|
|
45
|
+
byName.set(tag.name, merged);
|
|
46
|
+
}
|
|
47
|
+
const merged = [...byName.values()];
|
|
48
|
+
return merged.length > 0 ? merged : undefined;
|
|
49
|
+
}
|
|
50
|
+
static createDocument(app, config, options = {}) {
|
|
51
|
+
const swaggerScanner = new swagger_scanner_1.SwaggerScanner();
|
|
52
|
+
const document = swaggerScanner.scanApplication(app, options);
|
|
53
|
+
document.components = (0, assign_two_levels_deep_1.assignTwoLevelsDeep)({}, config.components, document.components);
|
|
54
|
+
const mergedTags = SwaggerModule.mergeTags(config.tags, document.tags);
|
|
55
|
+
const mergedDocument = Object.assign(Object.assign(Object.assign({ openapi: '3.0.0', paths: {} }, config), document), (mergedTags ? { tags: mergedTags } : {}));
|
|
56
|
+
return mergedDocument;
|
|
57
|
+
}
|
|
58
|
+
static loadPluginMetadata(metadataFn) {
|
|
59
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
60
|
+
const metadata = yield metadataFn();
|
|
61
|
+
return this.metadataLoader.load(metadata);
|
|
62
|
+
});
|
|
63
|
+
}
|
|
64
|
+
static serveStatic(finalPath, app, customStaticPath) {
|
|
65
|
+
const httpAdapter = app.getHttpAdapter();
|
|
66
|
+
const swaggerAssetsPath = customStaticPath
|
|
67
|
+
? (0, resolve_path_util_1.resolvePath)(customStaticPath)
|
|
68
|
+
: (0, swagger_ui_1.getSwaggerAssetsAbsoluteFSPath)();
|
|
69
|
+
if (httpAdapter && httpAdapter.getType() === 'fastify') {
|
|
70
|
+
app.useStaticAssets({
|
|
71
|
+
root: swaggerAssetsPath,
|
|
72
|
+
prefix: finalPath,
|
|
73
|
+
decorateReply: false
|
|
74
|
+
});
|
|
75
|
+
}
|
|
76
|
+
else {
|
|
77
|
+
app.useStaticAssets(swaggerAssetsPath, {
|
|
78
|
+
prefix: finalPath
|
|
79
|
+
});
|
|
80
|
+
}
|
|
81
|
+
}
|
|
82
|
+
static serveDocuments(finalPath, urlLastSubdirectory, httpAdapter, documentOrFactory, options) {
|
|
83
|
+
let document;
|
|
84
|
+
const getBuiltDocument = () => {
|
|
85
|
+
if (!document) {
|
|
86
|
+
document =
|
|
87
|
+
typeof documentOrFactory === 'function'
|
|
88
|
+
? documentOrFactory()
|
|
89
|
+
: documentOrFactory;
|
|
90
|
+
}
|
|
91
|
+
return document;
|
|
92
|
+
};
|
|
93
|
+
if (options.ui) {
|
|
94
|
+
this.serveSwaggerUi(finalPath, urlLastSubdirectory, httpAdapter, getBuiltDocument, options.swaggerOptions);
|
|
95
|
+
}
|
|
96
|
+
if (options.raw === true ||
|
|
97
|
+
(Array.isArray(options.raw) && options.raw.length > 0)) {
|
|
98
|
+
const serveJson = options.raw === true || options.raw.includes('json');
|
|
99
|
+
const serveYaml = options.raw === true || options.raw.includes('yaml');
|
|
100
|
+
this.serveDefinitions(httpAdapter, getBuiltDocument, options, {
|
|
101
|
+
serveJson,
|
|
102
|
+
serveYaml
|
|
103
|
+
});
|
|
104
|
+
}
|
|
105
|
+
}
|
|
106
|
+
static serveSwaggerUi(finalPath, urlLastSubdirectory, httpAdapter, getBuiltDocument, swaggerOptions) {
|
|
107
|
+
const baseUrlForSwaggerUI = (0, normalize_rel_path_1.normalizeRelPath)(`./${urlLastSubdirectory}/`);
|
|
108
|
+
let swaggerUiHtml;
|
|
109
|
+
let swaggerUiInitJS;
|
|
110
|
+
httpAdapter.get((0, normalize_rel_path_1.normalizeRelPath)(`${finalPath}/swagger-ui-init.js`), (req, res) => {
|
|
111
|
+
res.type('application/javascript');
|
|
112
|
+
const document = getBuiltDocument();
|
|
113
|
+
if (swaggerOptions.patchDocumentOnRequest) {
|
|
114
|
+
const documentToSerialize = swaggerOptions.patchDocumentOnRequest(req, res, document);
|
|
115
|
+
const swaggerInitJsPerRequest = (0, swagger_ui_1.buildSwaggerInitJS)(documentToSerialize, swaggerOptions);
|
|
116
|
+
return res.send(swaggerInitJsPerRequest);
|
|
117
|
+
}
|
|
118
|
+
if (!swaggerUiInitJS) {
|
|
119
|
+
swaggerUiInitJS = (0, swagger_ui_1.buildSwaggerInitJS)(document, swaggerOptions);
|
|
120
|
+
}
|
|
121
|
+
res.send(swaggerUiInitJS);
|
|
122
|
+
});
|
|
123
|
+
try {
|
|
124
|
+
httpAdapter.get((0, normalize_rel_path_1.normalizeRelPath)(`${finalPath}/${urlLastSubdirectory}/swagger-ui-init.js`), (req, res) => {
|
|
125
|
+
res.type('application/javascript');
|
|
126
|
+
const document = getBuiltDocument();
|
|
127
|
+
if (swaggerOptions.patchDocumentOnRequest) {
|
|
128
|
+
const documentToSerialize = swaggerOptions.patchDocumentOnRequest(req, res, document);
|
|
129
|
+
const swaggerInitJsPerRequest = (0, swagger_ui_1.buildSwaggerInitJS)(documentToSerialize, swaggerOptions);
|
|
130
|
+
return res.send(swaggerInitJsPerRequest);
|
|
131
|
+
}
|
|
132
|
+
if (!swaggerUiInitJS) {
|
|
133
|
+
swaggerUiInitJS = (0, swagger_ui_1.buildSwaggerInitJS)(document, swaggerOptions);
|
|
134
|
+
}
|
|
135
|
+
res.send(swaggerUiInitJS);
|
|
136
|
+
});
|
|
137
|
+
}
|
|
138
|
+
catch (_a) {
|
|
139
|
+
}
|
|
140
|
+
function serveSwaggerHtml(_, res) {
|
|
141
|
+
res.type('text/html');
|
|
142
|
+
if (!swaggerUiHtml) {
|
|
143
|
+
swaggerUiHtml = (0, swagger_ui_1.buildSwaggerHTML)(baseUrlForSwaggerUI, swaggerOptions);
|
|
144
|
+
}
|
|
145
|
+
res.send(swaggerUiHtml);
|
|
146
|
+
}
|
|
147
|
+
httpAdapter.get(finalPath, serveSwaggerHtml);
|
|
148
|
+
httpAdapter.get(`${finalPath}/index.html`, serveSwaggerHtml);
|
|
149
|
+
httpAdapter.get(`${finalPath}/LICENSE`, () => {
|
|
150
|
+
throw new common_1.NotFoundException();
|
|
151
|
+
});
|
|
152
|
+
try {
|
|
153
|
+
httpAdapter.get((0, normalize_rel_path_1.normalizeRelPath)(`${finalPath}/`), serveSwaggerHtml);
|
|
154
|
+
}
|
|
155
|
+
catch (_b) {
|
|
156
|
+
}
|
|
157
|
+
}
|
|
158
|
+
static serveDefinitions(httpAdapter, getBuiltDocument, options, serveOptions) {
|
|
159
|
+
if (serveOptions.serveJson) {
|
|
160
|
+
httpAdapter.get((0, normalize_rel_path_1.normalizeRelPath)(options.jsonDocumentUrl), (req, res) => {
|
|
161
|
+
res.type('application/json');
|
|
162
|
+
const document = getBuiltDocument();
|
|
163
|
+
const documentToSerialize = options.swaggerOptions
|
|
164
|
+
.patchDocumentOnRequest
|
|
165
|
+
? options.swaggerOptions.patchDocumentOnRequest(req, res, document)
|
|
166
|
+
: document;
|
|
167
|
+
res.send(JSON.stringify(documentToSerialize));
|
|
168
|
+
});
|
|
169
|
+
}
|
|
170
|
+
if (serveOptions.serveYaml) {
|
|
171
|
+
httpAdapter.get((0, normalize_rel_path_1.normalizeRelPath)(options.yamlDocumentUrl), (req, res) => {
|
|
172
|
+
res.type('text/yaml');
|
|
173
|
+
const document = getBuiltDocument();
|
|
174
|
+
const documentToSerialize = options.swaggerOptions
|
|
175
|
+
.patchDocumentOnRequest
|
|
176
|
+
? options.swaggerOptions.patchDocumentOnRequest(req, res, document)
|
|
177
|
+
: document;
|
|
178
|
+
const yamlDocument = jsyaml.dump(documentToSerialize, {
|
|
179
|
+
skipInvalid: true,
|
|
180
|
+
noRefs: true
|
|
181
|
+
});
|
|
182
|
+
res.send(yamlDocument);
|
|
183
|
+
});
|
|
184
|
+
}
|
|
185
|
+
}
|
|
186
|
+
static setup(path, app, documentOrFactory, options) {
|
|
187
|
+
var _a, _b, _c;
|
|
188
|
+
const globalPrefix = (0, get_global_prefix_1.getGlobalPrefix)(app);
|
|
189
|
+
const finalPath = (0, validate_path_util_1.validatePath)((options === null || options === void 0 ? void 0 : options.useGlobalPrefix) && (0, validate_global_prefix_util_1.validateGlobalPrefix)(globalPrefix)
|
|
190
|
+
? `${globalPrefix}${(0, validate_path_util_1.validatePath)(path)}`
|
|
191
|
+
: path);
|
|
192
|
+
const urlLastSubdirectory = finalPath.split('/').slice(-1).pop() || '';
|
|
193
|
+
const validatedGlobalPrefix = (options === null || options === void 0 ? void 0 : options.useGlobalPrefix) && (0, validate_global_prefix_util_1.validateGlobalPrefix)(globalPrefix)
|
|
194
|
+
? (0, validate_path_util_1.validatePath)(globalPrefix)
|
|
195
|
+
: '';
|
|
196
|
+
const finalJSONDocumentPath = (options === null || options === void 0 ? void 0 : options.jsonDocumentUrl)
|
|
197
|
+
? `${validatedGlobalPrefix}${(0, validate_path_util_1.validatePath)(options.jsonDocumentUrl)}`
|
|
198
|
+
: `${finalPath}-json`;
|
|
199
|
+
const finalYAMLDocumentPath = (options === null || options === void 0 ? void 0 : options.yamlDocumentUrl)
|
|
200
|
+
? `${validatedGlobalPrefix}${(0, validate_path_util_1.validatePath)(options.yamlDocumentUrl)}`
|
|
201
|
+
: `${finalPath}-yaml`;
|
|
202
|
+
const ui = (_b = (_a = options === null || options === void 0 ? void 0 : options.ui) !== null && _a !== void 0 ? _a : options === null || options === void 0 ? void 0 : options.swaggerUiEnabled) !== null && _b !== void 0 ? _b : true;
|
|
203
|
+
const raw = (_c = options === null || options === void 0 ? void 0 : options.raw) !== null && _c !== void 0 ? _c : true;
|
|
204
|
+
const httpAdapter = app.getHttpAdapter();
|
|
205
|
+
SwaggerModule.serveDocuments(finalPath, urlLastSubdirectory, httpAdapter, documentOrFactory, {
|
|
206
|
+
ui,
|
|
207
|
+
raw,
|
|
208
|
+
jsonDocumentUrl: finalJSONDocumentPath,
|
|
209
|
+
yamlDocumentUrl: finalYAMLDocumentPath,
|
|
210
|
+
swaggerOptions: options || {}
|
|
211
|
+
});
|
|
212
|
+
if (ui) {
|
|
213
|
+
SwaggerModule.serveStatic(finalPath, app, options === null || options === void 0 ? void 0 : options.customSwaggerUiPath);
|
|
214
|
+
if (finalPath === `/${urlLastSubdirectory}`) {
|
|
215
|
+
return;
|
|
216
|
+
}
|
|
217
|
+
const serveStaticSlashEndingPath = `${finalPath}/${urlLastSubdirectory}`;
|
|
218
|
+
SwaggerModule.serveStatic(serveStaticSlashEndingPath, app);
|
|
219
|
+
}
|
|
220
|
+
}
|
|
221
|
+
}
|
|
222
|
+
exports.SwaggerModule = SwaggerModule;
|
|
223
|
+
SwaggerModule.metadataLoader = new metadata_loader_1.MetadataLoader();
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
import { INestApplication, InjectionToken } from '@nestjs/common';
|
|
2
|
+
import { ApplicationConfig } from '@nestjs/core';
|
|
3
|
+
import { InstanceWrapper } from '@nestjs/core/injector/instance-wrapper';
|
|
4
|
+
import { Module } from '@nestjs/core/injector/module';
|
|
5
|
+
import { OpenAPIObject, OperationIdFactory, SwaggerDocumentOptions } from './interfaces';
|
|
6
|
+
import { ModuleRoute } from './interfaces/module-route.interface';
|
|
7
|
+
import { SchemaObject } from './interfaces/open-api-spec.interface';
|
|
8
|
+
export declare class SwaggerScanner {
|
|
9
|
+
private readonly transformer;
|
|
10
|
+
private readonly schemaObjectFactory;
|
|
11
|
+
private explorer;
|
|
12
|
+
scanApplication(app: INestApplication, options: SwaggerDocumentOptions): Omit<OpenAPIObject, 'openapi' | 'info'>;
|
|
13
|
+
scanModuleControllers(controller: Map<InjectionToken, InstanceWrapper>, applicationConfig: ApplicationConfig, options: {
|
|
14
|
+
modulePath: string | undefined;
|
|
15
|
+
globalPrefix: string | undefined;
|
|
16
|
+
operationIdFactory?: OperationIdFactory;
|
|
17
|
+
linkNameFactory?: (controllerKey: string, methodKey: string, fieldKey: string) => string;
|
|
18
|
+
autoTagControllers?: boolean;
|
|
19
|
+
}): ModuleRoute[];
|
|
20
|
+
getModules(modulesContainer: Map<string, Module>, include: Function[]): Module[];
|
|
21
|
+
addExtraModels(schemas: Record<string, SchemaObject>, extraModels: Function[]): void;
|
|
22
|
+
private getModulePathMetadata;
|
|
23
|
+
private initializeSwaggerExplorer;
|
|
24
|
+
private exploreTagGroups;
|
|
25
|
+
}
|