UNPKG

han-prev-core

Version:

Core framework for Han - A powerful Node.js framework inspired by NestJS

31 lines 1.31 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.PerformanceInterceptor = void 0; const interceptor_interface_1 = require("../interfaces/interceptor.interface"); class PerformanceInterceptor extends interceptor_interface_1.BaseInterceptor { constructor(slowThreshold = 1000) { super(); this.slowThreshold = slowThreshold; } beforeHandle(context) { context.startTime = Date.now(); } afterHandle(context, response) { const { method, path, traceId, res } = context; const { duration } = response; res.setHeader("X-Response-Time", `${duration}ms`); if (traceId) { res.setHeader("X-Trace-ID", traceId); } if (duration > this.slowThreshold) { console.warn(`🐌 SLOW REQUEST: ${method} ${path} took ${duration}ms (threshold: ${this.slowThreshold}ms) - [${traceId}]`); } } onError(context, error) { const { method, path, traceId } = context; const duration = Date.now() - context.startTime; console.error(`⚡ PERFORMANCE ERROR: ${method} ${path} failed after ${duration}ms - [${traceId}]:`, error.message); } } exports.PerformanceInterceptor = PerformanceInterceptor; //# sourceMappingURL=performance.interceptor.js.map