This file provides guidance to Claude Code (claude.ai/code) when working with code in this repository.
OpenAPI React Query Codegen generates React Query (TanStack Query) hooks from OpenAPI specifications. It uses @hey-api/openapi-ts to generate TypeScript clients and then creates additional query/mutation hooks on top.
# Build
npm run build
# Run tests with coverage
npm test
# Run a single test file
npx vitest tests/generate.test.ts
# Update snapshots
npm run snapshot
# Lint
npm run lint
npm run lint:fix
# Preview generated output in example apps
npm run preview:react
npm run preview:nextjs
npm run preview:tanstack-router- CLI Entry (
src/cli.mts): Parses command-line options using Commander - Generate (
src/generate.mts): Orchestrates the generation process:- Calls
@hey-api/openapi-tsto generate base TypeScript client inopenapi/requests/ - Calls
createSource()to generate React Query hooks inopenapi/queries/
- Calls
- Service Parsing (
src/service.mts): Uses ts-morph to parse the generatedservices.gen.tsfile and extract function descriptions (method name, HTTP method, JSDoc, etc.) - Export Creation (
src/createExports.mts): Routes methods to appropriate generators based on HTTP method:- GET methods →
createUseQuery()(queries, suspense queries, infinite queries) - POST/PUT/PATCH/DELETE →
createUseMutation()
- GET methods →
- Hook Generators:
src/createUseQuery.mts: GeneratesuseQuery,useSuspenseQuery, anduseInfiniteQueryhookssrc/createUseMutation.mts: GeneratesuseMutationhookssrc/createPrefetchOrEnsure.mts: GeneratesprefetchQueryandensureQueryDatafunctions
- Print (
src/print.mts): Writes generated TypeScript to files
The tool generates files in openapi/queries/:
common.ts: Shared types, query keys, and key functionsqueries.ts:useQueryanduseMutationhookssuspense.ts:useSuspenseQueryhooksinfiniteQueries.ts:useInfiniteQueryhooksprefetch.ts:prefetchQueryfunctionsensureQueryData.ts:ensureQueryDatafunctionsindex.ts: Re-exports
- ts-morph: AST manipulation for reading the generated service file
- typescript: AST creation for generating new TypeScript code
- @hey-api/openapi-ts: Base OpenAPI to TypeScript client generator
Tests use Vitest with snapshot testing. Test files in tests/ correspond to source modules. The tests/utils.ts file provides a shared project fixture using examples/petstore.yaml.
Coverage thresholds: 95% lines/functions/statements, 90% branches.