@@ -22,15 +22,15 @@ Register the command to the `scripts` property in your package.json file.
22
22
``` json
23
23
{
24
24
"scripts" : {
25
- "codegen" : " openapi-rq -i ./petstore.yaml -c axios "
25
+ "codegen" : " openapi-rq -i ./petstore.yaml -c @hey-api/client-fetch "
26
26
}
27
27
}
28
28
```
29
29
30
30
You can also run the command without installing it in your project using the npx command.
31
31
32
32
``` 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
34
34
```
35
35
36
36
## Usage
@@ -46,23 +46,20 @@ Options:
46
46
-V, --version output the version number
47
47
-i, --input <value> OpenAPI specification, can be a path, url or string content (required)
48
48
-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")
51
50
--format <value> Process output folder with formatter? (choices: "biome", "prettier")
52
51
--lint <value> Process output folder with linter? (choices: "biome", "eslint")
53
52
--operationId Use operation ID to generate operation names?
54
53
--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']
57
54
--enums <value> Generate JavaScript objects from enum definitions? (choices: "javascript", "typescript")
58
55
--useDateType Use Date type instead of string for date types for models, this will not convert the data to a Date object
59
56
--debug Run in debug mode?
60
57
--noSchemas Disable generating JSON schemas
61
58
--schemaType <value> Type of JSON schema [Default: 'json'] (choices: "form", "json")
62
59
--pageParam <value> Name of the query parameter used for pagination (default: "page")
63
60
--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
66
63
```
67
64
68
65
### Example Usage
@@ -95,9 +92,9 @@ $ openapi-rq -i ./petstore.yaml
95
92
96
93
``` tsx
97
94
// App.tsx
98
- import { usePetServiceFindPetsByStatus } from " ../openapi/queries" ;
95
+ import { useFindPets } from " ../openapi/queries" ;
99
96
function App() {
100
- const { data } = usePetServiceFindPetsByStatus ({ status: [ " available " ] } );
97
+ const { data } = useFindPets ( );
101
98
102
99
return (
103
100
<div className = " App" >
@@ -114,16 +111,16 @@ export default App;
114
111
115
112
``` tsx
116
113
import { 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" ;
119
116
120
117
function App() {
121
118
// You can still use the auto-generated query key
122
119
const { data } = useQuery ({
123
- queryKey: [usePetServiceFindPetsByStatusKey ],
120
+ queryKey: [useFindPetsKey ],
124
121
queryFn : () => {
125
122
// Do something here
126
- return PetService . findPetsByStatus ([ " available " ] );
123
+ return findPets ( );
127
124
},
128
125
});
129
126
@@ -137,9 +134,11 @@ export default App;
137
134
138
135
``` tsx
139
136
// App.tsx
140
- import { useDefaultClientFindPetsSuspense } from " ../openapi/queries/suspense" ;
137
+ import { useFindPetsSuspense } from " ../openapi/queries/suspense" ;
141
138
function ChildComponent() {
142
- const { data } = useDefaultClientFindPetsSuspense ({ tags: [], limit: 10 });
139
+ const { data } = useFindPetsSuspense ({
140
+ query: { tags: [], limit: 10 },
141
+ });
143
142
144
143
return <ul >{ data ?.map ((pet , index ) => <li key = { pet .id } >{ pet .name } </li >)} </ul >;
145
144
}
@@ -170,13 +169,13 @@ export default App;
170
169
171
170
``` tsx
172
171
// App.tsx
173
- import { usePetServiceAddPet } from " ../openapi/queries" ;
172
+ import { useAddPet } from " ../openapi/queries" ;
174
173
175
174
function App() {
176
- const { mutate } = usePetServiceAddPet ();
175
+ const { mutate } = useAddPet ();
177
176
178
177
const handleAddPet = () => {
179
- mutate ({ name: " Fluffy " , status : " available " });
178
+ mutate ({ body: { name : " Fluffy " } });
180
179
};
181
180
182
181
return (
@@ -200,22 +199,22 @@ To ensure the query key is created the same way as the query hook, you can use t
200
199
201
200
``` tsx
202
201
import {
203
- usePetServiceFindPetsByStatus ,
204
- usePetServiceAddPet ,
205
- UsePetServiceFindPetsByStatusKeyFn ,
202
+ useFindPetsByStatus ,
203
+ useAddPet ,
204
+ UseFindPetsByStatusKeyFn ,
206
205
} from " ../openapi/queries" ;
207
206
208
207
// App.tsx
209
208
function App() {
210
209
const [status, setStatus] = React .useState ([" available" ]);
211
- const { data } = usePetServiceFindPetsByStatus ({ status });
212
- const { mutate } = usePetServiceAddPet ({
210
+ const { data } = useFindPetsByStatus ({ status });
211
+ const { mutate } = useAddPet ({
213
212
onSuccess : () => {
214
213
queryClient .invalidateQueries ({
215
214
// Call the query key function to get the query key
216
215
// This is important to ensure the query key is created the same way as the query hook
217
216
// This insures the cache is invalidated correctly and is typed correctly
218
- queryKey: [UsePetServiceFindPetsByStatusKeyFn ({
217
+ queryKey: [UseFindPetsByStatusKeyFn ({
219
218
status
220
219
})],
221
220
});
@@ -300,42 +299,13 @@ paths:
300
299
Usage of Generated Hooks:
301
300
302
301
` ` ` ts
303
- import { useDefaultServiceFindPaginatedPetsInfinite } from "@/openapi/queries/infiniteQueries";
302
+ import { useFindPaginatedPetsInfinite } from "@/openapi/queries/infiniteQueries";
304
303
305
- const { data, fetchNextPage } = useDefaultServiceFindPaginatedPetsInfinite({
306
- limit : 10,
307
- tags : [],
304
+ const { data, fetchNextPage } = useFindPaginatedPetsInfinite({
305
+ query : { tags: [], limit: 10 }
308
306
});
309
307
```
310
308
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
-
339
309
## Development
340
310
341
311
### Install dependencies
@@ -365,6 +335,7 @@ pnpm snapshot
365
335
```
366
336
367
337
### Build example and validate generated code
338
+
368
339
``` bash
369
340
npm run build && pnpm --filter @7nohe/react-app generate:api && pnpm --filter @7nohe/react-app test:generated
370
341
```
0 commit comments