axios-cookiejar-support
Add tough-cookie cookie jar support to the axios HTTP client
Repository Health
Technical Analysis
axios-cookiejar-support is a small adapter that teaches the axios HTTP client to persist and reuse cookies through a tough-cookie CookieJar. It wraps an axios instance so that every request automatically stores Set-Cookie responses in the jar and sends the appropriate cookies on subsequent requests.
This makes stateful HTTP flows in Node.js such as login sessions, CSRF token handling, and multi-step scraping work the same way a browser would, without manually threading cookie headers through each call.
What You Get
- A single wrapper() function that adds cookie-jar behavior to any axios instance
- Automatic persistence of Set-Cookie responses and replay on later requests
- A per-request jar option added to axios request config via TypeScript module augmentation
- Compatibility with axios.create so derived instances inherit cookie support
Common Use Cases
- Maintaining an authenticated session across multiple axios requests
- Handling CSRF tokens or session cookies during multi-step API flows
- Web scraping that requires login state to persist between requests
Under The Hood
Architecture - The library is a thin interceptor layer. wrapper() registers a request interceptor that, when a config.jar is present, swaps in HttpCookieAgent/HttpsCookieAgent instances (from http-cookie-agent) bound to that jar, and marks them with a private Symbol so it won’t clobber user-provided agents. It also patches axios.create so instances spawned after wrapping inherit the interceptor.
Tech Stack - Written in TypeScript. It depends on http-cookie-agent to do the actual cookie-aware socket handling and treats axios and tough-cookie as peers. Module augmentation on ‘axios’ adds the typed jar field to AxiosRequestConfig.
Code Quality - The source is compact and defensive: it throws clear errors for the removed boolean jar API and for conflicting custom http(s) agents, and guards against double-wrapping. A Vitest suite under src/tests covers create, wrap, default config, and no-jar scenarios, and releases are automated via semantic-release.
API Design - The public surface is a single wrapper() call plus a jar field on the request config, which is about as minimal as an integration can be. Usage mirrors idiomatic axios (wrapper(axios.create({ jar })) ) and the typing makes the jar option discoverable.