@@ -22,15 +22,15 @@ Register the command to the `scripts` property in your package.json file.
2222``` json
2323{
2424 "scripts" : {
25- "codegen" : " openapi-rq -i ./petstore.yaml -c axios "
25+ "codegen" : " openapi-rq -i ./petstore.yaml -c @hey-api/client-fetch "
2626 }
2727}
2828```
2929
3030You can also run the command without installing it in your project using the npx command.
3131
3232``` bash
33- $ npx --package @7nohe/openapi-react-query-codegen openapi-rq -i ./petstore.yaml -c axios
33+ $ npx --package @7nohe/openapi-react-query-codegen openapi-rq -i ./petstore.yaml -c @hey-api/client-fetch
3434```
3535
3636## Usage
@@ -46,23 +46,20 @@ Options:
4646 -V, --version output the version number
4747 -i, --input <value> OpenAPI specification, can be a path, url or string content (required)
4848 -o, --output <value> Output directory (default: "openapi")
49- -c, --client <value> HTTP client to generate (choices: "angular", "axios", "fetch", "node", "xhr", default: "fetch")
50- --request <value> Path to custom request file
49+ -c, --client <value> HTTP client to generate (choices: "@hey-api/client-fetch", "@hey-api/client-axios", default: "@hey-api/client-fetch")
5150 --format <value> Process output folder with formatter? (choices: "biome", "prettier")
5251 --lint <value> Process output folder with linter? (choices: "biome", "eslint")
5352 --operationId Use operation ID to generate operation names?
5453 --serviceResponse <value> Define shape of returned value from service calls (choices: "body", "response", default: "body")
55- --base <value> Manually set base in OpenAPI config instead of inferring from server value
56- --enums <value> Generate JavaScript objects from enum definitions? ['javascript', 'typescript', 'typescript+namespace']
5754 --enums <value> Generate JavaScript objects from enum definitions? (choices: "javascript", "typescript")
5855 --useDateType Use Date type instead of string for date types for models, this will not convert the data to a Date object
5956 --debug Run in debug mode?
6057 --noSchemas Disable generating JSON schemas
6158 --schemaType <value> Type of JSON schema [Default: 'json'] (choices: "form", "json")
6259 --pageParam <value> Name of the query parameter used for pagination (default: "page")
6360 --nextPageParam <value> Name of the response parameter used for next page (default: "nextPage")
64- --initialPageParam <value> Initial value for the pagination parameter (default: "1 ")
65- -h, --help display help for command
61+ --initialPageParam <value> Initial page value to query (default: "initialPageParam ")
62+ -h, --help display help for command
6663```
6764
6865### Example Usage
@@ -95,9 +92,9 @@ $ openapi-rq -i ./petstore.yaml
9592
9693``` tsx
9794// App.tsx
98- import { usePetServiceFindPetsByStatus } from " ../openapi/queries" ;
95+ import { useFindPets } from " ../openapi/queries" ;
9996function App() {
100- const { data } = usePetServiceFindPetsByStatus ({ status: [ " available " ] } );
97+ const { data } = useFindPets ( );
10198
10299 return (
103100 <div className = " App" >
@@ -114,16 +111,16 @@ export default App;
114111
115112``` tsx
116113import { useQuery } from " @tanstack/react-query" ;
117- import { PetService } from " ../openapi/requests/services" ;
118- import { usePetServiceFindPetsByStatusKey } from " ../openapi/queries" ;
114+ import { findPets } from " ../openapi/requests/services.gen " ;
115+ import { useFindPetsKey } from " ../openapi/queries" ;
119116
120117function App() {
121118 // You can still use the auto-generated query key
122119 const { data } = useQuery ({
123- queryKey: [usePetServiceFindPetsByStatusKey ],
120+ queryKey: [useFindPetsKey ],
124121 queryFn : () => {
125122 // Do something here
126- return PetService . findPetsByStatus ([ " available " ] );
123+ return findPets ( );
127124 },
128125 });
129126
@@ -137,9 +134,11 @@ export default App;
137134
138135``` tsx
139136// App.tsx
140- import { useDefaultClientFindPetsSuspense } from " ../openapi/queries/suspense" ;
137+ import { useFindPetsSuspense } from " ../openapi/queries/suspense" ;
141138function ChildComponent() {
142- const { data } = useDefaultClientFindPetsSuspense ({ tags: [], limit: 10 });
139+ const { data } = useFindPetsSuspense ({
140+ query: { tags: [], limit: 10 },
141+ });
143142
144143 return <ul >{ data ?.map ((pet , index ) => <li key = { pet .id } >{ pet .name } </li >)} </ul >;
145144}
@@ -170,13 +169,13 @@ export default App;
170169
171170``` tsx
172171// App.tsx
173- import { usePetServiceAddPet } from " ../openapi/queries" ;
172+ import { useAddPet } from " ../openapi/queries" ;
174173
175174function App() {
176- const { mutate } = usePetServiceAddPet ();
175+ const { mutate } = useAddPet ();
177176
178177 const handleAddPet = () => {
179- mutate ({ name: " Fluffy " , status : " available " });
178+ mutate ({ body: { name : " Fluffy " } });
180179 };
181180
182181 return (
@@ -200,22 +199,22 @@ To ensure the query key is created the same way as the query hook, you can use t
200199
201200``` tsx
202201import {
203- usePetServiceFindPetsByStatus ,
204- usePetServiceAddPet ,
205- UsePetServiceFindPetsByStatusKeyFn ,
202+ useFindPetsByStatus ,
203+ useAddPet ,
204+ UseFindPetsByStatusKeyFn ,
206205} from " ../openapi/queries" ;
207206
208207// App.tsx
209208function App() {
210209 const [status, setStatus] = React .useState ([" available" ]);
211- const { data } = usePetServiceFindPetsByStatus ({ status });
212- const { mutate } = usePetServiceAddPet ({
210+ const { data } = useFindPetsByStatus ({ status });
211+ const { mutate } = useAddPet ({
213212 onSuccess : () => {
214213 queryClient .invalidateQueries ({
215214 // Call the query key function to get the query key
216215 // This is important to ensure the query key is created the same way as the query hook
217216 // This insures the cache is invalidated correctly and is typed correctly
218- queryKey: [UsePetServiceFindPetsByStatusKeyFn ({
217+ queryKey: [UseFindPetsByStatusKeyFn ({
219218 status
220219 })],
221220 });
@@ -300,42 +299,13 @@ paths:
300299Usage of Generated Hooks:
301300
302301` ` ` ts
303- import { useDefaultServiceFindPaginatedPetsInfinite } from "@/openapi/queries/infiniteQueries";
302+ import { useFindPaginatedPetsInfinite } from "@/openapi/queries/infiniteQueries";
304303
305- const { data, fetchNextPage } = useDefaultServiceFindPaginatedPetsInfinite({
306- limit : 10,
307- tags : [],
304+ const { data, fetchNextPage } = useFindPaginatedPetsInfinite({
305+ query : { tags: [], limit: 10 }
308306});
309307```
310308
311- ##### Runtime Configuration
312-
313- You can modify the default values used by the generated service calls by modifying the OpenAPI configuration singleton object.
314-
315- It's default location is ` openapi/requests/core/OpenAPI.ts ` and it is also exported from ` openapi/index.ts `
316-
317- Import the constant into your runtime and modify it before setting up the react app.
318-
319- ``` typescript
320- /** main.tsx */
321- import { OpenAPI as OpenAPIConfig } from ' ./openapi/requests/core/OpenAPI' ;
322- ...
323- OpenAPIConfig .BASE = ' www.domain.com/api' ;
324- OpenAPIConfig .HEADERS = {
325- ' x-header-1' : ' value-1' ,
326- ' x-header-2' : ' value-2' ,
327- };
328- ...
329- ReactDOM .createRoot (document .getElementById (" root" ) as HTMLElement ).render (
330- < React .StrictMode >
331- < QueryClientProvider client = {queryClient }>
332- < App / >
333- < / QueryClientProvider >
334- < / React .StrictMode >
335- );
336-
337- ```
338-
339309## Development
340310
341311### Install dependencies
@@ -365,6 +335,7 @@ pnpm snapshot
365335```
366336
367337### Build example and validate generated code
338+
368339``` bash
369340npm run build && pnpm --filter @7nohe/react-app generate:api && pnpm --filter @7nohe/react-app test:generated
370341```
0 commit comments