Add tough-cookie support to axios.
npx @tessl/cli install tessl/npm-axios-cookiejar-support@6.0.0axios-cookiejar-support is a TypeScript library that adds tough-cookie support to axios HTTP client for Node.js applications. It enables persistent cookie management across HTTP requests by wrapping axios instances with a request interceptor that automatically configures HTTP/HTTPS agents with cookie jar support. The library maintains cookie state through CookieJar instances from tough-cookie and provides a browser-compatible noop implementation.
npm install axios tough-cookie axios-cookiejar-supportimport { wrapper } from 'axios-cookiejar-support';
import { CookieJar } from 'tough-cookie';
import axios from 'axios';const { wrapper } = require('axios-cookiejar-support');
const { CookieJar } = require('tough-cookie');
const axios = require('axios');The most common usage pattern is to create a cookie jar and wrap an axios instance:
import axios from 'axios';
import { wrapper } from 'axios-cookiejar-support';
import { CookieJar } from 'tough-cookie';
// Create a cookie jar
const jar = new CookieJar();
// Create and wrap an axios instance
const client = wrapper(axios.create({ jar }));
// Use the client - cookies will be automatically managed
await client.get('https://example.com');You can also wrap the static axios object:
import axios from 'axios';
import { wrapper } from 'axios-cookiejar-support';
import { CookieJar } from 'tough-cookie';
// Wrap the static axios
wrapper(axios);
// Create a cookie jar
const jar = new CookieJar();
// Use axios directly with jar config
await axios.get('https://example.com', { jar });axios-cookiejar-support uses an interceptor-based architecture that integrates seamlessly with axios:
jar config is presentHttpCookieAgent and HttpsCookieAgent instances from the http-cookie-agent librarywrapper function adds the interceptor to both instances and the create method for comprehensive coverageThe main function that adds cookie jar support to axios instances.
function wrapper<T extends AxiosStatic | AxiosInstance>(axios: T): T;Parameters:
axios (AxiosStatic | AxiosInstance): Either the static axios object or an axios instanceReturns:
T: The same axios object/instance with cookie jar interceptor addedGeneric Type:
T extends AxiosStatic | AxiosInstance: Preserves the exact type of the input axiosBehavior:
create method to ensure new instances get cookie supportjar is present in request configExample:
import axios from 'axios';
import { wrapper } from 'axios-cookiejar-support';
import { CookieJar } from 'tough-cookie';
const jar = new CookieJar();
const client = wrapper(axios.create({ jar }));
// The client now supports cookie management
await client.get('https://httpbin.org/cookies/set/session_id/abc123');
await client.get('https://httpbin.org/cookies'); // Will include the session_id cookiedeclare module 'axios' {
interface AxiosRequestConfig {
jar?: CookieJar;
}
}Extends axios request configuration to include optional cookie jar support.
Properties:
jar?: CookieJar: Optional tough-cookie CookieJar instance for cookie managementdeclare module 'axios' {
interface AxiosInterceptorManager {
handlers: Array<{
fulfilled: (...args: unknown[]) => unknown;
rejected: (...args: unknown[]) => unknown;
runWhen: unknown;
synchronous: unknown;
}>;
}
}Extends axios interceptor manager to expose handlers array (used internally for idempotent wrapping).
Full functionality with HTTP/HTTPS agent configuration for cookie management.
Provides a noop implementation since browsers handle cookies automatically:
// Browser implementation (automatic via package.json exports)
export function wrapper(axios) {
return axios; // No-op, returns axios unchanged
}The library throws specific errors for configuration issues:
// This will throw an error
const config = { jar: true }; // Invalid since v2.0.0
// Error: "config.jar does not accept boolean since axios-cookiejar-support@2.0.0."import https from 'https';
// This will throw an error if jar is also specified
const config = {
jar: new CookieJar(),
httpsAgent: new https.Agent() // Conflicts with cookie jar agent
};
// Error: "axios-cookiejar-support does not support for use with other http(s).Agent."You can use different cookie jars for different requests:
const jar1 = new CookieJar();
const jar2 = new CookieJar();
const client = wrapper(axios.create());
// Use different jars per request
await client.get('https://site1.com', { jar: jar1 });
await client.get('https://site2.com', { jar: jar2 });Ideal for maintaining session state across requests:
const jar = new CookieJar();
const client = wrapper(axios.create({ jar }));
// Login and establish session
await client.post('https://example.com/login', {
username: 'user',
password: 'pass'
});
// Subsequent requests will include session cookies
const response = await client.get('https://example.com/profile');The library provides full TypeScript support with proper generic type preservation:
import axios, { AxiosInstance } from 'axios';
import { wrapper } from 'axios-cookiejar-support';
const instance: AxiosInstance = axios.create();
const wrappedInstance: AxiosInstance = wrapper(instance); // Type preserved
// All original axios methods and properties available
wrappedInstance.defaults.timeout = 5000;
wrappedInstance.interceptors.response.use(/* ... */);