diff --git a/CHANGELOG.md b/CHANGELOG.md index bff0770..580ae3d 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,9 @@ # Changelog +## 2.0.4 + +- README and package description position the tool as a generator of the TanStack Query layer from existing API functions, with fit and roadmap sections. No code changes. + ## 2.0.3 - README leads with how the tool differs from OpenAPI generators and key factories. No code changes. diff --git a/README.md b/README.md index fe91dbc..e417950 100644 --- a/README.md +++ b/README.md @@ -8,7 +8,7 @@

React Query Helper

- TypeScript API 함수 파일에서 쿼리 키와 TanStack Query v5 옵션 팩토리(queryOptions, infiniteQueryOptions, mutationOptions 호출 코드)를 생성하는 CLI와 런타임 헬퍼입니다. + 기존 TypeScript API 함수는 그대로 두고, 그 위의 TanStack Query 계층(쿼리 키·옵션)을 생성하고 동기화하는 코드 생성 도구

@@ -30,43 +30,56 @@ > **버전 안내:** 이 문서는 **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-게시-전-소스에서-설치하기)할 수 있습니다. -이미 쓰고 있는 TypeScript API 함수를 그대로 두고, CLI가 그 함수에서 쿼리 키와 TanStack Query v5 옵션 팩토리(`queryOptions`, `infiniteQueryOptions`, `mutationOptions`)를 자동으로 생성합니다. API 코드는 한 줄도 바꾸지 않고, OpenAPI 명세도 필요 없습니다. +**OpenAPI 없이, 기존 API는 그대로, Query 계층은 자동으로.** 이미 쓰고 있는 TypeScript API 함수에서 쿼리 키와 TanStack Query v5 옵션 팩토리(`queryOptions`, `infiniteQueryOptions`, `mutationOptions`)를 생성하고, API가 바뀌면 다시 맞춥니다. + +## 왜 필요한가 + +TanStack Query는 `queryOptions`로 쿼리 키와 조회 함수를 묶어 재사용하는 방식을 공식으로 제공합니다. 다만 그 연결은 API 함수마다 사람이 직접 써야 합니다. + +```ts +export const userOptions = (id: string) => + queryOptions({ queryKey: ["users", id], queryFn: () => getUser(id) }); +``` + +API 함수가 수백 개가 되고 여러 사람이 함께 관리하면, 키 이름 규칙, 함수와 옵션의 연결, 인자가 바뀔 때의 동기화가 매번 반복되는 일이 됩니다. React Query Helper는 이 연결을 생성기로 고정합니다. 캐싱, 재시도, 데이터 요청은 지금처럼 TanStack Query가 하고, 이 도구는 그 앞의 연결 코드만 만듭니다. ## 다른 도구와 다른 점 -- **API 코드를 건드리지 않습니다.** 생성 파일은 `outputDir`에 따로 만들어지고 원본 API 함수를 import만 합니다. 인증 헤더, 인터셉터, 에러 처리를 넣어 둔 기존 `fetch`·axios 래퍼를 그대로 씁니다. [Orval](https://orval.dev), [Kubb](https://kubb.dev), [Hey API](https://heyapi.dev)는 OpenAPI 명세에서 API 함수 자체를 생성하므로, 손으로 쓴 API 함수가 생성된 함수로 대체됩니다(fetch 인스턴스는 직접 지정할 수 있습니다). -- **명세 없이 TypeScript 함수만으로 동작합니다.** 백엔드가 OpenAPI 명세를 제공하지 않는 프로젝트에서도 씁니다. -- **키를 손으로 선언하지 않습니다.** [`@lukemorales/query-key-factory`](https://github.com/lukemorales/query-key-factory)는 키 구조를 직접 작성해야 하지만, 이 도구는 파일 경로와 함수명으로 키를 만들고 옵션 팩토리까지 생성합니다. -- **타입이 원본 함수에서 그대로 추론됩니다.** 인자, 반환값, `select` 결과 타입을 따로 선언하지 않습니다. -- **API가 바뀌면 따라갑니다.** `watch`가 다시 생성하고, `generate --check`는 CI에서 오래된 생성 파일을 잡아냅니다. +- **기존 API 계층을 그대로 씁니다.** 생성 파일은 `outputDir`에 따로 만들어지고 원본 API 함수를 import만 합니다. 인증 헤더, 인터셉터, 에러 처리, 응답 변환을 넣어 둔 기존 `fetch`·axios 래퍼와 비즈니스 로직이 그대로 남습니다. [Orval](https://orval.dev), [Kubb](https://kubb.dev), [Hey API](https://heyapi.dev)는 OpenAPI 명세에서 API 함수 자체를 생성하므로, 손으로 쓴 API 함수가 생성된 함수로 대체됩니다(fetch 인스턴스는 직접 지정할 수 있습니다). +- **명세 없이 동작합니다.** 입력은 이미 있는 TypeScript 함수입니다. OpenAPI·Swagger 명세가 없거나 최신이 아닌 프로젝트에서도 씁니다. +- **키 규칙이 자동으로 통일됩니다.** `libs/users/api.ts`의 `getUser`는 `["users", "api", "getUser"]`가 됩니다. 사람마다 다른 키 이름을 쓰는 문제가 없습니다. [`@lukemorales/query-key-factory`](https://github.com/lukemorales/query-key-factory)는 키 구조를 직접 선언해야 하고, 이 도구는 옵션 팩토리까지 생성합니다. +- **타입을 다시 선언하지 않습니다.** 인자, 반환값, `select` 결과 타입이 원본 함수에서 추론됩니다. +- **생성물은 평범한 TypeScript 파일입니다.** 런타임에 API를 탐색하지 않고 실제 `.ts` 파일을 만들기 때문에, 코드 리뷰에서 보이고 IDE에서 정의로 따라갈 수 있습니다. 별도의 캐시나 쿼리 엔진도 없습니다. +- **API와 Query 계층이 어긋나지 않습니다.** `watch`가 API 변경을 감지해 다시 생성하고, `generate --check`가 CI에서 오래된 생성 파일을 잡아냅니다. +- **규칙을 프로젝트에 맞출 수 있습니다.** 함수별 생성 종류(`artifactsByName`), 분석기(`customAnalyzerPath`), 템플릿(`customTemplatePath`)을 바꿀 수 있습니다. + +## 이런 프로젝트에 맞습니다 + +- 인증·에러 처리가 들어간 `fetch`·axios API 계층이 이미 있는 프로젝트 +- 백엔드가 OpenAPI 명세를 제공하지 않거나 명세가 실제와 다른 프로젝트 +- API 함수가 많고, 여러 개발자가 같은 쿼리 키 규칙으로 일해야 하는 팀 +- 기존 코드를 크게 바꾸지 않고 TanStack Query 사용 규칙을 표준화하려는 팀 ## 지금 되는 것 - `generate`: `sourceDir`의 최상위 함수마다 키 상수와 query, infinite query, mutation 옵션 팩토리를 `*Options.ts`로 생성합니다. -- 키는 소스 경로의 디렉토리, 파일명, 함수명으로 만듭니다. -- 함수 이름 접두사(`get`, `list`, `create` 등)로 생성 종류를 고릅니다. 설정으로 바꿀 수 있습니다. +- 함수 이름 접두사로 생성 종류를 고릅니다. `get`·`list`·`fetch` 등은 query와 infinite query, `create`·`update`·`delete` 등은 mutation, 그 밖의 이름은 query와 mutation입니다. `artifactsByName`과 `enabledArtifacts`로 직접 지정할 수 있습니다. +- infinite query 옵션은 다음 페이지 규칙을 `withOptions`의 `initialPageParam`·`getNextPageParam`으로 지정해 씁니다. 지정하지 않으면 첫 페이지만 불러옵니다. - 인자가 둘 이상인 함수의 mutation에 변수 객체 타입과 `mapVariablesToArgs`를 생성합니다. - `withOptions`로 `staleTime`, `select` 등을 덮어쓰고, 인자·반환·`select` 결과 타입이 추론됩니다. - `generate --check`: 파일을 쓰지 않고 누락·변경·오래된 결과가 있으면 종료 코드 1을 반환합니다. - `watch`: 소스, 설정 파일, 지정한 로컬 플러그인이 바뀌면 다시 생성합니다. - 생성 파일 첫 줄의 소유 표시로 구분해, 표시 없는 기존 파일은 덮어쓰지 않고 오류로 처리합니다. -- `customAnalyzerPath`, `customTemplatePath`로 분석기와 템플릿을 교체할 수 있습니다. - -## 아직 안 되는 것 / 진행 예정 - -- OpenAPI, Swagger 등 명세 파일 읽기. -- 훅 생성. `useQuery` 등은 TanStack Query의 것을 직접 호출합니다. -- HTTP 클라이언트 생성. 기존 API 함수를 그대로 호출합니다. -- React 외 클라이언트(Vue, Svelte, Solid 등). TanStack Query v5 React만 대상입니다. -- 목(mock) 생성. -- 페이지네이션 커서와 `AbortSignal` 전달의 자동 추측. API마다 직접 설정합니다. -- re-export barrel, 클래스·객체 메서드, 고차 함수가 반환한 함수 분석. -- 모든 generic/overload 관계를 옵션 팩토리 타입으로 보존하는 변환. -- 함수 이름만으로 HTTP 의미를 판단하는 것. 접두사 규칙에 맞지 않으면 `artifactsByName`으로 지정합니다. -- `watch`가 설정이 import하는 외부 파일의 변경을 감지하는 것. 재시작이 필요합니다. -- 디렉토리 전체 단위의 트랜잭션. 파일 단위로만 원자적으로 씁니다. -- 생성 파일을 Node ESM에서 직접 실행하는 것. TS 번들러용입니다. -- 표시 없는 1.x 생성 파일의 자동 이전. 수동으로 옮겨야 합니다. + +## 진행 예정 + +- **JSDoc으로 생성 종류 지정:** `/** @rqh mutation */`처럼 함수 옆에서 query·mutation을 정합니다. 지금은 설정의 `artifactsByName`으로 지정합니다. +- **infinite query 선택 생성:** 페이지네이션을 지원하는 함수에만 만들도록 기본값을 바꿉니다. 지금은 `enabledArtifacts`·`artifactsByName`으로 끕니다. +- **파일 이동에 흔들리지 않는 키:** 파일 위치와 무관한 키 네임스페이스를 지정해, 리팩터링이 캐시 키를 바꾸지 않게 합니다. +- **생성 결과 미리보기:** 데모에서 API 함수를 붙여 넣으면 생성될 키와 옵션을 설치 전에 보여 줍니다. +- **분석 범위 확장:** re-export barrel, 클래스·객체 메서드, 고차 함수가 반환한 함수. +- **React 외 프레임워크:** Vue, Solid, Svelte용 TanStack Query. +- **OpenAPI 선택 연동:** 명세가 있는 신규 API와 기존 함수를 한 설정에서 함께 생성. diff --git a/README_EN.md b/README_EN.md index fc9b47e..89ac3f3 100644 --- a/README_EN.md +++ b/README_EN.md @@ -8,7 +8,7 @@

React Query Helper

- A CLI and runtime helpers that generate query keys and TanStack Query v5 option factories (code that calls queryOptions, infiniteQueryOptions and mutationOptions) from TypeScript API function files. + Keep your existing TypeScript API functions; generate and sync the TanStack Query layer (query keys and options) on top of them

@@ -30,43 +30,56 @@ > **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). -Keep the TypeScript API functions you already have; the CLI generates query keys and TanStack Query v5 option factories (`queryOptions`, `infiniteQueryOptions`, `mutationOptions`) from them. Your API code is not changed, and no OpenAPI spec is needed. +**No OpenAPI required. Keep your existing API. Automate the query layer.** React Query Helper generates query keys and TanStack Query v5 option factories (`queryOptions`, `infiniteQueryOptions`, `mutationOptions`) from the TypeScript API functions you already have, and regenerates them when the API changes. + +## Why React Query Helper? + +TanStack Query already provides `queryOptions` for reusable, type-safe query definitions. But you still connect each API function to those options by hand: + +```ts +export const userOptions = (id: string) => + queryOptions({ queryKey: ["users", id], queryFn: () => getUser(id) }); +``` + +Across hundreds of API functions and several developers, key naming, the link between functions and options, and keeping them in sync when arguments change become repetitive work. React Query Helper fixes that connection in a generator. TanStack Query still handles caching, retries and fetching; this tool generates only the code that connects your API to it. ## How it differs -- **Your API code stays as it is.** Generated files go to `outputDir` and only import your API functions, so existing `fetch` or axios wrappers with auth headers, interceptors and error handling keep working. [Orval](https://orval.dev), [Kubb](https://kubb.dev) and [Hey API](https://heyapi.dev) generate the API functions themselves from an OpenAPI spec, so hand-written API functions are replaced by generated ones (you can plug in your own fetch instance). -- **No spec required.** It works from TypeScript functions alone, including projects whose backend has no OpenAPI spec. -- **No hand-written keys.** [`@lukemorales/query-key-factory`](https://github.com/lukemorales/query-key-factory) needs the key structure written by hand; this tool derives keys from the file path and function name and generates the option factories too. -- **Types come from the original function.** Arguments, return values and `select` results are inferred, not declared again. -- **It follows API changes.** `watch` regenerates, and `generate --check` catches stale generated files in CI. +- **Your API layer stays.** Generated files go to `outputDir` and only import your API functions, so existing `fetch` or axios wrappers with auth headers, interceptors, error handling and response transforms, and your business logic, stay as they are. [Orval](https://orval.dev), [Kubb](https://kubb.dev) and [Hey API](https://heyapi.dev) generate the API functions themselves from an OpenAPI spec, so hand-written API functions are replaced by generated ones (you can plug in your own fetch instance). +- **No spec required.** The input is the TypeScript functions you already have, including projects whose OpenAPI or Swagger spec is missing or out of date. +- **One key convention, automatically.** `getUser` in `libs/users/api.ts` becomes `["users", "api", "getUser"]`, so developers stop inventing their own key names. [`@lukemorales/query-key-factory`](https://github.com/lukemorales/query-key-factory) needs the key structure declared by hand; this tool also generates the option factories. +- **No repeated type declarations.** Arguments, return values and `select` results are inferred from the original function. +- **Generated code is ordinary TypeScript.** Real `.ts` files, not runtime API discovery, so they show up in code review and you can jump to definitions in your IDE. There is no extra cache or query engine. +- **API and query layer stay in sync.** `watch` regenerates on API changes, and `generate --check` catches stale generated files in CI. +- **Adapts to your project.** Choose artifacts per function (`artifactsByName`), or replace the analyzer (`customAnalyzerPath`) and template (`customTemplatePath`). + +## Made for projects like these + +- An existing `fetch` or axios API layer with auth and error handling +- A backend without an OpenAPI spec, or with one that does not match reality +- Many API functions and several developers who need one query key convention +- Teams standardizing TanStack Query usage without rewriting existing code ## What works now -- `generate`: for each top-level function in `sourceDir`, writes a key constant and query, infinite-query and mutation option factories to `*Options.ts`. -- Keys are built from the source path's directories, the file name and the function name. -- The function name prefix (`get`, `list`, `create` and so on) picks what is generated. Configuration can change this. +- `generate`: for each top-level function in `sourceDir`, writes a key constant and query, infinite query and mutation option factories to `*Options.ts`. +- The function-name prefix picks the artifacts: `get`, `list`, `fetch` and similar get query and infinite query; `create`, `update`, `delete` and similar get a mutation; other names get query and mutation. `artifactsByName` and `enabledArtifacts` set them explicitly. +- Infinite query options take their paging rules from `withOptions` (`initialPageParam`, `getNextPageParam`); without them only the first page loads. - Mutations of functions with two or more arguments get a variables object type and `mapVariablesToArgs`. -- `withOptions` overrides `staleTime`, `select` and similar; argument, result and `select` result types are inferred. -- `generate --check`: writes nothing and exits with code 1 when output is missing, changed or stale. -- `watch`: regenerates when the sources, the config file or a listed local plugin changes. -- Generated files carry an ownership header on the first line; an existing file without it is not overwritten and causes an error. -- `customAnalyzerPath` and `customTemplatePath` replace the analyzer and the template. - -## Not done / planned - -- Reading OpenAPI, Swagger or other spec files. -- Hook generation. `useQuery` and the rest are called from TanStack Query directly. -- HTTP client generation. Your existing API functions are called as they are. -- Clients other than React (Vue, Svelte, Solid and so on). Only TanStack Query v5 for React is targeted. -- Mock generation. -- Guessing pagination cursors or forwarding `AbortSignal`. Both are configured per API. -- Analysis of re-export barrels, class or object methods, and functions returned by higher-order functions. -- Preserving every generic/overload relationship in the option factory types. -- Deducing HTTP semantics from function names. When a name does not follow the prefix rules, set it with `artifactsByName`. -- `watch` noticing changes in external files that the config imports. A restart is needed. -- Directory-wide transactions. Writes are atomic per file only. -- Running generated files directly in Node ESM. They target TypeScript bundlers. -- Automatic migration of unmarked 1.x generated files. They must be moved by hand. +- `withOptions` overrides `staleTime`, `select` and others, with argument, return and `select` result types inferred. +- `generate --check` writes nothing and exits with code 1 when output is missing, changed or stale. +- `watch` regenerates when sources, the config or listed local plugins change. +- An ownership header on the first line marks generated files; unmarked existing files are never overwritten. + +## Planned + +- **Choose artifacts with JSDoc,** such as `/** @rqh mutation */` next to the function. Today `artifactsByName` in the config does this. +- **Infinite queries on request,** generated only for functions that paginate. Today `enabledArtifacts` or `artifactsByName` turn them off. +- **Keys that survive file moves,** with a namespace independent of the file path so refactoring does not change cache keys. +- **Preview before installing:** paste API functions into the demo and see the keys and options that would be generated. +- **Wider analysis:** re-export barrels, class and object methods, functions returned by higher-order functions. +- **Beyond React:** TanStack Query for Vue, Solid and Svelte. +- **Optional OpenAPI input,** generating new spec-based APIs and existing functions from one config. diff --git a/docs/REFERENCE.md b/docs/REFERENCE.md index 83f6d7d..0f18f31 100644 --- a/docs/REFERENCE.md +++ b/docs/REFERENCE.md @@ -294,8 +294,8 @@ npm test npm pack ``` -생성된 `uiwwsw-react-query-helper-2.0.3.tgz`의 실제 경로를 사용해 **앱 디렉토리에서** -`npm install /absolute/path/to/uiwwsw-react-query-helper-2.0.3.tgz`를 실행합니다. +생성된 `uiwwsw-react-query-helper-2.0.4.tgz`의 실제 경로를 사용해 **앱 디렉토리에서** +`npm install /absolute/path/to/uiwwsw-react-query-helper-2.0.4.tgz`를 실행합니다. React와 TanStack Query도 앱에 설치한 뒤 README의 `init` 단계부터 진행하세요. `npm pack`은 prepack 빌드를 실행하므로 배포에 필요한 dist도 포함됩니다. diff --git a/docs/REFERENCE_EN.md b/docs/REFERENCE_EN.md index 718f8a8..58c0925 100644 --- a/docs/REFERENCE_EN.md +++ b/docs/REFERENCE_EN.md @@ -296,7 +296,7 @@ npm pack ``` From **your application directory**, install the resulting tarball using its actual path: -`npm install /absolute/path/to/uiwwsw-react-query-helper-2.0.3.tgz`. +`npm install /absolute/path/to/uiwwsw-react-query-helper-2.0.4.tgz`. Also install React and TanStack Query in the application, then continue at the README's `init` step. The prepack build includes the required dist files in the tarball. diff --git a/package-lock.json b/package-lock.json index d3b4952..b0e3326 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,12 +1,12 @@ { "name": "@uiwwsw/react-query-helper", - "version": "2.0.3", + "version": "2.0.4", "lockfileVersion": 3, "requires": true, "packages": { "": { "name": "@uiwwsw/react-query-helper", - "version": "2.0.3", + "version": "2.0.4", "license": "MIT", "dependencies": { "chokidar": "^4.0.3", diff --git a/package.json b/package.json index b9840d2..37dda8d 100644 --- a/package.json +++ b/package.json @@ -1,12 +1,12 @@ { "name": "@uiwwsw/react-query-helper", - "version": "2.0.3", + "version": "2.0.4", "module": "dist/index.js", "type": "module", "types": "dist/index.d.ts", "main": "dist/index.js", "license": "MIT", - "description": "CLI and runtime helpers that generate query keys and TanStack Query v5 option factories (queryOptions, infiniteQueryOptions, mutationOptions) from top-level TypeScript API functions.", + "description": "Generate and sync the TanStack Query v5 layer (query keys, queryOptions, infiniteQueryOptions, mutationOptions) from your existing TypeScript API functions. No OpenAPI spec needed; your API code stays as it is.", "keywords": [ "react-query", "tanstack-query", @@ -19,7 +19,9 @@ "query-key-factory", "query-options", "mutation-options", - "infinite-query" + "infinite-query", + "existing-api", + "no-openapi" ], "homepage": "https://react-query-helper.vercel.app/", "bugs": {