mirror of https://github.com/fantasticit/think.git
41 lines
899 B
TypeScript
41 lines
899 B
TypeScript
|
import {
|
||
|
Injectable,
|
||
|
NestInterceptor,
|
||
|
CallHandler,
|
||
|
ExecutionContext,
|
||
|
} from '@nestjs/common';
|
||
|
import { map } from 'rxjs/operators';
|
||
|
import { Observable } from 'rxjs';
|
||
|
|
||
|
interface Response<T> {
|
||
|
data: T;
|
||
|
}
|
||
|
|
||
|
@Injectable()
|
||
|
export class HttpResponseTransformInterceptor<T>
|
||
|
implements NestInterceptor<T, Response<T>>
|
||
|
{
|
||
|
intercept(
|
||
|
context: ExecutionContext,
|
||
|
next: CallHandler<T>,
|
||
|
): Observable<Response<T>> {
|
||
|
return next.handle().pipe(
|
||
|
map((data) => {
|
||
|
const ctx = context.switchToHttp();
|
||
|
const response = ctx.getResponse();
|
||
|
const request = ctx.getRequest();
|
||
|
// const url = request.originalUrl;
|
||
|
const statusCode = response.statusCode;
|
||
|
const res = {
|
||
|
statusCode,
|
||
|
message: null,
|
||
|
success: true,
|
||
|
data,
|
||
|
};
|
||
|
// console.info(url, res);
|
||
|
return res;
|
||
|
}),
|
||
|
);
|
||
|
}
|
||
|
}
|