Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 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 68 69 70 | 5x 5x 28x 24x 19x 17x 6x 6x 11x 5x 4x 5x 5x 5x 15x 4x | import { MutationCache, QueryCache, QueryClient } from '@tanstack/react-query'
import { ApiError } from './http-client'
/**
* The web shell (app/providers) injects the actual navigation behavior for a
* session-expiry (401) here, so this module stays free of DOM/browser APIs —
* a React Native port would inject its own navigation instead.
*/
let unauthorizedHandler: (() => void) | null = null
let requestErrorHandler: ((error: unknown, operation: 'query' | 'mutation') => void) | null = null
export function setUnauthorizedHandler(handler: () => void): void {
unauthorizedHandler = handler
}
// Reporting is injected alongside navigation to keep this module independent
// of the browser SDK. Never pass query keys or mutation variables to telemetry.
export function setRequestErrorHandler(handler: typeof requestErrorHandler): void {
requestErrorHandler = handler
}
/**
* A query may declare that a 401 is an expected answer rather than a session
* expiry. Without this every 401 is treated as "your session died, go to
* /login" — which is right for the protected shell and wrong for a public
* route that merely *asks* whether anyone is signed in.
*/
export interface QueryMeta {
/** A 401 here means "not signed in", not "session expired". */
readonly expectsAnonymous?: boolean
}
function handleError(error: unknown, operation: 'query' | 'mutation', source?: { meta?: Record<string, unknown> }): void {
if (error instanceof ApiError) {
if (error.status === 401) {
if (!(source?.meta as QueryMeta | undefined)?.expectsAnonymous) unauthorizedHandler?.()
return
}
// Validation, permissions and rate limiting are handled by the UI, not incidents.
if (error.status >= 400 && error.status < 500) return
}
// DOMException may come from another realm and fail an instanceof Error check.
if (typeof error === 'object' && error !== null && 'name' in error && error.name === 'AbortError') return
requestErrorHandler?.(error, operation)
}
export const queryClient = new QueryClient({
defaultOptions: {
queries: {
staleTime: 30_000,
retry: (failureCount, error) => {
Eif (error instanceof ApiError && (error.status === 401 || error.status === 404)) {
return false
}
return failureCount < 2
},
},
mutations: {
retry: false,
},
},
// The second argument is the query (or mutation) that failed — that is where
// `meta.expectsAnonymous` is read from, so the opt-out is per call site
// rather than global. `useCurrentUser` is shared: the protected shell wants
// the redirect, the public invite screen must not have it.
queryCache: new QueryCache({ onError: (error, query) => handleError(error, 'query', query) }),
mutationCache: new MutationCache({ onError: (error, _vars, _ctx, mutation) => handleError(error, 'mutation', mutation) }),
})
|