How do I add a cookie jar to axios in typescript?

1.5k views Asked by At

The following code is used to add a cookie jar to an axios instance. It fails because the interface AxiosRequestConfig doesn't contain a member named "jar". Can I augment the existing AxiosRequestConfig type, or is there a workaround for this?

const tough = require('tough-cookie');
const axiosCookieJarSupport = require('axios-cookiejar-support').default;
//...
  const instance = axios.create();
  axiosCookieJarSupport(instance);
  instance.defaults.jar = new tough.CookieJar();

The error message from typescript is "Property 'jar' does not exist on type 'AxiosRequestConfig'", which is true, but how do I fix it?

1

There are 1 answers

0
Raul_M On

For me it works as below:

const axios = require('axios').default;
const tough = require('tough-cookie');
const axiosCookieJarSupport = require('axios-cookiejar-support').default;

axiosCookieJarSupport(axios);

export const cookieJar = new tough.CookieJar();