tooot/src/utils/api/instance.ts

73 lines
2.2 KiB
TypeScript
Raw Normal View History

2023-02-08 19:22:20 +01:00
import { GLOBAL } from '@utils/storage'
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'
2023-01-29 15:32:40 +01:00
import { CONNECT_DOMAIN } from './helpers/connect'
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'
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
}
headers?: { [key: string]: string }
body?: FormData | Object
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)
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
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 '),
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 15:32:40 +01:00
return axios({
2021-01-04 14:55:34 +01:00
timeout: method === 'post' ? 1000 * 60 : 1000 * 15,
method,
2023-01-29 15:32:40 +01:00
baseURL: `https://${
GLOBAL.connect ? CONNECT_DOMAIN() : accountDetails['auth.domain']
2023-01-29 15:32:40 +01:00
}/api/${version}`,
url,
params,
2020-11-17 23:57:23 +01:00
headers: {
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']}`,
2023-01-29 15:32:40 +01:00
...(body && body instanceof FormData && { 'Content-Type': 'multipart/form-data' }),
...(GLOBAL.connect && { 'x-tooot-domain': accountDetails['auth.domain'] })
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) }))
.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