Skip to content

Commit

Permalink
Avoid rest destructuring React Query result (#1925)
Browse files Browse the repository at this point in the history
use TS hack to avoid bad ...rest destructure of RQ result
  • Loading branch information
david-crespo authored Jan 31, 2024
1 parent 3007029 commit 695d367
Showing 1 changed file with 9 additions and 3 deletions.
12 changes: 9 additions & 3 deletions libs/api/hooks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import {
type UseMutationOptions,
type UseQueryOptions,
} from '@tanstack/react-query'
import { type SetNonNullable } from 'type-fest'

import { invariant } from '@oxide/util'

Expand Down Expand Up @@ -138,7 +139,7 @@ export const getUsePrefetchedApiQuery =
options: UseQueryOtherOptions<Result<A[M]>, ApiError> = {}
) => {
const queryKey = [method, params]
const { data, ...rest } = useQuery({
const result = useQuery({
queryKey,
// no catch, let unexpected errors bubble up
queryFn: ({ signal }) => api[method](params, { signal }).then(handleResult(method)),
Expand All @@ -149,8 +150,13 @@ export const getUsePrefetchedApiQuery =
throwOnError: (err) => err.statusCode === 404,
...options,
})
invariant(data, `Expected query to be prefetched. Key: ${JSON.stringify(queryKey)}`)
return { data, ...rest }
invariant(
result.data,
`Expected query to be prefetched. Key: ${JSON.stringify(queryKey)}`
)
// TS infers non-nullable on a freestanding variable, but doesn't like to do
// it on a property. So we give it a hint
return result as SetNonNullable<typeof result, 'data'>
}

const ERRORS_ALLOWED = 'errors-allowed'
Expand Down

0 comments on commit 695d367

Please sign in to comment.