33 lines
591 B
TypeScript
33 lines
591 B
TypeScript
import {
|
|
Injectable,
|
|
NestInterceptor,
|
|
ExecutionContext,
|
|
CallHandler,
|
|
} from '@nestjs/common';
|
|
import { Observable } from 'rxjs';
|
|
import { map } from 'rxjs/operators';
|
|
|
|
export interface Response<T> {
|
|
code: number;
|
|
message: string;
|
|
data: T;
|
|
}
|
|
|
|
@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,
|
|
})),
|
|
);
|
|
}
|
|
}
|