Menu

Stop adapting axios to TanStack Query: a fetch client built for queryFn
📰

Stop adapting axios to TanStack Query: a fetch client built for queryFn

DEV Community: react·Mohammad Garmabi·13 days ago
#lEhxqsnM
#devtoreact#devto#dev#tanstack#fetch
Reading 0:00
15s threshold

TanStack Query doesn't care how you fetch. It only asks one thing of your queryFn : return data, throw on failure, and respect the AbortSignal.

Most HTTP clients don't do that out of the box, so every project grows the same glue.

The usual glue

queryFn : async ({ signal }) => { try { const res = await axios . get ( ' /users ' , { signal }) return res . data } catch ( e ) { if ( axios . isAxiosError ( e ) && e . response ?. status === 401 ) { await refreshToken () // what if 5 queries hit 401 at once? // retry...? } throw normalizeError ( e ) } }

Enter fullscreen mode

Exit fullscreen mode

Multiply that by every endpoint, add SSR cookie forwarding and an SSE stream that needs an Authorization header, and you have a small framework nobody meant to write.

The same thing with tanstack-fetch

import { createFetch } from ' tanstack-fetch ' import { createAuthInterceptor , createStatusInterceptor } from ' tanstack-fetch/plugins '

export const api = createFetch ({ baseUrl : ' https://api.example.com ' ,…

Continue reading — create a free account

Join HashtagPLUS to read full articles, follow hashtags, vote, and join the conversation.