axios-cookiejar-support

repository·main·Indexed 43 days ago

https://github.com/3846masa/axios-cookiejar-support

A library that adds tough-cookie support to Axios, allowing Axios instances to automatically manage, store, and send cookies during HTTP requests. It provides a wrapper function to enable cookie-handling capabilities and extends AxiosRequestConfig to include a jar property for CookieJar instances.

Tokens
2.8K
Snippets
8
Records
10
Agent score
51%

What's inside axios-cookiejar-support

  1. Migrate from v1.0.x to v2.0.0

    main

    Upgrading from v1.x to v2.x involves several breaking changes regarding requirements, exports, and configuration usage.

    Update requirements

    • Node.js: v12.19.0 / v14.5.0 or above
    • axios: @0.20.0 or above
    • tough-cookie: @4.0.0 or above

    Wrapper function is now a named export

    The wrapper function is no longer the default export. You must use named exports.

    config.withCredentials is no longer used

    Do not use withCredentials to manage cookie behavior.

    • To prevent sending cookies, unset config.jar directly.
    • To allow cookies, provide a CookieJar instance to config.jar.

    config.httpAgent and config.httpsAgent compatibility

    You cannot use config.httpAgent or config.httpsAgent directly with axios-cookiejar-support. Instead, use http-cookie-agent to allow using other Agents (like Proxy Agents) with a CookieJar.

    config.jar no longer accepts boolean

    Passing true to config.jar is no longer supported. You must pass an actual CookieJar instance.

    Invalid cookies are ignored

    Since v2.0, invalid cookies are always ignored instead of throwing an error. The ignoreCookieErrors: true option is no longer necessary.

      // CommonJS
    - const axiosCookieJarSupport = require('axios-cookiejar-support');
    + const { wrapper: axiosCookieJarSupport } = require('axios-cookiejar-support');
      // ES Module
    - import axiosCookieJarSupport from 'axios-cookiejar-support';
    + import { wrapper as axiosCookieJarSupport } from 'axios-cookiejar-support';
      // Remove config.withCredentials
      axios.get('https://example.com', {
        jar: new CookieJar(),
    -   withCredentials: true,
      });
      // Remove config.jar for preventing to send cookie
      axios.get('https://example.com', {
    -   jar: new CookieJar(),
    -   withCredentials: false,
      });
    import axios from 'axios';
    import { CookieJar } from 'tough-cookie';
    import { createCookieAgent } from 'http-cookie-agent';
    import httpProxyAgent from 'http-proxy-agent';
    import httpsProxyAgent from 'https-proxy-agent';
    
    const HttpProxyCookieAgent = createCookieAgent(httpProxyAgent.HttpProxyAgent);
    const HttpsProxyCookieAgent = createCookieAgent(httpsProxyAgent.HttpsProxyAgent);
    
    const jar = new CookieJar();
    const client = axios.create({
      httpAgent: new HttpProxyCookieAgent({ jar, hostname: '127.0.0.1', port: 8080 }),
      httpsAgent: new HttpsProxyCookieAgent({ jar, hostname: '127.0.0.1', port: 8080 }),
    });
    
    await client.get('https://example.com');
      // Cannot use boolean as config.jar since v2.x
    + const cookieJar = new CookieJar();
      axios.get('https://example.com', {
    -   jar: true,
    +   jar: cookieJar,
      });
      const cookieJar = new CookieJar();
      axios.get('https://example.com', {
        jar: cookieJar,
    -   ignoreCookieErrors: true,
      });
  2. Migrate from v3.x.x to v4.0.0

    main

    As of v4.0.0, asynchronous Cookie stores (such as redis-cookie-store) are no longer supported directly by axios-cookiejar-support.

    Note: This change has no effect if you create a CookieJar without options.

    To use an asynchronous Cookie store, you must use http-cookie-agent instead.

    import axios from 'axios';
    import { CookieJar } from 'tough-cookie';
    import redis from 'redis';
    import RedisCookieStore from 'redis-cookie-store';
    import { HttpCookieAgent, HttpsCookieAgent } from 'http-cookie-agent/node:http';
    
    const redisClient = redis.createClient();
    const store = new RedisCookieStore(redisClient);
    const jar = new CookieJar(store);
    
    const client = axios.create({
      httpAgent: new HttpCookieAgent({ cookies: { async_UNSTABLE: true, jar } }),
      httpsAgent: new HttpsCookieAgent({ cookies: { async_UNSTABLE: true, jar } }),
    });
    
    await client.get('https://example.com');
  3. Migrate from v5.x.x to v6.0.0

    main

    When upgrading to v6.0.0, note the following changes:

    Update requirements

    • Requires Node.js v20.0.0 or above.

    CommonJS Support

    • Standard CommonJS is no longer provided as the primary export.
    • If you are using Node.js >= v20.19.0 or >= v22.12.0, axios-cookiejar-support can still be loaded from a CommonJS file.
  4. Troubleshooting Agent compatibility

    main

    Why can't I assign the httpAgent / httpsAgent?

    axios-cookiejar-support relies on the default httpAgent and httpsAgent to read and write cookies. If you manually assign different Agents, the library will be unable to process cookies.

    Using other Agents (e.g., http-proxy-agent)

    If you need to use a custom Agent like http-proxy-agent, consider using http-cookie-agent instead. axios-cookiejar-support itself uses http-cookie-agent internally.

  5. Use axios-cookiejar-support to enable cookie support

    main

    To enable tough-cookie support in your Axios instance, create a new CookieJar from tough-cookie, pass it into the Axios configuration via the jar key, and then wrap your Axios instance using the wrapper function provided by axios-cookiejar-support.

    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 }));
    
    await client.get('https://example.com');
  6. Extend AxiosRequestConfig with the jar property

    main

    If you are using TypeScript, you need to augment the AxiosRequestConfig interface from the axios module to include the jar property, which accepts a CookieJar instance from tough-cookie.

    import type { CookieJar } from 'tough-cookie';
    
    declare module 'axios' {
      interface AxiosRequestConfig {
        jar?: CookieJar;
      }
    }
  7. Configure `jar` in `AxiosRequestConfig`

    main

    The axios-cookiejar-support library extends the standard AxiosRequestConfig interface to include a jar property.

    • Type: CookieJar (from the tough-cookie package).
    • Usage: Pass an instance of a CookieJar into your request configuration object. The interceptor will use this jar to initialize the necessary HttpCookieAgent or HttpsCookieAgent.
    • Constraint: Since version 2.0.0, config.jar does not accept a boolean value. Passing true will throw an error.
    • Constraint: This library does not support use with other custom httpAgent or httpsAgent settings. If you provide a custom agent in the config, the library will throw an error to prevent conflicts.
    import { CookieJar } from 'tough-cookie';
    
    // ... after wrapping axios
    const jar = new CookieJar();
    
    // The 'jar' key is added to AxiosRequestConfig
    await axiosInstance.get('/api/data', {
      jar: jar
    });
  8. Use the wrapper function to add cookie support to Axios

    main

    The wrapper function is the primary entrypoint for adding cookie support to an Axios instance. It accepts an Axios instance (or the Axios library itself) and returns it, allowing you to augment the instance with cookie-handling capabilities.

    Note: In the current version of the source provided, the wrapper function acts as a pass-through, returning the input axios instance unchanged. This is typically used as a hook for the library to inject cookie-related interceptors and configuration into the Axios lifecycle.

    import axios from 'axios';
    import { wrapper } from 'axios-cookiejar-support';
    
    const axiosWithCookies = wrapper(axios);
  9. Wrap an Axios instance with `wrapper()`

    main

    To enable cookie support in Axios, you must wrap your Axios instance (or the global Axios object) using the wrapper function. This attaches a request interceptor that automatically manages cookie agents based on a provided CookieJar in the request configuration.

    When you wrap an instance, the wrapper function:

    1. Attaches a request interceptor to the main instance.
    2. If the instance has a .create() method, it overrides it so that any new instances created via .create() also automatically receive the cookie support interceptor.
    import axios from 'axios';
    import { wrapper } from 'axios-cookiejar-support';
    import { CookieJar } from 'tough-cookie';
    
    const client = wrapper(axios.create());
    const jar = new CookieJar();
    
    // Use the jar in your request config
    await client.get('https://example.com', {
      jar
    });