NestJS
NestJS 拦截器 (Interceptors) 与 AOP 切面编程:响应转换与耗时统计
Waitwalker2026-08-2313 min
## 1. 统一数据结构拦截器 (TransformInterceptor)
```typescript
@Injectable()
export class TransformInterceptor<T> implements NestInterceptor<T, Response<T>> {
intercept(context: ExecutionContext, next: CallHandler): Observable<Response<T>> {
return next.handle().pipe(
map((data) => ({
code: 200,
message: "success",
data,
timestamp: Date.now(),
}))
);
}
}
```
#NestJS#Interceptors#AOP#RxJS
回到文章列表 →