-
-
Notifications
You must be signed in to change notification settings - Fork 57
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: allow to reuse config (e.g. axios-retry) (#650)
* add test for reusing config * fix: allow to reuse config (e.g. axios-retry) * closes #602 * closes #609 * docs: update README
- Loading branch information
Showing
3 changed files
with
43 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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; | ||
|
@@ -18,12 +20,28 @@ function requestInterceptor(config: AxiosRequestConfig): AxiosRequestConfig { | |
throw new Error('config.jar does not accept boolean since [email protected].'); | ||
} | ||
|
||
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; | ||
} | ||
|