-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapiClient.js
33 lines (29 loc) · 1.05 KB
/
apiClient.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
const axios = require('axios');
const axiosRetryModule = require('axios-retry');
const axiosRetry = axiosRetryModule.default || axiosRetryModule;
// Log to verify the type
console.log("Type of axiosRetry:", typeof axiosRetry);
// Configure axios-retry with desired options
axiosRetry(axios, {
retries: 3, // Number of retry attempts
retryDelay: axiosRetry.exponentialDelay, // Exponential backoff delay
retryCondition: (error) => {
// Retry on network errors or if the response status is in the 5xx range
return axiosRetry.isNetworkError(error) || axiosRetry.isRetryableError(error);
}
});
/**
* Fetch external data with a timeout of 5000ms (5 seconds)
* @param {string} url - The URL to fetch data from.
* @returns {Promise<any>} - The response data.
*/
async function fetchExternalData(url) {
try {
const response = await axios.get(url, { timeout: 5000 });
return response.data;
} catch (error) {
console.error(`Error fetching external data from ${url}: ${error.message}`);
throw error;
}
}
module.exports = { fetchExternalData };