HEX
Server: Apache/2.4.41
System: Linux mainweb 5.4.0-182-generic #202-Ubuntu SMP Fri Apr 26 12:29:36 UTC 2024 x86_64
User: nationalmedicaregrp (1119)
PHP: 8.3.7
Disabled: exec,passthru,shell_exec,system,popen,proc_open,pcntl_exec
Upload Files
File: /home/nationalfundingsolutionsgrp/public_html/wp-content/plugins/code-snippets/js/utils/api/wp.ts
import axios, { AxiosResponse } from 'axios'

const REST_BASE = window.CODE_SNIPPETS?.restAPI.base ?? ''

export const trimLeadingSlash = (path: string): string =>
	'/' === path.charAt(0) ? path.slice(1) : path

export const trimTrailingSlash = (path: string): string =>
	'/' === path.charAt(path.length - 1) ? path.slice(0, -1) : path

const getRestUrl = (endpoint: string): string =>
	`${trimTrailingSlash(REST_BASE)}/${trimLeadingSlash(endpoint)}`

const GET_CACHE: Record<string, AxiosResponse> = {}

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