Update client.js

This commit is contained in:
Aleksandr Statciuk 2023-05-21 21:05:18 +03:00
parent eb3b3d39b4
commit bdef9aefb4
1 changed files with 99 additions and 99 deletions

View File

@ -13,125 +13,125 @@ module.exports.parseResponse = parseResponse
let timeout let timeout
function create(config) { function create(config) {
const client = setupCache( const client = setupCache(
axios.create({ axios.create({
headers: { headers: {
'User-Agent': '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' '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'
} }
}) })
) )
client.interceptors.request.use( client.interceptors.request.use(
function (request) { function (request) {
if (config.debug) { if (config.debug) {
console.log('Request:', JSON.stringify(request, null, 2)) console.log('Request:', JSON.stringify(request, null, 2))
} }
return request return request
}, },
function (error) { function (error) {
return Promise.reject(error) return Promise.reject(error)
} }
) )
client.interceptors.response.use( client.interceptors.response.use(
function (response) { function (response) {
if (config.debug) { if (config.debug) {
const data = const data =
isObject(response.data) || Array.isArray(response.data) isObject(response.data) || Array.isArray(response.data)
? JSON.stringify(response.data) ? JSON.stringify(response.data)
: response.data.toString() : response.data.toString()
console.log( console.log(
'Response:', 'Response:',
JSON.stringify( JSON.stringify(
{ {
headers: response.headers, headers: response.headers,
data, data,
cached: response.cached cached: response.cached
}, },
null, null,
2 2
) )
) )
} }
clearTimeout(timeout) clearTimeout(timeout)
return response return response
}, },
function (error) { function (error) {
clearTimeout(timeout) clearTimeout(timeout)
return Promise.reject(error) return Promise.reject(error)
} }
) )
return client return client
} }
async function buildRequest({ channel, date, config }) { async function buildRequest({ channel, date, config }) {
const CancelToken = axios.CancelToken const CancelToken = axios.CancelToken
const source = CancelToken.source() const source = CancelToken.source()
const request = { ...config.request } const request = { ...config.request }
timeout = setTimeout(() => { timeout = setTimeout(() => {
source.cancel('Connection timeout') source.cancel('Connection timeout')
}, request.timeout) }, request.timeout)
request.headers = await getRequestHeaders({ channel, date, config }) request.headers = await getRequestHeaders({ channel, date, config })
request.url = await getRequestUrl({ channel, date, config }) request.url = await getRequestUrl({ channel, date, config })
request.data = await getRequestData({ channel, date, config }) request.data = await getRequestData({ channel, date, config })
request.cancelToken = source.token request.cancelToken = source.token
if (config.curl) { if (config.curl) {
const curl = CurlGenerator({ const curl = CurlGenerator({
url: request.url, url: request.url,
method: request.method, method: request.method,
headers: request.headers, headers: request.headers,
body: request.data body: request.data
}) })
console.log(curl) console.log(curl)
} }
return request return request
} }
function parseResponse(response) { function parseResponse(response) {
return { return {
content: response.data.toString(), content: response.data.toString(),
buffer: response.data, buffer: response.data,
headers: response.headers, headers: response.headers,
request: response.request, request: response.request,
cached: response.cached cached: response.cached
} }
} }
async function getRequestHeaders({ channel, date, config }) { async function getRequestHeaders({ channel, date, config }) {
if (typeof config.request.headers === 'function') { if (typeof config.request.headers === 'function') {
const headers = config.request.headers({ channel, date }) const headers = config.request.headers({ channel, date })
if (isPromise(headers)) { if (isPromise(headers)) {
return await headers return await headers
} }
return headers return headers
} }
return config.request.headers || null return config.request.headers || null
} }
async function getRequestData({ channel, date, config }) { async function getRequestData({ channel, date, config }) {
if (typeof config.request.data === 'function') { if (typeof config.request.data === 'function') {
const data = config.request.data({ channel, date }) const data = config.request.data({ channel, date })
if (isPromise(data)) { if (isPromise(data)) {
return await data return await data
} }
return data return data
} }
return config.request.data || null return config.request.data || null
} }
async function getRequestUrl({ channel, date, config }) { async function getRequestUrl({ channel, date, config }) {
if (typeof config.url === 'function') { if (typeof config.url === 'function') {
const url = config.url({ channel, date }) const url = config.url({ channel, date })
if (isPromise(url)) { if (isPromise(url)) {
return await url return await url
} }
return url return url
} }
return config.url return config.url
} }