How to extract value from API response inside base query in a type safe way? #4616
Unanswered
robert-westenberger
asked this question in
Q&A
Replies: 1 comment 6 replies
-
I would assume that either your |
Beta Was this translation helpful? Give feedback.
6 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
Hi, all of the APIs I interact with use the
APIResponse
interface, found below.I'd like to transform the API responses to just extract the
result
value on success. So if my api slice is like thisThen for example the
data
key on the object returned byuseGetPostQuery(someId)
should be aPost
interface on a successful request, not{ success: true, result: POST }
.Doing it in transformResponse is straightforward enough
But I would like to do it in the baseQuery, since I don't want to have to write a transformResponse for literally every endpoint definition.
Is it possible to return
result.data.result
inbaseQueryWithGlobalTransform
in the snippet above, in a type safe way? By type safe i mean I want typescript to know theresult
value has already been extracted from the response object whenever i am using a generated hook.Here is a little dummy codebase I made for this question: https://stackblitz.com/edit/vitejs-vite-muwfxe?file=src%2FbaseQuery.ts
Beta Was this translation helpful? Give feedback.
All reactions