tooot/src/api/instance.ts

121 lines
3.1 KiB
TypeScript
Raw Normal View History

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'
2020-12-24 10:28:51 +01:00
import chalk from 'chalk'
2021-02-11 01:33:31 +01:00
import li from 'li'
2020-10-23 09:22:17 +02:00
2020-12-24 10:28:51 +01:00
const ctx = new chalk.Instance({ level: 3 })
2021-02-20 19:12:44 +01:00
export type Params = {
2020-11-20 01:41:46 +01:00
method: 'get' | 'post' | 'put' | 'delete'
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
2021-03-21 23:06:53 +01:00
extras?: Omit<
AxiosRequestConfig,
'method' | 'url' | 'params' | 'headers' | 'data'
>
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-02-20 19:12:44 +01:00
}: Params): Promise<{ body: T; links: { prev?: string; next?: string } }> => {
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
return axios({
2021-01-04 14:55:34 +01:00
timeout: method === 'post' ? 1000 * 60 : 1000 * 15,
method,
baseURL: `https://${domain}/api/${version}/`,
url,
params,
2020-11-17 23:57:23 +01:00
headers: {
'Content-Type': 'application/json',
...headers,
2021-01-22 01:34:20 +01:00
...(token && {
Authorization: `Bearer ${token}`
2020-11-17 23:57:23 +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 }
})
})
.catch(error => {
if (error.response) {
// The request was made and the server responded with a status code
// that falls out of the range of 2xx
2020-12-28 23:20:18 +01:00
console.error(
2021-02-20 19:12:44 +01:00
ctx.bold(' API instance '),
2020-12-28 23:20:18 +01:00
ctx.bold('response'),
error.response.status,
2021-01-19 01:13:45 +01:00
error.response.data.error
2020-12-28 23:20:18 +01:00
)
return Promise.reject(error.response)
} else if (error.request) {
// The request was made but no response was received
// `error.request` is an instance of XMLHttpRequest in the browser and an instance of
// http.ClientRequest in node.js
2021-02-20 19:12:44 +01:00
console.error(ctx.bold(' API instance '), ctx.bold('request'), error)
return Promise.reject()
} else {
2021-02-11 01:33:31 +01:00
console.error(
2021-02-20 19:12:44 +01:00
ctx.bold(' API instance '),
2021-02-11 01:33:31 +01:00
ctx.bold('internal'),
error.message,
url
)
return Promise.reject()
}
2020-10-31 02:22:08 +01:00
})
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