(
endpoint: string,
params: Record<string, string | number | string[] | undefined>,
options?: { cacheable?: boolean; ttlMs?: number },
)
| 90 | |
| 91 | export const api = { |
| 92 | async get( |
| 93 | endpoint: string, |
| 94 | params: Record<string, string | number | string[] | undefined>, |
| 95 | options?: { cacheable?: boolean; ttlMs?: number }, |
| 96 | ): Promise<ApiResponse> { |
| 97 | const label = describeRequest(endpoint, params); |
| 98 | |
| 99 | // Check local cache first — avoids redundant network calls for immutable data |
| 100 | if (options?.cacheable) { |
| 101 | const cached = readCache(endpoint, params, options.ttlMs); |
| 102 | if (cached) { |
| 103 | return cached; |
| 104 | } |
| 105 | } |
| 106 | |
| 107 | const url = new URL(`${BASE_URL}${endpoint}`); |
| 108 | |
| 109 | // Add params to URL, handling arrays |
| 110 | for (const [key, value] of Object.entries(params)) { |
| 111 | if (value !== undefined && value !== null) { |
| 112 | if (Array.isArray(value)) { |
| 113 | value.forEach((v) => url.searchParams.append(key, v)); |
| 114 | } else { |
| 115 | url.searchParams.append(key, String(value)); |
| 116 | } |
| 117 | } |
| 118 | } |
| 119 | |
| 120 | const data = await executeRequest(url.toString(), label, {}); |
| 121 | |
| 122 | // Persist for future requests when the caller marked the response as cacheable |
| 123 | if (options?.cacheable) { |
| 124 | writeCache(endpoint, params, data, url.toString()); |
| 125 | } |
| 126 | |
| 127 | return { data, url: url.toString() }; |
| 128 | }, |
| 129 | |
| 130 | async post( |
| 131 | endpoint: string, |
nothing calls this directly
no test coverage detected