add axios

This commit is contained in:
2025-02-22 11:57:18 -03:00
parent bc77e892a5
commit 772cc74550
3 changed files with 49 additions and 8 deletions

View File

@@ -1,5 +1,25 @@
import axios from 'axios' import axios from 'axios'
export default axios.create({ const instance = axios.create({
baseURL: import.meta.env.VITE_API_URL, baseURL: import.meta.env.VITE_API_URL,
}) })
instance.interceptors.response.use(
// Any status code that lie within the range of 2xx cause this function to trigger
function (response) {
return response
},
// Any status codes that falls outside the range of 2xx cause this function to trigger
function ({ response }) {
const error = new Error(response.data?.message)
error.code = response.data?.__type
error.name = response.data?.__type
error.statusCode = response.status
return Promise.reject(error)
},
)
export default instance

View File

@@ -15,7 +15,6 @@ import amplifyconfig from './amplifyconfiguration.json'
import stylesheet from './app.css?url' import stylesheet from './app.css?url'
Amplify.configure(amplifyconfig) Amplify.configure(amplifyconfig)
const queryClient = new QueryClient()
export const links = () => [ export const links = () => [
{ rel: 'preconnect', href: 'https://fonts.googleapis.com' }, { rel: 'preconnect', href: 'https://fonts.googleapis.com' },
@@ -51,6 +50,8 @@ export function Layout({ children }) {
} }
export default function App() { export default function App() {
const queryClient = new QueryClient()
return ( return (
<QueryClientProvider client={queryClient}> <QueryClientProvider client={queryClient}>
<AuthProvider> <AuthProvider>
@@ -86,6 +87,7 @@ export function ErrorBoundary({ error }) {
<main className="pt-16 p-4 container mx-auto"> <main className="pt-16 p-4 container mx-auto">
<h1>{message}</h1> <h1>{message}</h1>
<p>{details}</p> <p>{details}</p>
{stack && ( {stack && (
<pre className="w-full p-4 overflow-x-auto"> <pre className="w-full p-4 overflow-x-auto">
<code>{stack}</code> <code>{stack}</code>

View File

@@ -52,17 +52,36 @@ function SignIn() {
const { data } = await axios.get(`/search/lookup/${username}`) const { data } = await axios.get(`/search/lookup/${username}`)
return data return data
}, },
onSuccess: (data) => {
setSearchParams((searchParams) => {
searchParams.set('state', encode(data))
return searchParams
})
},
}) })
const onSubmit = async ({ username }) => { const formError = (message) => {
const data = await mutateAsync({ username }) setError('username', {
type: 'manual',
setSearchParams((searchParams) => { message,
searchParams.set('state', encode(data))
return searchParams
}) })
} }
const onSubmit = async ({ username }) => {
try {
await mutateAsync({ username })
} catch ({ message }) {
switch (message) {
case 'User not found.':
return formError(
'Não encontramos sua conta. Por favor, verifique se seu Email ou CPF estão corretos.',
)
default:
return formError(message)
}
}
}
return ( return (
<Card> <Card>
<Heading>Digite o seu email ou cpf para continuar</Heading> <Heading>Digite o seu email ou cpf para continuar</Heading>