tooot/src/api/client.ts

107 lines
2.8 KiB
TypeScript
Raw Normal View History

2021-01-07 19:13:09 +01:00
import { RootState } from '@root/store'
import axios from 'axios'
2020-12-24 10:28:51 +01:00
import chalk from 'chalk'
2020-10-23 09:22:17 +02:00
2020-12-24 10:28:51 +01:00
const ctx = new chalk.Instance({ level: 3 })
2021-01-07 19:13:09 +01:00
const client = async <T = unknown>({
2020-10-31 21:04:46 +01:00
method,
instance,
2021-01-07 19:13:09 +01:00
localIndex,
instanceDomain,
version = 'v1',
url,
params,
2020-11-15 22:33:09 +01:00
headers,
body,
onUploadProgress
2020-10-31 21:04:46 +01:00
}: {
2020-11-20 01:41:46 +01:00
method: 'get' | 'post' | 'put' | 'delete'
2020-10-31 21:04:46 +01:00
instance: 'local' | 'remote'
2021-01-07 19:13:09 +01:00
localIndex?: number
instanceDomain?: string
version?: 'v1' | 'v2'
url: string
params?: {
2020-10-31 21:04:46 +01:00
[key: string]: string | number | boolean
}
headers?: { [key: string]: string }
2020-11-17 23:57:23 +01:00
body?: FormData
onUploadProgress?: (progressEvent: any) => void
2021-01-07 19:13:09 +01:00
}): Promise<T> => {
const { store } = require('@root/store')
const state = (store.getState() as RootState).instances
const theLocalIndex =
localIndex !== undefined ? localIndex : state.local.activeIndex
let domain = null
2021-01-22 01:34:20 +01:00
let token = null
2021-01-07 19:13:09 +01:00
if (instance === 'remote') {
domain = instanceDomain || state.remote.url
} else {
if (theLocalIndex !== null && state.local.instances[theLocalIndex]) {
domain = state.local.instances[theLocalIndex].url
2021-01-22 01:34:20 +01:00
token = state.local.instances[theLocalIndex].token
2021-01-07 19:13:09 +01:00
} else {
console.error(
ctx.bgRed.white.bold(' API ') + ' ' + 'No instance domain is provided'
)
return Promise.reject()
}
}
2020-12-21 13:56:04 +01:00
console.log(
2020-12-24 10:28:51 +01:00
ctx.bgGreen.bold(' API ') +
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 }),
...(onUploadProgress && { onUploadProgress: onUploadProgress })
2020-11-17 23:57:23 +01:00
})
2021-01-07 19:13:09 +01:00
.then(response => Promise.resolve(response.data))
.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(
ctx.bold(' API '),
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
2020-12-28 23:20:18 +01:00
console.error(ctx.bold(' API '), ctx.bold('request'), error)
return Promise.reject()
} else {
2020-12-28 23:20:18 +01:00
console.error(ctx.bold(' API '), ctx.bold('other'), error.message)
return Promise.reject({ body: error.message })
}
2020-10-31 02:22:08 +01:00
})
2020-10-23 09:22:17 +02:00
}
2020-10-31 21:04:46 +01:00
export default client