Skip to content

Commit fa19d29

Browse files
committed
Update client.js
1 parent 7cbe1e8 commit fa19d29

File tree

1 file changed

+56
-11
lines changed

1 file changed

+56
-11
lines changed

src/client.js

+56-11
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11
const { CurlGenerator } = require('curl-generator')
2-
const axios = require('axios').default
3-
const axiosCookieJarSupport = require('axios-cookiejar-support').default
2+
const { default: axios } = require('axios')
3+
const { CookieJar } = require('tough-cookie')
44
const { setupCache } = require('axios-cache-interceptor')
55
const { isObject, isPromise } = require('./utils')
6+
const { HttpCookieAgent, HttpsCookieAgent } = require('http-cookie-agent/http')
67

7-
axiosCookieJarSupport(axios)
8+
const jar = new CookieJar()
89

910
module.exports.create = create
1011
module.exports.buildRequest = buildRequest
@@ -13,14 +14,17 @@ module.exports.parseResponse = parseResponse
1314
let timeout
1415

1516
function create(config) {
16-
const client = setupCache(
17-
axios.create({
18-
ignoreCookieErrors: true,
19-
headers: {
20-
'User-Agent':
21-
'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/79.0.3945.130 Safari/537.36 Edg/79.0.309.71'
22-
}
23-
})
17+
const client = setupCookie(
18+
setupCache(
19+
axios.create({
20+
jar,
21+
ignoreCookieErrors: true,
22+
headers: {
23+
'User-Agent':
24+
'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/79.0.3945.130 Safari/537.36 Edg/79.0.309.71'
25+
}
26+
})
27+
)
2428
)
2529

2630
client.interceptors.request.use(
@@ -136,3 +140,44 @@ async function getRequestUrl({ channel, date, config }) {
136140
}
137141
return config.url
138142
}
143+
144+
const AGENT_CREATED_BY_AXIOS_COOKIEJAR_SUPPORT = Symbol('AGENT_CREATED_BY_AXIOS_COOKIEJAR_SUPPORT')
145+
146+
function requestInterceptor(config) {
147+
if (!config.jar) {
148+
return config
149+
}
150+
151+
config.httpAgent = new HttpCookieAgent({ cookies: { jar: config.jar } })
152+
Object.defineProperty(config.httpAgent, AGENT_CREATED_BY_AXIOS_COOKIEJAR_SUPPORT, {
153+
configurable: false,
154+
enumerable: false,
155+
value: true,
156+
writable: false
157+
})
158+
159+
config.httpsAgent = new HttpsCookieAgent({ cookies: { jar: config.jar } })
160+
Object.defineProperty(config.httpsAgent, AGENT_CREATED_BY_AXIOS_COOKIEJAR_SUPPORT, {
161+
configurable: false,
162+
enumerable: false,
163+
value: true,
164+
writable: false
165+
})
166+
167+
return config
168+
}
169+
170+
function setupCookie(axios) {
171+
axios.interceptors.request.use(requestInterceptor)
172+
173+
if ('create' in axios) {
174+
const create = axios.create
175+
axios.create = (...args) => {
176+
const instance = create.apply(axios, args)
177+
instance.interceptors.request.use(requestInterceptor)
178+
return instance
179+
}
180+
}
181+
182+
return axios
183+
}

0 commit comments

Comments
 (0)