| 1 |
import axios, { AxiosResponse } from 'axios' |
| 2 |
import { trimLeadingChar, trimTrailingChar } from '../general' |
| 3 |
|
| 4 |
const REST_BASE = window.CODE_SNIPPETS?.restAPI.base ?? '' |
| 5 |
|
| 6 |
const getRestUrl = (endpoint: string): string => |
| 7 |
`${trimTrailingChar(REST_BASE, '/')}/${trimLeadingChar(endpoint, '/')}` |
| 8 |
|
| 9 |
const GET_CACHE: Record<string, AxiosResponse> = {} |
| 10 |
|
| 11 |
export const getCached = <T>(endpoint: string, refresh = false): Promise<AxiosResponse<T>> => |
| 12 |
!refresh && GET_CACHE[endpoint] ? |
| 13 |
Promise.resolve(GET_CACHE[endpoint]) : |
| 14 |
axios.get<T>(getRestUrl(endpoint)) |
| 15 |
.then(response => { |
| 16 |
GET_CACHE[endpoint] = response |
| 17 |
return response |
| 18 |
}) |
| 19 |
|