@digitalcredentials/http-client

中文文档
5.0.4发布于 2024/09/02BSD-3-ClauseESM + CJS近 30 天下载 7.5万

An opinionated, isomorphic HTTP client.

原始使用文档

http-client

NPM Version

An opinionated, isomorphic HTTP client for Node.js, browsers, and React Native.

Usage

Import httpClient (Node.js)

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

Import httpClient (browsers or React Native)

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

Import and initialize a custom Bearer Token client

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

GET a JSON response in the browser

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;
}

GET a JSON response in Node with an HTTP Agent

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;
}

GET HTML by overriding default headers

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;
}

POST a JSON payload

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;
}

POST a JSON payload in Node with an HTTP Agent

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 共发布 8 个版本,首次发布于 2021/05/20;过去 12 个月发布了 0 个版本。

版本发布日期距今
5.0.4latest2024/09/022 年前
5.0.32024/09/022 年前
5.0.22024/08/062 年前
5.0.12024/08/042 年前
5.0.02024/08/042 年前
1.2.22021/09/305 年前

关于 @digitalcredentials/http-client 的常见问题

@digitalcredentials/http-client 是什么?

An opinionated, isomorphic HTTP client.

如何安装 @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 和提交记录判断。