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
Uh oh!
There was an error while loading. Please reload this page.
-
Hi, all of the APIs I interact with use the
APIResponseinterface, found below.I'd like to transform the API responses to just extract the
resultvalue on success. So if my api slice is like thisThen for example the
datakey on the object returned byuseGetPostQuery(someId)should be aPostinterface 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.resultinbaseQueryWithGlobalTransformin the snippet above, in a type safe way? By type safe i mean I want typescript to know theresultvalue 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