2021-01-07 19:13:09 +01:00
|
|
|
import { RootState } from '@root/store'
|
2021-03-21 23:06:53 +01:00
|
|
|
import axios, { AxiosRequestConfig } from 'axios'
|
2021-10-24 00:43:00 +02:00
|
|
|
import Constants from 'expo-constants'
|
2021-02-11 01:33:31 +01:00
|
|
|
import li from 'li'
|
2022-08-12 16:44:28 +02:00
|
|
|
import handleError, { ctx } from './handleError'
|
2020-12-24 10:28:51 +01:00
|
|
|
|
2021-02-20 19:12:44 +01:00
|
|
|
export type Params = {
|
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 }
|
2020-11-17 23:57:23 +01:00
|
|
|
body?: FormData
|
2021-03-21 23:06:53 +01:00
|
|
|
extras?: Omit<
|
|
|
|
AxiosRequestConfig,
|
|
|
|
'method' | 'url' | 'params' | 'headers' | 'data'
|
|
|
|
>
|
2021-02-20 19:12:44 +01:00
|
|
|
}
|
|
|
|
|
2021-12-18 19:59:38 +01:00
|
|
|
export type InstanceResponse<T = unknown> = {
|
|
|
|
body: T
|
|
|
|
links: { prev?: string; next?: string }
|
|
|
|
}
|
|
|
|
|
2021-02-20 19:12:44 +01:00
|
|
|
const apiInstance = async <T = unknown>({
|
|
|
|
method,
|
|
|
|
version = 'v1',
|
|
|
|
url,
|
|
|
|
params,
|
|
|
|
headers,
|
|
|
|
body,
|
2021-03-21 23:06:53 +01:00
|
|
|
extras
|
2021-12-18 19:59:38 +01:00
|
|
|
}: Params): Promise<InstanceResponse<T>> => {
|
2021-01-07 19:13:09 +01:00
|
|
|
const { store } = require('@root/store')
|
2021-02-20 19:12:44 +01:00
|
|
|
const state = store.getState() as RootState
|
|
|
|
const instanceActive = state.instances.instances.findIndex(
|
|
|
|
instance => instance.active
|
|
|
|
)
|
2021-01-07 19:13:09 +01:00
|
|
|
|
2021-02-20 19:12:44 +01:00
|
|
|
let domain
|
|
|
|
let token
|
|
|
|
if (instanceActive !== -1 && state.instances.instances[instanceActive]) {
|
|
|
|
domain = state.instances.instances[instanceActive].url
|
|
|
|
token = state.instances.instances[instanceActive].token
|
2021-01-07 19:13:09 +01:00
|
|
|
} else {
|
2021-03-15 22:30:29 +01:00
|
|
|
console.warn(
|
2021-02-20 19:12:44 +01:00
|
|
|
ctx.bgRed.white.bold(' API ') + ' ' + 'No instance domain is provided'
|
|
|
|
)
|
|
|
|
return Promise.reject()
|
2021-01-07 19:13:09 +01:00
|
|
|
}
|
|
|
|
|
2020-12-21 13:56:04 +01:00
|
|
|
console.log(
|
2021-02-20 19:12:44 +01:00
|
|
|
ctx.bgGreen.bold(' API instance ') +
|
2021-01-07 19:13:09 +01:00
|
|
|
' ' +
|
|
|
|
domain +
|
2020-12-24 10:28:51 +01:00
|
|
|
' ' +
|
|
|
|
method +
|
|
|
|
ctx.green(' -> ') +
|
|
|
|
`/${url}` +
|
|
|
|
(params ? ctx.green(' -> ') : ''),
|
|
|
|
params ? params : ''
|
2020-12-21 13:56:04 +01:00
|
|
|
)
|
2020-10-26 00:27:53 +01:00
|
|
|
|
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,
|
|
|
|
baseURL: `https://${domain}/api/${version}/`,
|
|
|
|
url,
|
|
|
|
params,
|
2020-11-17 23:57:23 +01:00
|
|
|
headers: {
|
2022-05-05 00:52:57 +02:00
|
|
|
'Content-Type':
|
|
|
|
body && body instanceof FormData
|
|
|
|
? 'multipart/form-data'
|
|
|
|
: 'application/json',
|
2021-08-29 15:25:38 +02:00
|
|
|
'User-Agent': `tooot/${Constants.manifest?.version}`,
|
2021-06-02 22:27:42 +02:00
|
|
|
Accept: '*/*',
|
2020-11-17 23:57:23 +01:00
|
|
|
...headers,
|
2021-01-22 01:34:20 +01:00
|
|
|
...(token && {
|
|
|
|
Authorization: `Bearer ${token}`
|
2020-11-17 23:57:23 +01:00
|
|
|
})
|
|
|
|
},
|
2020-12-05 01:55:53 +01:00
|
|
|
...(body && { data: body }),
|
2021-03-21 23:06:53 +01:00
|
|
|
...extras
|
2020-11-17 23:57:23 +01:00
|
|
|
})
|
2021-02-11 01:33:31 +01:00
|
|
|
.then(response => {
|
|
|
|
let prev
|
|
|
|
let next
|
|
|
|
if (response.headers.link) {
|
|
|
|
const headersLinks = li.parse(response.headers.link)
|
|
|
|
prev = headersLinks.prev?.match(/_id=([0-9]*)/)[1]
|
|
|
|
next = headersLinks.next?.match(/_id=([0-9]*)/)[1]
|
|
|
|
}
|
|
|
|
return Promise.resolve({
|
|
|
|
body: response.data,
|
|
|
|
links: { prev, next }
|
|
|
|
})
|
|
|
|
})
|
2022-08-12 16:44:28 +02: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
|