@api-ts/superagent-wrapper

中文文档
1.3.8发布于 2026/08/05Apache-2.0TypeScriptCommonJS构建来源已验证近 30 天下载 7.5万

用超级剂使类型安全 HTTP 请求

中文使用文档

@ api-ts/ 超剂- wrapper

将一个像谱与 superagent编号 :supertest 以创建已选中的 api 客户端.

开始

首先,定义或导入 io-ts-http api spec. (英语). 接下来将使用 本指南:

import * as h from '@api-ts/io-ts-http';
import * as t from 'io-ts';
import { NumberFromString } from 'io-ts-types';

export const Example = t.type({
  foo: t.string,
  bar: t.number,
});

export const GenericAPIError = t.type({
  message: t.string,
});

export const PutExample = h.httpRoute({
  path: '/example/{id}',
  method: 'PUT',
  request: h.httpRequest({
    params: {
      id: NumberFromString,
    },
    body: {
      example: Example,
    },
  }),
  response: {
    ok: Example,
    invalidRequest: GenericAPIError,
  },
});

export const ExampleAPI = h.apiSpec({
  'api.example': {
    put: PutExample,
  },
});

ExampleAPI 然后可用于为其中任一创建一个类型安全 api 客户端 superagent 或者说 supertest这需要两个步骤: superagent编号 :supertest 举例来说, 然后把它绑在皮克上.

对于 superagent数字 :

import { superagentRequestFactory, buildApiClient } from '@api-ts/superagent-wrapper';
import superagent from 'superagent';

import { ExampleAPI } from './see-the-above-example';

// The api root, in a real project probably coming from a config option
const BASE_URL = 'http://example.com/';

// Step one: wrap `superagent` and the api root
const requestFactory = superagentRequestFactory(superagent, BASE_URL);

// Step two: combine the request factory and imported api spec into an api client
// This is intended to be exported and used
export const apiClient = buildApiClient(requestFactory, ExampleAPI);

对于 supertest 过程几乎完全相同,但 supertest 自己处理 了解根 api url:

import { supertestRequestFactory, buildApiClient } from '@api-ts/superagent-wrapper';
import supertest from 'superagent';

import { ExampleAPI } from './see-the-above-example';

// For the purposes of this guide, say we have an Express app that can be imported from the project.
// See the `supertest` docs for the options it has for instantiation.
import { app } from '../src/index';
const request = supertest(app);

// Step one: wrap the `supertest` request function created above
const requestFactory = superatestRequestFactory(request);

// Step two: combine the request factory and imported api spec into an api client
// This is intended to be exported and used
export const apiClient = buildApiClient(requestFactory, ExampleAPI);

结果 apiClient 然后可以进口到其他地方并使用:

import { apiClient } from './api-client-example';

const doSomething = async () => {
  // The `api.example` here comes from the operation in the `ExampleAPI` definition from above
  const response = await apiClient['api.example']
    // The object passed to this function is type-checked against the request codec
    .put({ id: 42, example: { foo: 'hello', bar: 1 } })
    // This will use the set of response codecs to decode the response
    .decode();

  // The two main properties on `response` are `status` and `body`
  // If the value of `status` is checked, then TypeScript will infer the correct body type
  if (response.status === 200) {
    const { foo, bar } = response.body; // We know the correct body type here
  } else if (response.status === 400) {
    // The body is a GenericAPIError
    console.log(response.body.message);
  } else {
    // In case an unexpected status code comes back, or the response body does not correctly
    // decode, we can still access it as an `unknown` type.
    if (
      response.body &&
      typeof response.body === 'object' &&
      response.body.hasOwnProperty('message')
    ) {
      console.log(response.body.message);
    }
  }
};

为方便起见,a decodeExpecting 函数也被添加到请求中。 它接受一个 HTTP 状态代码和扔出,如果响应代码不匹配,或者它不匹配但 响应机构无法解码 .

const expectOk = async () => {
  // The `api.example` here comes from the operation in the `ExampleAPI` definition from above
  const response = await apiClient['api.example']
    // The object passed to this function is type-checked against the request codec
    .put({ id: 42, example: { foo: 'hello', bar: 1 } })
    // This will use the set of response codecs to decode the response
    .decodeExpecting(200);

  const { foo, bar } = response.body;
};

版本与发布记录

@api-ts/superagent-wrapper 共发布 50 个版本,首次发布于 2022/04/19;过去 12 个月发布了 4 个版本。最新版本附带 npm provenance 构建来源证明,可追溯到公开的 CI 构建记录。

版本发布日期距今
1.3.8latest2026/08/053 个月前
1.3.72026/08/013 个月前
1.3.62026/07/273 个月前
1.3.52026/07/223 个月前
1.3.42025/07/191.3 年前
1.3.32024/12/241.9 年前

其他发布通道:beta → 1.1.15-beta.1。

关于 @api-ts/superagent-wrapper 的常见问题

@api-ts/superagent-wrapper 是什么?

用超级剂使类型安全 HTTP 请求

如何安装 @api-ts/superagent-wrapper?

在项目目录运行 npm install @api-ts/superagent-wrapper,或使用 pnpm add / yarn add / bun add。它还需要宿主项目提供 peer 依赖:superagent(*)。npm 7 及以上与 pnpm 8 及以上默认会自动安装必需的 peer 依赖,Yarn 需要手动安装。

@api-ts/superagent-wrapper 支持 TypeScript 吗?

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

@api-ts/superagent-wrapper 支持 ESM 还是 CommonJS?

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

@api-ts/superagent-wrapper 可以商用吗?

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

@api-ts/superagent-wrapper 的下载量有多少?

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

@api-ts/superagent-wrapper 还在维护吗?

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