From 8736c1a26beef0334dd35fa13d36619dd54d351a Mon Sep 17 00:00:00 2001 From: Aleksandr Statciuk Date: Sat, 21 Aug 2021 19:11:01 +0300 Subject: [PATCH] Update utils.js --- src/utils.js | 52 ++++++++++++++++++++++++++++++++++++++++++++-------- 1 file changed, 44 insertions(+), 8 deletions(-) diff --git a/src/utils.js b/src/utils.js index fe0034d..63fa04c 100644 --- a/src/utils.js +++ b/src/utils.js @@ -11,6 +11,8 @@ dayjs.extend(utc) axiosCookieJarSupport(axios) const utils = {} +const defaultUserAgent = + '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' utils.loadConfig = function (file) { if (!file) throw new Error('Path to [site].config.js is missing') @@ -39,10 +41,6 @@ utils.loadConfig = function (file) { output: 'guide.xml', request: { method: 'GET', - headers: { - 'User-Agent': - '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' - }, maxContentLength: 5 * 1024 * 1024, timeout: 5000, withCredentials: true, @@ -194,15 +192,53 @@ utils.writeToFile = function (filename, data) { fs.writeFileSync(path.resolve(filename), data) } -utils.fetchData = function (item, config) { +utils.buildRequest = async function (item, config) { const request = { ...config.request } - request.url = typeof config.url === 'function' ? config.url(item) : config.url - request.data = - typeof config.request.data === 'function' ? config.request.data(item) : config.request.data + const headers = await utils.getRequestHeaders(item, config) + request.headers = { 'User-Agent': defaultUserAgent, ...headers } + request.url = await utils.getRequestUrl(item, config) + request.data = await utils.getRequestData(item, config) + return request +} + +utils.fetchData = function (request) { return axios(request) } +utils.getRequestHeaders = async function (item, config) { + if (typeof config.request.headers === 'function') { + const headers = config.request.headers(item) + if (typeof headers === 'Promise') { + return await headers + } + return headers + } + return config.request.headers +} + +utils.getRequestData = async function (item, config) { + if (typeof config.request.data === 'function') { + const data = config.request.data(item) + if (typeof data === 'Promise') { + return await data + } + return data + } + return config.request.data +} + +utils.getRequestUrl = async function (item, config) { + if (typeof config.url === 'function') { + const url = config.url(item) + if (typeof url === 'Promise') { + return await url + } + return url + } + return config.url +} + utils.getUTCDate = function () { return dayjs.utc() }