@uiwwsw/react-query-helper 1.3.0 → 2.0.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +50 -0
- package/README.md +109 -362
- package/README_EN.md +165 -0
- package/dist/cli.d.ts +0 -1
- package/dist/cli.js +681 -2081
- package/dist/config-loader.d.ts +10 -6
- package/dist/config-loader.js +201 -0
- package/dist/config.d.ts +2 -3
- package/dist/core/analyzer.d.ts +3 -1
- package/dist/core/analyzer.js +109 -55
- package/dist/core/generator.d.ts +4 -3
- package/dist/core/generator.js +97 -85
- package/dist/index.d.ts +2 -3
- package/dist/index.js +98 -91
- package/dist/utils/query.d.ts +59 -50
- package/package.json +39 -27
- package/dist/cli.d.ts.map +0 -1
- package/dist/config-loader.d.ts.map +0 -1
- package/dist/config.d.ts.map +0 -1
- package/dist/core/analyzer.d.ts.map +0 -1
- package/dist/core/generator.d.ts.map +0 -1
- package/dist/index.d.ts.map +0 -1
- package/dist/utils/query.d.ts.map +0 -1
package/README_EN.md
ADDED
|
@@ -0,0 +1,165 @@
|
|
|
1
|
+
<p align="center">
|
|
2
|
+
<picture>
|
|
3
|
+
<source media="(prefers-color-scheme: dark)" srcset="https://raw.githubusercontent.com/uiwwsw/react-query-helper/main/assets/readme-hero-dark.svg" />
|
|
4
|
+
<img src="https://raw.githubusercontent.com/uiwwsw/react-query-helper/main/assets/readme-hero.svg" alt="React Query Helper logo" width="1120" />
|
|
5
|
+
</picture>
|
|
6
|
+
</p>
|
|
7
|
+
|
|
8
|
+
<h1 align="center">React Query Helper</h1>
|
|
9
|
+
|
|
10
|
+
<p align="center">
|
|
11
|
+
A CLI and runtime helpers that generate query keys and TanStack Query v5 option factories (code that calls <code>queryOptions</code>, <code>infiniteQueryOptions</code> and <code>mutationOptions</code>) from TypeScript API function files.
|
|
12
|
+
</p>
|
|
13
|
+
|
|
14
|
+
<p align="center">
|
|
15
|
+
<a href="https://www.npmjs.com/package/@uiwwsw/react-query-helper"><img src="https://img.shields.io/npm/v/@uiwwsw/react-query-helper?style=flat-square" alt="npm version" /></a>
|
|
16
|
+
<a href="https://github.com/uiwwsw/react-query-helper/blob/main/LICENSE"><img src="https://img.shields.io/npm/l/@uiwwsw/react-query-helper?style=flat-square" alt="License" /></a>
|
|
17
|
+
<a href="https://github.com/uiwwsw/react-query-helper/actions/workflows/ci.yml"><img src="https://img.shields.io/github/actions/workflow/status/uiwwsw/react-query-helper/ci.yml?branch=main&style=flat-square&label=CI" alt="CI status on main" /></a>
|
|
18
|
+
<a href="https://tanstack.com/query"><img src="https://img.shields.io/npm/dependency-version/@uiwwsw/react-query-helper/peer/@tanstack/react-query?style=flat-square&label=TanStack%20Query" alt="TanStack Query peer dependency range" /></a>
|
|
19
|
+
<a href="https://www.npmjs.com/package/@uiwwsw/react-query-helper"><img src="https://img.shields.io/node/v/@uiwwsw/react-query-helper?style=flat-square" alt="Node.js engine requirement" /></a>
|
|
20
|
+
<a href="https://www.npmjs.com/package/@uiwwsw/react-query-helper"><img src="https://img.shields.io/npm/types/@uiwwsw/react-query-helper?style=flat-square" alt="Bundled TypeScript types" /></a>
|
|
21
|
+
</p>
|
|
22
|
+
|
|
23
|
+
<p align="center">
|
|
24
|
+
<a href="https://react-query-helper.vercel.app/en/">Demo</a> ·
|
|
25
|
+
<a href="https://github.com/uiwwsw/react-query-helper/blob/main/docs/ARCHITECTURE_EN.md">Architecture</a> ·
|
|
26
|
+
<a href="https://www.npmjs.com/package/@uiwwsw/react-query-helper">npm</a> ·
|
|
27
|
+
<a href="https://github.com/uiwwsw/react-query-helper/blob/main/CHANGELOG.md">Changelog</a> ·
|
|
28
|
+
<a href="https://github.com/uiwwsw/react-query-helper/blob/main/README.md">한국어</a>
|
|
29
|
+
</p>
|
|
30
|
+
|
|
31
|
+
> **Version notice:** This README describes **2.x**. Check the npm badge above for the published version. If you use 1.x, read the [1.3.0 docs](https://github.com/uiwwsw/react-query-helper/blob/v1.3.0/README_EN.md) and [migration guide](https://github.com/uiwwsw/react-query-helper/blob/main/docs/REFERENCE_EN.md#migrating-from-1x). To try changes not yet published, [install from source](https://github.com/uiwwsw/react-query-helper/blob/main/docs/REFERENCE_EN.md#install-from-source-before-npm-publication).
|
|
32
|
+
|
|
33
|
+
A CLI and runtime helpers that read TypeScript API function files and generate query keys and TanStack Query v5 option factories (code that calls `queryOptions`, `infiniteQueryOptions` and `mutationOptions`). The input is existing TypeScript functions. It does not read OpenAPI specs. It has one maintainer and is not widely used.
|
|
34
|
+
|
|
35
|
+
## What works now
|
|
36
|
+
|
|
37
|
+
- `generate`: for each top-level function in `sourceDir`, writes a key constant and query, infinite-query and mutation option factories to `*Options.ts`.
|
|
38
|
+
- Keys are built from the source path's directories, the file name and the function name.
|
|
39
|
+
- The function name prefix (`get`, `list`, `create` and so on) picks what is generated. Configuration can change this.
|
|
40
|
+
- Mutations of functions with two or more arguments get a variables object type and `mapVariablesToArgs`.
|
|
41
|
+
- `withOptions` overrides `staleTime`, `select` and similar; argument, result and `select` result types are inferred.
|
|
42
|
+
- `generate --check`: writes nothing and exits with code 1 when output is missing, changed or stale.
|
|
43
|
+
- `watch`: regenerates when the sources, the config file or a listed local plugin changes.
|
|
44
|
+
- Generated files carry an ownership header on the first line; an existing file without it is not overwritten and causes an error.
|
|
45
|
+
- `customAnalyzerPath` and `customTemplatePath` replace the analyzer and the template.
|
|
46
|
+
|
|
47
|
+
## Not done / planned
|
|
48
|
+
|
|
49
|
+
- Reading OpenAPI, Swagger or other spec files.
|
|
50
|
+
- Hook generation. `useQuery` and the rest are called from TanStack Query directly.
|
|
51
|
+
- HTTP client generation. Your existing API functions are called as they are.
|
|
52
|
+
- Clients other than React (Vue, Svelte, Solid and so on). Only TanStack Query v5 for React is targeted.
|
|
53
|
+
- Mock generation.
|
|
54
|
+
- Guessing pagination cursors or forwarding `AbortSignal`. Both are configured per API.
|
|
55
|
+
- Analysis of re-export barrels, class or object methods, and functions returned by higher-order functions.
|
|
56
|
+
- Preserving every generic/overload relationship in the option factory types.
|
|
57
|
+
- Deducing HTTP semantics from function names. When a name does not follow the prefix rules, set it with `artifactsByName`.
|
|
58
|
+
- `watch` noticing changes in external files that the config imports. A restart is needed.
|
|
59
|
+
- Directory-wide transactions. Writes are atomic per file only.
|
|
60
|
+
- Running generated files directly in Node ESM. They target TypeScript bundlers.
|
|
61
|
+
- Automatic migration of unmarked 1.x generated files. They must be moved by hand.
|
|
62
|
+
|
|
63
|
+
## Where it stands
|
|
64
|
+
|
|
65
|
+
Weekly npm downloads (last week, api.npmjs.org):
|
|
66
|
+
|
|
67
|
+
| Package | Downloads |
|
|
68
|
+
| --------------------------------------- | -----------: |
|
|
69
|
+
| `@hey-api/openapi-ts` | 8,305,092 |
|
|
70
|
+
| `orval` | 4,328,761 |
|
|
71
|
+
| `openapi-react-query` | 956,262 |
|
|
72
|
+
| `@lukemorales/query-key-factory` | 493,363 |
|
|
73
|
+
| `@kubb/plugin-react-query` | 140,067 |
|
|
74
|
+
| `@uiwwsw/react-query-helper` (this one) | 202 |
|
|
75
|
+
|
|
76
|
+
- It has almost no users and one maintainer. There is no adoption to compare with the tools above.
|
|
77
|
+
- The larger tools generate types, clients, hooks, output for several frameworks and mocks from an OpenAPI spec. This package does none of that.
|
|
78
|
+
- If you have an OpenAPI spec, use one of those tools.
|
|
79
|
+
- This package fits one case: hand-written TypeScript API functions without a spec, from which you want keys and option factories.
|
|
80
|
+
- `@lukemorales/query-key-factory` covers keys only, and you declare the key structure by hand. This package generates keys and option factories from file paths and function names.
|
|
81
|
+
- With only a few queries, writing `queryOptions` directly is simpler. A comparison is in [Architecture and dependencies](docs/ARCHITECTURE_EN.md#similar-tools).
|
|
82
|
+
|
|
83
|
+
<a id="quick-start"></a>
|
|
84
|
+
|
|
85
|
+
## Install and Use
|
|
86
|
+
|
|
87
|
+
```sh
|
|
88
|
+
npm install @uiwwsw/react-query-helper@^2 @tanstack/react-query react
|
|
89
|
+
npx @uiwwsw/react-query-helper init
|
|
90
|
+
```
|
|
91
|
+
|
|
92
|
+
Generated code imports this package, so install it as a regular dependency (do not use `-D`).
|
|
93
|
+
|
|
94
|
+
```ts
|
|
95
|
+
// rqh.config.ts
|
|
96
|
+
import type { AutoQueryConfig } from "@uiwwsw/react-query-helper/config";
|
|
97
|
+
|
|
98
|
+
export default {
|
|
99
|
+
sourceDir: "./libs",
|
|
100
|
+
outputDir: "./src/options",
|
|
101
|
+
template: { artifactStrategy: "smart" },
|
|
102
|
+
} satisfies AutoQueryConfig;
|
|
103
|
+
```
|
|
104
|
+
|
|
105
|
+
```ts
|
|
106
|
+
// libs/users/api.ts
|
|
107
|
+
export const getUser = async (id: string) => ({ id, name: "Ada" });
|
|
108
|
+
```
|
|
109
|
+
|
|
110
|
+
Running `npx @uiwwsw/react-query-helper generate` writes `src/options/users/apiOptions.ts`.
|
|
111
|
+
|
|
112
|
+
```ts
|
|
113
|
+
// Generated by @uiwwsw/react-query-helper. DO NOT EDIT.
|
|
114
|
+
// Source root: "../../libs"
|
|
115
|
+
import { getUser } from "../../../libs/users/api";
|
|
116
|
+
import {
|
|
117
|
+
queryOption as __rqh_query,
|
|
118
|
+
infiniteOption as __rqh_infinite,
|
|
119
|
+
} from "@uiwwsw/react-query-helper";
|
|
120
|
+
|
|
121
|
+
export const getUserKey = ["users", "api", "getUser"] as const;
|
|
122
|
+
export const getUserQueryOption = __rqh_query(getUserKey, getUser);
|
|
123
|
+
export const getUserInfiniteQueryOption = __rqh_infinite(getUserKey, getUser);
|
|
124
|
+
```
|
|
125
|
+
|
|
126
|
+
Do not edit the generated file; change the source API instead. Usage:
|
|
127
|
+
|
|
128
|
+
```tsx
|
|
129
|
+
// src/UserName.tsx
|
|
130
|
+
import { useQuery } from "@tanstack/react-query";
|
|
131
|
+
import { getUserQueryOption } from "./options/users/apiOptions";
|
|
132
|
+
|
|
133
|
+
export function UserName({ id }: { id: string }) {
|
|
134
|
+
const user = useQuery(
|
|
135
|
+
getUserQueryOption.withOptions(
|
|
136
|
+
{ staleTime: 60_000, select: (data) => data.name },
|
|
137
|
+
id,
|
|
138
|
+
),
|
|
139
|
+
);
|
|
140
|
+
|
|
141
|
+
if (user.isPending) return <p>Loading...</p>;
|
|
142
|
+
if (user.isError) return <p>{user.error.message}</p>;
|
|
143
|
+
return <p>{user.data}</p>;
|
|
144
|
+
}
|
|
145
|
+
```
|
|
146
|
+
|
|
147
|
+
```ts
|
|
148
|
+
// With your existing QueryClient instance, client
|
|
149
|
+
await client.prefetchQuery(getUserQueryOption("u1"));
|
|
150
|
+
const cached = client.getQueryData(getUserQueryOption("u1").queryKey);
|
|
151
|
+
// { id: string; name: string } | undefined
|
|
152
|
+
```
|
|
153
|
+
|
|
154
|
+
API arguments become part of the cache key. Do not pass tokens or authorization headers as arguments.
|
|
155
|
+
|
|
156
|
+
## Docs
|
|
157
|
+
|
|
158
|
+
- [Configuration and usage reference](docs/REFERENCE_EN.md): CLI, configuration, generation rules, runtime options, infinite queries, migrating from 1.x
|
|
159
|
+
- [Sections moved from the README](docs/GUIDE_EN.md): full feature list, step-by-step install, recipe table, compatibility, dependencies, demo
|
|
160
|
+
- [Architecture and dependencies](docs/ARCHITECTURE_EN.md): module layout, comparison with similar tools
|
|
161
|
+
- [Repository review](docs/REVIEW.md) · [Changelog](CHANGELOG.md) · [Demo](https://react-query-helper.vercel.app/en/)
|
|
162
|
+
|
|
163
|
+
## License
|
|
164
|
+
|
|
165
|
+
[MIT License](LICENSE)
|
package/dist/cli.d.ts
CHANGED