Skip to content

Commit

Permalink
fix: allow to reuse config (e.g. axios-retry) (#650)
Browse files Browse the repository at this point in the history
* add test for reusing config

* fix: allow to reuse config (e.g. axios-retry)

* closes #602
* closes #609

* docs: update README
  • Loading branch information
3846masa committed Dec 26, 2022
1 parent ee5d4a2 commit 86a759c
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 1 deletion.
7 changes: 7 additions & 0 deletions README.md
Expand Up @@ -51,6 +51,13 @@ declare module 'axios' {

See also https://github.com/axios/axios#request-config .

## FAQ

- Q. Why can't I assign the httpAgent / httpsAgent?
- A. axios-cookiejar-support uses httpAgent / httpsAgent to read and write cookies. If other Agents are assigned, cookies cannot be read/written.
- Q. I want to use it with another Agent (e.g., http-proxy-agent).
- A. Consider using http-cookie-agent. axios-cookiejar-support also uses http-cookie-agent. Read http-cookie-agent's README for more details.

## Contributing

PRs accepted.
Expand Down
17 changes: 17 additions & 0 deletions src/__tests__/basic.spec.ts
Expand Up @@ -215,3 +215,20 @@ test.serial('should throw error when config.jar was assigned with boolean', asyn
t.plan(1);
server.close();
});

test.serial('should allow to reuse config', async (t) => {
const { port } = await createTestServer([
(_req, res) => {
res.end('Hello World!');
},
(_req, res) => {
res.end('Hello World!');
},
]);

const jar = new CookieJar();

const { config } = await axios.get(`http://localhost:${port}`, { jar, responseType: 'text' });
await axios.get(`http://localhost:${port}`, config);
t.pass();
});
20 changes: 19 additions & 1 deletion src/index.ts
Expand Up @@ -2,6 +2,8 @@ import type { AxiosInstance, AxiosRequestConfig, AxiosStatic } from 'axios';
import { HttpCookieAgent, HttpsCookieAgent } from 'http-cookie-agent/http';
import type { CookieJar } from 'tough-cookie';

const AGENT_CREATED_BY_AXIOS_COOKIEJAR_SUPPORT = Symbol('AGENT_CREATED_BY_AXIOS_COOKIEJAR_SUPPORT');

declare module 'axios' {
interface AxiosRequestConfig {
jar?: CookieJar;
Expand All @@ -18,12 +20,28 @@ function requestInterceptor(config: AxiosRequestConfig): AxiosRequestConfig {
throw new Error('config.jar does not accept boolean since axios-cookiejar-support@2.0.0.');
}

if (config.httpAgent || config.httpsAgent) {
if (
(config.httpAgent != null && config.httpAgent[AGENT_CREATED_BY_AXIOS_COOKIEJAR_SUPPORT] !== true) ||
(config.httpsAgent != null && config.httpsAgent[AGENT_CREATED_BY_AXIOS_COOKIEJAR_SUPPORT] !== true)
) {
throw new Error('axios-cookiejar-support does not support for use with other http(s).Agent.');
}

config.httpAgent = new HttpCookieAgent({ cookies: { jar: config.jar } });
Object.defineProperty(config.httpAgent, AGENT_CREATED_BY_AXIOS_COOKIEJAR_SUPPORT, {
configurable: false,
enumerable: false,
value: true,
writable: false,
});

config.httpsAgent = new HttpsCookieAgent({ cookies: { jar: config.jar } });
Object.defineProperty(config.httpsAgent, AGENT_CREATED_BY_AXIOS_COOKIEJAR_SUPPORT, {
configurable: false,
enumerable: false,
value: true,
writable: false,
});

return config;
}
Expand Down

0 comments on commit 86a759c

Please sign in to comment.