import { HttpService } from '@nestjs/axios' ; import { AxiosError , AxiosResponse } from 'axios' ; import { throwError } from 'rxjs' ; import { BadRequestException , HttpException , HttpStatus , InternalServerErrorException , Logger , NotFoundException , } from '@nestjs/common' ; export abstract class BaseRemoteService { protected abstract logger : Logger ; protected abstract baseUrl : string ; readonly timeout : number = 10000 ; protected constructor ( protected http ) { } protected unwrapData ( response ) { return response . data ? response . data : response ; } handleError ( error ) { if ( error . response ) { this . logger . log ( error . response . data ) ; this . logger . log ( error . response . status + '' ) ; console . log ( error . response . headers ) ; if ( error . response . status === HttpStatus . NOT_FOUND ) { return throwError ( new NotFoundException ( error . response . data ) ) ; } else if ( error . response . status === HttpStatus . BAD_REQUEST ) { return throwError ( new BadRequestException ( error . response . data ) ) ; } else { return throwError ( new HttpException ( error . response . data , error . response . status ) ) ; } } else if ( error . request ) { this . logger . log ( 'Error' + error . request ) ; return throwError ( new InternalServerErrorException ( error . request ) ) ; } else { return throwError ( 'Something bad happened; please try again later.' ) ; } } protected getHeaders ( json = true ) { const headers = { } ; if ( json ) { headers [ 'Content-Type' ] = 'application/json' ; } else { headers [ 'Content-Type' ] = 'application/x-www-form-urlencoded' ; } return headers ; } }