nest-monitor 0.3.3 → 0.4.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/dist/auto-test/auto-test.decorator.d.ts +2 -3
- package/dist/auto-test/auto-test.decorator.js +3 -7
- package/dist/auto-test/auto-test.module.d.ts +1 -0
- package/dist/auto-test/auto-test.module.js +21 -10
- package/dist/auto-test/auto-test.service.d.ts +4 -5
- package/dist/auto-test/auto-test.service.js +32 -31
- package/dist/interfaces/auto-test-endpoint.interface.d.ts +4 -0
- package/dist/interfaces/auto-test-option.interface.d.ts +9 -0
- package/dist/interfaces/auto-test-option.interface.js +11 -0
- package/dist/utils/load-config.util.d.ts +2 -0
- package/dist/utils/load-config.util.js +24 -0
- package/package.json +1 -1
- package/dist/interfaces/auto-test-metadata.interface.d.ts +0 -6
- /package/dist/interfaces/{auto-test-metadata.interface.js → auto-test-endpoint.interface.js} +0 -0
|
@@ -1,5 +1,4 @@
|
|
|
1
1
|
/**
|
|
2
|
-
* Помечает
|
|
3
|
-
* @param path Относительный путь к эндпоинту (например, '/health')
|
|
2
|
+
* Помечает весь контроллер для автоматического тестирования всех GET-эндпоинтов.
|
|
4
3
|
*/
|
|
5
|
-
export declare const AutoTest: (
|
|
4
|
+
export declare const AutoTest: () => ClassDecorator;
|
|
@@ -4,13 +4,9 @@ exports.AutoTest = void 0;
|
|
|
4
4
|
const common_1 = require("@nestjs/common");
|
|
5
5
|
const auto_test_constants_1 = require("./auto-test.constants");
|
|
6
6
|
/**
|
|
7
|
-
* Помечает
|
|
8
|
-
* @param path Относительный путь к эндпоинту (например, '/health')
|
|
7
|
+
* Помечает весь контроллер для автоматического тестирования всех GET-эндпоинтов.
|
|
9
8
|
*/
|
|
10
|
-
const AutoTest = (
|
|
11
|
-
return (0, common_1.SetMetadata)(auto_test_constants_1.AUTO_TEST_METADATA,
|
|
12
|
-
path,
|
|
13
|
-
method: 'GET',
|
|
14
|
-
});
|
|
9
|
+
const AutoTest = () => {
|
|
10
|
+
return (0, common_1.SetMetadata)(auto_test_constants_1.AUTO_TEST_METADATA, true);
|
|
15
11
|
};
|
|
16
12
|
exports.AutoTest = AutoTest;
|
|
@@ -23,18 +23,29 @@ let AutoTestModule = class AutoTestModule {
|
|
|
23
23
|
}
|
|
24
24
|
onModuleInit() {
|
|
25
25
|
const controllers = this.discoveryService.getControllers();
|
|
26
|
-
for (const
|
|
27
|
-
const
|
|
28
|
-
const
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
26
|
+
for (const wrapper of controllers) {
|
|
27
|
+
const metatype = wrapper.metatype;
|
|
28
|
+
const shouldAutoTest = this.reflector.get(auto_test_constants_1.AUTO_TEST_METADATA, metatype);
|
|
29
|
+
if (!shouldAutoTest)
|
|
30
|
+
continue;
|
|
31
|
+
const endpoints = this.extractGetEndpoints(metatype);
|
|
32
|
+
const controllerName = metatype.name || 'UnknownController';
|
|
33
|
+
this.autoTestService.registerControllerEndpoints(controllerName, endpoints);
|
|
34
|
+
}
|
|
35
|
+
}
|
|
36
|
+
extractGetEndpoints(metatype) {
|
|
37
|
+
const endpoints = [];
|
|
38
|
+
const prototype = Object.getPrototypeOf(metatype.prototype);
|
|
39
|
+
const methodNames = Object.getOwnPropertyNames(prototype).filter((name) => name !== 'constructor' && typeof prototype[name] === 'function');
|
|
40
|
+
for (const methodName of methodNames) {
|
|
41
|
+
const methodRef = prototype[methodName];
|
|
42
|
+
const routePath = Reflect.getMetadata('path', methodRef);
|
|
43
|
+
const requestMethod = Reflect.getMetadata('method', methodRef);
|
|
44
|
+
if (routePath !== undefined && requestMethod === 0) {
|
|
45
|
+
endpoints.push({ path: routePath, methodName });
|
|
36
46
|
}
|
|
37
47
|
}
|
|
48
|
+
return endpoints;
|
|
38
49
|
}
|
|
39
50
|
};
|
|
40
51
|
exports.AutoTestModule = AutoTestModule;
|
|
@@ -1,12 +1,11 @@
|
|
|
1
1
|
import { HttpService } from '@nestjs/axios';
|
|
2
2
|
import { OnApplicationBootstrap } from '@nestjs/common';
|
|
3
|
-
import {
|
|
4
|
-
import { IAutoTestMetadata } from '../interfaces/auto-test-metadata.interface';
|
|
3
|
+
import { IAutoTestEndpoint } from '../interfaces/auto-test-endpoint.interface';
|
|
5
4
|
export declare class AutoTestService implements OnApplicationBootstrap {
|
|
6
5
|
private readonly httpService;
|
|
7
|
-
private readonly reflector;
|
|
8
6
|
private endpoints;
|
|
9
|
-
|
|
10
|
-
|
|
7
|
+
private readonly options;
|
|
8
|
+
constructor(httpService: HttpService);
|
|
9
|
+
registerControllerEndpoints(controllerName: string, endpoints: IAutoTestEndpoint[]): void;
|
|
11
10
|
onApplicationBootstrap(): Promise<void>;
|
|
12
11
|
}
|
|
@@ -12,52 +12,53 @@ Object.defineProperty(exports, "__esModule", { value: true });
|
|
|
12
12
|
exports.AutoTestService = void 0;
|
|
13
13
|
const axios_1 = require("@nestjs/axios");
|
|
14
14
|
const common_1 = require("@nestjs/common");
|
|
15
|
-
const core_1 = require("@nestjs/core");
|
|
16
15
|
const rxjs_1 = require("rxjs");
|
|
16
|
+
const load_config_util_1 = require("../utils/load-config.util");
|
|
17
17
|
let AutoTestService = class AutoTestService {
|
|
18
|
-
constructor(httpService
|
|
18
|
+
constructor(httpService) {
|
|
19
19
|
this.httpService = httpService;
|
|
20
|
-
this.reflector = reflector;
|
|
21
20
|
this.endpoints = [];
|
|
21
|
+
this.options = (0, load_config_util_1.loadAutoTestConfig)();
|
|
22
22
|
}
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
methodName,
|
|
28
|
-
});
|
|
23
|
+
registerControllerEndpoints(controllerName, endpoints) {
|
|
24
|
+
if (endpoints.length > 0) {
|
|
25
|
+
this.endpoints.push({ controllerName, endpoints });
|
|
26
|
+
}
|
|
29
27
|
}
|
|
30
28
|
async onApplicationBootstrap() {
|
|
31
|
-
if (this.endpoints.length === 0) {
|
|
32
|
-
console.log('[AutoTest] Нет эндпоинтов для автоматического тестирования.');
|
|
29
|
+
if (!this.options.enabled || this.endpoints.length === 0) {
|
|
33
30
|
return;
|
|
34
31
|
}
|
|
35
|
-
|
|
36
|
-
const host = process.env.HOST || '127.0.0.1';
|
|
37
|
-
const port = process.env.PORT || '3000';
|
|
32
|
+
const { host, port, timeoutMs, logSuccess, logErrors } = this.options;
|
|
38
33
|
const baseUrl = `http://${host}:${port}`;
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
const
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
34
|
+
console.log(`\n[AutoTest] Запуск тестов для ${this.endpoints.length} контроллеров...\n`);
|
|
35
|
+
for (const { controllerName, endpoints } of this.endpoints) {
|
|
36
|
+
console.log(`🔍 Контроллер: ${controllerName}`);
|
|
37
|
+
for (const { path } of endpoints) {
|
|
38
|
+
const url = `${baseUrl}${path}`;
|
|
39
|
+
const start = Date.now();
|
|
40
|
+
try {
|
|
41
|
+
await (0, rxjs_1.firstValueFrom)(this.httpService.get(url, { timeout: timeoutMs }));
|
|
42
|
+
const duration = Date.now() - start;
|
|
43
|
+
if (logSuccess) {
|
|
44
|
+
console.log(` ✅ ${url} → OK (${duration}мс)`);
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
catch (error) {
|
|
48
|
+
const axiosError = error;
|
|
49
|
+
const duration = Date.now() - start;
|
|
50
|
+
const status = axiosError.response?.status || '???';
|
|
51
|
+
if (logErrors) {
|
|
52
|
+
console.error(` ❌ ${url} → ${status} (${duration}мс)`);
|
|
53
|
+
}
|
|
54
|
+
}
|
|
53
55
|
}
|
|
54
56
|
}
|
|
55
|
-
console.log('[AutoTest]
|
|
57
|
+
console.log('\n[AutoTest] Завершено.\n');
|
|
56
58
|
}
|
|
57
59
|
};
|
|
58
60
|
exports.AutoTestService = AutoTestService;
|
|
59
61
|
exports.AutoTestService = AutoTestService = __decorate([
|
|
60
62
|
(0, common_1.Injectable)(),
|
|
61
|
-
__metadata("design:paramtypes", [axios_1.HttpService
|
|
62
|
-
core_1.Reflector])
|
|
63
|
+
__metadata("design:paramtypes", [axios_1.HttpService])
|
|
63
64
|
], AutoTestService);
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.DEFAULT_AUTO_TEST_OPTIONS = void 0;
|
|
4
|
+
exports.DEFAULT_AUTO_TEST_OPTIONS = {
|
|
5
|
+
enabled: true,
|
|
6
|
+
host: '127.0.0.1',
|
|
7
|
+
port: 3000,
|
|
8
|
+
timeoutMs: 5000,
|
|
9
|
+
logSuccess: true,
|
|
10
|
+
logErrors: true,
|
|
11
|
+
};
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.loadAutoTestConfig = loadAutoTestConfig;
|
|
4
|
+
const fs_1 = require("fs");
|
|
5
|
+
const path_1 = require("path");
|
|
6
|
+
const auto_test_option_interface_1 = require("../interfaces/auto-test-option.interface");
|
|
7
|
+
function loadAutoTestConfig() {
|
|
8
|
+
const configPath = (0, path_1.join)(process.cwd(), '.nestmonitor');
|
|
9
|
+
let userConfig = {};
|
|
10
|
+
try {
|
|
11
|
+
const rawContent = (0, fs_1.readFileSync)(configPath, 'utf8');
|
|
12
|
+
const parsed = JSON.parse(rawContent);
|
|
13
|
+
if (typeof parsed === 'object' && parsed !== null && !Array.isArray(parsed)) {
|
|
14
|
+
userConfig = parsed;
|
|
15
|
+
}
|
|
16
|
+
}
|
|
17
|
+
catch (e) {
|
|
18
|
+
// Файл не существует или содержит невалидный JSON — используем значения по умолчанию
|
|
19
|
+
}
|
|
20
|
+
return {
|
|
21
|
+
...auto_test_option_interface_1.DEFAULT_AUTO_TEST_OPTIONS,
|
|
22
|
+
...userConfig,
|
|
23
|
+
};
|
|
24
|
+
}
|
package/package.json
CHANGED
/package/dist/interfaces/{auto-test-metadata.interface.js → auto-test-endpoint.interface.js}
RENAMED
|
File without changes
|