@confighub/api

中文文档
0.10.11发布于 2026/11/08MITTypeScriptESM + CJS构建来源已验证近 30 天下载 9013

Typed, framework-agnostic client for the ConfigHub API

原始使用文档
文档目录4 节
  1. Options
  2. Configuration data
  3. Writes answer with the operation's result
  4. Types

@confighub/api

A typed, framework-agnostic client for the ConfigHub API, generated from the ConfigHub OpenAPI spec. Built on openapi-fetch — a few KB, no Redux, no React, no required provider. Every path, parameter, and response is typed against a pinned server version.

import { createConfigHubClient } from '@confighub/api';

const api = createConfigHubClient({
  baseUrl: 'https://hub.confighub.com',
  getToken: () => session.accessToken, // optional; sets Authorization: Bearer
});

const me = await api.GET('/me');
const { data, error } = await api.GET('/space/{space_id}/unit', {
  params: { path: { space_id } },
});

Options

  • baseUrl — absolute base URL of the ConfigHub instance.
  • getToken? — returns the current bearer token (may be async). Wired as a per-request middleware. The client never stores or refreshes tokens; pair it with @confighub/react-auth or supply your own token source.
  • onUnauthorized? — called on a 401 so the caller can refresh or re-login. The request is not retried automatically.
  • fetch? — override the fetch implementation.

Configuration data

A Unit's configuration is not a field of the Unit. Unit, Revision and Release carry DataHash and DataSize; the document itself lives on its own endpoints, which serve it as application/octet-stream. openapi-fetch parses every response as JSON and serializes every body with JSON.stringify, and both are wrong for a document — so use these helpers rather than calling those paths directly:

import { getUnitData, putUnitData, getUnitMutationSources } from '@confighub/api';

const { data: config, dataHash, notModified } = await getUnitData(api, { spaceId, unitId });

const { data: result } = await putUnitData(api, { spaceId, unitId }, config + '\n', {
  ifMatch: dataHash,             // 409 rather than clobbering a concurrent write
  lastChangeDescription: 'note',
  include: 'ConfigData',         // the configuration the write produced
});
const unit = result?.Unit;       // a write answers with the result, not the entity
  • getUnitData / getRevisionData / getReleaseData read text, and return the DataHash from the ETag. Pass it back as ifNoneMatch to get notModified: true instead of a body when nothing changed.
  • putUnitData writes it. Every parameter describing how the configuration should land belongs here, not on a metadata update that precedes it — mergeExternalSource, mergeBase, mergeEnableSubtraction, protect, clearance, tag, subgroup, changeSetId, lastChangeDescription and dryRun. A metadata call changes no configuration, so anything sent there is silently dropped.
  • An empty configuration is a configuration — emptying a Unit is how its resources are withdrawn — so never guard the write with if (config).
  • getUnitMutationSources / getRevisionMutationSources read what set each value.

For a list, read many at once rather than one request per Unit. Those are ordinary JSON, so the client handles them directly:

const { data } = await api.GET('/unit_data', {
  params: { query: { where: `SpaceID = '${spaceId}'` } },
});

/revision_data, /unit_mutation_sources and /revision_mutation_sources are the same shape. All four are organization-scoped and take a where clause.

Writes answer with the operation's result

POST /space/{id}/unit, PUT and PATCH on one, the data write, and the bulk forms all return UnitCreateOrUpdateResponse rather than a Unit — unwrap .Unit. Naming ConfigData or MutationSources in include adds the configuration the operation produced and what set each value in it; for a dryRun that is the only place the result exists, because nothing was stored. An include naming neither those nor an expandable field is a 400.

Types

The full generated surface is exported for reuse:

import type { paths, components } from '@confighub/api';
type Unit = components['schemas']['Unit'];

Data and MutationSources are not fields of any entity, so naming either in a select is a 400 rather than a silently absent field. There is one content hash, DataHash; ContentHash and RevisionHash no longer exist.

Types describe the API of the ConfigHub server whose version the package carries.

版本与发布记录

@confighub/api 共发布 108 个版本,首次发布于 2026/07/07;过去 12 个月发布了 108 个版本。最新版本附带 npm provenance 构建来源证明,可追溯到公开的 CI 构建记录。

版本发布日期距今
0.10.11latest2026/11/082 天前
0.10.102026/11/073 天前
0.10.92026/11/073 天前
0.10.82026/11/064 天前
0.10.72026/11/055 天前
0.10.62026/11/055 天前

关于 @confighub/api 的常见问题

@confighub/api 是什么?

Typed, framework-agnostic client for the ConfigHub API

如何安装 @confighub/api?

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

@confighub/api 支持 TypeScript 吗?

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

@confighub/api 支持 ESM 还是 CommonJS?

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

@confighub/api 可以商用吗?

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

@confighub/api 的下载量有多少?

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

@confighub/api 还在维护吗?

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