file module import { CallHandler , ExecutionContext , Injectable , NestInterceptor } from '@nestjs/common' ; import { Reflector } from '@nestjs/core' ; import { PaginateResult } from 'mongoose' ; import { Observable } from 'rxjs' ; import { map } from 'rxjs/operators' ; import * as META from '../common/constants/meta.constant' ; import * as TEXT from '../common/constants/text.constant' ; import { EHttpStatus , IHttpResultPaginate , THttpSuccessResponse } from '../common/types/interfaces/http.interface' ; import { TMessage } from '../common/types/interfaces/http.interface' ; export function transformDataToPaginate < T > ( data , request ? ) { return { data : data . docs , params : request ? request . queryParams : null , pagination : { total : data . total , current_page : data . page , total_page : data . pages , per_page : data . limit , } , } ; } class classdesc @ Injectable ( ) export class TransformInterceptor < T > implements NestInterceptor < T , THttpSuccessResponse < T > > { constructor ( private readonly reflector ) { } intercept ( context , next ) { const target = context . getHandler ( ) ; const request = context . switchToHttp ( ) . getRequest ( ) ; const message = this . reflector . get < TMessage > ( META . HTTP_SUCCESS_MESSAGE , target ) || TEXT . HTTP_DEFAULT_SUCCESS_TEXT ; const usePaginate = this . reflector . get < boolean > ( META . HTTP_RES_TRANSFORM_PAGINATE , target ) ; return next . handle ( ) . pipe ( map ( ( data ) => { const result = ! usePaginate ? data : transformDataToPaginate < T > ( data , request ) ; return { status : EHttpStatus . Success , message , result } ; } ) ) ; } }