import axios , { AxiosResponse } from 'axios' ; import handleError from './ErrorHandler.js' ; import PokeAPIOptions from '../interfaces/PokeAPIOptions.js' ; async function getJSON < T > ( values , url , callback ? ) { const options = { baseURL : `${ values . protocol }${ values . hostName }/` , timeout : values . timeout , } ; try { const cachedResult = values . cache . get < T > ( url ) ; if ( cachedResult ) { if ( callback ) { callback ( cachedResult ) ; } return cachedResult ; } const response = await axios . get < T > ( url , options ) ; if ( response . status !== 200 ) { handleError ( response , callback ) ; } else { const responseData = response . data ; if ( values . cacheLimit > 0 ) { values . cache . set < T > ( url , responseData , values . cacheLimit ) ; } if ( callback ) { callback ( responseData ) ; } else { return responseData ; } } } catch ( error ) { handleError ( error , callback ) ; } } export default getJSON ;