http-commons (韩语)
拥有 http 共同功能的图书馆, 围绕 http- 客户端 .
安装
npm install @applitools/http-commons
使用软件包
让我们看看一个例子 fetchAsJson
const {fetchAsJson} = require('@applitools/http-commons')
await fetchAsJson('https://swapi.co/api/people/1/') // ===> {name: "Luke Skywalker", ...}
API 密码
所有这些函数,如果状态代码不是2xx,则丢出一个例外. 解密将具有以下属性:
code将会是'ERR_X_STATUS_CODE_NOT_OK'status: HTTP 状态代码statusText: HTTP 状态文本headers: 带有响应头的对象
fetchAsBuffer(url, [fetchOptions], [options])
async 获取 URL并返回缓冲响应.
url
要获取的 URL .
fetchOptions
获取选项 用于 node-fetch 软件包。 。 。 .
options
有以下备选方案:
alternativeFetch
(用于测试) 用于替代 node-fetch-对 fetch 函数.
返回时
作为JSON解析的响应机构的对象.
示例
await fetchAsJson('https://swapi.co/api/people/1/') // ===> {name: "Luke Skywalker", ...}
fetchAsText(url, [fetchOptions], [options])
async 获取 URL 并返回响应作为字符串.
url
要获取的 URL .
fetchOptions
获取选项 用于 node-fetch 软件包。 。 。 .
options
有以下备选方案:
alternativeFetch
(用于测试) 用于替代 node-fetch-对 fetch 函数.
返回时
有反应机体的绳子.
示例
await fetchAsText('https://www.wikipedia.org')) // ===> "<!DOCTYPE html><html ..."
fetchAsTextWithJsonBody(url, json, [fetchOptions], [options])
async 以 JSON 机构发布 URL, 并将响应作为字符串返回 .
url
要获取的 URL .
fetchOptions
获取选项 用于 node-fetch 软件包。 。 。 .
注意默认选项是 {method: 'POST', body: '_the_json_'},带有正确的 content-type 标题,
但你可以使用 fetchOptions
options
有以下备选方案:
alternativeFetch
(用于测试) 用于替代 node-fetch-对 fetch 函数.
返回时
有反应机体的绳子.
示例
await fetchAsTextWithJsonBody('https://httpbin.org/anything', {x: 4})) // ===> "{..{"x": 4}..}"
fetchAsJsonWithJsonBody(url, json, [fetchOptions], [options])
async 以 JSON 机构发布 URL, 并将响应作为字符串返回 .
url
要获取的 URL .
fetchOptions
获取选项 用于 node-fetch 软件包。 。 。 .
注意默认选项是 {method: 'POST', body: '_the_json_'},带有正确的 content-type 标题,
但你可以使用 fetchOptions
options
有以下备选方案:
alternativeFetch
(用于测试) 用于替代 node-fetch-对 fetch 函数.
返回时
带有被剖析出的身体的"JSON"对象
示例
await fetchAsTextWithJsonBody('https://httpbin.org/anything', {x: 4}, {method: 'PUT'})) // ===> {..{"x": 4}..}
fetchAsBufferWithJsonBody(url, json, [fetchOptions], [options])
async 以 JSON 机构发布 URL, 并将响应作为字符串返回 .
url
要获取的 URL .
fetchOptions
获取选项 用于 node-fetch 软件包。 。 。 .
注意默认选项是 {method: 'POST', body: '_the_json_'},带有正确的 content-type 标题,
但你可以使用 fetchOptions
options
有以下备选方案:
alternativeFetch
(用于测试) 用于替代 node-fetch-对 fetch 函数.
返回时
与反应机体的缓冲.
示例
await fetchAsBufferWithJsonBody('https://httpbin.org/anything', {x: 4})) // ===> Buffer (....)
retryFetch(func, options)
检索代码并正确处理重试HTTP和连接错误.
func
一种叫作 fetch* 以上函数或 fetch 内 node-fetch 直接说.
options
retries
失败前重试次数 .
sleepTime
在复习间睡觉的时间(以毫秒计)
backoff
指标后退系数 sleepTime
idempotent
有些错误,比如 5xx 如果 fetch 操作为一能. 所以这个
旗子上写着操作是否具有知觉能力,以便知道是否要重试.
返回时
随便啦 func 返回时
示例
const json = await retry(() => fetchAsJson('http://httpbin.org/anything'), {idempotent: true})