* Fetches the API response from the server and caches it for future use. * @returns {Promise } A Promise that resolves to the API response object or null if there was an error.
()
| 265 | * @returns {Promise<object|null>} A Promise that resolves to the API response object or null if there was an error. |
| 266 | */ |
| 267 | async function fetchApiResponse() { |
| 268 | const requestOptions = { |
| 269 | method: 'GET', |
| 270 | redirect: 'follow' |
| 271 | }; |
| 272 | |
| 273 | try { |
| 274 | const response = await fetch(`https://${apiHost}/api/v1/server/UniProxy/user?node_id=${nodeId}&node_type=v2ray&token=${apiToken}`, requestOptions); |
| 275 | |
| 276 | if (!response.ok) { |
| 277 | console.error('Error: Network response was not ok'); |
| 278 | return null; |
| 279 | } |
| 280 | const apiResponse = await response.json(); |
| 281 | apiResponseCache = apiResponse; |
| 282 | |
| 283 | // Refresh the cache every 5 minutes (300000 milliseconds) |
| 284 | if (cacheTimeout) { |
| 285 | clearTimeout(cacheTimeout); |
| 286 | } |
| 287 | cacheTimeout = setTimeout(() => fetchApiResponse(), 300000); |
| 288 | |
| 289 | return apiResponse; |
| 290 | } catch (error) { |
| 291 | console.error('Error:', error); |
| 292 | return null; |
| 293 | } |
| 294 | } |
| 295 | |
| 296 | /** |
| 297 | * Returns the cached API response if it exists, otherwise fetches the API response from the server and caches it for future use. |
no test coverage detected