nest-monitor 0.4.0 → 0.4.1

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/Readme.md CHANGED
@@ -1 +1,22 @@
1
1
  Это моя библиотека nest-monitor
2
+
3
+ ## Конфигурация
4
+
5
+ Создайте файл `.nestmonitor` в корне проекта:
6
+
7
+ ```js
8
+ // .nestmonitor
9
+ module.exports = {
10
+ enabled: true,
11
+ host: '127.0.0.1',
12
+ port: 3000,
13
+ timeoutMs: 5000,
14
+ logSuccess: true,
15
+ logErrors: true
16
+ };
17
+
18
+ ## Соглашение об именах файлов
19
+ Для эндпоинта с путём /api/users/:id -> файл:
20
+ ```
21
+ api-users-id.test.ts
22
+ ```
@@ -6,6 +6,7 @@ export declare class AutoTestService implements OnApplicationBootstrap {
6
6
  private endpoints;
7
7
  private readonly options;
8
8
  constructor(httpService: HttpService);
9
+ private testEndpointUrl;
9
10
  registerControllerEndpoints(controllerName: string, endpoints: IAutoTestEndpoint[]): void;
10
11
  onApplicationBootstrap(): Promise<void>;
11
12
  }
@@ -14,12 +14,32 @@ const axios_1 = require("@nestjs/axios");
14
14
  const common_1 = require("@nestjs/common");
15
15
  const rxjs_1 = require("rxjs");
16
16
  const load_config_util_1 = require("../utils/load-config.util");
17
+ const load_test_cases_util_1 = require("../utils/load-test.cases.util");
17
18
  let AutoTestService = class AutoTestService {
18
19
  constructor(httpService) {
19
20
  this.httpService = httpService;
20
21
  this.endpoints = [];
21
22
  this.options = (0, load_config_util_1.loadAutoTestConfig)();
22
23
  }
24
+ async testEndpointUrl(url) {
25
+ const { timeoutMs, logSuccess, logErrors } = this.options;
26
+ const start = Date.now();
27
+ try {
28
+ await (0, rxjs_1.firstValueFrom)(this.httpService.get(url, { timeout: timeoutMs }));
29
+ const duration = Date.now() - start;
30
+ if (logSuccess) {
31
+ console.log(` ✅ ${url} → OK (${duration}мс)`);
32
+ }
33
+ }
34
+ catch (error) {
35
+ const axiosError = error;
36
+ const duration = Date.now() - start;
37
+ const status = axiosError.response?.status || '???';
38
+ if (logErrors) {
39
+ console.error(` ❌ ${url} → ${status} (${duration}мс)`);
40
+ }
41
+ }
42
+ }
23
43
  registerControllerEndpoints(controllerName, endpoints) {
24
44
  if (endpoints.length > 0) {
25
45
  this.endpoints.push({ controllerName, endpoints });
@@ -29,29 +49,28 @@ let AutoTestService = class AutoTestService {
29
49
  if (!this.options.enabled || this.endpoints.length === 0) {
30
50
  return;
31
51
  }
32
- const { host, port, timeoutMs, logSuccess, logErrors } = this.options;
52
+ const { host, port } = this.options;
33
53
  const baseUrl = `http://${host}:${port}`;
34
54
  console.log(`\n[AutoTest] Запуск тестов для ${this.endpoints.length} контроллеров...\n`);
35
55
  for (const { controllerName, endpoints } of this.endpoints) {
36
56
  console.log(`🔍 Контроллер: ${controllerName}`);
37
57
  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}мс)`);
58
+ const hasParams = path.includes(':') || path.includes('*');
59
+ if (hasParams) {
60
+ const testCases = await (0, load_test_cases_util_1.loadTestCases)(path);
61
+ if (testCases.length === 0) {
62
+ console.log(` ⚠️ Пропущен (нет .test.js): ${path}`);
63
+ continue;
45
64
  }
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}мс)`);
65
+ for (const testCase of testCases) {
66
+ const url = `${baseUrl}${testCase.path}`;
67
+ await this.testEndpointUrl(url);
53
68
  }
54
69
  }
70
+ else {
71
+ const url = `${baseUrl}${path}`;
72
+ await this.testEndpointUrl(url);
73
+ }
55
74
  }
56
75
  }
57
76
  console.log('\n[AutoTest] Завершено.\n');
@@ -5,5 +5,6 @@ export interface IAutoTestOptions {
5
5
  timeoutMs?: number;
6
6
  logSuccess?: boolean;
7
7
  logErrors?: boolean;
8
+ autoTestDir?: string;
8
9
  }
9
10
  export declare const DEFAULT_AUTO_TEST_OPTIONS: Required<IAutoTestOptions>;
@@ -8,4 +8,5 @@ exports.DEFAULT_AUTO_TEST_OPTIONS = {
8
8
  timeoutMs: 5000,
9
9
  logSuccess: true,
10
10
  logErrors: true,
11
+ autoTestDir: './auto-test',
11
12
  };
@@ -0,0 +1,15 @@
1
+ export interface IAutoTestRequest {
2
+ /**
3
+ * Полный путь для запроса (уже с подставленными параметрами)
4
+ * Пример: "/api/users/123"
5
+ */
6
+ path: string;
7
+ /**
8
+ * Необязательные query-параметры
9
+ */
10
+ query?: Record<string, string | number | boolean>;
11
+ /**
12
+ * Необязательные заголовки
13
+ */
14
+ headers?: Record<string, string>;
15
+ }
@@ -0,0 +1,2 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
@@ -0,0 +1 @@
1
+ export declare function generateTestFilename(route: string): string;
@@ -0,0 +1,13 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.generateTestFilename = generateTestFilename;
4
+ function generateTestFilename(route) {
5
+ return ((route
6
+ .replace(/^\/+/, '')
7
+ .replace(/:/g, '-')
8
+ .replace(/\//g, '-')
9
+ .replace(/[^a-zA-Z0-9\-_]/g, '')
10
+ .replace(/-+/g, '-')
11
+ .replace(/^-|-$/g, '')
12
+ .toLowerCase() || 'root') + '.test.js');
13
+ }
@@ -16,6 +16,7 @@ function loadAutoTestConfig() {
16
16
  }
17
17
  catch (e) {
18
18
  // Файл не существует или содержит невалидный JSON — используем значения по умолчанию
19
+ console.warn(`[AutoTest] Не удалось загрузить конфиг-файл, error: ${e}`);
19
20
  }
20
21
  return {
21
22
  ...auto_test_option_interface_1.DEFAULT_AUTO_TEST_OPTIONS,
@@ -0,0 +1,2 @@
1
+ import { IAutoTestRequest } from '../interfaces/auto-test-request.interface';
2
+ export declare function loadTestCases(route: string): Promise<IAutoTestRequest[]>;
@@ -0,0 +1,68 @@
1
+ "use strict";
2
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
+ if (k2 === undefined) k2 = k;
4
+ var desc = Object.getOwnPropertyDescriptor(m, k);
5
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
+ desc = { enumerable: true, get: function() { return m[k]; } };
7
+ }
8
+ Object.defineProperty(o, k2, desc);
9
+ }) : (function(o, m, k, k2) {
10
+ if (k2 === undefined) k2 = k;
11
+ o[k2] = m[k];
12
+ }));
13
+ var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
14
+ Object.defineProperty(o, "default", { enumerable: true, value: v });
15
+ }) : function(o, v) {
16
+ o["default"] = v;
17
+ });
18
+ var __importStar = (this && this.__importStar) || (function () {
19
+ var ownKeys = function(o) {
20
+ ownKeys = Object.getOwnPropertyNames || function (o) {
21
+ var ar = [];
22
+ for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
23
+ return ar;
24
+ };
25
+ return ownKeys(o);
26
+ };
27
+ return function (mod) {
28
+ if (mod && mod.__esModule) return mod;
29
+ var result = {};
30
+ if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
31
+ __setModuleDefault(result, mod);
32
+ return result;
33
+ };
34
+ })();
35
+ Object.defineProperty(exports, "__esModule", { value: true });
36
+ exports.loadTestCases = loadTestCases;
37
+ const fs_1 = require("fs");
38
+ const path_1 = require("path");
39
+ const generate_test_filename_util_1 = require("./generate-test-filename.util");
40
+ const TEST_DIR = '.auto-test';
41
+ async function loadTestCases(route) {
42
+ const filename = (0, generate_test_filename_util_1.generateTestFilename)(route);
43
+ const fullPath = (0, path_1.join)(process.cwd(), TEST_DIR, filename);
44
+ if (!existsWithCaseSync(fullPath)) {
45
+ return [];
46
+ }
47
+ try {
48
+ delete require.cache[require.resolve(fullPath)];
49
+ const mod = await Promise.resolve(`${fullPath}`).then(s => __importStar(require(s)));
50
+ return Array.isArray(mod.testCases) ? mod.testCases : [];
51
+ }
52
+ catch (e) {
53
+ if (process.env.NODE_ENV !== 'production') {
54
+ console.warn(`[AutoTest] Не удалось загрузить тест-файл: ${fullPath}, error: ${e}`);
55
+ }
56
+ return [];
57
+ }
58
+ }
59
+ function existsWithCaseSync(path) {
60
+ if (!(0, fs_1.existsSync)(path))
61
+ return false;
62
+ const dir = path.substring(0, path.lastIndexOf('/'));
63
+ const basename = path.substring(dir.length + 1);
64
+ if (dir === '')
65
+ return true;
66
+ const files = (0, fs_1.readdirSync)(dir);
67
+ return files.includes(basename);
68
+ }
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "nest-monitor",
3
3
  "type": "commonjs",
4
- "version": "0.4.0",
4
+ "version": "0.4.1",
5
5
  "description": "Библиотека для сбора метрик с высоконагруженного приложения на Nest.js",
6
6
  "main": "dist/index.js",
7
7
  "types": "dist/index.d.ts",