@uiwwsw/react-query-helper 2.0.1 → 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 +4 -0
- package/README.md +87 -90
- package/README_EN.md +87 -90
- package/package.json +13 -9
package/CHANGELOG.md
CHANGED
|
@@ -1,5 +1,9 @@
|
|
|
1
1
|
# Changelog
|
|
2
2
|
|
|
3
|
+
## 2.0.2
|
|
4
|
+
|
|
5
|
+
- Shorten the README to what works, what is not done, and how it compares with OpenAPI-based generators; move the details into docs/GUIDE.md. No code changes.
|
|
6
|
+
|
|
3
7
|
## 2.0.1
|
|
4
8
|
|
|
5
9
|
The v2.0.0 tag was not published to npm. This release includes all of the 2.0.0
|
package/README.md
CHANGED
|
@@ -1,65 +1,95 @@
|
|
|
1
1
|
<p align="center">
|
|
2
2
|
<picture>
|
|
3
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
|
|
4
|
+
<img src="https://raw.githubusercontent.com/uiwwsw/react-query-helper/main/assets/readme-hero.svg" alt="React Query Helper logo" width="1120" />
|
|
5
5
|
</picture>
|
|
6
6
|
</p>
|
|
7
7
|
|
|
8
8
|
<h1 align="center">React Query Helper</h1>
|
|
9
9
|
|
|
10
10
|
<p align="center">
|
|
11
|
-
API
|
|
12
|
-
TypeScript 함수에서 TanStack Query v5의 타입 안전한 옵션 팩토리를 만듭니다.
|
|
11
|
+
TypeScript API 함수 파일에서 쿼리 키와 TanStack Query v5 옵션 팩토리(<code>queryOptions</code>, <code>infiniteQueryOptions</code>, <code>mutationOptions</code> 호출 코드)를 생성하는 CLI와 런타임 헬퍼입니다.
|
|
13
12
|
</p>
|
|
14
13
|
|
|
15
14
|
<p align="center">
|
|
16
|
-
<a href="https://www.npmjs.com/package/@uiwwsw/react-query-helper"><img src="https://img.shields.io/npm/v
|
|
17
|
-
<a href="https://github.com/uiwwsw/react-query-helper/
|
|
18
|
-
<a href="https://github.com/uiwwsw/react-query-helper/
|
|
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 버전" /></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="라이선스" /></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="main 브랜치 CI 상태" /></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 의존성 범위" /></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 요구 버전" /></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="포함된 TypeScript 타입" /></a>
|
|
19
21
|
</p>
|
|
20
22
|
|
|
21
23
|
<p align="center">
|
|
22
|
-
<a href="
|
|
23
|
-
<a href="https://github.com/uiwwsw/react-query-helper/blob/main/docs/
|
|
24
|
-
<a href="https://
|
|
24
|
+
<a href="https://react-query-helper.vercel.app/">데모</a> ·
|
|
25
|
+
<a href="https://github.com/uiwwsw/react-query-helper/blob/main/docs/ARCHITECTURE.md">구성과 의존성</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">변경 이력</a> ·
|
|
25
28
|
<a href="https://github.com/uiwwsw/react-query-helper/blob/main/README_EN.md">English</a>
|
|
26
29
|
</p>
|
|
27
30
|
|
|
28
31
|
> **버전 안내:** 이 문서는 **2.x** 기준입니다. 실제 게시 버전은 위 npm 배지에서 확인하세요. 1.x를 사용 중이라면 [1.3.0 문서](https://github.com/uiwwsw/react-query-helper/blob/v1.3.0/README.md)와 [마이그레이션 가이드](https://github.com/uiwwsw/react-query-helper/blob/main/docs/REFERENCE.md#1x에서-이전)를 참고하세요. 아직 게시되지 않은 변경은 [소스에서 설치](https://github.com/uiwwsw/react-query-helper/blob/main/docs/REFERENCE.md#npm-게시-전-소스에서-설치하기)할 수 있습니다.
|
|
29
32
|
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
33
|
+
TypeScript API 함수 파일을 읽어 쿼리 키와 TanStack Query v5 옵션 팩토리(`queryOptions`, `infiniteQueryOptions`, `mutationOptions`를 호출하는 코드)를 생성하는 CLI와 런타임 헬퍼입니다. 입력은 이미 있는 TypeScript 함수입니다. OpenAPI 명세는 읽지 않습니다. 제작자는 한 명이고, 아직 널리 쓰이지 않습니다.
|
|
34
|
+
|
|
35
|
+
## 지금 되는 것
|
|
36
|
+
|
|
37
|
+
- `generate`: `sourceDir`의 최상위 함수마다 키 상수와 query, infinite query, mutation 옵션 팩토리를 `*Options.ts`로 생성합니다.
|
|
38
|
+
- 키는 소스 경로의 디렉토리, 파일명, 함수명으로 만듭니다.
|
|
39
|
+
- 함수 이름 접두사(`get`, `list`, `create` 등)로 생성 종류를 고릅니다. 설정으로 바꿀 수 있습니다.
|
|
40
|
+
- 인자가 둘 이상인 함수의 mutation에 변수 객체 타입과 `mapVariablesToArgs`를 생성합니다.
|
|
41
|
+
- `withOptions`로 `staleTime`, `select` 등을 덮어쓰고, 인자·반환·`select` 결과 타입이 추론됩니다.
|
|
42
|
+
- `generate --check`: 파일을 쓰지 않고 누락·변경·오래된 결과가 있으면 종료 코드 1을 반환합니다.
|
|
43
|
+
- `watch`: 소스, 설정 파일, 지정한 로컬 플러그인이 바뀌면 다시 생성합니다.
|
|
44
|
+
- 생성 파일 첫 줄의 소유 표시로 구분해, 표시 없는 기존 파일은 덮어쓰지 않고 오류로 처리합니다.
|
|
45
|
+
- `customAnalyzerPath`, `customTemplatePath`로 분석기와 템플릿을 교체할 수 있습니다.
|
|
46
|
+
|
|
47
|
+
## 아직 안 되는 것 / 진행 예정
|
|
48
|
+
|
|
49
|
+
- OpenAPI, Swagger 등 명세 파일 읽기.
|
|
50
|
+
- 훅 생성. `useQuery` 등은 TanStack Query의 것을 직접 호출합니다.
|
|
51
|
+
- HTTP 클라이언트 생성. 기존 API 함수를 그대로 호출합니다.
|
|
52
|
+
- React 외 클라이언트(Vue, Svelte, Solid 등). TanStack Query v5 React만 대상입니다.
|
|
53
|
+
- 목(mock) 생성.
|
|
54
|
+
- 페이지네이션 커서와 `AbortSignal` 전달의 자동 추측. API마다 직접 설정합니다.
|
|
55
|
+
- re-export barrel, 클래스·객체 메서드, 고차 함수가 반환한 함수 분석.
|
|
56
|
+
- 모든 generic/overload 관계를 옵션 팩토리 타입으로 보존하는 변환.
|
|
57
|
+
- 함수 이름만으로 HTTP 의미를 판단하는 것. 접두사 규칙에 맞지 않으면 `artifactsByName`으로 지정합니다.
|
|
58
|
+
- `watch`가 설정이 import하는 외부 파일의 변경을 감지하는 것. 재시작이 필요합니다.
|
|
59
|
+
- 디렉토리 전체 단위의 트랜잭션. 파일 단위로만 원자적으로 씁니다.
|
|
60
|
+
- 생성 파일을 Node ESM에서 직접 실행하는 것. TS 번들러용입니다.
|
|
61
|
+
- 표시 없는 1.x 생성 파일의 자동 이전. 수동으로 옮겨야 합니다.
|
|
62
|
+
|
|
63
|
+
## 현재 위치
|
|
64
|
+
|
|
65
|
+
주간 npm 다운로드(최근 1주, api.npmjs.org 기준):
|
|
66
|
+
|
|
67
|
+
| 패키지 | 다운로드 |
|
|
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` (이 패키지) | 202 |
|
|
75
|
+
|
|
76
|
+
- 사용자가 사실상 없습니다. 관리자는 한 명입니다. 위 도구들과 비교할 만한 채택 사례가 없습니다.
|
|
77
|
+
- 규모가 큰 도구는 OpenAPI 명세에서 타입, 클라이언트, 훅, 여러 프레임워크용 출력, 목까지 만듭니다. 이 패키지는 그중 어느 것도 하지 않습니다.
|
|
78
|
+
- OpenAPI 명세가 있으면 위 도구를 쓰는 것이 맞습니다.
|
|
79
|
+
- 이 패키지가 맞는 경우는 하나입니다. 명세 없이 손으로 쓴 TypeScript API 함수가 있고, 그 함수에서 키와 옵션 팩토리를 만들고 싶을 때입니다.
|
|
80
|
+
- `@lukemorales/query-key-factory`는 키만 다루고, 키 구조를 손으로 선언합니다. 이 패키지는 키와 옵션 팩토리를 파일 경로와 함수명에서 생성합니다.
|
|
81
|
+
- 쿼리가 몇 개뿐이면 `queryOptions`를 직접 쓰는 편이 단순합니다. 비교는 [구성과 의존성](docs/ARCHITECTURE.md#비슷한-도구)에 있습니다.
|
|
44
82
|
|
|
45
83
|
<a id="quick-start"></a>
|
|
46
84
|
|
|
47
|
-
##
|
|
48
|
-
|
|
49
|
-
### 1. 설치하고 초기화
|
|
85
|
+
## 설치와 사용
|
|
50
86
|
|
|
51
87
|
```sh
|
|
52
88
|
npm install @uiwwsw/react-query-helper@^2 @tanstack/react-query react
|
|
53
89
|
npx @uiwwsw/react-query-helper init
|
|
54
90
|
```
|
|
55
91
|
|
|
56
|
-
생성 코드가 이 패키지를 import하므로
|
|
57
|
-
React와 TanStack Query가 이미 설치되어 있다면 필요한 의존성만 추가하세요.
|
|
58
|
-
|
|
59
|
-
### 2. API 경로 지정
|
|
60
|
-
|
|
61
|
-
`init`이 만든 `rqh.config.ts`의 경로를 앱 구조에 맞춥니다.
|
|
62
|
-
별도 플러그인이나 alias 설정 없이 시작할 수 있습니다.
|
|
92
|
+
생성 코드가 이 패키지를 import하므로 일반 dependency로 설치합니다(`-D` 사용 금지).
|
|
63
93
|
|
|
64
94
|
```ts
|
|
65
95
|
// rqh.config.ts
|
|
@@ -68,27 +98,32 @@ import type { AutoQueryConfig } from "@uiwwsw/react-query-helper/config";
|
|
|
68
98
|
export default {
|
|
69
99
|
sourceDir: "./libs",
|
|
70
100
|
outputDir: "./src/options",
|
|
71
|
-
ignoredFiles: ["domain.ts", "adaptor.ts", "**/*.test.ts", "**/*.spec.ts"],
|
|
72
101
|
template: { artifactStrategy: "smart" },
|
|
73
102
|
} satisfies AutoQueryConfig;
|
|
74
103
|
```
|
|
75
104
|
|
|
76
|
-
실제 API 함수를 해당 디렉토리에 두세요. 아래는 흐름을 확인하기 위한 간단한 예시입니다.
|
|
77
|
-
|
|
78
105
|
```ts
|
|
79
106
|
// libs/users/api.ts
|
|
80
107
|
export const getUser = async (id: string) => ({ id, name: "Ada" });
|
|
81
108
|
```
|
|
82
109
|
|
|
83
|
-
|
|
110
|
+
`npx @uiwwsw/react-query-helper generate`를 실행하면 `src/options/users/apiOptions.ts`가 생성됩니다.
|
|
84
111
|
|
|
85
|
-
```
|
|
86
|
-
|
|
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);
|
|
87
124
|
```
|
|
88
125
|
|
|
89
|
-
|
|
90
|
-
`getUserInfiniteQueryOption`이 생성됩니다. **생성 파일 대신 원본 API를 수정하세요.**
|
|
91
|
-
무한 쿼리 팩토리도 생성되지만 실제 페이지네이션 설정은 API에 맞게 별도로 지정합니다.
|
|
126
|
+
생성 파일은 수정하지 않습니다. 고칠 때는 원본 API를 수정합니다. 사용:
|
|
92
127
|
|
|
93
128
|
```tsx
|
|
94
129
|
// src/UserName.tsx
|
|
@@ -109,9 +144,6 @@ export function UserName({ id }: { id: string }) {
|
|
|
109
144
|
}
|
|
110
145
|
```
|
|
111
146
|
|
|
112
|
-
앱 상위에 `QueryClientProvider`가 필요합니다. 위의 `user.data`는 `select`에 따라
|
|
113
|
-
`string`으로 추론됩니다. 옵션은 훅 바깥에서도 재사용할 수 있습니다.
|
|
114
|
-
|
|
115
147
|
```ts
|
|
116
148
|
// 기존 QueryClient 인스턴스 client에서
|
|
117
149
|
await client.prefetchQuery(getUserQueryOption("u1"));
|
|
@@ -119,50 +151,15 @@ const cached = client.getQueryData(getUserQueryOption("u1").queryKey);
|
|
|
119
151
|
// { id: string; name: string } | undefined
|
|
120
152
|
```
|
|
121
153
|
|
|
122
|
-
|
|
154
|
+
API 인자는 캐시 키에 포함됩니다. 토큰이나 인증 헤더를 인자로 넘기지 마세요.
|
|
123
155
|
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
```
|
|
156
|
+
## 문서
|
|
157
|
+
|
|
158
|
+
- [설정과 사용 가이드](docs/REFERENCE.md): CLI, 설정, 생성 규칙, 런타임 옵션, 무한 쿼리, 1.x 이전
|
|
159
|
+
- [README에서 옮긴 상세](docs/GUIDE.md): 전체 기능 목록, 설치 단계별 설명, 예제 찾기 표, 지원 환경, 의존성, 데모
|
|
160
|
+
- [구성과 의존성](docs/ARCHITECTURE.md): 모듈 구조, 비슷한 도구와의 비교
|
|
161
|
+
- [저장소 검토 기록](docs/REVIEW.md) · [변경 이력](CHANGELOG.md) · [데모](https://react-query-helper.vercel.app/)
|
|
162
|
+
|
|
163
|
+
## 라이선스
|
|
133
164
|
|
|
134
|
-
|
|
135
|
-
check는 파일을 수정하지 않고 누락·변경·오래된 결과를 발견하면 종료 코드 1을 반환합니다.
|
|
136
|
-
생성물을 커밋하지 않는 프로젝트라면 CI에서 `query:generate` 후 앱 타입 검사를 실행하세요.
|
|
137
|
-
|
|
138
|
-
## 필요한 예제 찾기
|
|
139
|
-
|
|
140
|
-
| 하고 싶은 일 | 가이드 |
|
|
141
|
-
| ----------------------------------- | --------------------------------------------------------------------------------------------------------------- |
|
|
142
|
-
| 설정 파일, 경로, ignore glob 변경 | [설정](https://github.com/uiwwsw/react-query-helper/blob/main/docs/REFERENCE.md#설정) |
|
|
143
|
-
| 함수별 query/mutation 생성 제어 | [생성 규칙과 캐시 키](https://github.com/uiwwsw/react-query-helper/blob/main/docs/REFERENCE.md#생성-규칙) |
|
|
144
|
-
| mutation 인자, 취소, 옵션 확장 | [런타임 옵션](https://github.com/uiwwsw/react-query-helper/blob/main/docs/REFERENCE.md#런타임-옵션) |
|
|
145
|
-
| 커서 페이지네이션과 `maxPages` | [무한 쿼리](https://github.com/uiwwsw/react-query-helper/blob/main/docs/REFERENCE.md#무한-쿼리) |
|
|
146
|
-
| 기존 1.x 프로젝트 업그레이드 | [마이그레이션 체크리스트](https://github.com/uiwwsw/react-query-helper/blob/main/docs/REFERENCE.md#1x에서-이전) |
|
|
147
|
-
| 지원하지 않는 문법, 커스텀 플러그인 | [지원 범위와 확장](https://github.com/uiwwsw/react-query-helper/blob/main/docs/REFERENCE.md#범위와-확장) |
|
|
148
|
-
| 테스트, 로컬 설치, npm 배포 설정 | [개발과 배포](https://github.com/uiwwsw/react-query-helper/blob/main/docs/REFERENCE.md#개발과-배포) |
|
|
149
|
-
| 이번 개선의 근거와 남은 한계 | [저장소 검토 기록](https://github.com/uiwwsw/react-query-helper/blob/main/docs/REVIEW.md) |
|
|
150
|
-
|
|
151
|
-
## 지원 환경과 주의점
|
|
152
|
-
|
|
153
|
-
| 구분 | 지원 기준 |
|
|
154
|
-
| -------------- | -------------------------------------------------- |
|
|
155
|
-
| Node.js | CLI/빌드에 22 이상, CI는 22/24 |
|
|
156
|
-
| React | 18/19 |
|
|
157
|
-
| TanStack Query | `>=5.102.8 <6` |
|
|
158
|
-
| TypeScript | 5.8 이상, 생성 코드는 TS 번들러용 |
|
|
159
|
-
| 브라우저 | 루트 import는 브라우저용, Node 도구는 별도 subpath |
|
|
160
|
-
|
|
161
|
-
- **2.x는 호환성을 깨는 변경입니다.** 기존 생성 파일을 옮긴 뒤 재생성하고, 수동 키와 persisted cache를 마이그레이션하세요.
|
|
162
|
-
- API 인자는 캐시 키에 포함됩니다. 직렬화 가능한 값만 전달하고 토큰·인증 헤더는 넣지 마세요.
|
|
163
|
-
- 페이지네이션과 요청 취소는 자동 추측하지 않습니다. 커서 매핑과 `AbortSignal` 전달은 API에 맞춰 설정하세요.
|
|
164
|
-
- 최상위 함수가 기본 대상입니다. re-export barrel, 메서드, 모든 generic/overload 관계를 처리하는 범용 TypeScript 변환기는 아닙니다.
|
|
165
|
-
|
|
166
|
-
[변경 이력](https://github.com/uiwwsw/react-query-helper/blob/main/CHANGELOG.md) · [로고와 브랜드 자산](https://github.com/uiwwsw/react-query-helper/blob/main/assets/README.md) · [MIT License](https://github.com/uiwwsw/react-query-helper/blob/main/LICENSE)
|
|
167
|
-
|
|
168
|
-
독립적인 커뮤니티 도구이며 React 또는 TanStack의 공식 프로젝트가 아닙니다.
|
|
165
|
+
[MIT License](LICENSE)
|
package/README_EN.md
CHANGED
|
@@ -1,65 +1,95 @@
|
|
|
1
1
|
<p align="center">
|
|
2
2
|
<picture>
|
|
3
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
|
|
4
|
+
<img src="https://raw.githubusercontent.com/uiwwsw/react-query-helper/main/assets/readme-hero.svg" alt="React Query Helper logo" width="1120" />
|
|
5
5
|
</picture>
|
|
6
6
|
</p>
|
|
7
7
|
|
|
8
8
|
<h1 align="center">React Query Helper</h1>
|
|
9
9
|
|
|
10
10
|
<p align="center">
|
|
11
|
-
|
|
12
|
-
Type-safe TanStack Query v5 option factories, built from TypeScript.
|
|
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.
|
|
13
12
|
</p>
|
|
14
13
|
|
|
15
14
|
<p align="center">
|
|
16
|
-
<a href="https://www.npmjs.com/package/@uiwwsw/react-query-helper"><img src="https://img.shields.io/npm/v
|
|
17
|
-
<a href="https://github.com/uiwwsw/react-query-helper/
|
|
18
|
-
<a href="https://github.com/uiwwsw/react-query-helper/
|
|
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>
|
|
19
21
|
</p>
|
|
20
22
|
|
|
21
23
|
<p align="center">
|
|
22
|
-
<a href="
|
|
23
|
-
<a href="https://github.com/uiwwsw/react-query-helper/blob/main/docs/
|
|
24
|
-
<a href="https://
|
|
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> ·
|
|
25
28
|
<a href="https://github.com/uiwwsw/react-query-helper/blob/main/README.md">한국어</a>
|
|
26
29
|
</p>
|
|
27
30
|
|
|
28
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).
|
|
29
32
|
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
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).
|
|
44
82
|
|
|
45
83
|
<a id="quick-start"></a>
|
|
46
84
|
|
|
47
|
-
##
|
|
48
|
-
|
|
49
|
-
### 1. Install and Initialize
|
|
85
|
+
## Install and Use
|
|
50
86
|
|
|
51
87
|
```sh
|
|
52
88
|
npm install @uiwwsw/react-query-helper@^2 @tanstack/react-query react
|
|
53
89
|
npx @uiwwsw/react-query-helper init
|
|
54
90
|
```
|
|
55
91
|
|
|
56
|
-
|
|
57
|
-
Only add React and TanStack Query if your app does not already have them.
|
|
58
|
-
|
|
59
|
-
### 2. Point to Your APIs
|
|
60
|
-
|
|
61
|
-
Adjust the paths in the generated `rqh.config.ts` to match your app.
|
|
62
|
-
No custom plugins or import aliases are required.
|
|
92
|
+
Generated code imports this package, so install it as a regular dependency (do not use `-D`).
|
|
63
93
|
|
|
64
94
|
```ts
|
|
65
95
|
// rqh.config.ts
|
|
@@ -68,27 +98,32 @@ import type { AutoQueryConfig } from "@uiwwsw/react-query-helper/config";
|
|
|
68
98
|
export default {
|
|
69
99
|
sourceDir: "./libs",
|
|
70
100
|
outputDir: "./src/options",
|
|
71
|
-
ignoredFiles: ["domain.ts", "adaptor.ts", "**/*.test.ts", "**/*.spec.ts"],
|
|
72
101
|
template: { artifactStrategy: "smart" },
|
|
73
102
|
} satisfies AutoQueryConfig;
|
|
74
103
|
```
|
|
75
104
|
|
|
76
|
-
Place your existing API functions there. This minimal example demonstrates the flow:
|
|
77
|
-
|
|
78
105
|
```ts
|
|
79
106
|
// libs/users/api.ts
|
|
80
107
|
export const getUser = async (id: string) => ({ id, name: "Ada" });
|
|
81
108
|
```
|
|
82
109
|
|
|
83
|
-
|
|
110
|
+
Running `npx @uiwwsw/react-query-helper generate` writes `src/options/users/apiOptions.ts`.
|
|
84
111
|
|
|
85
|
-
```
|
|
86
|
-
|
|
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);
|
|
87
124
|
```
|
|
88
125
|
|
|
89
|
-
|
|
90
|
-
and `getUserInfiniteQueryOption`. **Edit the source API, not the generated file.**
|
|
91
|
-
An infinite-query factory is generated too; configure pagination explicitly for your API.
|
|
126
|
+
Do not edit the generated file; change the source API instead. Usage:
|
|
92
127
|
|
|
93
128
|
```tsx
|
|
94
129
|
// src/UserName.tsx
|
|
@@ -109,9 +144,6 @@ export function UserName({ id }: { id: string }) {
|
|
|
109
144
|
}
|
|
110
145
|
```
|
|
111
146
|
|
|
112
|
-
Wrap the app in `QueryClientProvider`. The selected `user.data` is inferred as `string`.
|
|
113
|
-
Reuse the same factory outside components:
|
|
114
|
-
|
|
115
147
|
```ts
|
|
116
148
|
// With your existing QueryClient instance, client
|
|
117
149
|
await client.prefetchQuery(getUserQueryOption("u1"));
|
|
@@ -119,50 +151,15 @@ const cached = client.getQueryData(getUserQueryOption("u1").queryKey);
|
|
|
119
151
|
// { id: string; name: string } | undefined
|
|
120
152
|
```
|
|
121
153
|
|
|
122
|
-
|
|
154
|
+
API arguments become part of the cache key. Do not pass tokens or authorization headers as arguments.
|
|
123
155
|
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
```
|
|
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
|
|
133
164
|
|
|
134
|
-
|
|
135
|
-
Check never writes files; missing, changed, or stale output causes exit code 1.
|
|
136
|
-
If you do not commit generated files, run `query:generate` before the application's typecheck instead.
|
|
137
|
-
|
|
138
|
-
## Find a Recipe
|
|
139
|
-
|
|
140
|
-
| Task | Guide |
|
|
141
|
-
| ------------------------------------------------- | -------------------------------------------------------------------------------------------------------------------------------- |
|
|
142
|
-
| Config files, paths, and ignore globs | [Configuration](https://github.com/uiwwsw/react-query-helper/blob/main/docs/REFERENCE_EN.md#configuration) |
|
|
143
|
-
| Choose query/mutation output per function | [Generation and cache keys](https://github.com/uiwwsw/react-query-helper/blob/main/docs/REFERENCE_EN.md#generation-rules) |
|
|
144
|
-
| Mutation arguments, cancellation, overrides | [Runtime options](https://github.com/uiwwsw/react-query-helper/blob/main/docs/REFERENCE_EN.md#runtime-options) |
|
|
145
|
-
| Cursor pagination and `maxPages` | [Infinite queries](https://github.com/uiwwsw/react-query-helper/blob/main/docs/REFERENCE_EN.md#infinite-queries) |
|
|
146
|
-
| Upgrade an existing 1.x application | [Migration checklist](https://github.com/uiwwsw/react-query-helper/blob/main/docs/REFERENCE_EN.md#migrating-from-1x) |
|
|
147
|
-
| Unsupported syntax and custom plugins | [Scope and extensions](https://github.com/uiwwsw/react-query-helper/blob/main/docs/REFERENCE_EN.md#scope-and-extensions) |
|
|
148
|
-
| Tests, local installation, npm publishing | [Development and releases](https://github.com/uiwwsw/react-query-helper/blob/main/docs/REFERENCE_EN.md#development-and-releases) |
|
|
149
|
-
| Findings behind this release and remaining limits | [Repository review](https://github.com/uiwwsw/react-query-helper/blob/main/docs/REVIEW.md) |
|
|
150
|
-
|
|
151
|
-
## Compatibility and Boundaries
|
|
152
|
-
|
|
153
|
-
| Area | Support |
|
|
154
|
-
| -------------- | ---------------------------------------------------- |
|
|
155
|
-
| Node.js | 22+ for CLI/build; CI covers 22/24 |
|
|
156
|
-
| React | 18/19 |
|
|
157
|
-
| TanStack Query | `>=5.102.8 <6` |
|
|
158
|
-
| TypeScript | 5.8+; generated code targets TS bundlers |
|
|
159
|
-
| Browser | Browser-safe root entry; separate Node tool subpaths |
|
|
160
|
-
|
|
161
|
-
- **2.x contains breaking changes.** Move legacy generated files aside, regenerate, and migrate manual keys and persisted caches.
|
|
162
|
-
- API arguments become part of the cache key. Use serializable values and never include tokens or authorization headers.
|
|
163
|
-
- Pagination and cancellation are explicit. Configure cursor mapping and forward AbortSignal to the request yourself.
|
|
164
|
-
- The analyzer targets top-level functions, not every TypeScript construct. Re-export barrels, methods, and all generic/overload relationships are not universally supported.
|
|
165
|
-
|
|
166
|
-
[Changelog](https://github.com/uiwwsw/react-query-helper/blob/main/CHANGELOG.md) · [Logo and brand assets](https://github.com/uiwwsw/react-query-helper/blob/main/assets/README.md) · [MIT License](https://github.com/uiwwsw/react-query-helper/blob/main/LICENSE)
|
|
167
|
-
|
|
168
|
-
An independent community tool, not an official React or TanStack project.
|
|
165
|
+
[MIT License](LICENSE)
|
package/package.json
CHANGED
|
@@ -1,24 +1,27 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@uiwwsw/react-query-helper",
|
|
3
|
-
"version": "2.0.
|
|
3
|
+
"version": "2.0.2",
|
|
4
4
|
"module": "dist/index.js",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"types": "dist/index.d.ts",
|
|
7
7
|
"main": "dist/index.js",
|
|
8
8
|
"license": "MIT",
|
|
9
|
-
"description": "
|
|
9
|
+
"description": "CLI and runtime helpers that generate query keys and TanStack Query v5 option factories (queryOptions, infiniteQueryOptions, mutationOptions) from top-level TypeScript API functions.",
|
|
10
10
|
"keywords": [
|
|
11
|
-
"react",
|
|
12
11
|
"react-query",
|
|
13
12
|
"tanstack-query",
|
|
13
|
+
"tanstack-query-v5",
|
|
14
|
+
"react",
|
|
15
|
+
"typescript",
|
|
14
16
|
"codegen",
|
|
15
|
-
"generator",
|
|
17
|
+
"code-generator",
|
|
16
18
|
"cli",
|
|
17
|
-
"
|
|
18
|
-
"
|
|
19
|
-
"
|
|
19
|
+
"query-key-factory",
|
|
20
|
+
"query-options",
|
|
21
|
+
"mutation-options",
|
|
22
|
+
"infinite-query"
|
|
20
23
|
],
|
|
21
|
-
"homepage": "https://
|
|
24
|
+
"homepage": "https://react-query-helper.vercel.app/",
|
|
22
25
|
"bugs": {
|
|
23
26
|
"url": "https://github.com/uiwwsw/react-query-helper/issues"
|
|
24
27
|
},
|
|
@@ -63,7 +66,8 @@
|
|
|
63
66
|
"generate": "npm run build && node dist/cli.js generate",
|
|
64
67
|
"test": "npm run build && node --test test/*.test.mjs && tsc -p test/tsconfig.json && tsc -p test/tsconfig.register.json",
|
|
65
68
|
"prepack": "npm run build",
|
|
66
|
-
"smoke": "node scripts/smoke.mjs"
|
|
69
|
+
"smoke": "node scripts/smoke.mjs",
|
|
70
|
+
"build:site": "npm run build && node scripts/build-site.mjs"
|
|
67
71
|
},
|
|
68
72
|
"devDependencies": {
|
|
69
73
|
"@tanstack/react-query": "^5.102.8",
|