HEX
Server: Apache
System: Linux WWW 6.1.0-40-amd64 #1 SMP PREEMPT_DYNAMIC Debian 6.1.153-1 (2025-09-20) x86_64
User: web11 (1011)
PHP: 8.2.29
Disabled: NONE
Upload Files
File: /var/www/itself.kauko.lt/wp-content/plugins/widget-1746629063/js/utils/restAPI.ts
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<string, AxiosResponse<unknown> | undefined> = {}

export const getCached = <T, D>(endpoint: string, refresh = false, config?: AxiosRequestConfig<D>): Promise<AxiosResponse<T, D>> =>
	!refresh && GET_CACHE[endpoint]
		? Promise.resolve(<AxiosResponse<T, D>> GET_CACHE[endpoint])
		: axios
			.get<T, AxiosResponse<T, D>, D>(getRestUrl(endpoint), config)
			.then(response => {
				GET_CACHE[endpoint] = response
				return response
			})