# @harnessio/oats-plugin-react-query-harness 中文文档

规范地址：https://npmcn.com/package/%40harnessio/oats-plugin-react-query-harness/

版本：2\.2\.0
npm 元数据更新时间：2026-11-16T17:37:09.809Z
中文译文更新时间：2026-11-17T10:17:46.176Z
软件包许可：MIT

本文由上游 README 翻译，可能存在偏差，请结合原文核对 API、命令与配置。版权与许可归原作者所有。

原始 npm 页面：https://www.npmjs.com/package/%40harnessio/oats-plugin-react-query-harness
原文对照：https://npmcn.com/package/%40harnessio/oats-plugin-react-query-harness/?lang=original
项目主页：https://github.com/harness/oats#readme
源码仓库：https://github.com/harness/oats

## 关键信息（来自 npm 元数据）

- 包体积（解压后）：81.6 kB，41 个文件
- 模块格式：ES Module
- TypeScript 类型：未声明
- Node.js 要求：\>=16\.0\.0
- 运行依赖：4 个
- peer 依赖：react \>=16\.8\.0 \<17\.0\.0 \|\| \>=17\.0\.0 \<18\.0\.0 \|\| \>=18\.0\.0 \<19\.0\.0，@harnessio/oats-cli ^4\.0\.0，@tanstack/react-query ^4\.0\.0
- 首次发布：2023-02-27T05:04:16.139Z
- 最新版本发布：2026-08-20T13:43:44.776Z
- 近 30 天下载量：47489（2026-10-18 至 2026-11-16，npm 官方下载统计）
- 已发布版本数：11（近 12 个月 2 个）

## 中文简介

![@harnessio/oats-plugin-react-query-harness](https://img.shields.io/npm/v/@harnessio/oats-plugin-react-query-harness.svg?style=flat-square)

## 安装

```sh
npm install @harnessio/oats-plugin-react-query-harness
```

也可以使用：`pnpm add @harnessio/oats-plugin-react-query-harness`、`yarn add @harnessio/oats-plugin-react-query-harness`、`bun add @harnessio/oats-plugin-react-query-harness`

## README

# 响应查询插件

![@harnessio/oats-plugin-react-query-harness](https://img.shields.io/npm/v/@harnessio/oats-plugin-react-query-harness.svg?style=flat-square)

插件用于 `oats` cli,用于生成反应查询钩. 这将生成钩子 使用 `react-query` 专为哈尼斯而设的个案.

## 安装

利用国家预防机制:

```
npm i -D @harnessio/oats-cli @harnessio/oats-plugin-react-query-harness
```

使用 Yarn :

```
yarn add -D @harnessio/oats-cli @harnessio/oats-plugin-react-query-harness
```

## 使用量

```ts
// oats.config.ts
import { defineConfig } from '@harnessio/oats-cli/config';
import reactQueryPlugin from '@harnessio/oats-plugin-react-query-harness';

export default defineConfig({
  plugins: [
    reactQueryPlugin({
      /**
       * Path poiniting to the file where the custom fetcher resides.
       * This path will be used as is, while generating the hooks.
       *
       * This path must be a relative path which can be resolved with respect
       * to a file within the hooks folder.
       */
      customFetcher: '../realtive/path/to/custom/fetcher',
      /**
       * An allow-list, for filtering out operation IDs which are to be generated.
       *
       * When defined, hooks will be generated only for the operation IDs
       * defined in this list.
       *
       * This field is optional.
       */
      allowedOperationIds: [],
      /**
       * Override options per operation.
       * The key must the operation Id from the spec.
       * This field is optional.
       */
      overrides: {
        operationId: {
          /**
           * By default, any operation expect "GET", will generate a mutation
           * hook using `useMutation`. This option can be used to overide this
           * behaviour for a given operation ID.
           *
           * When set to `true`, no matter what the verb, query hook will be
           * generated using `useQuery`.
           */
          useQuery: true,
        },
      },
      /**
       * Config for logically grouping APIs together
       * This can is map/record, where the key is a string
       * and value can an array of length 3
       * or  an object (for advanced configuration) as shown below
       *
       * The key will be used as the operation Id for the new hook.
       *
       * If the value is an array, it must be of length 3 and should
       * give operationIds in order: account, organisation, project.
       *
       * For value as object, please read the inline documentation below.
       */
      scopeGroups: {
        'test-connector': [
          'test-account-scoped-connector',
          'test-org-scoped-connector',
          'test-project-scoped-connector',
        ],
        'get-connectors': {
          /**
           * This is used to define advanced settings
           */
          operations: {
            /**
             * OperationId for account scope
             */
            account: 'get-account-scoped-connectors',
            /**
             *  OperationId for organisation scope
             */
            organisation: 'get-org-scoped-connectors',
            /**
             *  OperationId for project scope
             */
            project: 'get-project-scoped-connectors',
          },
          /**
           * By default the codegen will generate hooks using `useQuery` in case of grouping.
           * If you want to use `useMutation`, set this to `true`
           */
          useMutation: false,
        },
      },
    }),
  ],
});
```

## 自定义获取器

此插件会为您生成一个基本的获取器, 但这在 现实世界 您可以提供自己的获取器功能,处理您的使用大小写 .

您可以使用 `customFetcher` 相会. 应该是绳子 指向带有自定义获取器的文件 。 文件必须导出一个函数 名称 `fetcher` 和接口 `FetcherOptions` 签字日期:

```ts
export interface FetcherOptions<TQueryParams = never, TBody = never>
  extends Omit<RequestInit, 'body'> {
  url: string;
  queryParams?: TQueryParams extends never ? undefined : TQueryParams;
  body?: TBody extends never ? undefined : TBody;
}

export function fetcher<TResponse = unknown, TQueryParams = never, TBody = never>(
  options: FetcherOptions<TQueryParams, TBody>,
): Promise<TResponse> {
  // your code here
}
```

您可以添加额外的属性到 `FetcherOptions` 根据你的要求, 因为这些额外的属性将从钩子传递到取取器.

你可以看看那个 [在此生成的默认获取器](https://npmcn.com/package/examples/output/petstore-openapi-v3.0/hooks/fetcher.ts)

## 常见问题

### 如何安装 @harnessio/oats-plugin-react-query-harness？

在项目目录运行 npm install @harnessio/oats-plugin-react-query-harness，或使用 pnpm add / yarn add / bun add。它还需要宿主项目提供 peer 依赖：react（\>=16\.8\.0 \<17\.0\.0 \|\| \>=17\.0\.0 \<18\.0\.0 \|\| \>=18\.0\.0 \<19\.0\.0）、@harnessio/oats-cli（^4\.0\.0）、@tanstack/react-query（^4\.0\.0）。npm 7 及以上与 pnpm 8 及以上默认会自动安装必需的 peer 依赖，Yarn 需要手动安装。

### @harnessio/oats-plugin-react-query-harness 支持 TypeScript 吗？

该包的 package\.json 没有声明类型入口。需要类型提示时，可以在 npm 上查找社区维护的 @types/harnessio\_\_oats-plugin-react-query-harness 是否存在。

### @harnessio/oats-plugin-react-query-harness 支持 ESM 还是 CommonJS？

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

### @harnessio/oats-plugin-react-query-harness 需要什么版本的 Node\.js？

package\.json 的 engines 字段要求 Node\.js \>=16\.0\.0。版本不符时 npm 默认只给出警告，开启 engine-strict 后会拒绝安装。

### @harnessio/oats-plugin-react-query-harness 可以商用吗？

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

### @harnessio/oats-plugin-react-query-harness 的下载量有多少？

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

### @harnessio/oats-plugin-react-query-harness 还在维护吗？

最新版本 2\.2\.0 发布于 2026/08/20（2 个月前）；至今共发布 11 个版本，其中过去 12 个月发布了 2 个；首次发布于 2023/02/27。发布频率只是参考信号：功能稳定的工具可能很少需要更新，建议结合源码仓库的 issue 和提交记录判断。

