forked from GitHub-Xzhi/AutoSignMachine
-
Notifications
You must be signed in to change notification settings - Fork 0
/
request.js
68 lines (64 loc) · 1.93 KB
/
request.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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
const axios = require("axios");
const axiosCookieJarSupport = require("axios-cookiejar-support").default;
const tough = require("tough-cookie");
axiosCookieJarSupport(axios);
const err = (error) => {
return Promise.reject(error);
};
var parseDefaultCookie = function (cookies) {
let cookie = [];
if (Object.prototype.toString.call(cookies) == "[object String]") {
cookie = cookies ? [cookies] : [];
} else if (Object.prototype.toString.call(cookies) == "[object Object]") {
Object.keys(cookies).forEach((item) => {
cookie.push(item + "=" + cookies[item]);
});
}
return cookie.join("; ");
};
var setCookieString = function (jar, cookies, config) {
let url;
if (config.url.indexOf("http") === 0) {
url = config.url;
} else {
url = config.baseURL + config.url;
}
let uuuu = new URL(url);
// console.log('setCookieString for', uuuu.origin)
cookies = parseDefaultCookie(cookies);
if (Object.prototype.toString.call(cookies) == "[object String]") {
cookies.length &&
cookies.split("; ").forEach((cookie) => {
jar.setCookieSync(cookie, uuuu.origin + "/", {});
});
}
return jar;
};
module.exports = (cookies) => {
const service = axios.create({
headers: {
Cookie: parseDefaultCookie(cookies),
},
jar: new tough.CookieJar(),
timeout: 60000,
withCredentials: true,
});
service.interceptors.request.use(async (config) => {
if (!("user-agent" in config.headers)) {
config.headers["user-agent"] =
"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/86.0.4240.183 Safari/537.36";
}
let jar = config.jar;
if (!jar) {
jar = new tough.CookieJar();
} else {
config.headers["Cookie"] = "";
}
config.jar = setCookieString(jar, cookies, config);
return config;
}, err);
service.interceptors.response.use(async (response) => {
return response;
}, err);
return service;
};