1.0.0 • Published 2 years ago

@argueta10/http-request-retry v1.0.0

Weekly downloads
-
License
ISC
Repository
github
Last release
2 years ago

Request Retry

Request retry is the axios interface implementing 3 new parameters fallbackResponse, cache, maxRetry.

  • fallbackResponse

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) //in case it fails the object is returned []

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) //in case it fails, the function passed by parameter will be executed
  • cache

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) 
  • maxRetry

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)