@hazeljs/swagger 0.2.0-beta.8 → 0.2.0-beta.80

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.
Files changed (37) hide show
  1. package/LICENSE +192 -21
  2. package/README.md +9 -8
  3. package/dist/index.d.ts.map +1 -0
  4. package/dist/swagger.controller.d.ts.map +1 -0
  5. package/dist/{src/swagger.controller.js → swagger.controller.js} +17 -12
  6. package/dist/swagger.controller.test.d.ts +2 -0
  7. package/dist/swagger.controller.test.d.ts.map +1 -0
  8. package/dist/swagger.controller.test.js +250 -0
  9. package/dist/swagger.decorator.d.ts.map +1 -0
  10. package/dist/swagger.decorator.test.d.ts +2 -0
  11. package/dist/swagger.decorator.test.d.ts.map +1 -0
  12. package/dist/swagger.decorator.test.js +72 -0
  13. package/dist/swagger.module.d.ts.map +1 -0
  14. package/dist/swagger.service.d.ts.map +1 -0
  15. package/dist/{src/swagger.service.js → swagger.service.js} +7 -3
  16. package/dist/swagger.service.test.d.ts +2 -0
  17. package/dist/swagger.service.test.d.ts.map +1 -0
  18. package/dist/swagger.service.test.js +364 -0
  19. package/dist/swagger.types.d.ts.map +1 -0
  20. package/package.json +11 -5
  21. package/dist/src/index.d.ts.map +0 -1
  22. package/dist/src/swagger.controller.d.ts.map +0 -1
  23. package/dist/src/swagger.decorator.d.ts.map +0 -1
  24. package/dist/src/swagger.module.d.ts.map +0 -1
  25. package/dist/src/swagger.service.d.ts.map +0 -1
  26. package/dist/src/swagger.types.d.ts.map +0 -1
  27. package/dist/tsconfig.tsbuildinfo +0 -1
  28. /package/dist/{src/index.d.ts → index.d.ts} +0 -0
  29. /package/dist/{src/index.js → index.js} +0 -0
  30. /package/dist/{src/swagger.controller.d.ts → swagger.controller.d.ts} +0 -0
  31. /package/dist/{src/swagger.decorator.d.ts → swagger.decorator.d.ts} +0 -0
  32. /package/dist/{src/swagger.decorator.js → swagger.decorator.js} +0 -0
  33. /package/dist/{src/swagger.module.d.ts → swagger.module.d.ts} +0 -0
  34. /package/dist/{src/swagger.module.js → swagger.module.js} +0 -0
  35. /package/dist/{src/swagger.service.d.ts → swagger.service.d.ts} +0 -0
  36. /package/dist/{src/swagger.types.d.ts → swagger.types.d.ts} +0 -0
  37. /package/dist/{src/swagger.types.js → swagger.types.js} +0 -0
package/LICENSE CHANGED
@@ -1,21 +1,192 @@
1
- MIT License
2
-
3
- Copyright (c) 2024 HazelJS Team
4
-
5
- Permission is hereby granted, free of charge, to any person obtaining a copy
6
- of this software and associated documentation files (the "Software"), to deal
7
- in the Software without restriction, including without limitation the rights
8
- to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
- copies of the Software, and to permit persons to whom the Software is
10
- furnished to do so, subject to the following conditions:
11
-
12
- The above copyright notice and this permission notice shall be included in all
13
- copies or substantial portions of the Software.
14
-
15
- THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
- IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
- FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
- AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
- LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
- OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
- SOFTWARE.
1
+ Apache License
2
+ Version 2.0, January 2004
3
+ http://www.apache.org/licenses/
4
+
5
+ Copyright 2024 HazelJS Team
6
+
7
+ Licensed under the Apache License, Version 2.0 (the "License");
8
+ you may not use this file except in compliance with the License.
9
+ You may obtain a copy of the License at
10
+
11
+ http://www.apache.org/licenses/LICENSE-2.0
12
+
13
+ Unless required by applicable law or agreed to in writing, software
14
+ distributed under the License is distributed on an "AS IS" BASIS,
15
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16
+ See the License for the specific language governing permissions and
17
+ limitations under the License.
18
+
19
+ ---
20
+
21
+ TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
22
+
23
+ 1. Definitions.
24
+
25
+ "License" shall mean the terms and conditions for use, reproduction,
26
+ and distribution as defined by Sections 1 through 9 of this document.
27
+
28
+ "Licensor" shall mean the copyright owner or entity authorized by
29
+ the copyright owner that is granting the License.
30
+
31
+ "Legal Entity" shall mean the union of the acting entity and all
32
+ other entities that control, are controlled by, or are under common
33
+ control with that entity. For the purposes of this definition,
34
+ "control" means (i) the power, direct or indirect, to cause the
35
+ direction or management of such entity, whether by contract or
36
+ otherwise, or (ii) ownership of fifty percent (50%) or more of the
37
+ outstanding shares, or (iii) beneficial ownership of such entity.
38
+
39
+ "You" (or "Your") shall mean an individual or Legal Entity
40
+ exercising permissions granted by this License.
41
+
42
+ "Source" form shall mean the preferred form for making modifications,
43
+ including but not limited to software source code, documentation
44
+ source, and configuration files.
45
+
46
+ "Object" form shall mean any form resulting from mechanical
47
+ transformation or translation of a Source form, including but
48
+ not limited to compiled object code, generated documentation,
49
+ and conversions to other media types.
50
+
51
+ "Work" shall mean the work of authorship, whether in Source or
52
+ Object form, made available under the License, as indicated by a
53
+ copyright notice that is included in or attached to the work
54
+ (an example is provided in the Appendix below).
55
+
56
+ "Derivative Works" shall mean any work, whether in Source or Object
57
+ form, that is based on (or derived from) the Work and for which the
58
+ editorial revisions, annotations, elaborations, or other modifications
59
+ represent, as a whole, an original work of authorship. For the purposes
60
+ of this License, Derivative Works shall not include works that remain
61
+ separable from, or merely link (or bind by name) to the interfaces of,
62
+ the Work and Derivative Works thereof.
63
+
64
+ "Contribution" shall mean any work of authorship, including
65
+ the original version of the Work and any modifications or additions
66
+ to that Work or Derivative Works thereof, that is intentionally
67
+ submitted to Licensor for inclusion in the Work by the copyright owner
68
+ or by an individual or Legal Entity authorized to submit on behalf of
69
+ the copyright owner. For the purposes of this definition, "submitted"
70
+ means any form of electronic, verbal, or written communication sent
71
+ to the Licensor or its representatives, including but not limited to
72
+ communication on electronic mailing lists, source code control systems,
73
+ and issue tracking systems that are managed by, or on behalf of, the
74
+ Licensor for the purpose of discussing and improving the Work, but
75
+ excluding communication that is conspicuously marked or otherwise
76
+ designated in writing by the copyright owner as "Not a Contribution."
77
+
78
+ "Contributor" shall mean Licensor and any individual or Legal Entity
79
+ on behalf of whom a Contribution has been received by Licensor and
80
+ subsequently incorporated within the Work.
81
+
82
+ 2. Grant of Copyright License. Subject to the terms and conditions of
83
+ this License, each Contributor hereby grants to You a perpetual,
84
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
85
+ copyright license to reproduce, prepare Derivative Works of,
86
+ publicly display, publicly perform, sublicense, and distribute the
87
+ Work and such Derivative Works in Source or Object form.
88
+
89
+ 3. Grant of Patent License. Subject to the terms and conditions of
90
+ this License, each Contributor hereby grants to You a perpetual,
91
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
92
+ (except as stated in this section) patent license to make, have made,
93
+ use, offer to sell, sell, import, and otherwise transfer the Work,
94
+ where such license applies only to those patent claims licensable
95
+ by such Contributor that are necessarily infringed by their
96
+ Contribution(s) alone or by combination of their Contribution(s)
97
+ with the Work to which such Contribution(s) was submitted. If You
98
+ institute patent litigation against any entity (including a
99
+ cross-claim or counterclaim in a lawsuit) alleging that the Work
100
+ or a Contribution incorporated within the Work constitutes direct
101
+ or contributory patent infringement, then any patent licenses
102
+ granted to You under this License for that Work shall terminate
103
+ as of the date such litigation is filed.
104
+
105
+ 4. Redistribution. You may reproduce and distribute copies of the
106
+ Work or Derivative Works thereof in any medium, with or without
107
+ modifications, and in Source or Object form, provided that You
108
+ meet the following conditions:
109
+
110
+ (a) You must give any other recipients of the Work or
111
+ Derivative Works a copy of this License; and
112
+
113
+ (b) You must cause any modified files to carry prominent notices
114
+ stating that You changed the files; and
115
+
116
+ (c) You must retain, in the Source form of any Derivative Works
117
+ that You distribute, all copyright, patent, trademark, and
118
+ attribution notices from the Source form of the Work,
119
+ excluding those notices that do not pertain to any part of
120
+ the Derivative Works; and
121
+
122
+ (d) If the Work includes a "NOTICE" text file as part of its
123
+ distribution, then any Derivative Works that You distribute must
124
+ include a readable copy of the attribution notices contained
125
+ within such NOTICE file, excluding those notices that do not
126
+ pertain to any part of the Derivative Works, in at least one
127
+ of the following places: within a NOTICE text file distributed
128
+ as part of the Derivative Works; within the Source form or
129
+ documentation, if provided along with the Derivative Works; or,
130
+ within a display generated by the Derivative Works, if and
131
+ wherever such third-party notices normally appear. The contents
132
+ of the NOTICE file are for informational purposes only and
133
+ do not modify the License. You may add Your own attribution
134
+ notices within Derivative Works that You distribute, alongside
135
+ or as an addendum to the NOTICE text from the Work, provided
136
+ that such additional attribution notices cannot be construed
137
+ as modifying the License.
138
+
139
+ You may add Your own copyright statement to Your modifications and
140
+ may provide additional or different license terms and conditions
141
+ for use, reproduction, or distribution of Your modifications, or
142
+ for any such Derivative Works as a whole, provided Your use,
143
+ reproduction, and distribution of the Work otherwise complies with
144
+ the conditions stated in this License.
145
+
146
+ 5. Submission of Contributions. Unless You explicitly state otherwise,
147
+ any Contribution intentionally submitted for inclusion in the Work
148
+ by You to the Licensor shall be under the terms and conditions of
149
+ this License, without any additional terms or conditions.
150
+ Notwithstanding the above, nothing herein shall supersede or modify
151
+ the terms of any separate license agreement you may have executed
152
+ with Licensor regarding such Contributions.
153
+
154
+ 6. Trademarks. This License does not grant permission to use the trade
155
+ names, trademarks, service marks, or product names of the Licensor,
156
+ except as required for reasonable and customary use in describing the
157
+ origin of the Work and reproducing the content of the NOTICE file.
158
+
159
+ 7. Disclaimer of Warranty. Unless required by applicable law or
160
+ agreed to in writing, Licensor provides the Work (and each
161
+ Contributor provides its Contributions) on an "AS IS" BASIS,
162
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
163
+ implied, including, without limitation, any warranties or conditions
164
+ of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
165
+ PARTICULAR PURPOSE. You are solely responsible for determining the
166
+ appropriateness of using or redistributing the Work and assume any
167
+ risks associated with Your exercise of permissions under this License.
168
+
169
+ 8. Limitation of Liability. In no event and under no legal theory,
170
+ whether in tort (including negligence), contract, or otherwise,
171
+ unless required by applicable law (such as deliberate and grossly
172
+ negligent acts) or agreed to in writing, shall any Contributor be
173
+ liable to You for damages, including any direct, indirect, special,
174
+ incidental, or consequential damages of any character arising as a
175
+ result of this License or out of the use or inability to use the
176
+ Work (including but not limited to damages for loss of goodwill,
177
+ work stoppage, computer failure or malfunction, or any and all
178
+ other commercial damages or losses), even if such Contributor
179
+ has been advised of the possibility of such damages.
180
+
181
+ 9. Accepting Warranty or Additional Liability. While redistributing
182
+ the Work or Derivative Works thereof, You may choose to offer,
183
+ and charge a fee for, acceptance of support, warranty, indemnity,
184
+ or other liability obligations and/or rights consistent with this
185
+ License. However, in accepting such obligations, You may act only
186
+ on Your own behalf and on Your sole responsibility, not on behalf
187
+ of any other Contributor, and only if You agree to indemnify,
188
+ defend, and hold each Contributor harmless for any liability
189
+ incurred by, or claims asserted against, such Contributor by reason
190
+ of your accepting any such warranty or additional liability.
191
+
192
+ END OF TERMS AND CONDITIONS
package/README.md CHANGED
@@ -1,11 +1,12 @@
1
1
  # @hazeljs/swagger
2
2
 
3
- **Swagger/OpenAPI Documentation Module for HazelJS**
3
+ **API docs that write themselves.**
4
4
 
5
- Auto-generate interactive API documentation with Swagger UI and OpenAPI specifications.
5
+ Add `@ApiOperation` and `@ApiResponse` get Swagger UI. No manual OpenAPI JSON. Decorators on your controllers, interactive docs at `/api-docs`. Your frontend team will thank you.
6
6
 
7
7
  [![npm version](https://img.shields.io/npm/v/@hazeljs/swagger.svg)](https://www.npmjs.com/package/@hazeljs/swagger)
8
- [![License: MIT](https://img.shields.io/badge/License-MIT-yellow.svg)](https://opensource.org/licenses/MIT)
8
+ [![npm downloads](https://img.shields.io/npm/dm/@hazeljs/swagger)](https://www.npmjs.com/package/@hazeljs/swagger)
9
+ [![License: Apache-2.0](https://img.shields.io/badge/License-Apache%202.0-blue.svg)](https://www.apache.org/licenses/LICENSE-2.0)
9
10
 
10
11
  ## Features
11
12
 
@@ -327,8 +328,8 @@ SwaggerModule.forRoot({
327
328
 
328
329
  // License
329
330
  license: {
330
- name: 'MIT',
331
- url: 'https://opensource.org/licenses/MIT',
331
+ name: 'Apache-2.0',
332
+ url: 'https://www.apache.org/licenses/LICENSE-2.0',
332
333
  },
333
334
 
334
335
  // Terms of service
@@ -571,7 +572,7 @@ Contributions are welcome! Please read our [Contributing Guide](../../CONTRIBUTI
571
572
 
572
573
  ## License
573
574
 
574
- MIT © [HazelJS](https://hazeljs.com)
575
+ Apache 2.0 © [HazelJS](https://hazeljs.com)
575
576
 
576
577
  ## Links
577
578
 
@@ -579,5 +580,5 @@ MIT © [HazelJS](https://hazeljs.com)
579
580
  - [OpenAPI Specification](https://swagger.io/specification/)
580
581
  - [Swagger UI](https://swagger.io/tools/swagger-ui/)
581
582
  - [GitHub](https://github.com/hazel-js/hazeljs)
582
- - [Issues](https://github.com/hazeljs/hazel-js/issues)
583
- - [Discord](https://discord.gg/hazeljs)
583
+ - [Issues](https://github.com/hazel-js/hazeljs/issues)
584
+ - [Discord](https://discord.com/channels/1448263814238965833/1448263814859456575)
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA;;GAEG;AAEH,OAAO,EAAE,aAAa,EAAE,MAAM,kBAAkB,CAAC;AACjD,OAAO,EAAE,cAAc,EAAE,KAAK,WAAW,EAAE,MAAM,mBAAmB,CAAC;AACrE,OAAO,EACL,OAAO,EACP,YAAY,EACZ,kBAAkB,EAClB,oBAAoB,GACrB,MAAM,qBAAqB,CAAC;AAC7B,YAAY,EAAE,cAAc,EAAE,gBAAgB,EAAE,aAAa,EAAE,MAAM,iBAAiB,CAAC"}
@@ -0,0 +1 @@
1
+ {"version":3,"file":"swagger.controller.d.ts","sourceRoot":"","sources":["../src/swagger.controller.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,cAAc,EAAE,MAAM,mBAAmB,CAAC;AACnD,OAAO,EAAE,cAAc,EAAE,IAAI,EAAE,MAAM,eAAe,CAAC;AAIrD,OAAO,EAAE,WAAW,EAAE,MAAM,mBAAmB,CAAC;AAEhD,qBAca,iBAAiB;IAGhB,OAAO,CAAC,cAAc;IAFlC,OAAO,CAAC,MAAM,CAAC,UAAU,CAAgB;gBAErB,cAAc,EAAE,cAAc;IAElD,MAAM,CAAC,aAAa,CAAC,MAAM,EAAE,IAAI,CAAC,OAAO,CAAC,GAAG,IAAI;IA6B3C,OAAO,CAAC,QAAQ,EAAE,cAAc,GAAG,OAAO,CAAC,WAAW,CAAC;IAmIvD,OAAO,CAAC,QAAQ,EAAE,cAAc,GAAG,OAAO,CAAC,MAAM,CAAC;CA6DzD"}
@@ -50,30 +50,33 @@ let SwaggerController = SwaggerController_1 = class SwaggerController {
50
50
  core_3.default.debug('Module metadata:', JSON.stringify(moduleMetadata, null, 2));
51
51
  const controllers = new Set();
52
52
  // Helper function to recursively collect controllers from modules
53
- const collectControllers = (moduleType) => {
54
- core_3.default.debug(`Collecting controllers from module: ${moduleType.name}`);
55
- const metadata = (0, core_2.getModuleMetadata)(moduleType);
53
+ const collectControllers = (moduleRef) => {
54
+ const moduleName = typeof moduleRef === 'function'
55
+ ? moduleRef.name
56
+ : moduleRef.module?.name;
57
+ core_3.default.debug(`Collecting controllers from module: ${moduleName}`);
58
+ const metadata = (0, core_2.getModuleMetadata)(moduleRef);
56
59
  if (!metadata) {
57
- core_3.default.warn(`No metadata found for module: ${moduleType.name}`);
60
+ core_3.default.warn(`No metadata found for module: ${moduleName}`);
58
61
  return;
59
62
  }
60
63
  // Add controllers from current module
61
64
  if (metadata.controllers) {
62
65
  const validControllers = metadata.controllers.filter((c) => c && typeof c === 'function');
63
- core_3.default.debug(`${moduleType.name} controllers:`, validControllers.map((c) => typeof c === 'function' ? c.name : undefined));
66
+ core_3.default.debug(`${moduleName} controllers:`, validControllers.map((c) => typeof c === 'function' ? c.name : undefined));
64
67
  validControllers.forEach((controller) => controllers.add(controller));
65
68
  }
66
69
  else {
67
- core_3.default.debug(`No controllers found in module: ${moduleType.name}`);
70
+ core_3.default.debug(`No controllers found in module: ${moduleName}`);
68
71
  }
69
- // Recursively process imported modules
72
+ // Recursively process imported modules (Type or DynamicModule)
70
73
  if (metadata.imports) {
71
- const validModules = metadata.imports.filter((m) => m && typeof m === 'function');
72
- core_3.default.debug(`${moduleType.name} imported modules:`, validModules.map((m) => (typeof m === 'function' ? m.name : undefined)));
73
- validModules.forEach((moduleType) => collectControllers(moduleType));
74
+ const validModules = metadata.imports.filter((m) => m && (typeof m === 'function' || (typeof m === 'object' && 'module' in m)));
75
+ core_3.default.debug(`${moduleName} imported modules:`, validModules.map((m) => typeof m === 'function' ? m.name : m.module?.name));
76
+ validModules.forEach((moduleRef) => collectControllers(moduleRef));
74
77
  }
75
78
  else {
76
- core_3.default.debug(`No imports found in module: ${moduleType.name}`);
79
+ core_3.default.debug(`No imports found in module: ${moduleName}`);
77
80
  }
78
81
  };
79
82
  // Start collecting controllers from the root module
@@ -100,7 +103,9 @@ let SwaggerController = SwaggerController_1 = class SwaggerController {
100
103
  return spec;
101
104
  }
102
105
  catch (error) {
103
- core_3.default.error('Error generating Swagger spec:', error);
106
+ if (process.env.NODE_ENV !== 'test') {
107
+ core_3.default.error('Error generating Swagger spec:', error);
108
+ }
104
109
  throw error;
105
110
  }
106
111
  }
@@ -0,0 +1,2 @@
1
+ export {};
2
+ //# sourceMappingURL=swagger.controller.test.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"swagger.controller.test.d.ts","sourceRoot":"","sources":["../src/swagger.controller.test.ts"],"names":[],"mappings":""}
@@ -0,0 +1,250 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ const swagger_controller_1 = require("./swagger.controller");
4
+ const core_1 = require("@hazeljs/core");
5
+ // Mock the logger and getModuleMetadata
6
+ jest.mock('@hazeljs/core', () => {
7
+ const actual = jest.requireActual('@hazeljs/core');
8
+ return {
9
+ ...actual,
10
+ __esModule: true,
11
+ getModuleMetadata: jest.fn(),
12
+ default: {
13
+ error: jest.fn(),
14
+ debug: jest.fn(),
15
+ warn: jest.fn(),
16
+ info: jest.fn(),
17
+ http: jest.fn(),
18
+ silly: jest.fn(),
19
+ },
20
+ };
21
+ });
22
+ describe('SwaggerController', () => {
23
+ let controller;
24
+ let swaggerService;
25
+ beforeEach(() => {
26
+ swaggerService = {
27
+ generateSpec: jest.fn(),
28
+ };
29
+ controller = new swagger_controller_1.SwaggerController(swaggerService);
30
+ // Reset all mocks before each test
31
+ jest.clearAllMocks();
32
+ });
33
+ it('should be defined', () => {
34
+ expect(controller).toBeDefined();
35
+ });
36
+ it('should return swagger spec', async () => {
37
+ const mockSpec = {
38
+ openapi: '3.0.0',
39
+ info: {
40
+ title: 'API Documentation',
41
+ version: '1.0.0',
42
+ description: 'No root module provided',
43
+ },
44
+ paths: {},
45
+ components: {
46
+ schemas: {},
47
+ },
48
+ };
49
+ swaggerService.generateSpec.mockReturnValue(mockSpec);
50
+ const spec = await controller.getSpec({});
51
+ expect(spec).toEqual(mockSpec);
52
+ });
53
+ describe('getSpec', () => {
54
+ it('should return Swagger specification', async () => {
55
+ const mockSpec = {
56
+ openapi: '3.0.0',
57
+ info: {
58
+ title: 'API Documentation',
59
+ version: '1.0.0',
60
+ description: 'No root module provided',
61
+ },
62
+ paths: {},
63
+ components: {
64
+ schemas: {},
65
+ },
66
+ };
67
+ // Set up the root module
68
+ class TestModule {
69
+ }
70
+ swagger_controller_1.SwaggerController.setRootModule(TestModule);
71
+ // Mock getModuleMetadata to return some controllers
72
+ core_1.getModuleMetadata.mockReturnValue({
73
+ controllers: [class TestController {
74
+ }],
75
+ imports: [],
76
+ });
77
+ swaggerService.generateSpec.mockReturnValue(mockSpec);
78
+ const context = {};
79
+ const result = await controller.getSpec(context);
80
+ expect(result).toEqual(mockSpec);
81
+ expect(swaggerService.generateSpec).toHaveBeenCalled();
82
+ });
83
+ it('should handle errors', async () => {
84
+ const error = new Error('Test error');
85
+ swaggerService.generateSpec.mockImplementation(() => {
86
+ throw error;
87
+ });
88
+ // Set up the root module
89
+ class TestModule {
90
+ }
91
+ swagger_controller_1.SwaggerController.setRootModule(TestModule);
92
+ // Mock getModuleMetadata to return some controllers
93
+ core_1.getModuleMetadata.mockReturnValue({
94
+ controllers: [class TestController {
95
+ }],
96
+ imports: [],
97
+ });
98
+ const context = {};
99
+ await expect(controller.getSpec(context)).rejects.toThrow('Test error');
100
+ expect(swaggerService.generateSpec).toHaveBeenCalled();
101
+ });
102
+ });
103
+ describe('getSpec edge cases', () => {
104
+ it('should return default spec when no root module is set', async () => {
105
+ // Clear root module by setting it to undefined directly
106
+ swagger_controller_1.SwaggerController.rootModule = undefined;
107
+ const context = {};
108
+ const result = await controller.getSpec(context);
109
+ expect(result).toEqual({
110
+ openapi: '3.0.0',
111
+ info: {
112
+ title: 'API Documentation',
113
+ version: '1.0.0',
114
+ description: 'No root module provided',
115
+ },
116
+ paths: {},
117
+ components: {
118
+ schemas: {},
119
+ },
120
+ });
121
+ });
122
+ it('should handle module with no metadata', async () => {
123
+ class TestModule {
124
+ }
125
+ swagger_controller_1.SwaggerController.setRootModule(TestModule);
126
+ core_1.getModuleMetadata.mockReturnValue(null);
127
+ const context = {};
128
+ const result = await controller.getSpec(context);
129
+ expect(result).toEqual({
130
+ openapi: '3.0.0',
131
+ info: {
132
+ title: 'API Documentation',
133
+ version: '1.0.0',
134
+ description: 'No controllers found',
135
+ },
136
+ paths: {},
137
+ components: {
138
+ schemas: {},
139
+ },
140
+ });
141
+ });
142
+ it('should handle module with no controllers', async () => {
143
+ class TestModule {
144
+ }
145
+ swagger_controller_1.SwaggerController.setRootModule(TestModule);
146
+ core_1.getModuleMetadata.mockReturnValue({
147
+ controllers: [],
148
+ imports: [],
149
+ });
150
+ const context = {};
151
+ const result = await controller.getSpec(context);
152
+ expect(result).toEqual({
153
+ openapi: '3.0.0',
154
+ info: {
155
+ title: 'API Documentation',
156
+ version: '1.0.0',
157
+ description: 'No controllers found',
158
+ },
159
+ paths: {},
160
+ components: {
161
+ schemas: {},
162
+ },
163
+ });
164
+ });
165
+ it('should handle module with no imports', async () => {
166
+ class TestModule {
167
+ }
168
+ class TestController {
169
+ }
170
+ swagger_controller_1.SwaggerController.setRootModule(TestModule);
171
+ core_1.getModuleMetadata.mockReturnValue({
172
+ controllers: [TestController],
173
+ imports: undefined,
174
+ });
175
+ swaggerService.generateSpec.mockReturnValue({
176
+ openapi: '3.0.0',
177
+ info: { title: 'Test API', version: '1.0.0' },
178
+ paths: {},
179
+ components: { schemas: {} },
180
+ });
181
+ const context = {};
182
+ await controller.getSpec(context);
183
+ expect(swaggerService.generateSpec).toHaveBeenCalledWith([TestController]);
184
+ });
185
+ it('should recursively collect controllers from imported modules', async () => {
186
+ class RootModule {
187
+ }
188
+ class ImportedModule {
189
+ }
190
+ class RootController {
191
+ }
192
+ class ImportedController {
193
+ }
194
+ swagger_controller_1.SwaggerController.setRootModule(RootModule);
195
+ core_1.getModuleMetadata.mockImplementation((moduleType) => {
196
+ if (moduleType === RootModule) {
197
+ return {
198
+ controllers: [RootController],
199
+ imports: [ImportedModule],
200
+ };
201
+ }
202
+ if (moduleType === ImportedModule) {
203
+ return {
204
+ controllers: [ImportedController],
205
+ imports: [],
206
+ };
207
+ }
208
+ return null;
209
+ });
210
+ swaggerService.generateSpec.mockReturnValue({
211
+ openapi: '3.0.0',
212
+ info: { title: 'Test API', version: '1.0.0' },
213
+ paths: {},
214
+ components: { schemas: {} },
215
+ });
216
+ const context = {};
217
+ await controller.getSpec(context);
218
+ expect(swaggerService.generateSpec).toHaveBeenCalledWith(expect.arrayContaining([RootController, ImportedController]));
219
+ });
220
+ it('should filter out invalid controllers', async () => {
221
+ class TestModule {
222
+ }
223
+ class ValidController {
224
+ }
225
+ swagger_controller_1.SwaggerController.setRootModule(TestModule);
226
+ core_1.getModuleMetadata.mockReturnValue({
227
+ controllers: [ValidController, null, undefined, {}],
228
+ imports: [],
229
+ });
230
+ swaggerService.generateSpec.mockReturnValue({
231
+ openapi: '3.0.0',
232
+ info: { title: 'Test API', version: '1.0.0' },
233
+ paths: {},
234
+ components: { schemas: {} },
235
+ });
236
+ const context = {};
237
+ await controller.getSpec(context);
238
+ expect(swaggerService.generateSpec).toHaveBeenCalledWith([ValidController]);
239
+ });
240
+ });
241
+ describe('getDocs', () => {
242
+ it('should return Swagger UI HTML', async () => {
243
+ const context = {};
244
+ const result = await controller.getDocs(context);
245
+ expect(result).toContain('<!DOCTYPE html>');
246
+ expect(result).toContain('API Documentation');
247
+ expect(result).toContain('Loading API Documentation...');
248
+ });
249
+ });
250
+ });
@@ -0,0 +1 @@
1
+ {"version":3,"file":"swagger.decorator.d.ts","sourceRoot":"","sources":["../src/swagger.decorator.ts"],"names":[],"mappings":"AAAA,OAAO,kBAAkB,CAAC;AAC1B,OAAO,EAAE,cAAc,EAAE,gBAAgB,EAAE,MAAM,iBAAiB,CAAC;AAKnE,wBAAgB,OAAO,CAAC,OAAO,EAAE,cAAc,GAAG,cAAc,CAM/D;AAED,wBAAgB,YAAY,CAAC,SAAS,EAAE,gBAAgB,GAAG,eAAe,CAIzE;AAED,wBAAgB,kBAAkB,CAAC,MAAM,EAAE,MAAM,GAAG,cAAc,GAAG,SAAS,CAE7E;AAED,wBAAgB,oBAAoB,CAClC,MAAM,EAAE,MAAM,EACd,WAAW,EAAE,MAAM,GAAG,MAAM,GAC3B,gBAAgB,GAAG,SAAS,CAE9B"}
@@ -0,0 +1,2 @@
1
+ import 'reflect-metadata';
2
+ //# sourceMappingURL=swagger.decorator.test.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"swagger.decorator.test.d.ts","sourceRoot":"","sources":["../src/swagger.decorator.test.ts"],"names":[],"mappings":"AAAA,OAAO,kBAAkB,CAAC"}
@@ -0,0 +1,72 @@
1
+ "use strict";
2
+ var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
3
+ var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
4
+ if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
5
+ else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
6
+ return c > 3 && r && Object.defineProperty(target, key, r), r;
7
+ };
8
+ var __metadata = (this && this.__metadata) || function (k, v) {
9
+ if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
10
+ };
11
+ Object.defineProperty(exports, "__esModule", { value: true });
12
+ require("reflect-metadata");
13
+ const swagger_decorator_1 = require("./swagger.decorator");
14
+ describe('Swagger Decorators', () => {
15
+ describe('@Swagger', () => {
16
+ it('should store metadata on the prototype', () => {
17
+ const options = {
18
+ title: 'Test API',
19
+ description: 'Test API description',
20
+ version: '1.0.0',
21
+ tags: [{ name: 'test', description: 'Test operations' }],
22
+ };
23
+ let TestController = class TestController {
24
+ };
25
+ TestController = __decorate([
26
+ (0, swagger_decorator_1.Swagger)(options)
27
+ ], TestController);
28
+ const metadata = (0, swagger_decorator_1.getSwaggerMetadata)(TestController.prototype);
29
+ expect(metadata).toBeDefined();
30
+ expect(metadata).toEqual(options);
31
+ });
32
+ it('should handle undefined metadata', () => {
33
+ class TestController {
34
+ }
35
+ const metadata = (0, swagger_decorator_1.getSwaggerMetadata)(TestController.prototype);
36
+ expect(metadata).toBeUndefined();
37
+ });
38
+ });
39
+ describe('@ApiOperation', () => {
40
+ it('should store operation metadata on the prototype', () => {
41
+ const operation = {
42
+ summary: 'Test operation',
43
+ description: 'Test operation description',
44
+ tags: ['test'],
45
+ responses: {
46
+ '200': {
47
+ description: 'Success',
48
+ },
49
+ },
50
+ };
51
+ class TestController {
52
+ testMethod() { }
53
+ }
54
+ __decorate([
55
+ (0, swagger_decorator_1.ApiOperation)(operation),
56
+ __metadata("design:type", Function),
57
+ __metadata("design:paramtypes", []),
58
+ __metadata("design:returntype", void 0)
59
+ ], TestController.prototype, "testMethod", null);
60
+ const metadata = (0, swagger_decorator_1.getOperationMetadata)(TestController.prototype, 'testMethod');
61
+ expect(metadata).toBeDefined();
62
+ expect(metadata).toEqual(operation);
63
+ });
64
+ it('should handle undefined operation metadata', () => {
65
+ class TestController {
66
+ testMethod() { }
67
+ }
68
+ const metadata = (0, swagger_decorator_1.getOperationMetadata)(TestController.prototype, 'testMethod');
69
+ expect(metadata).toBeUndefined();
70
+ });
71
+ });
72
+ });