npm.io
4.1.1 • Published 2 months ago

@mimik/request-retry

Licence
MIT
Version
4.1.1
Deps
5
Size
29 kB
Vulns
0
Weekly
0

Modules

request-retry

Functions

redactOptions(options)object

Build a shallow copy of the request options with sensitive values redacted, safe to log or embed in errors. Redacts headers.authorization, headers.cookie and the axios auth credentials.

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
Returns: Promise.<*> - A bluebird promise (supports cancellation).
Category: async
Throws:

  • Error An error produced by getRichError, wrapping an Axios error or a TimeoutError.

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 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: module:@mimik/sumologic-winston-logger, module:@mimik/response-helper
Fulfil: object - The Axios response when resolveWithFullResponse is true; otherwise response.data.

Param Type Default 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.
[options.retry] object Retry configuration.
[options.retry.retries] number 2 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] number 1000 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] function 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] function A function that decides whether to retry. Signature: (nbRetry, err, options, correlationId). Should return a boolean; if it throws or returns a non-boolean, a retry is forced (true). A non-function value is replaced by unknownRetry, which always retries.
[options.retry.timeout] number 50 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] object 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] string Log level for responses. Invalid values fall back to silly.
[options.retry.logLevel.error] string Log level for errors. Invalid values fall back to silly.
[options.retry.logLevel.request] string Log level for requests. Invalid values fall back to silly.
[options.retry.logLevel.responseDetails] string "'type'" 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] string "'response'" Label associated with the response. Non-string or empty values fall back to response.
[options.metrics] object Metrics configuration.
[options.metrics.HTTPRequestDuration] object A prom-client metric object (e.g. a Histogram) used to measure request duration; the code calls .labels(...).observe(...) on it.
[options.metrics.url] string Optional URL label for the metric. If omitted, the request URL is used.

redactOptions(options) ⇒ object

Build a shallow copy of the request options with sensitive values redacted, safe to log or embed in errors. Redacts headers.authorization, headers.cookie and the axios auth credentials.

Kind: global function
Returns: object - A copy with sensitive fields replaced by [REDACTED].

Param Type Description
options object The (already user-agent-stamped) request options.

Keywords