2022-12-28 23:41:36 +01:00
|
|
|
import { getAccountDetails } from '@utils/storage/actions'
|
2023-01-08 16:59:35 +01:00
|
|
|
import { StorageGlobal } from '@utils/storage/global'
|
2021-03-21 23:06:53 +01:00
|
|
|
import axios, { AxiosRequestConfig } from 'axios'
|
2023-01-29 00:37:56 +01:00
|
|
|
import { ctx, handleError, PagedResponse, parseHeaderLinks, userAgent } from './helpers'
|
2020-12-24 10:28:51 +01:00
|
|
|
|
2021-02-20 19:12:44 +01:00
|
|
|
export type Params = {
|
2023-01-08 16:59:35 +01:00
|
|
|
account?: StorageGlobal['account.active']
|
2021-05-09 21:59:03 +02:00
|
|
|
method: 'get' | 'post' | 'put' | 'delete' | 'patch'
|
2020-12-05 01:55:53 +01:00
|
|
|
version?: 'v1' | 'v2'
|
|
|
|
url: string
|
|
|
|
params?: {
|
2021-03-17 15:30:28 +01:00
|
|
|
[key: string]: string | number | boolean | string[] | number[] | boolean[]
|
2020-10-31 21:04:46 +01:00
|
|
|
}
|
2020-12-05 01:55:53 +01:00
|
|
|
headers?: { [key: string]: string }
|
2023-01-23 23:05:25 +01:00
|
|
|
body?: FormData | Object
|
2023-01-14 16:58:01 +01:00
|
|
|
extras?: Omit<AxiosRequestConfig, 'method' | 'baseURL' | 'url' | 'params' | 'headers' | 'data'>
|
2021-02-20 19:12:44 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
const apiInstance = async <T = unknown>({
|
2023-01-08 16:59:35 +01:00
|
|
|
account,
|
2021-02-20 19:12:44 +01:00
|
|
|
method,
|
|
|
|
version = 'v1',
|
|
|
|
url,
|
|
|
|
params,
|
|
|
|
headers,
|
|
|
|
body,
|
2021-03-21 23:06:53 +01:00
|
|
|
extras
|
2022-12-15 14:28:36 +01:00
|
|
|
}: Params): Promise<PagedResponse<T>> => {
|
2023-01-08 16:59:35 +01:00
|
|
|
const accountDetails = getAccountDetails(['auth.domain', 'auth.token'], account)
|
2022-12-28 23:41:36 +01:00
|
|
|
if (!accountDetails) {
|
|
|
|
console.warn(ctx.bgRed.white.bold(' API instance '), 'No account detail available')
|
|
|
|
return Promise.reject()
|
|
|
|
}
|
2021-01-07 19:13:09 +01:00
|
|
|
|
2022-12-28 23:41:36 +01:00
|
|
|
if (!accountDetails['auth.domain'] || !accountDetails['auth.token']) {
|
|
|
|
console.warn(ctx.bgRed.white.bold(' API ') + ' ' + 'No domain or token available')
|
2021-02-20 19:12:44 +01:00
|
|
|
return Promise.reject()
|
2021-01-07 19:13:09 +01:00
|
|
|
}
|
|
|
|
|
2020-12-21 13:56:04 +01:00
|
|
|
console.log(
|
2023-01-08 16:59:35 +01:00
|
|
|
ctx.bgBlue.bold(' Instance '),
|
2022-12-28 23:41:36 +01:00
|
|
|
accountDetails['auth.domain'],
|
2023-01-08 16:59:35 +01:00
|
|
|
method + ctx.blue(' -> ') + `/${url}` + (params ? ctx.blue(' -> ') : ''),
|
2020-12-24 10:28:51 +01:00
|
|
|
params ? params : ''
|
2020-12-21 13:56:04 +01:00
|
|
|
)
|
2023-01-29 00:37:56 +01:00
|
|
|
console.log('body', body)
|
2020-12-05 01:55:53 +01:00
|
|
|
return axios({
|
2021-01-04 14:55:34 +01:00
|
|
|
timeout: method === 'post' ? 1000 * 60 : 1000 * 15,
|
2020-12-05 01:55:53 +01:00
|
|
|
method,
|
2022-12-28 23:41:36 +01:00
|
|
|
baseURL: `https://${accountDetails['auth.domain']}/api/${version}/`,
|
2020-12-05 01:55:53 +01:00
|
|
|
url,
|
|
|
|
params,
|
2020-11-17 23:57:23 +01:00
|
|
|
headers: {
|
2023-01-23 23:05:25 +01:00
|
|
|
Accept: 'application/json',
|
2022-11-05 01:11:09 +01:00
|
|
|
...userAgent,
|
2020-11-17 23:57:23 +01:00
|
|
|
...headers,
|
2023-01-29 00:37:56 +01:00
|
|
|
Authorization: `Bearer ${accountDetails['auth.token']}`,
|
|
|
|
...(body && body instanceof FormData && { 'Content-Type': 'multipart/form-data' })
|
2020-11-17 23:57:23 +01:00
|
|
|
},
|
2023-01-29 00:37:56 +01:00
|
|
|
data: body,
|
2021-03-21 23:06:53 +01:00
|
|
|
...extras
|
2020-11-17 23:57:23 +01:00
|
|
|
})
|
2023-01-07 18:01:08 +01:00
|
|
|
.then(response => ({ body: response.data, links: parseHeaderLinks(response.headers.link) }))
|
2022-12-05 19:24:03 +01:00
|
|
|
.catch(handleError())
|
2020-10-23 09:22:17 +02:00
|
|
|
}
|
2020-10-31 21:04:46 +01:00
|
|
|
|
2021-02-20 19:12:44 +01:00
|
|
|
export default apiInstance
|