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.
@@ -1,5 +1,4 @@
1
1
  /**
2
- * Помечает GET-эндпоинт для автоматического тестирования.
3
- * @param path Относительный путь к эндпоинту (например, '/health')
2
+ * Помечает весь контроллер для автоматического тестирования всех GET-эндпоинтов.
4
3
  */
5
- export declare const AutoTest: (path: string) => MethodDecorator;
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
- * Помечает GET-эндпоинт для автоматического тестирования.
8
- * @param path Относительный путь к эндпоинту (например, '/health')
7
+ * Помечает весь контроллер для автоматического тестирования всех GET-эндпоинтов.
9
8
  */
10
- const AutoTest = (path) => {
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;
@@ -7,4 +7,5 @@ export declare class AutoTestModule implements OnModuleInit {
7
7
  private readonly reflector;
8
8
  constructor(discoveryService: DiscoveryService, autoTestService: AutoTestService, reflector: Reflector);
9
9
  onModuleInit(): void;
10
+ private extractGetEndpoints;
10
11
  }
@@ -23,18 +23,29 @@ let AutoTestModule = class AutoTestModule {
23
23
  }
24
24
  onModuleInit() {
25
25
  const controllers = this.discoveryService.getControllers();
26
- for (const controller of controllers) {
27
- const instance = controller.instance;
28
- const prototype = Object.getPrototypeOf(instance);
29
- const methodNames = Object.getOwnPropertyNames(prototype).filter((name) => name !== 'constructor' && typeof prototype[name] === 'function');
30
- for (const methodName of methodNames) {
31
- const methodRef = prototype[methodName];
32
- const metadata = this.reflector.get(auto_test_constants_1.AUTO_TEST_METADATA, methodRef);
33
- if (metadata && metadata.method === 'GET' && metadata.path) {
34
- this.autoTestService.registerEndpoint(metadata, controller.metatype, methodName);
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 { Reflector } from '@nestjs/core';
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
- constructor(httpService: HttpService, reflector: Reflector);
10
- registerEndpoint(metadata: IAutoTestMetadata, controller: unknown, methodName: string): void;
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, reflector) {
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
- registerEndpoint(metadata, controller, methodName) {
24
- this.endpoints.push({
25
- ...metadata,
26
- controller,
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
- console.log(`\n[AutoTest] Запуск автоматических тестов для ${this.endpoints.length} GET-эндпоинтов...\n`);
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
- for (const endpoint of this.endpoints) {
40
- const url = `${baseUrl}${endpoint.path}`;
41
- const start = Date.now();
42
- try {
43
- console.log(`[AutoTest] Тестирую: GET ${url}`);
44
- const response = await (0, rxjs_1.firstValueFrom)(this.httpService.get(url));
45
- const duration = Date.now() - start;
46
- console.log(`✅ [AutoTest] УСПЕХ: ${url} статус ${response.status} (${duration}мс)`);
47
- }
48
- catch (error) {
49
- const duration = Date.now() - start;
50
- const status = error.response?.status || '???';
51
- const message = error.message || 'Неизвестная ошибка';
52
- console.error(`❌ [AutoTest] ОШИБКА: ${url} → статус ${status} (${duration}мс) — ${message}`);
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] Автоматическое тестирование завершено.\n');
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,4 @@
1
+ export interface IAutoTestEndpoint {
2
+ path: string;
3
+ methodName: string;
4
+ }
@@ -0,0 +1,9 @@
1
+ export interface IAutoTestOptions {
2
+ enabled?: boolean;
3
+ host?: string;
4
+ port?: number;
5
+ timeoutMs?: number;
6
+ logSuccess?: boolean;
7
+ logErrors?: boolean;
8
+ }
9
+ export declare const DEFAULT_AUTO_TEST_OPTIONS: Required<IAutoTestOptions>;
@@ -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,2 @@
1
+ import { IAutoTestOptions } from '../interfaces/auto-test-option.interface';
2
+ export declare function loadAutoTestConfig(): Required<IAutoTestOptions>;
@@ -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
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "nest-monitor",
3
3
  "type": "commonjs",
4
- "version": "0.3.3",
4
+ "version": "0.4.0",
5
5
  "description": "Библиотека для сбора метрик с высоконагруженного приложения на Nest.js",
6
6
  "main": "dist/index.js",
7
7
  "types": "dist/index.d.ts",
@@ -1,6 +0,0 @@
1
- export interface IAutoTestMetadata {
2
- path: string;
3
- method: 'GET';
4
- controller: unknown;
5
- methodName: string;
6
- }