got#RetryObject TypeScript Examples

The following examples show how to use got#RetryObject. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: index.ts    From blockfrost-js with Apache License 2.0 4 votes vote down vote up
validateOptions = (options?: Options): ValidatedOptions => {
  if (!options || (!options.customBackend && !options.projectId)) {
    throw Error('Missing customBackend or projectId option');
  }

  if (!options.projectId && !options.customBackend) {
    throw Error('Missing param projectId in options');
  }

  if (options.version && isNaN(options.version)) {
    throw Error('Param version is not a number');
  }

  if (options.requestTimeout && isNaN(options.requestTimeout)) {
    throw Error('Param requestTimeout is not a number');
  }

  const debug = options.debug ?? isDebugEnabled();

  let rateLimiter: ValidatedOptions['rateLimiter'];
  if (options.rateLimiter === false) {
    rateLimiter = false;
  } else if (
    options.rateLimiter === true ||
    options.rateLimiter === undefined
  ) {
    rateLimiter = RATE_LIMITER_DEFAULT_CONFIG;
  } else if (options.rateLimiter) {
    // custom config
    // eslint-disable-next-line prefer-destructuring
    rateLimiter = options.rateLimiter;
  }

  const errorCodesToRetry = [
    'ETIMEDOUT',
    'ECONNRESET',
    'EADDRINUSE',
    'ECONNREFUSED',
    'EPIPE',
    'ENOTFOUND',
    'ENETUNREACH',
    'EAI_AGAIN',
  ];
  return {
    customBackend: options.customBackend,
    projectId: options.projectId,
    isTestnet:
      options.isTestnet ??
      deriveTestnetOption(options.projectId, options.isTestnet),
    rateLimiter,
    version: options.version || DEFAULT_API_VERSION,
    debug,
    http2: options.http2 ?? false,
    requestTimeout: options.requestTimeout ?? 20000, // 20 seconds
    // see: https://github.com/sindresorhus/got/blob/main/documentation/7-retry.md#retry
    retrySettings: options.retrySettings ?? {
      limit: 3, // retry count
      methods: ['GET', 'PUT', 'HEAD', 'DELETE', 'OPTIONS', 'TRACE'], // no retry on POST
      statusCodes: [408, 413, 429, 500, 502, 503, 504, 521, 522, 524],
      errorCodes: [
        'ETIMEDOUT',
        'ECONNRESET',
        'EADDRINUSE',
        'ECONNREFUSED',
        'EPIPE',
        'ENOTFOUND',
        'ENETUNREACH',
        'EAI_AGAIN',
      ],
      calculateDelay: (retryObject: RetryObject) => {
        if (errorCodesToRetry.includes(retryObject.error.code)) {
          // network errors are retried only 3 times
          if (retryObject.attemptCount === 3) {
            return 0;
          }
        }
        // check if retry should be enabled, if so set 1s retry delay
        return retryObject.computedValue !== 0 ? 1000 : 0;
      },
      // maxRetryAfter: undefined,
      // backoffLimit: Number.POSITIVE_INFINITY,
      // noise: 100
    },
  };
}