2022-09-30 12:16:56 +02:00
|
|
|
import axios, { AxiosResponse } from 'axios'
|
2022-10-18 19:17:32 +02:00
|
|
|
import { Status } from '../@types'
|
2022-09-30 12:16:56 +02:00
|
|
|
|
2022-10-20 11:14:31 +02:00
|
|
|
export async function getData(url: string): Promise<{ [key: string]: Status }> {
|
2022-09-30 12:16:56 +02:00
|
|
|
try {
|
2022-10-20 11:14:31 +02:00
|
|
|
const response: AxiosResponse<Status[]> = await axios.get(url)
|
2022-10-18 19:17:32 +02:00
|
|
|
if (!response?.data || response.status !== 200)
|
|
|
|
throw Error('ERROR: no data recieved')
|
2022-10-17 14:34:22 +02:00
|
|
|
|
2022-10-19 19:05:19 +02:00
|
|
|
// transform data into object with network names as keys
|
|
|
|
let output = Object.fromEntries(
|
2022-10-18 19:58:05 +02:00
|
|
|
response.data?.map((item) => [item.network, item])
|
2022-10-18 19:17:32 +02:00
|
|
|
)
|
2022-10-19 19:05:19 +02:00
|
|
|
// make sure 'general' is always the first key
|
|
|
|
output = Object.assign({ general: output['general'] }, output)
|
2022-10-19 19:10:04 +02:00
|
|
|
console.log('Got new data', output)
|
2022-10-18 19:17:32 +02:00
|
|
|
return output
|
2022-10-05 15:21:07 +02:00
|
|
|
} catch (error) {
|
2022-10-18 16:27:41 +02:00
|
|
|
console.error(error.message)
|
2022-10-05 15:21:07 +02:00
|
|
|
}
|
|
|
|
}
|