@adobe/node-fetch-retry

中文文档
2.2.0发布于 2022/03/08Apache-2.0TypeScriptCommonJS近 30 天下载 3.9万

Library for performing retries on HTTP requests

原始使用文档
文档目录10 节
  1. Installation
  2. Usage
  3. Default Behavior
  4. Optional Custom Parameters
  5. Custom Parameter Examples
  6. Disable Retry
  7. Additional notes on retry duration
  8. Apache OpenWhisk Action Support
  9. Contributing
  10. Licensing

Version License Travis

node-fetch-retry

Node Module for performing retries for HTTP requests.

It is a wrapper around node-fetch library. It has default retry logic built in as described below, as well as configurable parameters. It also has built-in support for Apache OpenWhisk actions, adjusting the timeout to reflect the action timeout.

Installation

npm install @adobe/node-fetch-retry

Usage

This library works the same as the normal fetch api, but with some added features.

Default Behavior

Without configuring any parameters, the retry behavior will be as follows:

const fetch = require('@adobe/node-fetch-retry');

async main() {
    const response = await fetch(url);
}

This example uses only custom headers and will use default retry settings:

const fetch = require('@adobe/node-fetch-retry');

async main() {
    const response = await fetch(url, {
        headers: {
            'custom-header': '<<put custom header value here>>'
        }
    });
}

Optional Custom Parameters

All the retry options are configurable and can be set in retryOptions in the options object passed to fetch.

ParameterFormatDescriptionEnvironment variableDefault Value
retryMaxDurationNumbertime in milliseconds to retry until throwing an errorNODE_FETCH_RETRY_MAX_RETRY60000 ms
retryInitialDelayNumbertime in milliseconds to wait between retriesNODE_FETCH_RETRY_INITIAL_WAIT100 ms
retryBackoffNumberbackoff factor for wait time between retriesNODE_FETCH_RETRY_BACKOFF2.0
retryOnHttpResponseFunctiona function determining whether to retry given the HTTP response. Can be asynchronousnoneretry on all 5xx errors
retryOnHttpErrorFunctiona function determining whether to retry given the HTTP error exception thrown. Can be asynchronousnoneretry on all FetchError's of type system
socketTimeoutNumbertime until socket timeout in milliseconds. Note: if socketTimeout is >= retryMaxDuration, it will automatically adjust the socket timeout to be exactly half of the retryMaxDuration. To disable this feature, see forceSocketTimeout belowNODE_FETCH_RETRY_SOCKET_TIMEOUT30000 ms
forceSocketTimeoutBooleanIf true, socket timeout will be forced to use socketTimeout property declared regardless of the retryMaxDuration. Note: this feature was designed to help with unit testing and is not intended to be used in practiceNODE_FETCH_RETRY_FORCE_TIMEOUTfalse

Note: the environment variables override the default values if the corresponding parameter is not set. These are designed to help with unit testing. Passed in parameters will still override the environment variables

Custom Parameter Examples

This example decreases the retryMaxDuration and makes the retry delay a static 500ms. This will do no more than 4 retries.

const fetch = require('@adobe/node-fetch-retry');

async main() {
    const response = await fetch(url, {
        retryOptions: {
            retryMaxDuration: 2000,  // 30s retry max duration
            retryInitialDelay: 500,
            retryBackoff: 1.0 // no backoff
        }
    });
}

This example shows how to configure retries on specific HTTP responses:

const fetch = require('@adobe/node-fetch-retry');

async main() {
    const response = await fetch(url, {
        retryOptions: {
            retryOnHttpResponse: function (response) {
                if ( (response.status >= 500) || response.status >= 400) { // retry on all 5xx and all 4xx errors
                    return true;
                }
            }
        }
    });
}

This example uses custom socketTimeout values:

const fetch = require('@adobe/node-fetch-retry');

async main() {
    const response = await fetch(url, {
        retryOptions: {
            retryMaxDuration: 300000, // 5min retry duration
            socketTimeout: 60000, //  60s socket timeout
        }
    });
}

This example uses custom socketTimeout values and custom headers:

const fetch = require('@adobe/node-fetch-retry');

async main() {
    const response = await fetch(url, {
        retryOptions: {
            retryMaxDuration: 300000, // 5min retry duration
            socketTimeout: 60000, //  60s socket timeout
        },
        headers: {
            'custom-header': '<<put custom header value here>>'
        }
    });
}

This example shows how to retry on all HTTP errors thrown as an exception:

const fetch = require('@adobe/node-fetch-retry');

async main() {
    const response = await fetch(url, {
        retryOptions: {
            retryOnHttpError: function (error) {
                return true;
            }
        }
    });
}

Disable Retry

You can disable all retry behavior by setting retryOptions to false.

const fetch = require('@adobe/node-fetch-retry');

async main() {
    const response = await fetch(url, {
        retryOptions: false
    });
}

Disabling retry behavior will not prevent the usage of other options set on the options object.

Additional notes on retry duration

If the fetch is unsuccessful, the retry logic determines how long it will wait before the next attempt. If the time remaining will exceed the total time allowed by retryMaxDuration then another attempt will not be made. There are examples of how this works in the testing code.

Apache OpenWhisk Action Support

If you are running this in the context of an OpenWhisk action, it will take into account the action timeout deadline when setting the retryMaxDuration. It uses the __OW_ACTION_DEADLINE environment variable to determine if there is an action running.

Behavior: If retryMaxDuration is greater than the time till the action will timeout, it will adjust the retryMaxDuration to be equal to the time till action timeout.

Contributing

Contributions are welcomed! Read the Contributing Guide for more information.

Licensing

This project is licensed under the Apache V2 License. See LICENSE for more information.

版本与发布记录

@adobe/node-fetch-retry 共发布 12 个版本,首次发布于 2020/04/24;过去 12 个月发布了 0 个版本。

版本发布日期距今
2.2.0latest2022/03/084 年前
2.1.32022/02/124 年前
2.1.22022/02/084 年前
2.1.12022/02/014 年前
2.1.02022/01/254 年前
2.0.02021/10/275 年前

关于 @adobe/node-fetch-retry 的常见问题

@adobe/node-fetch-retry 是什么?

Library for performing retries on HTTP requests

如何安装 @adobe/node-fetch-retry?

在项目目录运行 npm install @adobe/node-fetch-retry,或使用 pnpm add / yarn add / bun add。

@adobe/node-fetch-retry 支持 TypeScript 吗?

支持。包内自带 TypeScript 类型声明(package.json 声明了 types 入口),安装后即可获得类型提示,无需再安装 @types 包。

@adobe/node-fetch-retry 支持 ESM 还是 CommonJS?

以 CommonJS 形式发布,可以直接 require();在 ES Module 项目中也可以用 import 引入(Node.js 会把 module.exports 作为默认导出)。

@adobe/node-fetch-retry 可以商用吗?

@adobe/node-fetch-retry 使用 Apache-2.0 许可。宽松许可:允许商业使用、修改和再分发,并附带明确的专利授权;分发时需保留许可与 NOTICE 文件,修改过的文件要注明改动。以上为概括,不构成法律意见,请以许可原文为准。

@adobe/node-fetch-retry 的下载量有多少?

根据 npm 官方下载统计,@adobe/node-fetch-retry 在 2026/10/05 – 2026/11/03 这 30 天内被下载 38,979 次,日均约 1,299 次。下载量会把 CI 构建、镜像同步和作为其他包依赖的安装一并计入,适合横向比较热度,不能直接等同于使用人数。

@adobe/node-fetch-retry 还在维护吗?

最新版本 2.2.0 发布于 2022/03/08(4 年前);至今共发布 12 个版本,其中过去 12 个月发布了 0 个;首次发布于 2020/04/24。发布频率只是参考信号:功能稳定的工具可能很少需要更新,建议结合源码仓库的 issue 和提交记录判断。