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

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

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

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

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

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

- 包体积（解压后）：39.9 kB，26 个文件
- 模块格式：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
- 首次发布：2022-10-20T06:43:12.499Z
- 最新版本发布：2026-08-20T13:43:40.671Z
- 近 30 天下载量：59108（2026-10-18 至 2026-11-16，npm 官方下载统计）
- 已发布版本数：52（近 12 个月 1 个）

## 中文简介

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

## 安装

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

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

## README

# 响应查询插件

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

插件用于 `oats` cli,用于生成反应查询钩. 这将生成钩子 使用 `react-query`。 。 。 .

## 安装

利用国家预防机制:

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

使用 Yarn :

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

## 使用量

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

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,
        },
      },
    }),
  ],
});
```

## 自定义获取器

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

您可以使用 `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？

在项目目录运行 npm install @harnessio/oats-plugin-react-query，或使用 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 支持 TypeScript 吗？

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

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

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

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

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

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

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

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

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

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

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

