@applitools/http-commons

中文文档
3.0.2发布于 2024/11/13Apache-2.0CommonJS近 30 天下载 1.1万
此软件包已被标记为弃用

this package has been deprecated

原始使用文档
文档目录9 节
  1. Installing
  2. Using the package
  3. API
  4. fetchAsBuffer(url, [fetchOptions], [options])
  5. fetchAsText(url, [fetchOptions], [options])
  6. fetchAsTextWithJsonBody(url, json, [fetchOptions], [options…
  7. fetchAsJsonWithJsonBody(url, json, [fetchOptions], [options…
  8. fetchAsBufferWithJsonBody(url, json, [fetchOptions], [optio…
  9. retryFetch(func, options)

http-commons

Library that has http common functionality, all around http-client currently.

Installing

npm install @applitools/http-commons

Using the package

Let's see an example with fetchAsJson

const {fetchAsJson} = require('@applitools/http-commons')

await fetchAsJson('https://swapi.co/api/people/1/') // ===> {name: "Luke Skywalker", ...}

API

All these functions with throw an exception if the status code is not 2xx. The excption will have the following properties:

  • code: it will be 'ERR_X_STATUS_CODE_NOT_OK'
  • status: the HTTP status code
  • statusText: the HTTP status text
  • headers: an object with the response headers

fetchAsBuffer(url, [fetchOptions], [options])

async fetches URL and returns a Buffer response.

url

The URL to fetch.

fetchOptions

The fetch options used by the node-fetch package.

options

The following options are available:

alternativeFetch

(For testing purposes) A function that will be used as an alternative to node-fetch-s fetch function.

returns

An object with the response body as JSON parsed.

Example

await fetchAsJson('https://swapi.co/api/people/1/') // ===> {name: "Luke Skywalker", ...}

fetchAsText(url, [fetchOptions], [options])

async fetches URL and returns the response as a string.

url

The URL to fetch.

fetchOptions

The fetch options used by the node-fetch package.

options

The following options are available:

alternativeFetch

(For testing purposes) A function that will be used as an alternative to node-fetch-s fetch function.

returns

A string with the response body.

Example

await fetchAsText('https://www.wikipedia.org')) // ===> "<!DOCTYPE html><html ..."

fetchAsTextWithJsonBody(url, json, [fetchOptions], [options])

async posts URL with a JSON body and returns the response as a string.

url

The URL to fetch.

fetchOptions

The fetch options used by the node-fetch package. Note that the default options are {method: 'POST', body: '_the_json_'}, with the correct content-type header, but you can override this using fetchOptions

options

The following options are available:

alternativeFetch

(For testing purposes) A function that will be used as an alternative to node-fetch-s fetch function.

returns

A string with the response body.

Example

await fetchAsTextWithJsonBody('https://httpbin.org/anything', {x: 4})) // ===> "{..{"x": 4}..}"

fetchAsJsonWithJsonBody(url, json, [fetchOptions], [options])

async posts URL with a JSON body and returns the response as a string.

url

The URL to fetch.

fetchOptions

The fetch options used by the node-fetch package. Note that the default options are {method: 'POST', body: '_the_json_'}, with the correct content-type header, but you can override this using fetchOptions

options

The following options are available:

alternativeFetch

(For testing purposes) A function that will be used as an alternative to node-fetch-s fetch function.

returns

A "JSON" object with the parsed body

Example

await fetchAsTextWithJsonBody('https://httpbin.org/anything', {x: 4}, {method: 'PUT'})) // ===> {..{"x": 4}..}

fetchAsBufferWithJsonBody(url, json, [fetchOptions], [options])

async posts URL with a JSON body and returns the response as a string.

url

The URL to fetch.

fetchOptions

The fetch options used by the node-fetch package. Note that the default options are {method: 'POST', body: '_the_json_'}, with the correct content-type header, but you can override this using fetchOptions

options

The following options are available:

alternativeFetch

(For testing purposes) A function that will be used as an alternative to node-fetch-s fetch function.

returns

A buffer with the response body.

Example

await fetchAsBufferWithJsonBody('https://httpbin.org/anything', {x: 4})) // ===> Buffer (....)

retryFetch(func, options)

Retries code and deals correctly with retrying HTTP and connection errors.

func

An async func that calls one of the fetch* functions above or the fetch in node-fetch directly.

options

retries

Number of retries before failing.

sleepTime

The time (in ms) for sleeping between retries

backoff

Exponential backoff factor for sleepTime

idempotent

Some errors, like 5xx http status error should not retry if the fetch operation is idempotent. So this flag says whether the operation is idempotent to know whether to retry.

returns

Whatever func returns

Example

const json = await retry(() => fetchAsJson('http://httpbin.org/anything'), {idempotent: true})

版本与发布记录

@applitools/http-commons 共发布 43 个版本,首次发布于 2017/10/12;过去 12 个月发布了 0 个版本。

版本发布日期距今
3.0.2latest2024/11/132 年前
3.0.1-fixHttpAgent.12024/11/062 年前
3.0.02023/09/053 年前
2.4.72022/05/304 年前
2.4.52021/09/295 年前
2.4.32020/02/266 年前

关于 @applitools/http-commons 的常见问题

如何安装 @applitools/http-commons?

在项目目录运行 npm install @applitools/http-commons,或使用 pnpm add / yarn add / bun add。

@applitools/http-commons 支持 TypeScript 吗?

该包的 package.json 没有声明类型入口。需要类型提示时,可以在 npm 上查找社区维护的 @types/applitools__http-commons 是否存在。

@applitools/http-commons 支持 ESM 还是 CommonJS?

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

@applitools/http-commons 需要什么版本的 Node.js?

package.json 的 engines 字段要求 Node.js >=18.0.0。版本不符时 npm 默认只给出警告,开启 engine-strict 后会拒绝安装。

@applitools/http-commons 可以商用吗?

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

@applitools/http-commons 的下载量有多少?

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

@applitools/http-commons 还在维护吗?

最新版本 3.0.2 发布于 2024/11/13(2 年前);至今共发布 43 个版本,其中过去 12 个月发布了 0 个;首次发布于 2017/10/12。作者已将其标记为弃用,新项目建议寻找替代方案。