17 lines
590 B
TypeScript
17 lines
590 B
TypeScript
import { Injectable, NestInterceptor, ExecutionContext, CallHandler } from '@nestjs/common';
|
|
import { Observable } from 'rxjs';
|
|
import { map } from 'rxjs/operators';
|
|
import { ApiResponse } from '../dto/api-response.dto';
|
|
|
|
@Injectable()
|
|
export class TransformInterceptor<T> implements NestInterceptor<T, ApiResponse<T>> {
|
|
intercept(context: ExecutionContext, next: CallHandler): Observable<ApiResponse<T>> {
|
|
return next.handle().pipe(
|
|
map((data) => {
|
|
if (data instanceof ApiResponse) return data;
|
|
return new ApiResponse(data, false, null);
|
|
}),
|
|
);
|
|
}
|
|
}
|