Dive into secure and efficient coding practices with our curated list of the top 10 examples showcasing 'axios-retry' in functional components in JavaScript. Our advanced machine learning engine meticulously scans each line of code, cross-referencing millions of open source libraries to ensure your implementation is not just functional, but also robust and secure. Elevate your React applications to new heights by mastering the art of handling side effects, API calls, and asynchronous operations with confidence and precision.
const uploadFileToUrlWithRetries = async (
file,
url,
{ onUploadProgress, onSuccess, onError }
) => {
const config = {
onUploadProgress,
};
// Retry up to 5 times with a 30s delay. axiosRetry interceptor means that 'catch' won't be
// called until all tries fail.
const client = axios.create();
axiosRetry(client, {
retries: 5,
retryDelay: () => 30000,
retryCondition: () => true,
});
return client
.put(url, file, config)
.then(onSuccess)
.catch(onError);
};
signedRequest.baseURL = url;
let client = axios.create(signedRequest);
// Allow user configurable delay, or built-in exponential delay
let retryDelay = function() {
return 0;
};
if (config.retryDelay === 'exponential') {
retryDelay = axiosRetry.exponentialDelay;
} else if (typeof config.retryDelay === 'number') {
retryDelay = () => parseInt(config.retryDelay);
} else if (typeof config.retryDelay === 'function') {
retryDelay = config.retryDelay;
}
axiosRetry(client, {
retries: config.retries,
retryCondition: config.retryCondition,
retryDelay,
});
return client.request({method: verb});
}
signedRequest.method = verb;
signedRequest.url = url;
return axios(signedRequest);
};
var arena = function( config ) {
var client = axios.create({
responseType: 'json',
baseURL: config.arena_api,
// params: { noCache: true },
});
axiosRetry( client, { retries: 3 });
function makeRequest( root, slug, action ) {
return client.get( path.join( root, slug, action ) );
}
return {
channel: function( slug, action ) {
return makeRequest( 'channels', slug || '', action || '');
},
block: function( slug, action ) {
import base64 from 'base-64';
// eslint-disable-next-line import/no-unresolved
import { Platform } from 'react-native';
import axios from 'axios';
import axiosRetry from 'axios-retry';
import assert from './helpers/assert';
import validate from './helpers/validate';
import uid from './helpers/uid';
axiosRetry(axios, { retries: 3 });
const VERSION = require('../package.json').version;
const noop = () => {};
/**
* Expose an `Analytics` client.
*/
export default class Analytics {
/**
* Initialize a new `Analytics` with your Segment project's `writeKey` and an
* optional dictionary of `options`.
*
* @param {String} writeKey
* @param {Object} options (optional)
* @property {Number} flushAt (default: 20)
let signedRequest = {
headers: headers,
timeout: timeout,
data: body
};
if (config.retries !== undefined) {
signedRequest.baseURL = url;
let client = axios.create(signedRequest);
// Allow user configurable delay, or built-in exponential delay
let retryDelay = function() {
return 0;
};
if (config.retryDelay === 'exponential') {
retryDelay = axiosRetry.exponentialDelay;
} else if (typeof config.retryDelay === 'number') {
retryDelay = () => parseInt(config.retryDelay);
} else if (typeof config.retryDelay === 'function') {
retryDelay = config.retryDelay;
}
axiosRetry(client, {
retries: config.retries,
retryCondition: config.retryCondition,
retryDelay,
});
return client.request({method: verb});
}
signedRequest.method = verb;
signedRequest.url = url;
return axios(signedRequest);
url += '?' + queryString;
}
let simpleHttpRequest = {
headers: headers,
timeout: timeout,
data: body
};
if (config.retries !== undefined) {
simpleHttpRequest.baseURL = url;
let client = axios.create(simpleHttpRequest);
// Allow user configurable delay, or built-in exponential delay
let retryDelay = () => 0;
if (config.retryDelay === 'exponential') {
retryDelay = axiosRetry.exponentialDelay;
} else if (typeof config.retryDelay === 'number') {
retryDelay = () => parseInt(config.retryDelay);
} else if (typeof config.retryDelay === 'function') {
retryDelay = config.retryDelay;
}
axiosRetry(client, {
retries: config.retries,
retryCondition: config.retryCondition,
retryDelay,
});
return client.request({method: verb});
}
simpleHttpRequest.method = verb;
simpleHttpRequest.url = url;
return axios(simpleHttpRequest);
// and will retry POST (normally POST requests are not
// retried since they are not idempotent, but in FHIR
// we use POST for searches and ingest, both of which are
// idempotent (assuming ingest uses client supplied ids
// which it usually does.), and will retry timeouts.
// This all makes ingest in particular much more reliable,
// which is important when ingesting large datasets.
const shouldRetry = error => {
return axiosRetry.isNetworkError(error) ||
axiosRetry.isRetryableError(error) ||
error.code === 'ECONNABORTED' ||
(error.response && error.response.status === 429);
};
const retryConfig = {
retries: 5,
retryDelay: axiosRetry.exponentialDelay,
retryCondition: shouldRetry,
shouldResetTimeout: true
};
axios.defaults.timeout = 16000; // 16 seconds
axios.defaults.headers.common['User-Agent'] = `${name}/${version}`;
function request (options) {
const environment = config.getEnvironment();
const account = options.account || config.get(`${environment}.defaults.account`);
if (!account) {
throw new Error(`Account needs to be set with 'lo defaults' or specified with the -a option.`);
}
const proxy = configureProxy.configureProxy();
_isErrorRetryable (error) {
// Retry Network Errors.
if (axiosRetry.isNetworkError(error)) {
return true
}
if (!error.response) {
// Cannot determine if the request can be retried
return false
}
// Retry Server Errors (5xx).
if (error.response.status >= 500 && error.response.status <= 599) {
return true
}
// Retry if rate limited.
if (error.response.status === 429) {
return true
export function createClient (opts: ClientOptions): AxiosInstance {
const {baseURL, authToken, userAgent, timeout = DEFAULT_TIMEOUT_MS} = opts
const headers = {
'Authorization': `bearer ${authToken}`,
'User-Agent': userAgent,
}
const http = axios.create({
baseURL,
headers,
timeout,
})
retry(http)
http.interceptors.response.use(handleResponse, (err) => {
if (err.response && err.response.config) {
const {url, method} = err.response.config
console.log(`Error calling ${method.toUpperCase()} ${url}`)
}
try {
delete err.response.request
delete err.response.config
delete err.config.res
delete err.config.data
} catch (e) {}
return Promise.reject(err)
})
return http
export const isNetworkErrorOrRouterTimeout = (e: any) => {
if (isNetworkError(e)) {
console.warn('Retry from network error', e.message)
return true
}
if (e && e.response && e.response.data && e.response.data.code === TIMEOUT_CODE) {
console.warn('Retry from timeout', e.message)
return true
}
return false
}