@mittwald/api-client-commons

中文文档
4.509.0发布于 2026/11/02MITTypeScriptES Module构建来源已验证

Common types and utilities for mittwald API clients

原始使用文档
文档目录3 节
  1. API
  2. assertStatus, assertOneOfStatus
  3. Common interceptors

Common code base used by @mittwald/api-client-* package.

API

assertStatus, assertOneOfStatus

The API client does not validate any response status by design, to give you the most flexibility while handling also erroneous responses. If you want to assert some desired response status, you can use the assertStatus resp. assertOneOfStatus function.

assertStatus(response, expectedStatus)

Returns: void

This method throws an ApiClientError if the given response does not match the expectedStatus.

When you are using TypeScript this function also asserts the correct response type.

const response = await client.project.getProject({
  pathParameters: {
    projectId: "...",
  },
});

assertStatus(response, 200);

const project = response.data;
// Project properties can now be accessed safely
const name = project.name;

assertOnOfStatus(response, expectedStatus)

Returns: void

This method throws an ApiClientError if the given response does not match the expectedStatus.

When you are using TypeScript this function also asserts the correct response type.

const response = await client.project.getProject({
  pathParameters: {
    projectId: "...",
  },
});

assertOneOfStatus(response, [200, 404]);

if (!response.data) {
  console.log("Project not found");
}

Common interceptors

To make it easy to opt in to some API mechanisms, a few interceptors can be easily configured.

withAccessToken

In most cases you want to add the users access token to every API-Request as a request header. This can be tedious if you need to do this on your own. To automatically set the request header use withAccessToken for your APIClient instance.

const client = new APIClient({ baseURL });
const authenticatedClient = withToken(client, token);

withEventConsistencyHandling

To opt in into the event consistency handling you might use withEventConsistencyHandling. This will set automatically handle the etag response header and set its value as if-event-reached request header for GET requests:

const client = new APIClient({ baseURL });
const authenticatedClient = withEventConsistencyHandling(client);

版本与发布记录

@mittwald/api-client-commons 共发布 487 个版本,首次发布于 2023/06/07;过去 12 个月发布了 372 个版本。最新版本附带 npm provenance 构建来源证明,可追溯到公开的 CI 构建记录。

版本发布日期距今
4.509.0latest2026/11/021 天前
4.508.02026/11/012 天前
4.507.02026/10/313 天前
0.0.0-development-7d1b6db-20261028development2026/10/295 天前
4.506.02026/10/295 天前
0.0.0-development-492ba61-202610272026/10/286 天前

其他发布通道:alpha → 4.4.0-alpha.4,old → 4.3.2,development → 0.0.0-development-7d1b6db-20261028。

关于 @mittwald/api-client-commons 的常见问题

@mittwald/api-client-commons 是什么?

中间件API客户端的常用类型和实用程序

如何安装 @mittwald/api-client-commons?

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

@mittwald/api-client-commons 支持 TypeScript 吗?

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

@mittwald/api-client-commons 支持 ESM 还是 CommonJS?

以 ES Module 形式发布,推荐使用 import 引入。在 CommonJS 项目中,Node.js 20.19+ / 22.12+ 可以直接 require() 不含顶层 await 的 ES Module,更早的版本需要改用动态 import()。

@mittwald/api-client-commons 可以商用吗?

@mittwald/api-client-commons 使用 MIT 许可。宽松许可:允许商业使用、修改和再分发,通常只需在分发时保留原版权与许可声明。以上为概括,不构成法律意见,请以许可原文为准。

@mittwald/api-client-commons 还在维护吗?

最新版本 4.509.0 发布于 2026/11/02(1 天前);至今共发布 487 个版本,其中过去 12 个月发布了 372 个;首次发布于 2023/06/07。发布频率只是参考信号:功能稳定的工具可能很少需要更新,建议结合源码仓库的 issue 和提交记录判断。