本次提交进行了大规模的项目结构调整: 1. 调整文件目录归属,将模块代码按功能域重新组织 2. 删除多处冗余的DTO、实体类和模块文件 3. 统一了常量、装饰器、工具类的存放位置 4. 修复了`.gitignore`的日志目录匹配规则 5. 新增了健康检查、认证、用户、剧集、订单等核心业务模块
56 lines
1.7 KiB
TypeScript
56 lines
1.7 KiB
TypeScript
import {
|
|
CallHandler,
|
|
ExecutionContext,
|
|
Injectable,
|
|
NestInterceptor,
|
|
} from '@nestjs/common';
|
|
import { Reflector } from '@nestjs/core';
|
|
import { Request, Response } from 'express';
|
|
import { Observable, tap } from 'rxjs';
|
|
import {
|
|
API_MESSAGE_METADATA,
|
|
} from '../decorators/api-message.decorator';
|
|
import { SUCCESS_MESSAGE } from '../constants/response-message.constants';
|
|
import { getRequestCost } from '../utils/request-context.util';
|
|
import { LogsService } from '../../modules/logs/logs.service';
|
|
|
|
@Injectable()
|
|
export class RequestLoggingInterceptor implements NestInterceptor {
|
|
constructor(
|
|
private readonly logsService: LogsService,
|
|
private readonly reflector: Reflector,
|
|
) {}
|
|
|
|
intercept(context: ExecutionContext, next: CallHandler): Observable<unknown> {
|
|
const http = context.switchToHttp();
|
|
const request = http.getRequest<
|
|
Request & { requestId?: string; adminId?: number; user?: { id: number } }
|
|
>();
|
|
const response = http.getResponse<Response>();
|
|
const message =
|
|
this.reflector.get<string>(API_MESSAGE_METADATA, context.getHandler()) ??
|
|
SUCCESS_MESSAGE;
|
|
|
|
return next.handle().pipe(
|
|
tap(() => {
|
|
this.logsService.recordRequest({
|
|
requestId: request.requestId ?? '',
|
|
method: request.method,
|
|
path: request.originalUrl ?? request.url,
|
|
query: request.query,
|
|
body: request.body,
|
|
statusCode: response.statusCode,
|
|
responseCode: 0,
|
|
message,
|
|
costMs: getRequestCost(request),
|
|
userId: request.user?.id,
|
|
adminId: request.adminId,
|
|
ip: request.ip,
|
|
userAgent: request.get('user-agent'),
|
|
errorStack: undefined,
|
|
});
|
|
}),
|
|
);
|
|
}
|
|
}
|