# @digitalcredentials/http-client 中文文档

规范地址：https://npmcn.com/package/%40digitalcredentials/http-client/

版本：5\.0\.4
npm 元数据更新时间：2026-09-30T17:46:18.625Z
中文译文更新时间：2026-11-12T08:52:51.786Z
软件包许可：BSD-3-Clause

本文由上游 README 翻译，可能存在偏差，请结合原文核对 API、命令与配置。版权与许可归原作者所有。

原始 npm 页面：https://www.npmjs.com/package/%40digitalcredentials/http-client
原文对照：https://npmcn.com/package/%40digitalcredentials/http-client/?lang=original
项目主页：https://github.com/digitalcredentials/http-client
源码仓库：https://github.com/digitalcredentials/http-client

## 关键信息（来自 npm 元数据）

- 包体积（解压后）：23.5 kB，18 个文件
- 模块格式：ESM + CJS
- TypeScript 类型：未声明
- Node.js 要求：\>=18\.0
- 运行依赖：2 个
- 首次发布：2021-05-19T19:17:48.058Z
- 最新版本发布：2024-09-01T21:50:59.571Z
- 近 30 天下载量：75252（2026-10-12 至 2026-11-10，npm 官方下载统计）
- 已发布版本数：8（近 12 个月 0 个）

## 中文简介

一个有观点,异形的HTTP客户端.

## 安装

```sh
npm install @digitalcredentials/http-client
```

也可以使用：`pnpm add @digitalcredentials/http-client`、`yarn add @digitalcredentials/http-client`、`bun add @digitalcredentials/http-client`

## README

# http-客户端

[![NPM Version](https://img.shields.io/npm/v/@digitalcredentials/http-client.svg)](https://npm.im/@digitalcredentials/http-client)

> 一个有观点,异构的HTTP客户端,用于Node.js,浏览器和React Industrial.

### 使用量

#### 导入 httpclient (Node.js)

```js
import https from 'https';
import {httpClient} from '@digitalcredentials/http-client';
```

#### 导入 httpClient( 浏览器或反应本地)

```js
import {httpClient} from '@digitalcredentials/http-client';
```

#### 导入并初始化自定义的 Bearer Token 客户端

```js
import {httpClient} from '@digitalcredentials/http-client';

const httpsAgent = new https.Agent({rejectUnauthorized: false});

const accessToken = '12345';
const headers = {Authorization: `Bearer ${accessToken}`};

const client = httpClient.extend({headers, httpsAgent});

// subsequent http calls will include an 'Authorization: Bearer 12345' header,
// and use the provided httpsAgent
```

#### 在浏览器中获取 JSON 响应

```js
try {
  const response = await httpClient.get('http://httpbin.org/json');
  return response.data;
} catch(e) {
  // status is HTTP status code
  // data is JSON error from the server
  const {data, status} = e;
  throw e;
}
```

#### 用 HTTP 代理在节点获得 JSON 响应

```js
import https from 'https';
// use an agent to avoid self-signed certificate errors
const agent = new https.Agent({rejectUnauthorized: false});
try {
  const response = await httpClient.get('http://httpbin.org/json', {agent});
  return response.data;
} catch(e) {
  // status is HTTP status code
  // data is JSON error from the server if available
  const {data, status} = e;
  throw e;
}
```

#### 通过压倒性的默认信头获取 HTML

```js
const headers = {Accept: 'text/html'};
try {
  const response = await httpClient.get('http://httpbin.org/html', {headers});
  // see: https://developer.mozilla.org/en-US/docs/Web/API/Response#methods
  return response.text();
} catch(e) {
  // status is HTTP status code
  // any message from the server can be parsed from the response if present
  const {response, status} = e;
  throw e;
}
```

#### JSON有效载荷之后

```js
try {
  const response = await httpClient.post('http://httpbin.org/json', {
    // `json` is the payload or body of the POST request
    json: {some: 'data'}
  });
  return response.data;
} catch(e) {
  // status is HTTP status code
  // data is JSON error from the server
  const {data, status} = e;
  throw e;
}
```

#### 在节点装有HTTP代理的JSON有效载荷

```js
import https from 'https';
// use an agent to avoid self-signed certificate errors
const agent = new https.Agent({rejectUnauthorized: false});
try {
  const response = await httpClient.post('http://httpbin.org/json', {
    agent,
    // `json` is the payload or body of the POST request
    json: {some: 'data'}
  });
  return response.data;
} catch(e) {
  // status is HTTP status code
  // data is JSON error from the server
  const {data, status} = e;
  throw e;
}
```

## 常见问题

### @digitalcredentials/http-client 是什么？

一个有观点,异形的HTTP客户端\.

### 如何安装 @digitalcredentials/http-client？

在项目目录运行 npm install @digitalcredentials/http-client，或使用 pnpm add / yarn add / bun add。

### @digitalcredentials/http-client 支持 TypeScript 吗？

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

### @digitalcredentials/http-client 支持 ESM 还是 CommonJS？

同时提供 ES Module 与 CommonJS 入口，import 和 require\(\) 都可以使用，打包工具会自动选择合适的版本。

### @digitalcredentials/http-client 需要什么版本的 Node\.js？

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

### @digitalcredentials/http-client 可以商用吗？

@digitalcredentials/http-client 使用 BSD-3-Clause 许可。宽松许可：允许商业使用、修改和再分发，通常只需在分发时保留原版权与许可声明。另外不得用原作者名义为衍生产品背书。以上为概括，不构成法律意见，请以许可原文为准。

### @digitalcredentials/http-client 的下载量有多少？

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

### @digitalcredentials/http-client 还在维护吗？

最新版本 5\.0\.4 发布于 2024/09/02（2 年前）；至今共发布 8 个版本，其中过去 12 个月发布了 0 个；首次发布于 2021/05/20。发布频率只是参考信号：功能稳定的工具可能很少需要更新，建议结合源码仓库的 issue 和提交记录判断。

