Request Retry
Request retry is the axios interface implementing 3 new parameters [fallbackResponse, cache, maxRetry].
In case the request fails, it returns the object stored in the fallbackResponse parameter:
import * as rp from 'request-retry';
const options = {
url: `${Env.get('API_URL')}`,
headers: { 'Content-Type': 'application/json' },
fallbackResponse: []
}
return rp.get(options)
Or also, if the request fails, execute a function that will receive the error and the options used to make the request as the only parameter.
import * as rp from 'request-retry';
const options = {
url: `${Env.get('API_URL')}`,
headers: { 'Content-Type': 'application/json' },
fallbackResponse: (e, data) => e.response.status === 400 ? [] : ['fake'],
}
return rp.get(options)
Cache the service response in case the http code is 200:
import * as rp from 'request-retry';
const options = {
url: `${Env.get('API_URL')}`,
headers: { 'Content-Type': 'application/json' },
cache: 1
}
return rp.get(options)
If the error code is 504 or 503, the request is retried the configured number of times:
import * as rp from 'request-retry';
const options = {
url: `${Env.get('API_URL')}`,
headers: { 'Content-Type': 'application/json' },
maxRetry: 3
}
return rp.get(options)