@flusys/nestjs-core 7.1.0 → 8.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.
Files changed (69) hide show
  1. package/README.md +2 -2
  2. package/config/index.d.ts +1 -1
  3. package/constants/index.d.ts +1 -1
  4. package/docs/docs.config.d.ts +1 -1
  5. package/docs/index.d.ts +1 -1
  6. package/fesm/229.js +729 -0
  7. package/fesm/config/index.js +126 -1
  8. package/fesm/constants/index.js +29 -1
  9. package/fesm/docs/index.js +288 -1
  10. package/fesm/index.js +124 -5
  11. package/fesm/interfaces/index.js +18 -4
  12. package/fesm/migration/cli.js +19 -2
  13. package/fesm/migration/index.js +45 -3
  14. package/fesm/runtime.js +85 -0
  15. package/fesm/utils/index.js +131 -1
  16. package/index.d.ts +5 -5
  17. package/interfaces/app-config.interfaces.d.ts +1 -1
  18. package/interfaces/index.d.ts +4 -4
  19. package/interfaces/migration.interface.d.ts +2 -2
  20. package/migration/cli.d.ts +0 -1
  21. package/migration/datasource.factory.d.ts +1 -1
  22. package/migration/index.d.ts +3 -3
  23. package/migration/migration.cli.d.ts +1 -1
  24. package/migration/migration.runner.d.ts +1 -1
  25. package/package.json +24 -32
  26. package/utils/datasource-config.builder.d.ts +2 -2
  27. package/utils/index.d.ts +1 -1
  28. package/cjs/config/env-config.service.js +0 -162
  29. package/cjs/config/env-config.service.spec.js +0 -325
  30. package/cjs/config/index.js +0 -18
  31. package/cjs/constants/database.constants.js +0 -20
  32. package/cjs/constants/index.js +0 -18
  33. package/cjs/docs/docs.config.js +0 -276
  34. package/cjs/docs/docs.config.spec.js +0 -923
  35. package/cjs/docs/index.js +0 -18
  36. package/cjs/index.js +0 -23
  37. package/cjs/interfaces/app-config.interfaces.js +0 -4
  38. package/cjs/interfaces/base-entity.interface.js +0 -9
  39. package/cjs/interfaces/database.interface.js +0 -4
  40. package/cjs/interfaces/index.js +0 -21
  41. package/cjs/interfaces/migration.interface.js +0 -4
  42. package/cjs/migration/cli.js +0 -10
  43. package/cjs/migration/datasource.factory.js +0 -43
  44. package/cjs/migration/datasource.factory.spec.js +0 -113
  45. package/cjs/migration/index.js +0 -45
  46. package/cjs/migration/migration.cli.js +0 -231
  47. package/cjs/migration/migration.cli.spec.js +0 -293
  48. package/cjs/migration/migration.runner.js +0 -276
  49. package/cjs/migration/migration.runner.spec.js +0 -268
  50. package/cjs/utils/datasource-config.builder.js +0 -103
  51. package/cjs/utils/datasource-config.builder.spec.js +0 -232
  52. package/cjs/utils/index.js +0 -18
  53. package/fesm/config/env-config.service.js +0 -112
  54. package/fesm/config/env-config.service.spec.js +0 -321
  55. package/fesm/constants/database.constants.js +0 -2
  56. package/fesm/docs/docs.config.js +0 -258
  57. package/fesm/docs/docs.config.spec.js +0 -919
  58. package/fesm/interfaces/app-config.interfaces.js +0 -1
  59. package/fesm/interfaces/base-entity.interface.js +0 -6
  60. package/fesm/interfaces/database.interface.js +0 -1
  61. package/fesm/interfaces/migration.interface.js +0 -1
  62. package/fesm/migration/datasource.factory.js +0 -25
  63. package/fesm/migration/datasource.factory.spec.js +0 -109
  64. package/fesm/migration/migration.cli.js +0 -180
  65. package/fesm/migration/migration.cli.spec.js +0 -289
  66. package/fesm/migration/migration.runner.js +0 -202
  67. package/fesm/migration/migration.runner.spec.js +0 -264
  68. package/fesm/utils/datasource-config.builder.js +0 -84
  69. package/fesm/utils/datasource-config.builder.spec.js +0 -228
@@ -1,325 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", {
3
- value: true
4
- });
5
- const _envconfigservice = require("./env-config.service");
6
- describe('EnvConfigService', ()=>{
7
- const ORIGINAL_ENV = {
8
- ...process.env
9
- };
10
- afterEach(()=>{
11
- for (const key of Object.keys(process.env)){
12
- if (!(key in ORIGINAL_ENV)) delete process.env[key];
13
- }
14
- Object.assign(process.env, ORIGINAL_ENV);
15
- });
16
- describe('tryGetValue', ()=>{
17
- it('should return env value for an unrestricted key', ()=>{
18
- process.env.CUSTOM_KEY = 'custom-value';
19
- expect(_envconfigservice.envConfig.tryGetValue('CUSTOM_KEY')).toBe('custom-value');
20
- });
21
- it('should return empty string when missing and throwOnMissing is false', ()=>{
22
- delete process.env.MISSING_KEY;
23
- expect(_envconfigservice.envConfig.tryGetValue('MISSING_KEY', false)).toBe('');
24
- });
25
- it('should throw when missing and throwOnMissing is true', ()=>{
26
- delete process.env.MISSING_KEY;
27
- expect(()=>_envconfigservice.envConfig.tryGetValue('MISSING_KEY', true)).toThrow('Config error - missing env.MISSING_KEY');
28
- });
29
- it.each([
30
- [
31
- 'FRONTEND_URL',
32
- 'getFrontendUrl'
33
- ],
34
- [
35
- 'ALLOW_ORIGINS',
36
- 'getOrigins'
37
- ],
38
- [
39
- 'PORT',
40
- 'getPort'
41
- ],
42
- [
43
- 'MODE',
44
- 'isProduction'
45
- ],
46
- [
47
- 'DB_TYPE',
48
- 'getTypeOrmConfig'
49
- ],
50
- [
51
- 'DB_HOST',
52
- 'getTypeOrmConfig'
53
- ],
54
- [
55
- 'DB_PORT',
56
- 'getTypeOrmConfig'
57
- ],
58
- [
59
- 'DB_USER',
60
- 'getTypeOrmConfig'
61
- ],
62
- [
63
- 'DB_PASSWORD',
64
- 'getTypeOrmConfig'
65
- ],
66
- [
67
- 'DB_NAME',
68
- 'getTypeOrmConfig'
69
- ]
70
- ])('should deny direct access to restricted key %s and point to %s()', (key, method)=>{
71
- expect(()=>_envconfigservice.envConfig.tryGetValue(key)).toThrow(`Access denied for env key "${key}". Use the method \`${method}()\` instead.`);
72
- });
73
- });
74
- describe('getNumber', ()=>{
75
- it('should coerce a numeric env value', ()=>{
76
- process.env.SOME_NUMBER = '42';
77
- expect(_envconfigservice.envConfig.getNumber('SOME_NUMBER')).toBe(42);
78
- });
79
- it('should return NaN for a non-numeric value', ()=>{
80
- process.env.SOME_NUMBER = 'not-a-number';
81
- expect(_envconfigservice.envConfig.getNumber('SOME_NUMBER')).toBeNaN();
82
- });
83
- });
84
- describe('getBoolean', ()=>{
85
- it.each([
86
- [
87
- 'true',
88
- true
89
- ],
90
- [
91
- 'TRUE',
92
- true
93
- ],
94
- [
95
- 'True',
96
- true
97
- ],
98
- [
99
- 'false',
100
- false
101
- ],
102
- [
103
- 'yes',
104
- false
105
- ],
106
- [
107
- '',
108
- false
109
- ]
110
- ])('should parse %s as %s', (value, expected)=>{
111
- process.env.SOME_BOOL = value;
112
- expect(_envconfigservice.envConfig.getBoolean('SOME_BOOL', false)).toBe(expected);
113
- });
114
- });
115
- describe('getPort', ()=>{
116
- it('should default to 3000 when PORT is unset', ()=>{
117
- delete process.env.PORT;
118
- expect(_envconfigservice.envConfig.getPort()).toBe(3000);
119
- });
120
- it('should return the configured PORT', ()=>{
121
- process.env.PORT = '2002';
122
- expect(_envconfigservice.envConfig.getPort()).toBe(2002);
123
- });
124
- });
125
- describe('isProduction', ()=>{
126
- it('should be true when MODE is unset', ()=>{
127
- delete process.env.MODE;
128
- expect(_envconfigservice.envConfig.isProduction()).toBe(true);
129
- });
130
- it('should be false when MODE is "dev" (case-insensitive)', ()=>{
131
- process.env.MODE = 'dev';
132
- expect(_envconfigservice.envConfig.isProduction()).toBe(false);
133
- process.env.MODE = 'DEV';
134
- expect(_envconfigservice.envConfig.isProduction()).toBe(false);
135
- });
136
- it('should be true for any non-DEV mode', ()=>{
137
- process.env.MODE = 'production';
138
- expect(_envconfigservice.envConfig.isProduction()).toBe(true);
139
- });
140
- });
141
- describe('getOrigins', ()=>{
142
- it('should split and trim a comma-separated origin list', ()=>{
143
- process.env.ALLOW_ORIGINS = 'http://localhost:2001, http://localhost:3000 ,http://example.com';
144
- expect(_envconfigservice.envConfig.getOrigins()).toEqual([
145
- 'http://localhost:2001',
146
- 'http://localhost:3000',
147
- 'http://example.com'
148
- ]);
149
- });
150
- it('should throw when unset since getOrigins requires the value', ()=>{
151
- delete process.env.ALLOW_ORIGINS;
152
- expect(()=>_envconfigservice.envConfig.getOrigins()).toThrow('Config error - missing env.ALLOW_ORIGINS');
153
- });
154
- });
155
- describe('getTypeOrmConfig', ()=>{
156
- it('should apply mysql defaults when DB_TYPE is unset', ()=>{
157
- delete process.env.DB_TYPE;
158
- delete process.env.DB_HOST;
159
- delete process.env.DB_PORT;
160
- delete process.env.DB_USER;
161
- delete process.env.DB_PASSWORD;
162
- delete process.env.DB_NAME;
163
- expect(_envconfigservice.envConfig.getTypeOrmConfig()).toEqual({
164
- type: 'mysql',
165
- host: 'localhost',
166
- port: 3306,
167
- username: 'root',
168
- password: '',
169
- database: 'flusys_main'
170
- });
171
- });
172
- it('should default the postgres port to 5432', ()=>{
173
- process.env.DB_TYPE = 'postgres';
174
- delete process.env.DB_PORT;
175
- expect(_envconfigservice.envConfig.getTypeOrmConfig().port).toBe(5432);
176
- });
177
- it('should use explicit connection values when provided', ()=>{
178
- process.env.DB_TYPE = 'postgres';
179
- process.env.DB_HOST = 'db.internal';
180
- process.env.DB_PORT = '5555';
181
- process.env.DB_USER = 'flusys';
182
- process.env.DB_PASSWORD = 'secret';
183
- process.env.DB_NAME = 'flusys_prod';
184
- expect(_envconfigservice.envConfig.getTypeOrmConfig()).toEqual({
185
- type: 'postgres',
186
- host: 'db.internal',
187
- port: 5555,
188
- username: 'flusys',
189
- password: 'secret',
190
- database: 'flusys_prod'
191
- });
192
- });
193
- });
194
- describe('getJwtConfig', ()=>{
195
- it('should throw when JWT_SECRET is missing', ()=>{
196
- delete process.env.JWT_SECRET;
197
- process.env.REFRESH_TOKEN_SECRET = 'refresh';
198
- expect(()=>_envconfigservice.envConfig.getJwtConfig()).toThrow('Config error - missing env.JWT_SECRET');
199
- });
200
- it('should throw when REFRESH_TOKEN_SECRET is missing', ()=>{
201
- process.env.JWT_SECRET = 'secret';
202
- delete process.env.REFRESH_TOKEN_SECRET;
203
- expect(()=>_envconfigservice.envConfig.getJwtConfig()).toThrow('Config error - missing env.REFRESH_TOKEN_SECRET');
204
- });
205
- it('should apply default expirations when unset', ()=>{
206
- process.env.JWT_SECRET = 'secret';
207
- process.env.REFRESH_TOKEN_SECRET = 'refresh-secret';
208
- delete process.env.JWT_EXPIRATION;
209
- delete process.env.REFRESH_TOKEN_EXPIRATION;
210
- expect(_envconfigservice.envConfig.getJwtConfig()).toEqual({
211
- secret: 'secret',
212
- expiration: '1h',
213
- refreshSecret: 'refresh-secret',
214
- refreshExpiration: '7d'
215
- });
216
- });
217
- it('should use explicit expirations when set', ()=>{
218
- process.env.JWT_SECRET = 'secret';
219
- process.env.JWT_EXPIRATION = '15m';
220
- process.env.REFRESH_TOKEN_SECRET = 'refresh-secret';
221
- process.env.REFRESH_TOKEN_EXPIRATION = '30d';
222
- expect(_envconfigservice.envConfig.getJwtConfig()).toEqual({
223
- secret: 'secret',
224
- expiration: '15m',
225
- refreshSecret: 'refresh-secret',
226
- refreshExpiration: '30d'
227
- });
228
- });
229
- });
230
- describe('getRedisUrl', ()=>{
231
- it('should default to localhost redis', ()=>{
232
- delete process.env.REDIS_URL;
233
- expect(_envconfigservice.envConfig.getRedisUrl()).toBe('redis://localhost:6379');
234
- });
235
- it('should return the configured redis url', ()=>{
236
- process.env.REDIS_URL = 'redis://redis-host:6380';
237
- expect(_envconfigservice.envConfig.getRedisUrl()).toBe('redis://redis-host:6380');
238
- });
239
- });
240
- describe('getMailConfig', ()=>{
241
- it('should default to empty strings when unset', ()=>{
242
- delete process.env.MAIL_FROM;
243
- delete process.env.MAIL_APP_PASSWORD;
244
- expect(_envconfigservice.envConfig.getMailConfig()).toEqual({
245
- MAIL_FROM: '',
246
- MAIL_APP_PASSWORD: ''
247
- });
248
- });
249
- it('should return configured mail settings', ()=>{
250
- process.env.MAIL_FROM = 'noreply@flusys.io';
251
- process.env.MAIL_APP_PASSWORD = 'app-pass';
252
- expect(_envconfigservice.envConfig.getMailConfig()).toEqual({
253
- MAIL_FROM: 'noreply@flusys.io',
254
- MAIL_APP_PASSWORD: 'app-pass'
255
- });
256
- });
257
- });
258
- describe('getTenantId', ()=>{
259
- it('should return null when unset', ()=>{
260
- delete process.env.TENANT_ID;
261
- expect(_envconfigservice.envConfig.getTenantId()).toBeNull();
262
- });
263
- it('should return the configured tenant id', ()=>{
264
- process.env.TENANT_ID = 'tenant-a';
265
- expect(_envconfigservice.envConfig.getTenantId()).toBe('tenant-a');
266
- });
267
- });
268
- describe('useTenantMode', ()=>{
269
- it('should be false by default', ()=>{
270
- delete process.env.USE_TENANT_MODE;
271
- expect(_envconfigservice.envConfig.useTenantMode()).toBe(false);
272
- });
273
- it('should be true when explicitly set', ()=>{
274
- process.env.USE_TENANT_MODE = 'true';
275
- expect(_envconfigservice.envConfig.useTenantMode()).toBe(true);
276
- });
277
- });
278
- describe('getLogConfig', ()=>{
279
- it('should default log level to info in production', ()=>{
280
- delete process.env.MODE;
281
- delete process.env.LOG_LEVEL;
282
- expect(_envconfigservice.envConfig.getLogConfig().level).toBe('info');
283
- });
284
- it('should default log level to debug outside production', ()=>{
285
- process.env.MODE = 'dev';
286
- delete process.env.LOG_LEVEL;
287
- expect(_envconfigservice.envConfig.getLogConfig().level).toBe('debug');
288
- });
289
- it('should apply all defaults when unset', ()=>{
290
- process.env.MODE = 'dev';
291
- delete process.env.LOG_DIR;
292
- delete process.env.LOG_LEVEL;
293
- delete process.env.LOG_MAX_SIZE;
294
- delete process.env.LOG_MAX_FILES;
295
- delete process.env.DISABLE_HTTP_LOGGING;
296
- expect(_envconfigservice.envConfig.getLogConfig()).toEqual({
297
- dir: 'logs',
298
- level: 'debug',
299
- maxSize: '20m',
300
- maxFiles: '14d',
301
- disableHttpLogging: false
302
- });
303
- });
304
- it('should honor explicit overrides', ()=>{
305
- process.env.LOG_DIR = '/var/log/flusys';
306
- process.env.LOG_LEVEL = 'warn';
307
- process.env.LOG_MAX_SIZE = '50m';
308
- process.env.LOG_MAX_FILES = '30d';
309
- process.env.DISABLE_HTTP_LOGGING = 'true';
310
- expect(_envconfigservice.envConfig.getLogConfig()).toEqual({
311
- dir: '/var/log/flusys',
312
- level: 'warn',
313
- maxSize: '50m',
314
- maxFiles: '30d',
315
- disableHttpLogging: true
316
- });
317
- });
318
- });
319
- describe('getEnv', ()=>{
320
- it('should return the full process.env map', ()=>{
321
- process.env.SOME_FLAG = 'x';
322
- expect(_envconfigservice.envConfig.getEnv()).toBe(process.env);
323
- });
324
- });
325
- });
@@ -1,18 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", {
3
- value: true
4
- });
5
- _export_star(require("./env-config.service"), exports);
6
- function _export_star(from, to) {
7
- Object.keys(from).forEach(function(k) {
8
- if (k !== "default" && !Object.prototype.hasOwnProperty.call(to, k)) {
9
- Object.defineProperty(to, k, {
10
- enumerable: true,
11
- get: function() {
12
- return from[k];
13
- }
14
- });
15
- }
16
- });
17
- return from;
18
- }
@@ -1,20 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", {
3
- value: true
4
- });
5
- function _export(target, all) {
6
- for(var name in all)Object.defineProperty(target, name, {
7
- enumerable: true,
8
- get: Object.getOwnPropertyDescriptor(all, name).get
9
- });
10
- }
11
- _export(exports, {
12
- get DEFAULT_TENANT_HEADER () {
13
- return DEFAULT_TENANT_HEADER;
14
- },
15
- get MODULE_OPTIONS () {
16
- return MODULE_OPTIONS;
17
- }
18
- });
19
- const MODULE_OPTIONS = Symbol('MODULE_OPTIONS');
20
- const DEFAULT_TENANT_HEADER = 'x-tenant-id';
@@ -1,18 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", {
3
- value: true
4
- });
5
- _export_star(require("./database.constants"), exports);
6
- function _export_star(from, to) {
7
- Object.keys(from).forEach(function(k) {
8
- if (k !== "default" && !Object.prototype.hasOwnProperty.call(to, k)) {
9
- Object.defineProperty(to, k, {
10
- enumerable: true,
11
- get: function() {
12
- return from[k];
13
- }
14
- });
15
- }
16
- });
17
- return from;
18
- }
@@ -1,276 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", {
3
- value: true
4
- });
5
- function _export(target, all) {
6
- for(var name in all)Object.defineProperty(target, name, {
7
- enumerable: true,
8
- get: Object.getOwnPropertyDescriptor(all, name).get
9
- });
10
- }
11
- _export(exports, {
12
- get setupModuleSwaggerDocs () {
13
- return setupModuleSwaggerDocs;
14
- },
15
- get setupSwaggerDocs () {
16
- return setupSwaggerDocs;
17
- }
18
- });
19
- const _swagger = require("@nestjs/swagger");
20
- function filterSchemaProperties(document, exclusions) {
21
- if (!exclusions?.length || !document.components?.schemas) {
22
- return document;
23
- }
24
- const schemas = {
25
- ...document.components.schemas
26
- };
27
- for (const exclusion of exclusions){
28
- const schema = schemas[exclusion.schemaName];
29
- if (schema && typeof schema === 'object' && 'properties' in schema) {
30
- const filteredProperties = {
31
- ...schema.properties
32
- };
33
- for (const prop of exclusion.properties){
34
- delete filteredProperties[prop];
35
- }
36
- // Also remove from required array if present
37
- const filteredRequired = schema.required?.filter((r)=>!exclusion.properties.includes(r));
38
- schemas[exclusion.schemaName] = {
39
- ...schema,
40
- properties: filteredProperties,
41
- ...filteredRequired?.length ? {
42
- required: filteredRequired
43
- } : {}
44
- };
45
- }
46
- }
47
- return {
48
- ...document,
49
- components: {
50
- ...document.components,
51
- schemas
52
- }
53
- };
54
- }
55
- function filterQueryParameters(document, exclusions) {
56
- if (!exclusions?.length || !document.paths) {
57
- return document;
58
- }
59
- const filteredPaths = {};
60
- for (const [path, pathItem] of Object.entries(document.paths)){
61
- const filteredPathItem = {};
62
- for (const [method, operation] of Object.entries(pathItem)){
63
- // Skip non-operation properties
64
- if (!operation || typeof operation !== 'object') {
65
- filteredPathItem[method] = operation;
66
- continue;
67
- }
68
- // Check if this endpoint matches any exclusion pattern
69
- const matchingExclusions = exclusions.filter((exclusion)=>{
70
- const pathMatches = pathMatchesPattern(path, exclusion.pathPattern);
71
- const methodMatches = !exclusion.method || exclusion.method === method;
72
- return pathMatches && methodMatches;
73
- });
74
- if (matchingExclusions.length > 0) {
75
- // Collect all parameters to exclude
76
- const paramsToExclude = new Set();
77
- matchingExclusions.forEach((exclusion)=>{
78
- exclusion.parameters.forEach((param)=>paramsToExclude.add(param));
79
- });
80
- // Filter out excluded query parameters
81
- const filteredOperation = {
82
- ...operation
83
- };
84
- if (Array.isArray(filteredOperation.parameters)) {
85
- filteredOperation.parameters = filteredOperation.parameters.filter((param)=>{
86
- return !(param.in === 'query' && paramsToExclude.has(param.name));
87
- });
88
- }
89
- filteredPathItem[method] = filteredOperation;
90
- } else {
91
- filteredPathItem[method] = operation;
92
- }
93
- }
94
- filteredPaths[path] = filteredPathItem;
95
- }
96
- return {
97
- ...document,
98
- paths: filteredPaths
99
- };
100
- }
101
- function filterExamples(document, exclusions) {
102
- if (!exclusions?.length || !document.paths) {
103
- return document;
104
- }
105
- const filteredPaths = {};
106
- for (const [path, pathItem] of Object.entries(document.paths)){
107
- const filteredPathItem = {};
108
- for (const [method, operation] of Object.entries(pathItem)){
109
- // Skip non-operation properties
110
- if (!operation || typeof operation !== 'object') {
111
- filteredPathItem[method] = operation;
112
- continue;
113
- }
114
- // Check if this endpoint matches any exclusion pattern
115
- const matchingExclusions = exclusions.filter((exclusion)=>{
116
- const pathMatches = pathMatchesPattern(path, exclusion.pathPattern);
117
- const methodMatches = !exclusion.method || exclusion.method === method;
118
- return pathMatches && methodMatches;
119
- });
120
- if (matchingExclusions.length > 0) {
121
- // Collect all examples to exclude
122
- const examplesToExclude = new Set();
123
- matchingExclusions.forEach((exclusion)=>{
124
- exclusion.examples.forEach((example)=>examplesToExclude.add(example));
125
- });
126
- // Deep clone the operation to avoid mutating original
127
- const filteredOperation = JSON.parse(JSON.stringify(operation));
128
- // Filter examples from responses
129
- if (filteredOperation.responses) {
130
- for (const [statusCode, response] of Object.entries(filteredOperation.responses)){
131
- const responseObj = response;
132
- if (responseObj?.content) {
133
- const content = responseObj.content;
134
- for (const [mediaType, mediaTypeObj] of Object.entries(content)){
135
- const media = mediaTypeObj;
136
- if (media?.examples && typeof media.examples === 'object') {
137
- const examples = media.examples;
138
- for (const exampleName of examplesToExclude){
139
- delete examples[exampleName];
140
- }
141
- }
142
- }
143
- }
144
- }
145
- }
146
- filteredPathItem[method] = filteredOperation;
147
- } else {
148
- filteredPathItem[method] = operation;
149
- }
150
- }
151
- filteredPaths[path] = filteredPathItem;
152
- }
153
- return {
154
- ...document,
155
- paths: filteredPaths
156
- };
157
- }
158
- function pathMatchesPattern(path, pattern) {
159
- // Convert wildcard pattern to regex
160
- const regexPattern = pattern.replace(/\*/g, '[^/]+') // * matches any characters except /
161
- .replace(/\*\*/g, '.*'); // ** matches any characters including /
162
- const regex = new RegExp(`^${regexPattern}$`);
163
- return regex.test(path);
164
- }
165
- function filterPaths(document, excludePaths) {
166
- if (!excludePaths?.length || !document.paths) {
167
- return document;
168
- }
169
- const filteredPaths = {};
170
- for (const [path, pathItem] of Object.entries(document.paths)){
171
- const isExcluded = excludePaths.some((pattern)=>pathMatchesPattern(path, pattern));
172
- if (!isExcluded) {
173
- filteredPaths[path] = pathItem;
174
- }
175
- }
176
- return {
177
- ...document,
178
- paths: filteredPaths
179
- };
180
- }
181
- function filterDocumentByTags(document, excludeTags) {
182
- if (!excludeTags?.length) {
183
- return document;
184
- }
185
- const excludeTagsSet = new Set(excludeTags.map((tag)=>tag.toLowerCase()));
186
- // Filter out paths that have any of the excluded tags
187
- const filteredPaths = {};
188
- for (const [path, pathItem] of Object.entries(document.paths)){
189
- const filteredPathItem = {};
190
- for (const [method, operation] of Object.entries(pathItem)){
191
- // Skip non-operation properties like 'parameters'
192
- if (!operation || typeof operation !== 'object' || !('tags' in operation)) {
193
- filteredPathItem[method] = operation;
194
- continue;
195
- }
196
- const operationTags = operation.tags || [];
197
- const hasExcludedTag = operationTags.some((tag)=>excludeTagsSet.has(tag.toLowerCase()));
198
- if (!hasExcludedTag) {
199
- filteredPathItem[method] = operation;
200
- }
201
- }
202
- // Only include path if it has at least one operation
203
- const hasOperations = Object.keys(filteredPathItem).some((key)=>[
204
- 'get',
205
- 'post',
206
- 'put',
207
- 'patch',
208
- 'delete',
209
- 'options',
210
- 'head'
211
- ].includes(key));
212
- if (hasOperations) {
213
- filteredPaths[path] = filteredPathItem;
214
- }
215
- }
216
- // Filter out excluded tags from the tags array
217
- const filteredTags = document.tags?.filter((tag)=>!excludeTagsSet.has(tag.name.toLowerCase()));
218
- return {
219
- ...document,
220
- paths: filteredPaths,
221
- tags: filteredTags
222
- };
223
- }
224
- function setupModuleSwaggerDocs(app, configs) {
225
- configs.forEach((config)=>{
226
- const builder = new _swagger.DocumentBuilder().setTitle(config.title).setDescription(config.description).setVersion(config.version || '1.0');
227
- // Add bearer auth
228
- if (config.bearerAuth) {
229
- builder.addBearerAuth();
230
- }
231
- // Add custom global headers
232
- if (config.globalHeaders?.length) {
233
- config.globalHeaders.forEach((header)=>{
234
- builder.addGlobalParameters({
235
- name: header.name,
236
- in: 'header',
237
- description: header.description,
238
- required: header.required ?? false,
239
- schema: {
240
- type: 'string',
241
- example: header.example || ''
242
- }
243
- });
244
- });
245
- }
246
- // Only filter by modules if specified
247
- const createDocOptions = config.modules?.length ? {
248
- include: config.modules
249
- } : {};
250
- let document = _swagger.SwaggerModule.createDocument(app, builder.build(), createDocOptions);
251
- // Filter out excluded paths
252
- if (config.excludePaths?.length) {
253
- document = filterPaths(document, config.excludePaths);
254
- }
255
- // Filter out excluded tags
256
- if (config.excludeTags?.length) {
257
- document = filterDocumentByTags(document, config.excludeTags);
258
- }
259
- // Filter out excluded schema properties
260
- if (config.excludeSchemaProperties?.length) {
261
- document = filterSchemaProperties(document, config.excludeSchemaProperties);
262
- }
263
- // Filter out excluded query parameters
264
- if (config.excludeQueryParameters?.length) {
265
- document = filterQueryParameters(document, config.excludeQueryParameters);
266
- }
267
- // Filter out excluded examples
268
- if (config.excludeExamples?.length) {
269
- document = filterExamples(document, config.excludeExamples);
270
- }
271
- _swagger.SwaggerModule.setup(config.path, app, document);
272
- });
273
- }
274
- function setupSwaggerDocs(app, ...modules) {
275
- setupModuleSwaggerDocs(app, modules);
276
- }