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

bybit-api

Package Overview
Dependencies
Maintainers
1
Versions
126
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

bybit-api - npm Package Compare versions

Comparing version 1.3.0-beta.0 to 1.3.0-beta.1

dist/bybitapi.js

1

lib/index.d.ts
export * from './rest-client';
export * from './websocket-client';
export * from './logger';
//# sourceMappingURL=index.d.ts.map

@@ -10,1 +10,2 @@ export declare type LogParams = null | any;

};
//# sourceMappingURL=logger.d.ts.map

5

lib/rest-client.d.ts

@@ -10,7 +10,7 @@ import { AxiosRequestConfig } from 'axios';

* @param {string} secret - your API secret
* @param {boolean} [livenet=false]
* @param {boolean} [useLivenet=false]
* @param {RestClientInverseOptions} [restInverseOptions={}] options to configure REST API connectivity
* @param {AxiosRequestConfig} [requestOptions={}] HTTP networking options for axios
*/
constructor(key?: string | undefined, secret?: string | undefined, livenet?: boolean, restInverseOptions?: RestClientInverseOptions, requestOptions?: AxiosRequestConfig);
constructor(key?: string | undefined, secret?: string | undefined, useLivenet?: boolean, restInverseOptions?: RestClientInverseOptions, httpOptions?: AxiosRequestConfig);
/**

@@ -312,1 +312,2 @@ *

}
//# sourceMappingURL=rest-client.d.ts.map

@@ -16,2 +16,3 @@ "use strict";

exports.RestClient = void 0;
const requestUtils_1 = require("./util/requestUtils");
const requestWrapper_1 = __importDefault(require("./util/requestWrapper"));

@@ -24,8 +25,9 @@ class RestClient {

* @param {string} secret - your API secret
* @param {boolean} [livenet=false]
* @param {boolean} [useLivenet=false]
* @param {RestClientInverseOptions} [restInverseOptions={}] options to configure REST API connectivity
* @param {AxiosRequestConfig} [requestOptions={}] HTTP networking options for axios
*/
constructor(key, secret, livenet, restInverseOptions = {}, requestOptions = {}) {
this.requestWrapper = new requestWrapper_1.default(key, secret, livenet, restInverseOptions, requestOptions);
constructor(key, secret, useLivenet, restInverseOptions = {}, httpOptions = {}) {
this.requestWrapper = new requestWrapper_1.default(key, secret, requestUtils_1.getBaseRESTInverseUrl(useLivenet), restInverseOptions, httpOptions);
return this;
}

@@ -91,5 +93,5 @@ /**

placeActiveOrder(orderRequest) {
if (orderRequest.order_type === 'Limit' && !orderRequest.price) {
throw new Error('Price required for limit orders');
}
// if (orderRequest.order_type === 'Limit' && !orderRequest.price) {
// throw new Error('Price required for limit orders');
// }
return this.requestWrapper.post('v2/private/order/create', orderRequest);

@@ -107,5 +109,5 @@ }

cancelActiveOrder(params) {
if (!params.order_id && !params.order_link_id) {
throw new Error('Parameter order_id OR order_link_id is required');
}
// if (!params.order_id && !params.order_link_id) {
// throw new Error('Parameter order_id OR order_link_id is required');
// }
return this.requestWrapper.post('v2/private/order/cancel', params);

@@ -117,5 +119,5 @@ }

replaceActiveOrder(params) {
if (!params.order_id && !params.order_link_id) {
throw new Error('Parameter order_id OR order_link_id is required');
}
// if (!params.order_id && !params.order_link_id) {
// throw new Error('Parameter order_id OR order_link_id is required');
// }
return this.requestWrapper.post('v2/private/order/replace', params);

@@ -127,17 +129,17 @@ }

replaceActiveOrderOld(params) {
if (!params.order_id && !params.order_link_id) {
throw new Error('Parameter order_id OR order_link_id is required');
}
// if (!params.order_id && !params.order_link_id) {
// throw new Error('Parameter order_id OR order_link_id is required');
// }
return this.requestWrapper.post('open-api/order/replace', params);
}
queryActiveOrder(params) {
if (!params.order_id && !params.order_link_id) {
throw new Error('Parameter order_id OR order_link_id is required');
}
// if (!params.order_id && !params.order_link_id) {
// throw new Error('Parameter order_id OR order_link_id is required');
// }
return this.requestWrapper.get('v2/private/order', params);
}
placeConditionalOrder(params) {
if (params.order_type === 'Limit' && !params.price) {
throw new Error('Parameter price is required for limit orders');
}
// if (params.order_type === 'Limit' && !params.price) {
// throw new Error('Parameter price is required for limit orders');
// }
return this.requestWrapper.post('v2/private/stop-order/create', params);

@@ -149,5 +151,5 @@ }

placeConditionalOrderOld(params) {
if (params.order_type === 'Limit' && !params.price) {
throw new Error('Parameter price is required for limit orders');
}
// if (params.order_type === 'Limit' && !params.price) {
// throw new Error('Parameter price is required for limit orders');
// }
return this.requestWrapper.post('open-api/stop-order/create', params);

@@ -165,5 +167,5 @@ }

cancelConditionalOrder(params) {
if (!params.stop_order_id && !params.order_link_id) {
throw new Error('Parameter stop_order_id OR order_link_id is required');
}
// if (!params.stop_order_id && !params.order_link_id) {
// throw new Error('Parameter stop_order_id OR order_link_id is required');
// }
return this.requestWrapper.post('v2/private/stop-order/cancel', params);

@@ -175,5 +177,5 @@ }

cancelConditionalOrderOld(params) {
if (!params.stop_order_id && !params.order_link_id) {
throw new Error('Parameter stop_order_id OR order_link_id is required');
}
// if (!params.stop_order_id && !params.order_link_id) {
// throw new Error('Parameter stop_order_id OR order_link_id is required');
// }
return this.requestWrapper.post('open-api/stop-order/cancel', params);

@@ -185,5 +187,5 @@ }

replaceConditionalOrder(params) {
if (!params.stop_order_id && !params.order_link_id) {
throw new Error('Parameter stop_order_id OR order_link_id is required');
}
// if (!params.stop_order_id && !params.order_link_id) {
// throw new Error('Parameter stop_order_id OR order_link_id is required');
// }
return this.requestWrapper.post('v2/private/stop-order/replace', params);

@@ -198,5 +200,5 @@ }

queryConditionalOrder(params) {
if (!params.stop_order_id && !params.order_link_id) {
throw new Error('Parameter stop_order_id OR order_link_id is required');
}
// if (!params.stop_order_id && !params.order_link_id) {
// throw new Error('Parameter stop_order_id OR order_link_id is required');
// }
return this.requestWrapper.get('v2/private/stop-order', params);

@@ -203,0 +205,0 @@ }

export interface RestClientInverseOptions {
recv_window?: number;
sync_interval_ms?: number | string;
disable_time_sync?: boolean;
strict_param_validation?: boolean;
baseUrl?: string;
parse_exceptions?: boolean;
}

@@ -10,1 +12,3 @@ export declare type GenericAPIResponse = Promise<any>;

export declare function serializeParams(params?: object, strict_validation?: boolean): string;
export declare function getBaseRESTInverseUrl(useLivenet?: boolean, restInverseOptions?: RestClientInverseOptions): string;
//# sourceMappingURL=requestUtils.d.ts.map
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.serializeParams = exports.signMessage = void 0;
exports.getBaseRESTInverseUrl = exports.serializeParams = exports.signMessage = void 0;
const crypto_1 = require("crypto");

@@ -26,2 +26,16 @@ function signMessage(message, secret) {

;
function getBaseRESTInverseUrl(useLivenet, restInverseOptions) {
const baseUrlsInverse = {
livenet: 'https://api.bybit.com',
testnet: 'https://api-testnet.bybit.com'
};
if (restInverseOptions === null || restInverseOptions === void 0 ? void 0 : restInverseOptions.baseUrl) {
return restInverseOptions.baseUrl;
}
if (useLivenet === true) {
return baseUrlsInverse.livenet;
}
return baseUrlsInverse.testnet;
}
exports.getBaseRESTInverseUrl = getBaseRESTInverseUrl;
//# sourceMappingURL=requestUtils.js.map

@@ -11,8 +11,22 @@ import { AxiosRequestConfig, Method } from 'axios';

private secret;
constructor(key: string | undefined, secret: string | undefined, livenet?: boolean, options?: RestClientInverseOptions, requestOptions?: AxiosRequestConfig);
constructor(key: string | undefined, secret: string | undefined, baseUrl: string, options?: RestClientInverseOptions, requestOptions?: AxiosRequestConfig);
get(endpoint: string, params?: any): GenericAPIResponse;
post(endpoint: string, params?: any): GenericAPIResponse;
/**
* @private Make a HTTP request to a specific endpoint. Private endpoints are automatically signed.
*/
_call(method: Method, endpoint: string, params?: any): GenericAPIResponse;
_signRequest(data: any): object;
/**
* @private generic handler to parse request exceptions
*/
parseException(e: any): unknown;
/**
* @private sign request and set recv window
*/
signRequest(data: any): any;
/**
* @private trigger time sync and store promise
*/
syncTime(): GenericAPIResponse;
/**
* @deprecated move this somewhere else, because v2/public/time shouldn't be hardcoded here

@@ -24,3 +38,3 @@ *

getTimeOffset(): Promise<number>;
_syncTime(): GenericAPIResponse;
}
//# sourceMappingURL=requestWrapper.d.ts.map

@@ -17,8 +17,4 @@ "use strict";

const requestUtils_1 = require("./requestUtils");
const baseUrls = {
livenet: 'https://api.bybit.com',
testnet: 'https://api-testnet.bybit.com'
};
class RequestUtil {
constructor(key, secret, livenet = false, options = {}, requestOptions = {}) {
constructor(key, secret, baseUrl, options = {}, requestOptions = {}) {
this.timeOffset = null;

@@ -31,17 +27,15 @@ this.syncTimePromise = null;

strict_param_validation: false }, options);
this.baseUrl = baseUrls[livenet === true ? 'livenet' : 'testnet'];
if (options.baseUrl) {
this.baseUrl = options.baseUrl;
}
this.globalRequestOptions = Object.assign(Object.assign({
// in ms == 5 minutes by default
timeout: 1000 * 60 * 5 }, requestOptions), { headers: {
'referer': 'bybitapinode',
'x-referer': 'bybitapinode'
} });
this.baseUrl = baseUrl;
if (key && !secret) {
throw new Error('API Key & Secret are both required for private enpoints');
}
this._syncTime();
setInterval(this._syncTime.bind(this), +this.options.sync_interval_ms);
if (this.options.disable_time_sync !== true) {
this.syncTime();
setInterval(this.syncTime.bind(this), +this.options.sync_interval_ms);
}
this.key = key;

@@ -57,2 +51,5 @@ this.secret = secret;

}
/**
* @private Make a HTTP request to a specific endpoint. Private endpoints are automatically signed.
*/
_call(method, endpoint, params) {

@@ -66,15 +63,13 @@ return __awaiter(this, void 0, void 0, function* () {

if (this.timeOffset === null) {
yield this._syncTime();
yield this.syncTime();
}
params = this._signRequest(params);
params = this.signRequest(params);
}
const options = Object.assign(Object.assign({}, this.globalRequestOptions), { url: [this.baseUrl, endpoint].join('/'), method: method, json: true });
switch (method) {
case 'GET':
options.params = params;
break;
default:
options.data = params;
break;
if (method === 'GET') {
options.params = params;
}
else {
options.data = params;
}
return axios_1.default(options).then(response => {

@@ -84,31 +79,36 @@ if (response.status == 200) {

}
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
};
});
throw response;
}).catch(this.parseException);
});
}
_signRequest(data) {
/**
* @private generic handler to parse request exceptions
*/
parseException(e) {
if (this.options.parse_exceptions === false) {
throw e;
}
// Something happened in setting up the request that triggered an Error
if (!e.response) {
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
const response = e.response;
throw {
code: response.status,
message: response.statusText,
body: response.data,
headers: response.headers,
requestOptions: this.options
};
}
/**
* @private sign request and set recv window
*/
signRequest(data) {
const params = Object.assign(Object.assign({}, data), { api_key: this.key, timestamp: Date.now() + (this.timeOffset || 0) });

@@ -126,2 +126,18 @@ // Optional, set to 5000 by default. Increase if timestamp/recv_window errors are seen.

/**
* @private trigger time sync and store promise
*/
syncTime() {
if (this.options.disable_time_sync === true) {
return Promise.resolve(false);
}
if (this.syncTimePromise !== null) {
return this.syncTimePromise;
}
this.syncTimePromise = this.getTimeOffset().then(offset => {
this.timeOffset = offset;
this.syncTimePromise = null;
});
return this.syncTimePromise;
}
/**
* @deprecated move this somewhere else, because v2/public/time shouldn't be hardcoded here

@@ -140,12 +156,2 @@ *

}
_syncTime() {
if (this.syncTimePromise !== null) {
return this.syncTimePromise;
}
this.syncTimePromise = this.getTimeOffset().then(offset => {
this.timeOffset = offset;
this.syncTimePromise = null;
});
return this.syncTimePromise;
}
}

@@ -152,0 +158,0 @@ exports.default = RequestUtil;

@@ -45,1 +45,2 @@ /// <reference types="node" />

export {};
//# sourceMappingURL=websocket-client.d.ts.map
{
"name": "bybit-api",
"version": "1.3.0-beta.0",
"version": "1.3.0-beta.1",
"description": "A production-ready Node.js connector for the Bybit APIs and WebSockets",
"main": "lib/index.js",
"browser": "dist/index.js",
"types": "lib/index.d.ts",

@@ -19,8 +20,5 @@ "files": [

"prepush": "npm run build",
"prepublish": "npm run build"
"prepublish": "npm run build && npm run pack",
"betapublish": "npm publish --tag beta"
},
"repository": {
"type": "git",
"url": "https://github.com/tiagosiebler/bybit-api"
},
"author": "Tiago Siebler (https://github.com/tiagosiebler)",

@@ -67,2 +65,6 @@ "contributors": [

"license": "MIT",
"repository": {
"type": "git",
"url": "https://github.com/tiagosiebler/bybit-api"
},
"bugs": {

@@ -69,0 +71,0 @@ "url": "https://github.com/tiagosiebler/bybit-api/issues"

@@ -43,3 +43,3 @@ # bybit-api

const restInverseOptions = {
// how much time to allow for valid request
// override the max size of the request window (in ms)
recv_window?: number;

@@ -50,3 +50,6 @@

// if true, we'll throw errors if any params are undefined
// Default: false. Disable above sync mechanism if true.
disable_time_sync?: boolean;
// Default: false. If true, we'll throw errors if any params are undefined
strict_param_validation?: boolean;

@@ -57,2 +60,5 @@

baseUrl?: string;
// Default: true. whether to try and post-process request exceptions.
parse_exceptions?: boolean;
};

@@ -69,3 +75,3 @@

// requestLibraryOptions
)
);

@@ -72,0 +78,0 @@ client.changeUserLeverage({leverage: 4, symbol: 'ETHUSD'})

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

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