stockshark-api-access
Advanced tools
Comparing version 1.0.8 to 1.0.9
{ | ||
"name": "stockshark-api-access", | ||
"version": "1.0.8", | ||
"version": "1.0.9", | ||
"main": "./src/index.js", | ||
@@ -5,0 +5,0 @@ "files": [ |
183
src/proxy.js
const axios = require("axios") | ||
const http = require('http'); | ||
const https = require('https'); | ||
const http = require("http") | ||
const https = require("https") | ||
class Proxy { | ||
constructor() { | ||
this.http = null | ||
this.cache = {} | ||
this.errorHandler = null | ||
this.util = null | ||
this.time = null | ||
} | ||
constructor() { | ||
this.http = null | ||
this.cache = {} | ||
this.errorHandler = null | ||
this.util = null | ||
this.time = null | ||
} | ||
init(util, time, config) { | ||
if (typeof config.baseUrl === "undefined") throw "Proxy Error: Missing baseUrl" | ||
init(util, time, config) { | ||
if (typeof config.baseUrl === "undefined") throw "Proxy Error: Missing baseUrl" | ||
const headers = {} | ||
if (config.authName) headers["x-auth-name"] = config.authName | ||
const headers = {} | ||
if (config.authName) headers['x-auth-name'] = config.authName | ||
this.http = axios.create({ | ||
baseURL: config.baseUrl, | ||
timeout: 1000 * 60 * 5000, | ||
headers, | ||
httpAgent: new http.Agent({ | ||
rejectUnauthorized: false, | ||
// keepAlive: true // Running on localhost will cause error: ECONNRESET, 'socket hang up', maybe I should handle it later... | ||
}), | ||
httpsAgent: new https.Agent({ | ||
rejectUnauthorized: false, | ||
// keepAlive: true // I try not using this... | ||
}), | ||
}) | ||
this.util = util | ||
this.time = time | ||
} | ||
this.http = axios.create({ | ||
baseURL: config.baseUrl, | ||
timeout: 1000 * 60, | ||
headers, | ||
httpAgent: new http.Agent({ | ||
rejectUnauthorized: false, | ||
keepAlive: true | ||
}), | ||
httpsAgent: new https.Agent({ | ||
rejectUnauthorized: false, | ||
keepAlive: true | ||
}) | ||
}); | ||
this.util = util | ||
this.time = time | ||
} | ||
setToken(token) { | ||
if (!token) { | ||
delete this.http.defaults.headers.common["Authorization"] | ||
return | ||
} | ||
this.http.defaults.headers.common["Authorization"] = `Bearer ${token}` | ||
} | ||
setToken(token) { | ||
if (!token) { | ||
delete this.http.defaults.headers.common['Authorization'] | ||
return | ||
} | ||
this.http.defaults.headers.common['Authorization'] = `Bearer ${token}` | ||
} | ||
setErrorHandler(errorHandler) { | ||
this.errorHandler = errorHandler | ||
} | ||
setErrorHandler(errorHandler) { | ||
this.errorHandler = errorHandler | ||
} | ||
async get(path, { cache }) { | ||
// console.log(path, cache) | ||
async get(path, { cache }) { | ||
// console.log(path, cache) | ||
if (cache && this.cache[path] && this.cache[path].valid > Date.now()) { | ||
// console.log("from cache") | ||
return this.util.cloneObj(this.cache[path].data) | ||
} | ||
if (cache && this.cache[path] && this.cache[path].valid > Date.now()) { | ||
// console.log("from cache") | ||
return this.util.cloneObj(this.cache[path].data) | ||
} | ||
let result = null | ||
try { | ||
result = await this.http(path) | ||
} catch (error) { | ||
if (this.errorHandler) { | ||
const { status, message } = this.parseError(error) | ||
this.errorHandler(status, message) | ||
return null | ||
} | ||
// console.log(error.response.status) | ||
console.error(error) | ||
return null | ||
} | ||
let result = null | ||
try { | ||
result = await this.http(path) | ||
} | ||
catch (error) { | ||
if (this.errorHandler) { | ||
const { status, message } = this.parseError(error) | ||
this.errorHandler(status, message) | ||
return null | ||
} | ||
// console.log(error.response.status) | ||
console.error(error) | ||
return null | ||
} | ||
if (cache) { | ||
this.cache[path] = { | ||
data: this.util.cloneObj(result.data.result), | ||
valid: this.time.nextHour(new Date(result.headers.date).getTime()), //valid until the end of current hour | ||
} | ||
// console.log(this.cache) | ||
} | ||
if (cache) { | ||
this.cache[path] = { | ||
data: this.util.cloneObj(result.data.result), | ||
valid: this.time.nextHour(new Date(result.headers.date).getTime()) //valid until the end of current hour | ||
} | ||
// console.log(this.cache) | ||
} | ||
return result.data.result | ||
} | ||
return result.data.result | ||
} | ||
async post(path, data) { | ||
try { | ||
const result = await this.http({ | ||
method: "POST", | ||
url: path, | ||
data: data, | ||
}) | ||
return result.data.result | ||
} catch (error) { | ||
if (this.errorHandler) { | ||
const { status, message } = this.parseError(error) | ||
this.errorHandler(status, message) | ||
return null | ||
} | ||
// console.log(error.response.status) | ||
console.error(error) | ||
return null | ||
} | ||
} | ||
async post(path, data) { | ||
try { | ||
const result = await this.http({ method: 'POST', url: path, data: data }) | ||
return result.data.result | ||
} | ||
catch (error) { | ||
if (this.errorHandler) { | ||
const { status, message } = this.parseError(error) | ||
this.errorHandler(status, message) | ||
return null | ||
} | ||
// console.log(error.response.status) | ||
console.error(error) | ||
return null | ||
} | ||
} | ||
parseError(error) { | ||
const status = (error.response && error.response.status) ? error.response.status : 500 | ||
const message = (error.response && error.response.data) ? error.response.data : ((error.message) ? error.message : (error.request ? error.request : "")) | ||
return { status, message } | ||
} | ||
parseError(error) { | ||
const status = error.response && error.response.status ? error.response.status : 500 | ||
const message = error.response && error.response.data ? error.response.data : error.message ? error.message : error.request ? error.request : "" | ||
return { status, message } | ||
} | ||
} | ||
module.exports = Proxy | ||
module.exports = Proxy |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Network access
Supply chain riskThis module accesses the network.
Found 2 instances in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Network access
Supply chain riskThis module accesses the network.
Found 2 instances in 1 package
133
10369