nest-monitor 0.3.4 → 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.
@@ -4,6 +4,7 @@ import { IAutoTestEndpoint } from '../interfaces/auto-test-endpoint.interface';
4
4
  export declare class AutoTestService implements OnApplicationBootstrap {
5
5
  private readonly httpService;
6
6
  private endpoints;
7
+ private readonly options;
7
8
  constructor(httpService: HttpService);
8
9
  registerControllerEndpoints(controllerName: string, endpoints: IAutoTestEndpoint[]): void;
9
10
  onApplicationBootstrap(): Promise<void>;
@@ -13,10 +13,12 @@ exports.AutoTestService = void 0;
13
13
  const axios_1 = require("@nestjs/axios");
14
14
  const common_1 = require("@nestjs/common");
15
15
  const rxjs_1 = require("rxjs");
16
+ const load_config_util_1 = require("../utils/load-config.util");
16
17
  let AutoTestService = class AutoTestService {
17
18
  constructor(httpService) {
18
19
  this.httpService = httpService;
19
20
  this.endpoints = [];
21
+ this.options = (0, load_config_util_1.loadAutoTestConfig)();
20
22
  }
21
23
  registerControllerEndpoints(controllerName, endpoints) {
22
24
  if (endpoints.length > 0) {
@@ -24,34 +26,35 @@ let AutoTestService = class AutoTestService {
24
26
  }
25
27
  }
26
28
  async onApplicationBootstrap() {
27
- if (this.endpoints.length === 0) {
28
- console.log('[AutoTest] Нет контроллеров для автоматического тестирования.');
29
+ if (!this.options.enabled || this.endpoints.length === 0) {
29
30
  return;
30
31
  }
31
- console.log(`\n[AutoTest] Запуск тестов для ${this.endpoints.length} контроллеров...\n`);
32
- const host = process.env.HOST || '127.0.0.1';
33
- const port = process.env.PORT || '3000';
32
+ const { host, port, timeoutMs, logSuccess, logErrors } = this.options;
34
33
  const baseUrl = `http://${host}:${port}`;
34
+ console.log(`\n[AutoTest] Запуск тестов для ${this.endpoints.length} контроллеров...\n`);
35
35
  for (const { controllerName, endpoints } of this.endpoints) {
36
- console.log(`\n🔍 [AutoTest] Контроллер: ${controllerName}`);
37
- for (const endpoint of endpoints) {
38
- const url = `${baseUrl}${endpoint.path}`;
36
+ console.log(`🔍 Контроллер: ${controllerName}`);
37
+ for (const { path } of endpoints) {
38
+ const url = `${baseUrl}${path}`;
39
39
  const start = Date.now();
40
40
  try {
41
- console.log(` → Тестирую: GET ${url}`);
42
- const response = await (0, rxjs_1.firstValueFrom)(this.httpService.get(url));
41
+ await (0, rxjs_1.firstValueFrom)(this.httpService.get(url, { timeout: timeoutMs }));
43
42
  const duration = Date.now() - start;
44
- console.log(` ✅ УСПЕХ: статус ${response.status} (${duration}мс)`);
43
+ if (logSuccess) {
44
+ console.log(` ✅ ${url} → OK (${duration}мс)`);
45
+ }
45
46
  }
46
47
  catch (error) {
48
+ const axiosError = error;
47
49
  const duration = Date.now() - start;
48
- const status = error.response?.status || '???';
49
- const message = error.message || 'Неизвестная ошибка';
50
- console.error(` ОШИБКА: статус ${status} (${duration}мс) — ${message}`);
50
+ const status = axiosError.response?.status || '???';
51
+ if (logErrors) {
52
+ console.error(` ${url} ${status} (${duration}мс)`);
53
+ }
51
54
  }
52
55
  }
53
56
  }
54
- console.log('\n[AutoTest] Автоматическое тестирование завершено.\n');
57
+ console.log('\n[AutoTest] Завершено.\n');
55
58
  }
56
59
  };
57
60
  exports.AutoTestService = AutoTestService;
@@ -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.4",
4
+ "version": "0.4.0",
5
5
  "description": "Библиотека для сбора метрик с высоконагруженного приложения на Nest.js",
6
6
  "main": "dist/index.js",
7
7
  "types": "dist/index.d.ts",