Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

stockshark-api-access

Package Overview
Dependencies
Maintainers
1
Versions
26
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

stockshark-api-access - npm Package Compare versions

Comparing version 1.0.8 to 1.0.9

2

package.json
{
"name": "stockshark-api-access",
"version": "1.0.8",
"version": "1.0.9",
"main": "./src/index.js",

@@ -5,0 +5,0 @@ "files": [

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
SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc