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