V
V
vovashaplin2020-10-11 21:32:29
React
vovashaplin, 2020-10-11 21:32:29

How to set fetcher to default SWR library in my case?

I have an axios addon that adds headers and inserts token.

export const instance = (token: string | undefined) => {
  return axios.create({
    baseURL: 'http://localhost:8000',
    headers: {
      'Content-Type': 'application/json',
      Authorization: `Token ${token}`
    }
  })
}

The problem appeared with the SWR library at the junction with Next.js.
Inside _app.js I put SWRConfig. (Without the add-on, it worked fine)
<SWRConfig
          value={{
            revalidateOnMount: true,
            dedupingInterval: 1000,
            fetcher: (url) => axios(url).then(r => r.data)
          }}
        >

The question is how to change this "fetcher: (url) => axios(url).then(r => r.data)" axios to instance?

I would be happy not to pass the token to inctance, because it is stored in cookies. But when I take from cookies inside instance, then undefined always gets out there.
Something like that
export const instance = axios.create({
  baseURL: 'http://localhost:8000',
  headers: {
    'Content-Type': 'application/json',
    Authorization: `Token ${Cookie.get('token')}`
  }
})

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question