-
Notifications
You must be signed in to change notification settings - Fork 0
/
client.js
54 lines (49 loc) · 1.08 KB
/
client.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
const axios = require('axios')
const { HttpsProxyAgent } = require('https-proxy-agent');
const qs = require('qs')
let csrf
// 添加请求拦截器aigc
axios.interceptors.request.use(
function (config) {
if (config.method === 'post') {
config.data = qs.stringify({
...config.data,
csrf_token: csrf,
csrf: csrf,
})
}
return config
},
function (error) {
return error
}
)
// 添加响应拦截器
axios.interceptors.response.use(
function (response) {
if (response.data.code !== 0) {
console.log(`response:`, response.data)
}
return response
},
function (error) {
console.log(error.response)
return error.response
}
)
const setCookies = function (cookie) {
// TODO 处理超时
axios.defaults.headers['cookie'] = Object.entries(cookie)
.map(([key, value]) => `${key}=${value}`)
.join(';')
csrf = cookie.bili_jct
}
const setProxy = function (proxy) {
// TODO 处理超时
axios.defaults.httpsAgent = new HttpsProxyAgent(proxy);
}
module.exports = {
setCookies,
setProxy,
http: axios,
}