status-frontend/src/utils/getData.ts

23 lines
798 B
TypeScript
Raw Normal View History

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'
import { statusApiUri } from '../../app.config'
2022-09-30 12:16:56 +02:00
2022-10-18 19:17:32 +02:00
export async function getData(): Promise<{ [key: string]: Status }> {
2022-09-30 12:16:56 +02:00
try {
2022-10-18 19:17:32 +02:00
const response: AxiosResponse<Status[]> = await axios.get(`${statusApiUri}`)
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)
console.log('Got new data', JSON.stringify(output))
2022-10-18 19:17:32 +02:00
return output
} catch (error) {
2022-10-18 16:27:41 +02:00
console.error(error.message)
}
}