feat: deprecate request and implement axios. bump ws. cleaning. expose request options for proxy support. resolves #12. resolves #4.
This commit is contained in:
18
.eslintrc.js
18
.eslintrc.js
@@ -2,29 +2,15 @@ module.exports = {
|
||||
env: {
|
||||
es6: true,
|
||||
node: true,
|
||||
'jest/globals': true
|
||||
},
|
||||
extends: ['eslint:recommended', 'plugin:jest/recommended'],
|
||||
extends: ['eslint:recommended'],
|
||||
parserOptions: {
|
||||
sourceType: 'module',
|
||||
ecmaVersion: 9
|
||||
},
|
||||
|
||||
plugins: ['jest', 'prettier'],
|
||||
plugins: [],
|
||||
rules: {
|
||||
// 'prettier/prettier': [
|
||||
// 'error',
|
||||
// {
|
||||
// singleQuote: true,
|
||||
// printWidth: 140,
|
||||
// arrowParens: 'avoid'
|
||||
// }
|
||||
// ],
|
||||
'jest/no-disabled-tests': 'warn',
|
||||
'jest/no-focused-tests': 'error',
|
||||
'jest/no-identical-title': 'error',
|
||||
'jest/prefer-to-have-length': 'warn',
|
||||
'jest/valid-expect': 'error',
|
||||
'array-bracket-spacing': ['error', 'never'],
|
||||
indent: ['warn', 2],
|
||||
'linebreak-style': ['error', 'unix'],
|
||||
|
||||
@@ -1,138 +0,0 @@
|
||||
const assert = require('assert');
|
||||
const request = require('request');
|
||||
|
||||
const {signMessage} = require('./utility.js');
|
||||
|
||||
const baseUrls = {
|
||||
livenet: 'https://api.bybit.com',
|
||||
testnet: 'https://api-testnet.bybit.com'
|
||||
};
|
||||
|
||||
module.exports = class Request {
|
||||
|
||||
constructor(key, secret, livenet=false, options={}) {
|
||||
this.baseUrl = baseUrls[livenet === true ? 'livenet' : 'testnet'];
|
||||
this._timeOffset = null;
|
||||
this._syncTimePromise = null;
|
||||
|
||||
this.options = {
|
||||
recv_window: 5000,
|
||||
sync_interval_ms: 3600000,
|
||||
...options
|
||||
};
|
||||
|
||||
if (key) assert(secret, 'Secret is required for private enpoints');
|
||||
|
||||
this._syncTime();
|
||||
setInterval(this._syncTime.bind(this), parseInt(this.options.sync_interval_ms));
|
||||
|
||||
this.key = key;
|
||||
this.secret = secret;
|
||||
}
|
||||
|
||||
async get(endpoint, params) {
|
||||
const result = await this._call('GET', endpoint, params);
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
async post(endpoint, params) {
|
||||
const result = await this._call('POST', endpoint, params);
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
async getTimeOffset() {
|
||||
const start = Date.now();
|
||||
const result = await this.get('v2/public/time');
|
||||
const end = Date.now();
|
||||
|
||||
return Math.ceil((result.time_now * 1000) - end + ((end - start) / 2));
|
||||
}
|
||||
|
||||
async _call(method, endpoint, params) {
|
||||
const publicEndpoint = endpoint.startsWith('v2/public');
|
||||
|
||||
if (!publicEndpoint) {
|
||||
if (!this.key || !this.secret) throw new Error('Private endpoints require api and private keys set');
|
||||
|
||||
if (this._timeOffset === null) await this._syncTime();
|
||||
|
||||
params = this._signRequest(params);
|
||||
}
|
||||
|
||||
const options = {
|
||||
url: [this.baseUrl, endpoint].join('/'),
|
||||
method: method,
|
||||
json: true
|
||||
};
|
||||
|
||||
switch (method) {
|
||||
case 'GET':
|
||||
options.qs = params;
|
||||
break;
|
||||
case 'POST':
|
||||
options.body = params;
|
||||
break;
|
||||
}
|
||||
|
||||
return new Promise((resolve, reject) => {
|
||||
request(options, function callback(error, response, body) {
|
||||
if (error) {
|
||||
return reject(error);
|
||||
}
|
||||
if (response.statusCode == 200) {
|
||||
return resolve(body);
|
||||
}
|
||||
return reject({
|
||||
code: response.statusCode,
|
||||
message: response.statusMessage,
|
||||
body: response.body,
|
||||
requestOptions: options
|
||||
});
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
_signRequest(data) {
|
||||
const params = {
|
||||
...data,
|
||||
api_key: this.key,
|
||||
timestamp: Date.now() + this._timeOffset
|
||||
};
|
||||
|
||||
// Optional, set to 5000 by default. Increase if timestamp/recv_window errors are seen.
|
||||
if (this.options.recv_window && !params.recv_window) {
|
||||
params.recv_window = this.options.recv_window;
|
||||
}
|
||||
|
||||
if (this.key && this.secret) {
|
||||
params.sign = signMessage(this._serializeParams(params), this.secret);
|
||||
}
|
||||
|
||||
return params;
|
||||
}
|
||||
|
||||
_serializeParams(params) {
|
||||
return Object.keys(params)
|
||||
.sort()
|
||||
.map(key => `${key}=${params[key]}`)
|
||||
.join('&');
|
||||
}
|
||||
|
||||
async _syncTime() {
|
||||
if (this._syncTimePromise !== null) return this._syncTimePromise;
|
||||
|
||||
this._syncTimePromise = new Promise(async (resolve, reject) => {
|
||||
try {
|
||||
this._timeOffset = await this.getTimeOffset();
|
||||
this._syncTimePromise = null;
|
||||
resolve();
|
||||
} catch (err) {
|
||||
reject(err);
|
||||
}
|
||||
});
|
||||
|
||||
return this._syncTimePromise;
|
||||
}
|
||||
};
|
||||
@@ -1,139 +0,0 @@
|
||||
const assert = require('assert');
|
||||
const request = require('request');
|
||||
|
||||
const {signMessage} = require('./utility');
|
||||
|
||||
const baseUrls = {
|
||||
livenet: 'https://api.bybit.com',
|
||||
testnet: 'https://api-testnet.bybit.com'
|
||||
};
|
||||
|
||||
module.exports = class Request {
|
||||
|
||||
constructor(key, secret, livenet=false, options={}) {
|
||||
this.baseUrl = baseUrls[livenet === true ? 'livenet' : 'testnet'];
|
||||
this._timeOffset = null;
|
||||
this._syncTimePromise = null;
|
||||
|
||||
this.options = {
|
||||
recv_window: 5000,
|
||||
sync_interval_ms: 3600000,
|
||||
...options
|
||||
};
|
||||
console.log('init new byaaabit api!!!!!!!!!');
|
||||
|
||||
if (key) assert(secret, 'Secret is required for private enpoints');
|
||||
|
||||
this._syncTime();
|
||||
setInterval(this._syncTime.bind(this), parseInt(this.options.sync_interval_ms));
|
||||
|
||||
this.key = key;
|
||||
this.secret = secret;
|
||||
}
|
||||
|
||||
async get(endpoint, params) {
|
||||
const result = await this._call('GET', endpoint, params);
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
async post(endpoint, params) {
|
||||
const result = await this._call('POST', endpoint, params);
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
async getTimeOffset() {
|
||||
const start = Date.now();
|
||||
const result = await this.get('v2/public/time');
|
||||
const end = Date.now();
|
||||
|
||||
return Math.ceil((result.time_now * 1000) - end + ((end - start) / 2));
|
||||
}
|
||||
|
||||
async _call(method, endpoint, params) {
|
||||
const publicEndpoint = endpoint.startsWith('v2/public');
|
||||
|
||||
if (!publicEndpoint) {
|
||||
if (!this.key || !this.secret) throw new Error('Private endpoints require api and private keys set');
|
||||
|
||||
if (this._timeOffset === null) await this._syncTime();
|
||||
|
||||
params = this._signRequest(params);
|
||||
}
|
||||
|
||||
const options = {
|
||||
url: [this.baseUrl, endpoint].join('/'),
|
||||
method: method,
|
||||
json: true
|
||||
};
|
||||
|
||||
switch (method) {
|
||||
case 'GET':
|
||||
options.qs = params;
|
||||
break;
|
||||
case 'POST':
|
||||
options.body = params;
|
||||
break;
|
||||
}
|
||||
|
||||
return new Promise((resolve, reject) => {
|
||||
request(options, function callback(error, response, body) {
|
||||
if (error) {
|
||||
return reject(error);
|
||||
}
|
||||
if (response.statusCode == 200) {
|
||||
return resolve(body);
|
||||
}
|
||||
return reject({
|
||||
code: response.statusCode,
|
||||
message: response.statusMessage,
|
||||
body: response.body,
|
||||
requestOptions: options
|
||||
});
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
_signRequest(data) {
|
||||
const params = {
|
||||
...data,
|
||||
api_key: this.key,
|
||||
timestamp: Date.now() + this._timeOffset
|
||||
};
|
||||
|
||||
// Optional, set to 5000 by default. Increase if timestamp/recv_window errors are seen.
|
||||
if (this.options.recv_window && !params.recv_window) {
|
||||
params.recv_window = this.options.recv_window;
|
||||
}
|
||||
|
||||
if (this.key && this.secret) {
|
||||
params.sign = signMessage(this._serializeParams(params), this.secret);
|
||||
}
|
||||
|
||||
return params;
|
||||
}
|
||||
|
||||
_serializeParams(params) {
|
||||
return Object.keys(params)
|
||||
.sort()
|
||||
.map(key => `${key}=${params[key]}`)
|
||||
.join('&');
|
||||
}
|
||||
|
||||
async _syncTime() {
|
||||
if (this._syncTimePromise !== null) return this._syncTimePromise;
|
||||
|
||||
this._syncTimePromise = new Promise(async (resolve, reject) => {
|
||||
try {
|
||||
this._timeOffset = await this.getTimeOffset();
|
||||
this._syncTimePromise = null;
|
||||
resolve();
|
||||
} catch (err) {
|
||||
reject(err);
|
||||
}
|
||||
});
|
||||
|
||||
return this._syncTimePromise;
|
||||
}
|
||||
};
|
||||
@@ -1,12 +1,12 @@
|
||||
|
||||
const assert = require('assert');
|
||||
|
||||
const Request = require('./request-v2');
|
||||
const RequestWrapper = require('./util/requestWrapper');
|
||||
|
||||
module.exports = class RestClient {
|
||||
|
||||
constructor(key, secret, livenet=false, options={}) {
|
||||
this.request = new Request(...arguments);
|
||||
this.request = new RequestWrapper(...arguments);
|
||||
}
|
||||
|
||||
async placeActiveOrder(params) {
|
||||
|
||||
21
lib/util/requestUtils.js
Normal file
21
lib/util/requestUtils.js
Normal file
@@ -0,0 +1,21 @@
|
||||
const { createHmac } = require('crypto');
|
||||
|
||||
module.exports = {
|
||||
signMessage(message, secret) {
|
||||
return createHmac('sha256', secret)
|
||||
.update(message)
|
||||
.digest('hex');
|
||||
},
|
||||
serializeParams(params, strict_validation) {
|
||||
return Object.keys(params)
|
||||
.sort()
|
||||
.map(key => {
|
||||
const value = params[key];
|
||||
if (strict_validation === true && typeof value === 'undefined') {
|
||||
throw new Error('Failed to sign API request due to undefined parameter');
|
||||
}
|
||||
return `${key}=${value}`;
|
||||
})
|
||||
.join('&');
|
||||
}
|
||||
};
|
||||
171
lib/util/requestWrapper.js
Normal file
171
lib/util/requestWrapper.js
Normal file
@@ -0,0 +1,171 @@
|
||||
const assert = require('assert');
|
||||
const axios = require('axios');
|
||||
|
||||
const { signMessage, serializeParams } = require('./requestUtils');
|
||||
|
||||
const baseUrls = {
|
||||
livenet: 'https://api.bybit.com',
|
||||
testnet: 'https://api-testnet.bybit.com'
|
||||
};
|
||||
|
||||
module.exports = class RequestUtil {
|
||||
constructor(key, secret, livenet=false, options={}, requestOptions={}) {
|
||||
this._timeOffset = null;
|
||||
this._syncTimePromise = null;
|
||||
|
||||
this.options = {
|
||||
recv_window: 5000,
|
||||
// how often to sync time drift with bybit servers
|
||||
sync_interval_ms: 3600000,
|
||||
// if true, we'll throw errors if any params are undefined
|
||||
strict_param_validation: false,
|
||||
...options
|
||||
};
|
||||
|
||||
this.baseUrl = baseUrls[livenet === true ? 'livenet' : 'testnet'];
|
||||
if (options.baseUrl) {
|
||||
this.baseUrl = options.baseUrl
|
||||
}
|
||||
|
||||
this.globalRequestOptions = {
|
||||
// in ms == 5 minutes by default
|
||||
timeout: 1000 * 60 * 5,
|
||||
// custom request options based on axios specs - see: https://github.com/axios/axios#request-config
|
||||
...requestOptions,
|
||||
headers: {
|
||||
Referer: 'bybitapinode'
|
||||
},
|
||||
}
|
||||
|
||||
if (key) {
|
||||
assert(secret, 'Secret is required for private enpoints');
|
||||
}
|
||||
|
||||
this._syncTime();
|
||||
setInterval(this._syncTime.bind(this), parseInt(this.options.sync_interval_ms));
|
||||
|
||||
this.key = key;
|
||||
this.secret = secret;
|
||||
}
|
||||
|
||||
async get(endpoint, params) {
|
||||
const result = await this._call('GET', endpoint, params);
|
||||
return result;
|
||||
}
|
||||
|
||||
async post(endpoint, params) {
|
||||
const result = await this._call('POST', endpoint, params);
|
||||
return result;
|
||||
}
|
||||
|
||||
async getTimeOffset() {
|
||||
const start = Date.now();
|
||||
const result = await this.get('v2/public/time');
|
||||
const end = Date.now();
|
||||
|
||||
return Math.ceil((result.time_now * 1000) - end + ((end - start) / 2));
|
||||
}
|
||||
|
||||
async _call(method, endpoint, params) {
|
||||
const publicEndpoint = endpoint.startsWith('v2/public');
|
||||
|
||||
if (!publicEndpoint) {
|
||||
if (!this.key || !this.secret) {
|
||||
throw new Error('Private endpoints require api and private keys set');
|
||||
}
|
||||
|
||||
if (this._timeOffset === null) {
|
||||
await this._syncTime();
|
||||
}
|
||||
|
||||
params = this._signRequest(params);
|
||||
}
|
||||
|
||||
const options = {
|
||||
url: [this.baseUrl, endpoint].join('/'),
|
||||
method: method,
|
||||
json: true
|
||||
};
|
||||
|
||||
switch (method) {
|
||||
case 'GET':
|
||||
options.params = params;
|
||||
break;
|
||||
default:
|
||||
options.data = params;
|
||||
break;
|
||||
}
|
||||
|
||||
return axios(options).then(response => {
|
||||
if (response.status == 200) {
|
||||
return response.data;
|
||||
}
|
||||
|
||||
throw {
|
||||
code: response.status,
|
||||
message: response.statusText,
|
||||
body: response.data,
|
||||
requestOptions: options
|
||||
};
|
||||
})
|
||||
.catch(e => {
|
||||
if (!e.response) {
|
||||
// Something happened in setting up the request that triggered an Error
|
||||
if (!e.request) {
|
||||
throw e.message;
|
||||
}
|
||||
|
||||
// request made but no response received
|
||||
throw e;
|
||||
}
|
||||
|
||||
// The request was made and the server responded with a status code
|
||||
// that falls out of the range of 2xx
|
||||
throw {
|
||||
code: e.response.statusCode,
|
||||
message: e.response.message,
|
||||
body: e.response.body,
|
||||
requestOptions: options,
|
||||
headers: e.response.headers
|
||||
};
|
||||
});
|
||||
}
|
||||
|
||||
_signRequest(data) {
|
||||
const params = {
|
||||
...data,
|
||||
api_key: this.key,
|
||||
timestamp: Date.now() + this._timeOffset
|
||||
};
|
||||
|
||||
// Optional, set to 5000 by default. Increase if timestamp/recv_window errors are seen.
|
||||
if (this.options.recv_window && !params.recv_window) {
|
||||
params.recv_window = this.options.recv_window;
|
||||
}
|
||||
|
||||
if (this.key && this.secret) {
|
||||
const serializedParams = serializeParams(params, this.options.strict_param_validation);
|
||||
params.sign = signMessage(serializedParams, this.secret);
|
||||
}
|
||||
|
||||
return params;
|
||||
}
|
||||
|
||||
_syncTime() {
|
||||
if (this._syncTimePromise !== null) {
|
||||
return this._syncTimePromise;
|
||||
}
|
||||
|
||||
this._syncTimePromise = new Promise(async (resolve, reject) => {
|
||||
try {
|
||||
this._timeOffset = await this.getTimeOffset();
|
||||
this._syncTimePromise = null;
|
||||
resolve();
|
||||
} catch (err) {
|
||||
reject(err);
|
||||
}
|
||||
});
|
||||
|
||||
return this._syncTimePromise;
|
||||
}
|
||||
};
|
||||
@@ -1,9 +0,0 @@
|
||||
const {createHmac} = require('crypto');
|
||||
|
||||
module.exports = {
|
||||
signMessage(message, secret) {
|
||||
return createHmac('sha256', secret)
|
||||
.update(message)
|
||||
.digest('hex');
|
||||
}
|
||||
};
|
||||
@@ -4,7 +4,7 @@ const WebSocket = require('ws');
|
||||
|
||||
const defaultLogger = require('./logger');
|
||||
const RestClient = require('./rest-client');
|
||||
const { signMessage } = require('./utility');
|
||||
const { signMessage } = require('./util/requestUtils');
|
||||
|
||||
const wsUrls = {
|
||||
livenet: 'wss://stream.bybit.com/realtime',
|
||||
|
||||
4703
package-lock.json
generated
4703
package-lock.json
generated
File diff suppressed because it is too large
Load Diff
10
package.json
10
package.json
@@ -1,6 +1,6 @@
|
||||
{
|
||||
"name": "bybit-api",
|
||||
"version": "1.1.9",
|
||||
"version": "1.2.0",
|
||||
"description": "A light node.js wrapper for the Bybit Cryptocurrency Derivative exchange API",
|
||||
"main": "index.js",
|
||||
"scripts": {
|
||||
@@ -27,13 +27,9 @@
|
||||
"homepage": "https://github.com/tiagosiebler/bybit-api#readme",
|
||||
"dependencies": {
|
||||
"axios": "^0.20.0",
|
||||
"request": "^2.88.0",
|
||||
"ws": "^7.1.2"
|
||||
"ws": "^7.3.1"
|
||||
},
|
||||
"devDependencies": {
|
||||
"eslint": "^7.10.0",
|
||||
"eslint-plugin-jest": "^24.0.2",
|
||||
"eslint-plugin-prettier": "^3.1.4",
|
||||
"jest": "^26.4.2"
|
||||
"eslint": "^7.10.0"
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user