1
1
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' )
4
4
const { setupCache } = require ( 'axios-cache-interceptor' )
5
5
const { isObject, isPromise } = require ( './utils' )
6
+ const { HttpCookieAgent, HttpsCookieAgent } = require ( 'http-cookie-agent/http' )
6
7
7
- axiosCookieJarSupport ( axios )
8
+ const jar = new CookieJar ( )
8
9
9
10
module . exports . create = create
10
11
module . exports . buildRequest = buildRequest
@@ -13,14 +14,17 @@ module.exports.parseResponse = parseResponse
13
14
let timeout
14
15
15
16
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
+ )
24
28
)
25
29
26
30
client . interceptors . request . use (
@@ -136,3 +140,44 @@ async function getRequestUrl({ channel, date, config }) {
136
140
}
137
141
return config . url
138
142
}
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