import axios , { AxiosRequestConfig } from 'axios' ; import { ApiResponseUtilInterface } from '../interface/api-response-util-interface' ; export class ApiRequestUtil { private readonly responseAnalyzer : ApiResponseUtilInterface ; constructor ( responseAnalyzer , auth ) { this . responseAnalyzer = responseAnalyzer ; axios . defaults . headers = auth ; } private static createBody ( data ? , headers ? ) { const options = { } ; if ( data ) { options . data = data ; } if ( headers ) { options . headers = headers ; } return options ; } public async getRequest ( url , params ? , headers ? ) { let requestRs ; try { requestRs = await axios . get ( this . createParams ( url , params ) , ApiRequestUtil . createBody ( undefined , headers ) ) ; } catch ( err ) { requestRs = err . response ; } return this . responseAnalyzer . analGet ( url , params , headers , requestRs . status , requestRs . data , requestRs . headers ) ; } public async postRequest ( url , params ? , headers ? ) { let requestRs ; try { requestRs = await axios . post ( url , ApiRequestUtil . createBody ( params , headers ) ) ; } catch ( err ) { requestRs = err . response ; } return this . responseAnalyzer . analPost ( url , params , headers , requestRs . status , requestRs . data , requestRs . headers ) ; } public async putRequest ( url , params ? , headers ? ) { let requestRs ; try { requestRs = await axios . put ( url , ApiRequestUtil . createBody ( params , headers ) ) ; } catch ( err ) { requestRs = err . response ; } return this . responseAnalyzer . analPut ( url , params , headers , requestRs . status , requestRs . data , requestRs . headers ) ; } public async deleteRequest ( url , params ? , headers ? ) { let requestRs ; try { requestRs = await axios . delete ( this . createParams ( url , params ) , ApiRequestUtil . createBody ( undefined , headers ) ) ; } catch ( err ) { requestRs = err . response ; } return this . responseAnalyzer . analDelete ( url , params , headers , requestRs . status , requestRs . data , requestRs . headers ) ; } private createParams ( url , params ) { if ( ! params ) { return url ; } let finUrl = url + '?' ; Object . entries ( params ) . forEach ( element => { const [ key , value ] = element ; finUrl += `${ key }=${ value }&` ; } ) ; return finUrl . substring ( 0 , finUrl . length - 1 ) ; } }