@mimik/request-retry

3.0.2 • Public • Published

request-retry

Example

const { rpRetry } = require('@mimik/request-retry');

request-retry~rpRetry(options) ⇒ Promise

Make a request with retry.

Kind: inner method of request-retry
Returns: Promise - .
Category: async
Throws:

  • Promise Will throw an error generated by getRichError encapsulating an error generated by request-promise or a TimeoutError.

The following properties are added to the options object under the retry property:

  • retries {int}: maximum number of retries independent to the retryStrategy. The default is 2. If the value is less than 0 the value is set to 0, and if the value is more that 15 the value is set to 15,
  • delay {int}: in millisecond delay between retries if there is no retryDelay strategy. The default is 1000 ms. If the value is less than 20 ms the value is set to 20 ms, and if the value is more than 30000 ms the value is set to 30000 ms,
  • delayStrategy {function}: function describing the delay strategy. The parameters are (nbRetry, err, options, correlationId). Must return a number of miliseconds which is greater to 0 ms but less that 30000 ms, if not the value of delay will be used,
  • logLevel {object}: has the following properties:
    • response: log level to be set for response. The default is silly or if the value is wrong it is set to silly,
    • error: log level to be set for error. The default is silly or if the value is wrong it is set to silly,
    • request: log level to be set for request: The default is silly or if the value is wrong it is set to silly,
    • responseDetails: level of detail when diplaying the response: count, type, full. The default is type or if the value is wrong it is set to type,
    • responseName: name to associate with the response. The default is responseor is the value is not a string or and empty string it is set to response,
  • timeout {int}: in seconds the timeout to be set for the request and retries. If the timeout is reached, a TimeoutError will be generated. The default is 50 seconds. If the value is less than 10 seconds the value is set to 10 seconds, and if the value is more than 120 seconds the value is set to 120 seconds,
  • retryStrategy {function}: function describing the retry strategy. The parameters are (nbRetry, err, options, correlationId). Must return a boolean, if not the function strategy is ignored. The following properties are added to the options object under the 'metrics' property:
  • HTTPRequestDuration function: prom-client function to measure the delay of the request,
  • url: optional url to be added for labelling the metric. If not present the url of the request will be used.

The default retryStategy is:

defaultRetry = (...args) => {
  const { statusCode } = args[1]; // err

  return (statusCode && (Math.floor(statusCode / 100) === 5 || statusCode === 429)) || (!statusCode && !args[1].message.includes('Invalid URI'));
};

If logLevel is empty, request and response will be logged as info and the response will be in response property with full details. Error on the request will generate a warning. If logLevel is not empty but not complete, logLevel will take control over default.

If not alredy set,the user agent will the set to mimik-{serverType}/{serverVersion}/{serverId} {architecture} {node};

To facilitate the transition from request-promise the following action are taken on options:

  • uri takes precednce on url
  • body takes precedence on json if json is an object and are used to assign data
  • qs is used to assign to params

Requires: module:@mimik/sumologic-winston-logger, module:@mimik/response-helper
Fulfil: object - Response of the axios response with option resolveWithFullResponse set to true otherwise only response.data is returned.

Param Type Description
options object Options for the request. Similar to axios options. validateStatus options is disabled, an error will be created for statusCode outside of [200, 300[. The options resolveWithFullResponse is added and if set to true, the response will be a full axios response. If set to false or missing only reponse.data will be returned.

Readme

Keywords

Package Sidebar

Install

npm i @mimik/request-retry

Weekly Downloads

0

Version

3.0.2

License

MIT

Unpacked Size

46.5 kB

Total Files

18

Last publish

Collaborators

  • sasan.raisdana
  • miburger
  • hofachiang
  • mimik-npm-editor
  • mimikopensource