@mimik/request-retry
v4.0.6
Published
Request retry wrapping axios
Downloads
213
Readme
request-retry
Example
import { rpRetry } from '@mimik/request-retry';
or
import rp from '@mimik/request-retry';request-retry~rpRetry(options) ⇒ Promise
Make a request with retries.
Kind: inner method of request-retry
Category: async
Throws:
- Error An error produced by
getRichError, wrapping an error from request-promise or aTimeoutError.
The following properties may be added to the options object under the retry property:
- retries
{int}: Maximum number of retries, independent of theretryStrategy. Default:2. If the value is< 0it is set to0; if> 15it is set to15. - delay
{int}: Delay between retries in milliseconds when nodelayStrategyis provided. Default:1000. If the value is< 20it is set to20; if> 30000it is set to30000. - delayStrategy
{function}: A function that returns the delay (in milliseconds) to wait before the next retry. Signature:(nbRetry, err, options, correlationId). Must return a number> 0and< 30000; otherwisedelayis used. - logLevel
{object}: Controls logging behavior:- response: Log level for responses. Default:
silly(invalid values fall back tosilly). - error: Log level for errors. Default:
silly(invalid values fall back tosilly). - request: Log level for requests. Default:
silly(invalid values fall back tosilly). - responseDetails: Detail level when displaying the response:
count,type, orfull. Default:type(invalid values fall back totype). - responseName: Label associated with the response. Default:
response(non-string or empty values fall back toresponse).
- response: Log level for responses. Default:
- timeout
{int}: Request timeout (in seconds) applied to the initial request and all retries. If reached, aTimeoutErroris thrown. Default:50. Values< 10are set to10; values> 120are set to120. - retryStrategy
{function}: A function that decides whether to retry. Signature:(nbRetry, err, options, correlationId). Must return a boolean; otherwise it is ignored.
The following properties may be added to the options object under the metrics property:
- HTTPRequestDuration
{function}: Aprom-clientmetric function to measure request duration. - url
{string}: Optional URL label for the metric. If omitted, the request URL is used.
The default retryStrategy is:
defaultRetry = (...args) => {
const { statusCode } = args[1]; // err
// Retry on 5xx and 429. If there is no statusCode, retry unless it's an "Invalid URI" error.
return (statusCode && (Math.floor(statusCode / 100) === 5 || statusCode === 429))
|| (!statusCode && !args[1].message.includes('Invalid URI'));
};If logLevel is empty, requests and responses are logged at info, the response is logged with full details
under the response property, and request errors generate a warn. If logLevel is provided but incomplete,
the provided keys override the defaults.
If not already set, the User-Agent header is set to:
mimik-{serverType}/{serverVersion}/{serverId} {architecture} {node}.
To ease migration from request-promise, the following adjustments are applied to options:
uritakes precedence overurl.- If
jsonis an object,bodytakes precedence overjson; both are mapped to Axiosdata. qsis mapped to Axiosparams.
Requires: module:@mimik/sumologic-winston-logger, module:@mimik/response-helper
Fulfil: object - The Axios response when resolveWithFullResponse is true; otherwise response.data.
| Param | Type | Description |
| --- | --- | --- |
| options | object | Options for the request (similar to axios). The validateStatus option is disabled: an error is thrown for status codes outside [200, 300). An additional option, resolveWithFullResponse, controls the return shape: when true, the full Axios response object is returned; when false or omitted, only response.data is returned. |
