forked from 7nohe/openapi-react-query-codegen
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathApp.tsx
More file actions
67 lines (60 loc) · 1.82 KB
/
App.tsx
File metadata and controls
67 lines (60 loc) · 1.82 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
import "./App.css";
import {
useDefaultClientAddPet,
useDefaultClientFindPets,
useDefaultClientFindPetsKey,
useDefaultClientGetNotDefined,
useDefaultClientPostNotDefined,
} from "../openapi/queries";
import { useState } from "react";
import { queryClient } from "./queryClient";
function App() {
const [tags, _setTags] = useState<string[]>([]);
const [limit, _setLimit] = useState<number>(10);
const { data, error, refetch } = useDefaultClientFindPets({ tags, limit });
// This is an example of a query that is not defined in the OpenAPI spec
// this defaults to any - here we are showing how to override the type
// Note - this is marked as deprecated in the OpenAPI spec and being passed to the client
const { data: notDefined } = useDefaultClientGetNotDefined<undefined>();
const { mutate: mutateNotDefined } = useDefaultClientPostNotDefined<undefined>();
const { mutate: addPet } = useDefaultClientAddPet();
if (error)
return (
<div>
<p>Failed to fetch pets</p>
<button onClick={() => refetch()}>Retry</button>
</div>
);
return (
<div className="App">
<h1>Pet List</h1>
<ul>
{data instanceof Array &&
data?.map((pet, index) => (
<li key={pet.id + "-" + index}>{pet.name}</li>
))}
</ul>
<button
onClick={() => {
addPet(
{
requestBody: { name: "Duggy" },
},
{
onSuccess: () => {
queryClient.invalidateQueries({
queryKey: [useDefaultClientFindPetsKey],
});
console.log("success");
},
onError: (error) => console.error(error),
}
);
}}
>
Create a pet
</button>
</div>
);
}
export default App;