Skip to content

Commit

Permalink
Add files via upload
Browse files Browse the repository at this point in the history
  • Loading branch information
lz0423 authored Feb 27, 2021
1 parent e532893 commit 6a58ba1
Show file tree
Hide file tree
Showing 3 changed files with 535 additions and 0 deletions.
68 changes: 68 additions & 0 deletions utils/request.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,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;
};
Loading

0 comments on commit 6a58ba1

Please sign in to comment.