33 lines
784 B
TypeScript
33 lines
784 B
TypeScript
import {
|
|
Injectable,
|
|
NestInterceptor,
|
|
ExecutionContext,
|
|
CallHandler,
|
|
} from '@nestjs/common';
|
|
import { Observable } from 'rxjs';
|
|
import { map } from 'rxjs/operators';
|
|
import { ApiResponse } from '../interfaces/communication';
|
|
import { v4 as uuidv4 } from 'uuid';
|
|
|
|
@Injectable()
|
|
export class TransformInterceptor<T> implements NestInterceptor<T, ApiResponse<T>> {
|
|
intercept(
|
|
context: ExecutionContext,
|
|
next: CallHandler,
|
|
): Observable<ApiResponse<T>> {
|
|
const serviceName = process.env.SERVICE_NAME || 'unknown-service';
|
|
|
|
return next.handle().pipe(
|
|
map((data) => ({
|
|
success: true,
|
|
data,
|
|
meta: {
|
|
timestamp: Date.now(),
|
|
traceId: uuidv4(),
|
|
service: serviceName,
|
|
},
|
|
})),
|
|
);
|
|
}
|
|
}
|