Skip to content

Instantly share code, notes, and snippets.

@noman798
Created February 16, 2020 08:24
Show Gist options
  • Save noman798/40d721f0b0de56959284e7231406fda6 to your computer and use it in GitHub Desktop.
Save noman798/40d721f0b0de56959284e7231406fda6 to your computer and use it in GitHub Desktop.
process.env["NODE_TLS_REJECT_UNAUTHORIZED"] = 0
{merge} = require 'lodash'
chalk = require 'chalk'
module.exports = axios = require('axios')
https = require 'https'
axios.defaults.timeout = 6000
axios.defaults.retry = 7
axios.defaults.retryDelay = 1000
axios.interceptors.response.use undefined, (err) ->
if err.response
console.error "❌" , chalk.redBright(err.response.status + " : " +err.response.statusText)
console.error chalk.red(err.response.data)
else
console.error chalk.red(err)
config = err.config
# If config does not exist or the retry option is not set, reject
if !config or !config.retry or err.response?.status == 404
return Promise.reject(err)
# Set the variable for keeping track of the retry count
config.__retryCount = config.__retryCount or 0
# Check if we've maxed out the total number of retries
if config.__retryCount >= config.retry
# Reject with the error
return Promise.reject(err)
# Increase the retry count
config.__retryCount += 1
# Create new promise to handle exponential backoff
backoff = new Promise((resolve) ->
setTimeout(
->
console.log "第 #{config.__retryCount} 次重试"
resolve()
return
config.retryDelay or 1
)
return
)
# Return the promise in which recalls axios to retry the request
return backoff.then ->
axios config
_option = (option)->
r = []
build = (method)=>
r[method] = ->
console.log "\n"+chalk.gray(arguments[0])
pos = arguments.length-1
arguments[pos] = merge arguments[pos], option
r = await axios[method].apply(axios, arguments)
return r.data
build 'get'
build 'post'
return r
axios.cache = _option({
proxy : {
host:"127.0.0.1"
port:3128
}
})
PROXY_URL = 'http://egurCWKwSWAUIYmz:Optws8nkIT1i8iBB@transfer.moguproxy.com:9001'
axios.proxy = _option({
httpsAgent: new require('https-proxy-agent')(PROXY_URL)
httpAgent: new require('http-proxy-agent')(PROXY_URL)
proxy:false
headers:
"Proxy-Authorization": "Basic ZWd1ckNXS3dTV0FVSVltejpPcHR3czhua0lUMWk4aUJC"
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment