Skip to content

Commit

Permalink
Update client.js
Browse files Browse the repository at this point in the history
  • Loading branch information
freearhey committed Nov 16, 2023
1 parent 7cbe1e8 commit fa19d29
Showing 1 changed file with 56 additions and 11 deletions.
67 changes: 56 additions & 11 deletions src/client.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
const { CurlGenerator } = require('curl-generator')
const axios = require('axios').default
const axiosCookieJarSupport = require('axios-cookiejar-support').default
const { default: axios } = require('axios')
const { CookieJar } = require('tough-cookie')
const { setupCache } = require('axios-cache-interceptor')
const { isObject, isPromise } = require('./utils')
const { HttpCookieAgent, HttpsCookieAgent } = require('http-cookie-agent/http')

axiosCookieJarSupport(axios)
const jar = new CookieJar()

module.exports.create = create
module.exports.buildRequest = buildRequest
Expand All @@ -13,14 +14,17 @@ module.exports.parseResponse = parseResponse
let timeout

function create(config) {
const client = setupCache(
axios.create({
ignoreCookieErrors: true,
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'
}
})
const client = setupCookie(
setupCache(
axios.create({
jar,
ignoreCookieErrors: true,
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'
}
})
)
)

client.interceptors.request.use(
Expand Down Expand Up @@ -136,3 +140,44 @@ async function getRequestUrl({ channel, date, config }) {
}
return config.url
}

const AGENT_CREATED_BY_AXIOS_COOKIEJAR_SUPPORT = Symbol('AGENT_CREATED_BY_AXIOS_COOKIEJAR_SUPPORT')

function requestInterceptor(config) {
if (!config.jar) {
return config
}

config.httpAgent = new HttpCookieAgent({ cookies: { jar: config.jar } })
Object.defineProperty(config.httpAgent, AGENT_CREATED_BY_AXIOS_COOKIEJAR_SUPPORT, {
configurable: false,
enumerable: false,
value: true,
writable: false
})

config.httpsAgent = new HttpsCookieAgent({ cookies: { jar: config.jar } })
Object.defineProperty(config.httpsAgent, AGENT_CREATED_BY_AXIOS_COOKIEJAR_SUPPORT, {
configurable: false,
enumerable: false,
value: true,
writable: false
})

return config
}

function setupCookie(axios) {
axios.interceptors.request.use(requestInterceptor)

if ('create' in axios) {
const create = axios.create
axios.create = (...args) => {
const instance = create.apply(axios, args)
instance.interceptors.request.use(requestInterceptor)
return instance
}
}

return axios
}

0 comments on commit fa19d29

Please sign in to comment.