# code-snippets/3.6.3/js/utils/api/wp.ts

Code Snippets, version 3.6.3. 19 lines.

- Page: https://pluginprobe.com/plugins/code-snippets/3.6.3/code/js/utils/api/wp.ts
- Raw: https://pluginprobe.com/plugins/code-snippets/3.6.3/raw/js/utils/api/wp.ts
- Modified: 2023-11-13T00:45:24+00:00

Line numbers below start at 1. Link to a line or a range by appending a fragment to the
page URL, for example `https://pluginprobe.com/plugins/code-snippets/3.6.3/code/js/utils/api/wp.ts#L10-L20`.

```typescript
import axios, { AxiosResponse } from 'axios'
import { trimLeadingChar, trimTrailingChar } from '../general'

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

const getRestUrl = (endpoint: string): string =>
	`${trimTrailingChar(REST_BASE, '/')}/${trimLeadingChar(endpoint, '/')}`

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

export const getCached = <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
			})

```
