1414$ npm install -D @7nohe/openapi-react-query-codegen
1515```
1616
17- Register the command to the ` scripts ` property in your package.json file.
17+ Register the command to the ` scripts ` property in your package.json file.
1818
1919``` json
2020{
@@ -30,7 +30,6 @@ You can also run the command without installing it in your project using the npx
3030$ npx --package @7nohe/openapi-react-query-codegen openapi-rq -i ./petstore.yaml -c axios
3131```
3232
33-
3433## Usage
3534
3635```
@@ -67,17 +66,18 @@ $ openapi-rq -i ./petstore.yaml
6766```
6867- openapi
6968 - queries
70- - index.ts <- custom react hooks
69+ - index.ts <- main file that exports common types, variables, and hooks
70+ - common.ts <- common types
71+ - queries.ts <- generated query hooks
72+ - suspenses.ts <- generated suspense hooks
7173 - requests <- output code generated by OpenAPI Typescript Codegen
7274```
7375
7476### In your app
7577
7678``` tsx
7779// App.tsx
78- import {
79- usePetServiceFindPetsByStatus ,
80- } from " ../openapi/queries" ;
80+ import { usePetServiceFindPetsByStatus } from " ../openapi/queries" ;
8181function App() {
8282 const { data } = usePetServiceFindPetsByStatus ({ status: [" available" ] });
8383
@@ -100,30 +100,55 @@ You can also use pure TS clients.
100100
101101``` tsx
102102import { useQuery } from " @tanstack/react-query" ;
103- import { PetService } from ' ../openapi/requests/services/PetService' ;
104- import {
105- usePetServiceFindPetsByStatusKey ,
106- } from " ../openapi/queries" ;
103+ import { PetService } from " ../openapi/requests/services/PetService" ;
104+ import { usePetServiceFindPetsByStatusKey } from " ../openapi/queries" ;
107105
108106function App() {
109107 // You can still use the auto-generated query key
110108 const { data } = useQuery ({
111109 queryKey: [usePetServiceFindPetsByStatusKey ],
112110 queryFn : () => {
113111 // Do something here
114- return PetService .findPetsByStatus ([' available' ]);
115- }
116- });
112+ return PetService .findPetsByStatus ([" available" ]);
113+ },
114+ });
115+
116+ return <div className = " App" >{ /* .... */ } </div >;
117+ }
118+
119+ export default App ;
120+ ```
121+
122+ You can also use suspense hooks.
123+
124+ ``` tsx
125+ // App.tsx
126+ import { useDefaultClientFindPetsSuspense } from " ../openapi/queries/suspense" ;
127+ function ChildComponent() {
128+ const { data } = useDefaultClientFindPetsSuspense ({ tags: [], limit: 10 });
117129
118130 return (
119- <div className = " App" >
120- { /* .... */ }
121- </div >
131+ <ul >
132+ { data ?.map ((pet , index ) => (
133+ <li key = { pet .id } >{ pet .name } </li >
134+ ))}
135+ </ul >
136+ );
137+ }
138+
139+ function ParentComponent() {
140+ return (
141+ <>
142+ <Suspense fallback = { <>loading...</>} >
143+ <ChildComponent />
144+ </Suspense >
145+ </>
122146 );
123147}
124148
125149export default App ;
126150```
127151
128152## License
153+
129154MIT
0 commit comments