tooot/src/utils/api/instance.ts

68 lines
2.1 KiB
TypeScript
Raw Normal View History

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-07 18:01:08 +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'
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 }
2020-11-17 23:57:23 +01:00
body?: FormData
extras?: Omit<AxiosRequestConfig, 'method' | '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
)
2020-10-26 00:27:53 +01:00
return axios({
2021-01-04 14:55:34 +01:00
timeout: method === 'post' ? 1000 * 60 : 1000 * 15,
method,
baseURL: `https://${accountDetails['auth.domain']}/api/${version}/`,
url,
params,
2020-11-17 23:57:23 +01:00
headers: {
'Content-Type': body && body instanceof FormData ? 'multipart/form-data' : 'application/json',
Accept: '*/*',
2022-11-05 01:11:09 +01:00
...userAgent,
2020-11-17 23:57:23 +01:00
...headers,
Authorization: `Bearer ${accountDetails['auth.token']}`
2020-11-17 23:57:23 +01:00
},
2022-12-21 14:38:09 +01:00
...((body as (FormData & { _parts: [][] }) | undefined)?._parts.length && { 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