@harnessio/oats-plugin-react-query-harness

中文文档
2.2.0发布于 2026/08/20MITES Module近 30 天下载 4.7万

@harnessio/oats-plugin-react-query-harness

中文使用文档
文档目录3 节
  1. 安装
  2. 使用量
  3. 自定义获取器

响应查询插件

@harnessio/oats-plugin-react-query-harness

插件用于 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

使用量

// 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 签字日期:

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 根据你的要求, 因为这些额外的属性将从钩子传递到取取器.

你可以看看那个 在此生成的默认获取器

版本与发布记录

@harnessio/oats-plugin-react-query-harness 共发布 11 个版本,首次发布于 2023/02/27;过去 12 个月发布了 2 个版本。

版本发布日期距今
2.2.0latest2026/08/202 个月前
2.1.02026/04/177 个月前
2.0.02025/04/181.6 年前
1.1.02023/11/222 年前
1.0.02023/11/173 年前
0.6.02023/03/273 年前

关于 @harnessio/oats-plugin-react-query-harness 的常见问题

如何安装 @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 和提交记录判断。