npm package: @mimik/request-retry
Description: Request retry wrapping axios
Author: mimik technology inc
Latest version: 4.0.12

<a name="module_request-retry"></a>

## request-retry
**Example**  
```js
import { rpRetry } from '@mimik/request-retry';
// or
import rp from '@mimik/request-retry';
```
<a name="module_request-retry..rpRetry"></a>

### request-retry~rpRetry(options) ⇒ <code>Promise.&lt;\*&gt;</code>
Make a request with retries.

**Kind**: inner method of [<code>request-retry</code>](#module_request-retry)  
**Returns**: <code>Promise.&lt;\*&gt;</code> - A bluebird promise (supports cancellation).  
**Category**: async  
**Throws**:

- <code>Error</code> An error produced by `getRichError`, wrapping an
[Axios](https://www.npmjs.com/package/axios) error or a `TimeoutError`.

The **default** `retryStrategy` is:

```javascript
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 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`:
- `uri` takes precedence over `url`.
- If `json` is an object, `body` takes precedence over `json`; both are mapped to Axios `data`.
- `qs` is mapped to Axios `params`.

**Requires**: <code>module:@mimik/sumologic-winston-logger</code>, <code>module:@mimik/response-helper</code>  
**Fulfil**: <code>object</code> - The Axios response when `resolveWithFullResponse` is `true`; otherwise `response.data`.  

| Param | Type | Default | Description |
| --- | --- | --- | --- |
| options | <code>object</code> |  | Options for the request (similar to [axios](https://www.npmjs.com/package/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. |
| [options.retry] | <code>object</code> |  | Retry configuration. |
| [options.retry.retries] | <code>number</code> | <code>2</code> | Maximum number of retries, independent of the `retryStrategy`. If the value is `< 0`, it is set to `0`; if `> 15`, it is set to `15`. |
| [options.retry.delay] | <code>number</code> | <code>1000</code> | Delay between retries in milliseconds when no `delayStrategy` is provided. If the value is `< 20`, it is set to `20`; if `> 30000`, it is set to `30000`. |
| [options.retry.delayStrategy] | <code>function</code> |  | A function that returns the delay (in milliseconds) to wait before the next retry. Signature: `(nbRetry, err, options, correlationId)`. Must return a number `> 0` and `< 30000`; otherwise `delay` is used. |
| [options.retry.retryStrategy] | <code>function</code> |  | A function that decides whether to retry. Signature: `(nbRetry, err, options, correlationId)`. Must return a boolean; otherwise it is ignored. |
| [options.retry.timeout] | <code>number</code> | <code>50</code> | Request timeout (in seconds) applied to the initial request and all retries. If reached, a `TimeoutError` is thrown. Values `< 10` are set to `10`; values `> 120` are set to `120`. |
| [options.retry.logLevel] | <code>object</code> |  | Controls logging behavior. When omitted, requests and responses are logged at `info` with full details, and errors at `warn`. When provided but incomplete, only the provided keys are active; missing keys disable that logging. Non-object values are ignored and defaults apply. |
| [options.retry.logLevel.response] | <code>string</code> |  | Log level for responses. Invalid values fall back to `silly`. |
| [options.retry.logLevel.error] | <code>string</code> |  | Log level for errors. Invalid values fall back to `silly`. |
| [options.retry.logLevel.request] | <code>string</code> |  | Log level for requests. Invalid values fall back to `silly`. |
| [options.retry.logLevel.responseDetails] | <code>string</code> | <code>&quot;&#x27;type&#x27;&quot;</code> | Detail level for the response: `count`, `type`, or `full`. Invalid values fall back to `type`. When `logLevel` is omitted entirely, the default is `full`. |
| [options.retry.logLevel.responseName] | <code>string</code> | <code>&quot;&#x27;response&#x27;&quot;</code> | Label associated with the response. Non-string or empty values fall back to `response`. |
| [options.metrics] | <code>object</code> |  | Metrics configuration. |
| [options.metrics.HTTPRequestDuration] | <code>function</code> |  | A `prom-client` metric function to measure request duration. |
| [options.metrics.url] | <code>string</code> |  | Optional URL label for the metric. If omitted, the request URL is used. |