import axios, { AxiosResponse } from 'axios' import { trimLeadingChar, trimTrailingChar } from '../general' const REST_BASE = window.CODE_SNIPPETS?.restAPI.base ?? '' const getRestUrl = (endpoint: string): string => `${trimTrailingChar(REST_BASE, '/')}/${trimLeadingChar(endpoint, '/')}` const GET_CACHE: Record = {} export const getCached = (endpoint: string, refresh = false): Promise> => !refresh && GET_CACHE[endpoint] ? Promise.resolve(GET_CACHE[endpoint]) : axios.get(getRestUrl(endpoint)) .then(response => { GET_CACHE[endpoint] = response return response })