import './App.css'; import * as Form from '@radix-ui/react-form'; import { DownloadIcon, PlusIcon, ReloadIcon } from '@radix-ui/react-icons'; import { Avatar, Box, Button, Card, Container, Flex, Heading, Section, Text, TextField, } from '@radix-ui/themes'; import { useMutation, useQuery } from '@tanstack/react-query'; import { useEffect, useState } from 'react'; import { addPetMutation, getPetByIdOptions, updatePetMutation, } from './client/@tanstack/react-query.gen'; import { createClient } from './client/client'; import { PetSchema } from './client/schemas.gen'; import type { Pet } from './client/types.gen'; const localClient = createClient({ // set default base url for requests made by this client baseUrl: 'https://petstore3.swagger.io/api/v3', /** * Set default headers only for requests made by this client. This is to * demonstrate local clients and their configuration taking precedence over * internal service client. */ headers: { Authorization: 'Bearer ', }, }); localClient.interceptors.request.use((request, options) => { // Middleware is great for adding authorization tokens to requests made to // protected paths. Headers are set randomly here to allow surfacing the // default headers, too. if ( options.url === '/pet/{petId}' && options.method === 'GET' && Math.random() < 0.5 ) { request.headers.set('Authorization', 'Bearer '); } return request; }); function App() { const [pet, setPet] = useState(); const [petId, setPetId] = useState(); const [isRequiredNameError, setIsRequiredNameError] = useState(false); const addPet = useMutation({ ...addPetMutation(), onError: (error) => { console.log(error); setIsRequiredNameError(false); }, onSuccess: (data) => { setPet(data); setIsRequiredNameError(false); }, }); const updatePet = useMutation({ ...updatePetMutation(), onError: (error) => { console.log(error); }, onSuccess: (data) => { setPet(data); }, }); const { data, error } = useQuery({ ...getPetByIdOptions({ client: localClient, path: { petId: petId!, }, }), enabled: Boolean(petId), }); const onAddPet = async (formData: FormData) => { // simple form field validation to demonstrate using schemas if (PetSchema.required.includes('name') && !formData.get('name')) { setIsRequiredNameError(true); return; } addPet.mutate({ body: { category: { id: 0, name: formData.get('category') as string, }, id: 0, name: formData.get('name') as string, photoUrls: ['string'], status: 'available', tags: [ { id: 0, name: 'string', }, ], }, }); }; const onGetPetById = async () => { // random id 1-10 setPetId(Math.floor(Math.random() * (10 - 1 + 1) + 1)); }; const onUpdatePet = async () => { updatePet.mutate({ body: { category: { id: 0, name: 'Cats', }, id: 2, name: 'Updated Kitty', photoUrls: ['string'], status: 'available', tags: [ { id: 0, name: 'string', }, ], }, // setting headers per request headers: { Authorization: 'Bearer ', }, }); }; useEffect(() => { if (error) { console.log(error); return; } setPet(data!); }, [data, error]); return (
Hey API logo @hey-api/openapi-ts 🤝 TanStack React Query
Name: {pet?.name ?? 'N/A'} Category: {pet?.category?.name ?? 'N/A'}
{ event.preventDefault(); onAddPet(new FormData(event.currentTarget)); }} >
Name {isRequiredNameError && ( Please enter a name )}
Category Please enter a category
); } export default App;