@mimik/request-retry
v3.0.2
Published
Request retry wrapping axios
Downloads
202
Readme
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 is2
. If the value is less than0
the value is set to0
, and if the value is more that15
the value is set to15
, - delay
{int}
: in millisecond delay between retries if there is no retryDelay strategy. The default is1000 ms
. If the value is less than20 ms
the value is set to20 ms
, and if the value is more than30000 ms
the value is set to30000 ms
, - delayStrategy
{function}
: function describing the delay strategy. The parameters are (nbRetry
,err
,options
,correlationId
). Must return anumber
of miliseconds which is greater to 0 ms but less that 30000 ms, if not the value ofdelay
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 tosilly
, - error: log level to be set for error. The default is
silly
or if the value is wrong it is set tosilly
, - request: log level to be set for request: The default is
silly
or if the value is wrong it is set tosilly
, - responseDetails: level of detail when diplaying the response:
count
,type
,full
. The default istype
or if the value is wrong it is set totype
, - responseName: name to associate with the response. The default is
response
or is the value is not a string or and empty string it is set toresponse
,
- response: log level to be set for response. The default is
- 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 is50 seconds
. If the value is less than10 seconds
the value is set to10 seconds
, and if the value is more than120 seconds
the value is set to120 seconds
, - retryStrategy
{function}
: function describing the retry strategy. The parameters are (nbRetry
,err
,options
,correlationId
). Must return aboolean
, if not the function strategy is ignored. The following properties are added to theoptions
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 onurl
body
takes precedence onjson
ifjson
is an object and are used to assigndata
qs
is used to assign toparams
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. |