@confighub/rtk-query

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

RTK Query client for the ConfigHub API (generated hooks, cache tags, bearer auth)

原始使用文档
文档目录4 节
  1. Setup
  2. Use
  3. Configuration data
  4. Notes

@confighub/rtk-query

An RTK Query client for the ConfigHub API — generated endpoints, hooks, and cache tags, with bearer auth. Use this if your app is already on Redux Toolkit and you want the useListUnitsQuery(...) style with automatic caching and invalidation.

If you are not on Redux, use @confighub/api instead — a tiny framework-agnostic client. The two are parallel: same version-pegged spec, same getToken auth seam, different generator. They share no code.

Setup

Configure the api once, then add it to your store:

import { configureStore } from '@reduxjs/toolkit';
import { confighubApi, configureConfigHub } from '@confighub/rtk-query';
import { getAccessToken } from '@confighub/react-auth';

configureConfigHub({
  baseUrl: 'https://hub.confighub.com',
  getToken: getAccessToken, // any () => string | undefined; token source is your choice
});

export const store = configureStore({
  reducer: { [confighubApi.reducerPath]: confighubApi.reducer },
  middleware: (getDefault) => getDefault().concat(confighubApi.middleware),
});

Wrap your app in the react-redux <Provider store={store}> as usual.

Use

import { useListSpacesQuery, useListUnitsQuery } from '@confighub/rtk-query';
import { skipToken } from '@reduxjs/toolkit/query';

function Spaces() {
  const { data: spaces, isLoading } = useListSpacesQuery();
  const [spaceId, setSpaceId] = useState<string | null>(null);
  const { data: units } = useListUnitsQuery(spaceId ? { spaceId } : skipToken);
  // ...
}

Configuration data

A Unit's configuration is not a field of the Unit. Unit, Revision and Release carry DataHash and DataSize; the document itself has its own endpoints, and its own hooks:

const { data: config } = useDownloadUnitDataQuery({ spaceId, unitId });   // a string
const [uploadUnitData] = useUploadUnitDataMutation();

await uploadUnitData({
  spaceId,
  unitId,
  body: edited,                       // the document, not a JSON envelope
  lastChangeDescription: 'raise replicas',
  include: 'ConfigData',              // the configuration the write produced
}).unwrap();

useGetUnitMutationSourcesQuery reads what set each value. For a list, use the bulk queries — useSearchUnitDataQuery, useSearchRevisionDataQuery, useSearchUnitMutationSourcesQuery, useSearchRevisionMutationSourcesQuery — each of which takes a where clause and answers for many Units in one request. One request per Unit is what taking the configuration off the entity was meant to avoid.

Three things to know, none of which the type checker enforces:

  • These endpoints serve application/octet-stream. The base query sets responseHandler: 'content-type' for exactly this reason: RTK Query's default 'json' handler JSON.parses every body, which fails on YAML and surfaces as a parse error with no data — an empty editor and nothing to say why. If you build your own base query, carry that setting over.
  • A write answers with the operation's result, not the entity. useCreateUnitMutation, useUpdateUnitMutation, usePatchUnitMutation, useUploadUnitDataMutation and the bulk forms return UnitCreateOrUpdateResponse; the Unit is in its Unit field. For a dryRun, include: 'ConfigData,MutationSources' is the only way to see what the operation produced, since nothing was stored.
  • An empty configuration is a configuration — emptying a Unit is how its resources are withdrawn — so never guard the upload with if (body).

Every parameter describing how the configuration should land goes on uploadUnitData, not on a metadata update before 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.

Naming Data or MutationSources in a select is a 400 rather than a silently absent field. There is one content hash, DataHash; ContentHash and RevisionHash are gone.

Notes

  • baseUrl is the instance origin; the client targets {baseUrl}/api. Same value as the auth provider.
  • The api is a module singleton, so baseUrl/getToken are set at configure time, not per instance — the one ergonomic difference from @confighub/api.
  • A 401 calls your onUnauthorized (if provided); it does not redirect. A 403 calls your onForbidden with the error, whose data.message says why.
  • Peer deps: @reduxjs/toolkit, react, react-redux.
  • Endpoints and types are generated from the spec of the ConfigHub server whose version the package carries, by @rtk-query/codegen-openapi.

版本与发布记录

@confighub/rtk-query 共发布 107 个版本,首次发布于 2026/07/07;过去 12 个月发布了 107 个版本。最新版本附带 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/rtk-query 的常见问题

@confighub/rtk-query 是什么?

RTK Query client for the ConfigHub API (generated hooks, cache tags, bearer auth)

如何安装 @confighub/rtk-query?

在项目目录运行 npm install @confighub/rtk-query,或使用 pnpm add / yarn add / bun add。它还需要宿主项目提供 peer 依赖:react(^18.0.0 || ^19.0.0)、react-redux(^9.0.0)、@reduxjs/toolkit(^2.12.0)。npm 7 及以上与 pnpm 8 及以上默认会自动安装必需的 peer 依赖,Yarn 需要手动安装。

@confighub/rtk-query 支持 TypeScript 吗?

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

@confighub/rtk-query 支持 ESM 还是 CommonJS?

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

@confighub/rtk-query 可以商用吗?

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

@confighub/rtk-query 的下载量有多少?

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

@confighub/rtk-query 还在维护吗?

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