import { Request } from '@ulangi/ulangi-common/interfaces' ; import { AxiosRequestConfig } from 'axios' ; import * as _ from 'lodash' ; export function createRequest < T extends Request > ( method , baseUrl , path , query , body , authPayload ) { const requestConfig = { url : baseUrl + path , method , } ; if ( query !== null ) { requestConfig . params = query ; } if ( body !== null ) { requestConfig . data = body ; } const headers = { } ; if ( authPayload !== null ) { if ( _ . has ( authPayload , 'accessToken' ) ) { headers [ 'Authorization' ] = 'Bearer ' + _ . get ( authPayload , 'accessToken' ) ; } else if ( _ . has ( authPayload , 'apiKey' ) ) { headers [ 'Authorization' ] = 'Bearer ' + _ . get ( authPayload , 'apiKey' ) ; } else if ( _ . has ( authPayload , 'email' ) && _ . has ( authPayload , 'password' ) ) { requestConfig . data = { ... requestConfig . data , email : _ . get ( authPayload , 'email' ) , password : _ . get ( authPayload , 'password' ) , } ; } } if ( ! _ . isEmpty ( headers ) ) { requestConfig . headers = headers ; } return requestConfig ; }