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