think/packages/server/src/transforms/http-response.transform.ts

41 lines
899 B
TypeScript
Raw Normal View History

2022-02-20 11:51:55 +00:00
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;
}),
);
}
}