本次提交进行了大规模的项目结构调整: 1. 调整文件目录归属,将模块代码按功能域重新组织 2. 删除多处冗余的DTO、实体类和模块文件 3. 统一了常量、装饰器、工具类的存放位置 4. 修复了`.gitignore`的日志目录匹配规则 5. 新增了健康检查、认证、用户、剧集、订单等核心业务模块
40 lines
1.1 KiB
TypeScript
40 lines
1.1 KiB
TypeScript
import {
|
|
CallHandler,
|
|
ExecutionContext,
|
|
Injectable,
|
|
NestInterceptor,
|
|
} from '@nestjs/common';
|
|
import { Reflector } from '@nestjs/core';
|
|
import { Request } from 'express';
|
|
import { map, Observable } from 'rxjs';
|
|
import { SUCCESS_MESSAGE } from '../constants/response-message.constants';
|
|
import {
|
|
API_MESSAGE_METADATA,
|
|
} from '../decorators/api-message.decorator';
|
|
import { getRequestCost } from '../utils/request-context.util';
|
|
|
|
@Injectable()
|
|
export class ResponseInterceptor implements NestInterceptor {
|
|
constructor(private readonly reflector = new Reflector()) {}
|
|
|
|
intercept(context: ExecutionContext, next: CallHandler): Observable<unknown> {
|
|
const request = context
|
|
.switchToHttp()
|
|
.getRequest<Request & { requestId?: string }>();
|
|
const message =
|
|
this.reflector.get<string>(API_MESSAGE_METADATA, context.getHandler()) ??
|
|
SUCCESS_MESSAGE;
|
|
|
|
return next.handle().pipe(
|
|
map((data) => ({
|
|
code: 0,
|
|
data: data ?? {},
|
|
message,
|
|
timestamp: Date.now(),
|
|
requestId: request.requestId,
|
|
cost: `${getRequestCost(request)}ms`,
|
|
})),
|
|
);
|
|
}
|
|
}
|