TanStackT
TanStack2y ago
11 replies
popular-magenta

Is there a way to use interceptors in tanstack query ?

A few days ago i learn how to use interceptors.request/response with axios and know i am curious if tanstack i can do the same
JUst a bit of code about how i do it:
import axios, { AxiosError, AxiosInstance } from "axios"
    _axiosInstance = axios.create({
        baseURL: import.meta.env.VITE_API_URL,
        timeout: 5000
    })
_axiosInstance!.interceptors.request.use(
        async (config) => {
            if (config.url?.includes(import.meta.env.VITE_TOKEN_URL)) {
                config.headers.set(
                    "Refresh-Token",
                    // do smt
                )
            } else {
                config.headers.set(
                    "Access-Token",
                                // do smt
                )
            }

            return config
        },
        (error) => Promise.reject(error)
    )

i am curios how can i do in tanstack the same
Was this page helpful?