import { HttpService } from '@nestjs/axios' import { Injectable } from '@nestjs/common' import { lastValueFrom } from 'rxjs' import { ServiceResponse } from '@/response/response.interface' import { HttpStatusCode } from '@/response/status-code.enum' @Injectable() export class HttpClientService { constructor(private readonly httpService: HttpService) {} async get( url: string, params?: Record ): Promise> { try { const responseObservable = await this.httpService.get>( url, { params } ) const { data } = await lastValueFrom(responseObservable) return data } catch (error) { return { code: HttpStatusCode.INTERNAL_SERVER_ERROR, msg: error.message, data: null as T } } } async post( url: string, params?: Record ): Promise> { try { const responseObservable = await this.httpService.post< ServiceResponse >(url, params, { headers: { 'Content-Type': 'application/json' } }) const { data } = await lastValueFrom(responseObservable) return data } catch (error) { return { code: HttpStatusCode.INTERNAL_SERVER_ERROR, msg: error.message, data: null as T } } } }