@@ -16,10 +16,10 @@ import { type ApiError } from './errors'
1616import {
1717 ensurePrefetched ,
1818 getApiQueryOptions ,
19+ getApiQueryOptionsErrorsAllowed ,
1920 getListQueryOptionsFn ,
2021 getUseApiMutation ,
2122 getUseApiQuery ,
22- getUseApiQueryErrorsAllowed ,
2323 getUsePrefetchedApiQuery ,
2424 wrapQueryClient ,
2525} from './hooks'
@@ -33,6 +33,23 @@ export type ApiMethods = typeof api.methods
3333
3434/** API-specific query options helper. */
3535export const apiq = getApiQueryOptions ( api . methods )
36+ /**
37+ * Variant of `apiq` that allows error responses as a valid result,
38+ * which importantly means they can be cached by RQ. This means we can prefetch
39+ * an endpoint that might error (see `prefetchQueryErrorsAllowed`) and use this
40+ * hook to retrieve the error result.
41+ *
42+ * Concretely, the difference from the usual query function is that we turn all
43+ * errors into successes. Instead of throwing the error, we return it as a valid
44+ * result. This means `data` has a type that includes the possibility of error,
45+ * plus a discriminant to let us handle both sides properly in the calling code.
46+ *
47+ * We also use a special query key to distinguish these from normal API queries.
48+ * If we hit a given endpoint twice on the same page, once the normal way and
49+ * once with errors allowed, the responses have different shapes, so we do not
50+ * want to share the cache and mix them up.
51+ */
52+ export const apiqErrorsAllowed = getApiQueryOptionsErrorsAllowed ( api . methods )
3653/**
3754 * Query options helper that only supports list endpoints. Returns
3855 * a function `(limit, pageToken) => QueryOptions` for use with
@@ -47,7 +64,6 @@ export const useApiQuery = getUseApiQuery(api.methods)
4764 * test loading the page to exercise the invariant in CI.
4865 */
4966export const usePrefetchedApiQuery = getUsePrefetchedApiQuery ( api . methods )
50- export const useApiQueryErrorsAllowed = getUseApiQueryErrorsAllowed ( api . methods )
5167export const useApiMutation = getUseApiMutation ( api . methods )
5268
5369export const usePrefetchedQuery = < TData > ( options : UseQueryOptions < TData , ApiError > ) =>
0 commit comments