2022-09-22 20:18:32 +02:00
|
|
|
import { LoggerInstance } from '@oceanprotocol/lib'
|
2021-10-13 18:48:59 +02:00
|
|
|
import axios, { AxiosResponse } from 'axios'
|
|
|
|
|
|
|
|
export async function fetchData(url: string): Promise<AxiosResponse['data']> {
|
|
|
|
try {
|
|
|
|
const response = await axios(url)
|
2022-09-22 20:18:32 +02:00
|
|
|
return response?.data
|
2021-10-13 18:48:59 +02:00
|
|
|
} catch (error) {
|
2022-09-22 20:18:32 +02:00
|
|
|
if (error.response) {
|
|
|
|
// The request was made and the server responded with a status code
|
|
|
|
// that falls out of the range of 2xx
|
|
|
|
LoggerInstance.error(`Non-200 response from ${url}:`, 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
|
|
|
|
LoggerInstance.error('No response with:', error.request)
|
|
|
|
} else {
|
|
|
|
// Something happened in setting up the request that triggered an Error
|
|
|
|
LoggerInstance.error('Error in setting up request:', error.message)
|
|
|
|
}
|
|
|
|
LoggerInstance.error(error.message)
|
2021-10-13 18:48:59 +02:00
|
|
|
}
|
|
|
|
}
|