Skip to content

Commit 1d8055e

Browse files
authored
doc: Add usage example for react-query (#41)
* Add TODO in the generated fetcher * Add usage example
1 parent 404b7f1 commit 1d8055e

File tree

2 files changed

+50
-9
lines changed

2 files changed

+50
-9
lines changed

plugins/typescript/README.md

Lines changed: 45 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -26,13 +26,13 @@ import {
2626
} from "@openapi-codegen/typescript";
2727

2828
export default defineConfig({
29-
myAPI: {
29+
petstore: {
3030
from: {
3131
/* file, url or github */
3232
},
33-
outputDir: "./myAPI",
33+
outputDir: "./petStore",
3434
to: async (context) => {
35-
const filenamePrefix = "myAPI";
35+
const filenamePrefix = "petStore";
3636
const { schemasFiles } = await generateSchemaTypes(context, {
3737
filenamePrefix,
3838
});
@@ -56,10 +56,10 @@ Only `{filenamePrefix}Components.ts` can’t be manually touch and will be regen
5656
The `{filenamePrefix}Context.ts` can be tweak to inject any props in the generated hooks, this is an example with some auth flow.
5757

5858
```ts
59-
// `BadassContext.ts`
59+
// `PetStoreContext.ts`
6060
import type { QueryKey, UseQueryOptions } from "react-query";
6161

62-
export type BadassContext = {
62+
export type PetStoreContext = {
6363
fetcherOptions: {
6464
/**
6565
* Headers to inject in the fetcher
@@ -88,7 +88,7 @@ export type BadassContext = {
8888
*
8989
* @param queryOptions options from the useQuery wrapper
9090
*/
91-
export function useBadassContext<
91+
export function usePetStoreContext<
9292
TQueryFnData = unknown,
9393
TError = unknown,
9494
TData = TQueryFnData,
@@ -115,7 +115,45 @@ export function useBadassContext<
115115
}
116116
```
117117

118-
You can also tweak `{filenamePrefix}Fetcher.ts`, especially the error management part, so everything fit the expected `ErrorType`.
118+
You also need to tweak `{filenamePrefix}Fetcher.ts`, to inject your `baseUrl` and adjust the error management part to fullfil the `ErrorType` (you can search for the `TODO` keyword).
119+
120+
#### Usage
121+
122+
First of all, we need to have a working react-query context (more information [here](https://react-query.tanstack.com/quick-start)).
123+
124+
Now that we have all this generated code and properly configured `{filenamePrefix}Fetcher.ts` to talk to the correct server. This is time to try!
125+
126+
Assuming that you have a route with the verb `GET` and the `operationId` as `listPets`. You can simply use `useListPets` in a react component.
127+
128+
```tsx
129+
import { useListPets } from "./petstoreComponents";
130+
131+
export const MyPage = () => {
132+
const { data, isLoading, error } = useListPets(["listPets"]); // <- You need to add the react-query cache key
133+
134+
return <div>{JSON.stringify({ data, isLoading, error })}</div>;
135+
};
136+
```
137+
138+
And for any mutation.
139+
140+
```tsx
141+
import { useUpdatePet } from "./pestoreComponents";
142+
143+
export const MyPage = () => {
144+
const { mutate: updatePet } = useUpdatePet();
145+
146+
return (
147+
<button
148+
onClick={() =>
149+
updatePet({ pathParams: { id: "2" }, body: { name: "Biscuit" } })
150+
}
151+
>
152+
Give a cute name
153+
</button>
154+
);
155+
};
156+
```
119157

120158
### generateFetchers
121159

plugins/typescript/src/templates/fetcher.ts

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@ import { camel, pascal } from "case";
77
*/
88
export const getFetcher = (prefix: string, contextPath?: string) =>
99
`import qs from "qs";
10+
11+
const baseUrl = ""; // TODO add your baseUrl
1012
${
1113
contextPath
1214
? `import { ${pascal(prefix)}Context } from "./${contextPath}";`
@@ -58,8 +60,8 @@ export async function ${camel(prefix)}Fetch<
5860
TQueryParams,
5961
TPathParams
6062
>): Promise<TData> {
61-
const response = await window.fetch(
62-
resolveUrl(url, queryParams, pathParams),
63+
const response = await window.fetch(\`\${baseUrl}
64+
\${resolveUrl(url, queryParams, pathParams)}\`,
6365
{
6466
method: method.toUpperCase(),
6567
body: body ? JSON.stringify(body) : undefined,
@@ -70,6 +72,7 @@ export async function ${camel(prefix)}Fetch<
7072
}
7173
);
7274
if (!response.ok) {
75+
// TODO validate & parse the error to fit the generated error types
7376
throw new Error("Network response was not ok");
7477
}
7578
return await response.json();

0 commit comments

Comments
 (0)