39 lines
1.2 KiB
TypeScript
39 lines
1.2 KiB
TypeScript
import { AUTH_API_NAME, DEFAULT_API_NAME } from '@/constants/app.constant'
|
|
import type { AxiosError, AxiosRequestConfig, AxiosResponse } from 'axios'
|
|
import authApiService from './authApi.service'
|
|
import platformApiService from './platformApi.service'
|
|
|
|
export interface Config {
|
|
apiName: string
|
|
}
|
|
|
|
export default {
|
|
fetchData<Response = unknown, Request = Record<string, unknown>>(
|
|
param: AxiosRequestConfig<Request>,
|
|
config?: Config,
|
|
) {
|
|
return new Promise<AxiosResponse<Response>>((resolve, reject) => {
|
|
if (!config || config?.apiName === DEFAULT_API_NAME) {
|
|
platformApiService(param)
|
|
.then((response: AxiosResponse<Response>) => {
|
|
resolve(response)
|
|
})
|
|
.catch((errors: AxiosError) => {
|
|
// console.error(errors)
|
|
reject(errors)
|
|
})
|
|
} else if (config?.apiName === AUTH_API_NAME) {
|
|
authApiService(param)
|
|
.then((response: AxiosResponse<Response>) => {
|
|
resolve(response)
|
|
})
|
|
.catch((errors: AxiosError) => {
|
|
console.error(errors)
|
|
reject(errors)
|
|
})
|
|
} else {
|
|
reject(new Error('NO API DEFINED'))
|
|
}
|
|
})
|
|
},
|
|
}
|