-
Notifications
You must be signed in to change notification settings - Fork 106
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(namadillo): avoid retrying for 4xx errors (#993)
- Loading branch information
1 parent
73cd206
commit f67437c
Showing
2 changed files
with
53 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
import { QueryClient, QueryClientProvider } from "@tanstack/react-query"; | ||
import { queryClientAtom } from "jotai-tanstack-query"; | ||
import { Provider as JotaiProvider } from "jotai/react"; | ||
import { useHydrateAtoms } from "jotai/utils"; | ||
|
||
type ErrorWithResponse = Error & { | ||
response?: { | ||
status?: number; | ||
}; | ||
}; | ||
|
||
const MAX_RETRIES = 3; | ||
|
||
export const queryClient = new QueryClient({ | ||
defaultOptions: { | ||
queries: { | ||
retry: (failureCount, error: ErrorWithResponse) => { | ||
if (failureCount > MAX_RETRIES) { | ||
return false; | ||
} | ||
|
||
// Handles all 4xx errors | ||
if (String(error?.response?.status).startsWith("4")) { | ||
return false; | ||
} | ||
|
||
return true; | ||
}, | ||
}, | ||
}, | ||
}); | ||
|
||
const HydrateAtoms = (props: { children: JSX.Element }): JSX.Element => { | ||
useHydrateAtoms([[queryClientAtom, queryClient]]); | ||
return props.children; | ||
}; | ||
|
||
export const QueryProvider = ({ | ||
children, | ||
}: { | ||
children: JSX.Element; | ||
}): JSX.Element => { | ||
return ( | ||
<QueryClientProvider client={queryClient}> | ||
<JotaiProvider> | ||
<HydrateAtoms>{children}</HydrateAtoms> | ||
</JotaiProvider> | ||
</QueryClientProvider> | ||
); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters