refactor tests for new constructor pattern
This commit is contained in:
@@ -24,7 +24,7 @@ interface SignedRequestContext {
|
||||
timestamp?: number;
|
||||
api_key?: string;
|
||||
recv_window?: number;
|
||||
// spot is diff from the rest...
|
||||
// spot v1 is diff from the rest...
|
||||
recvWindow?: number;
|
||||
}
|
||||
|
||||
@@ -45,8 +45,8 @@ interface UnsignedRequest<T> {
|
||||
type SignMethod = 'keyInBody' | 'usdc';
|
||||
|
||||
export default abstract class BaseRestClient {
|
||||
private timeOffset: number | null;
|
||||
private syncTimePromise: null | Promise<any>;
|
||||
private timeOffset: number | null = null;
|
||||
private syncTimePromise: null | Promise<any> = null;
|
||||
private options: RestClientOptions;
|
||||
private baseUrl: string;
|
||||
private globalRequestOptions: AxiosRequestConfig;
|
||||
@@ -66,19 +66,12 @@ export default abstract class BaseRestClient {
|
||||
* @param {string} secret - your API secret
|
||||
* @param {boolean} [useLivenet=false]
|
||||
* @param {RestClientOptions} [restClientOptions={}] options to configure REST API connectivity
|
||||
* @param {AxiosRequestConfig} [requestOptions={}] HTTP networking options for axios
|
||||
* @param {AxiosRequestConfig} [networkOptions={}] HTTP networking options for axios
|
||||
*/
|
||||
constructor(
|
||||
key?: string | undefined,
|
||||
secret?: string | undefined,
|
||||
useLivenet: boolean = false,
|
||||
options: RestClientOptions = {},
|
||||
requestOptions: AxiosRequestConfig = {}
|
||||
restOptions: RestClientOptions = {},
|
||||
networkOptions: AxiosRequestConfig = {}
|
||||
) {
|
||||
const baseUrl = getRestBaseUrl(useLivenet, options);
|
||||
this.timeOffset = null;
|
||||
this.syncTimePromise = null;
|
||||
|
||||
this.clientType = this.getClientType();
|
||||
|
||||
this.options = {
|
||||
@@ -89,24 +82,26 @@ export default abstract class BaseRestClient {
|
||||
enable_time_sync: false,
|
||||
/** How often to sync time drift with bybit servers (if time sync is enabled) */
|
||||
sync_interval_ms: 3600000,
|
||||
...options,
|
||||
...restOptions,
|
||||
};
|
||||
|
||||
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,
|
||||
...networkOptions,
|
||||
headers: {
|
||||
'x-referer': APIID,
|
||||
},
|
||||
};
|
||||
|
||||
this.baseUrl = baseUrl;
|
||||
this.baseUrl = getRestBaseUrl(!!this.options.testnet, restOptions);
|
||||
this.key = this.options.key;
|
||||
this.secret = this.options.secret;
|
||||
|
||||
if (key && !secret) {
|
||||
if (this.key && !this.secret) {
|
||||
throw new Error(
|
||||
'API Key & Secret are both required for private enpoints'
|
||||
'API Key & Secret are both required for private endpoints'
|
||||
);
|
||||
}
|
||||
|
||||
@@ -114,9 +109,6 @@ export default abstract class BaseRestClient {
|
||||
this.syncTime();
|
||||
setInterval(this.syncTime.bind(this), +this.options.sync_interval_ms!);
|
||||
}
|
||||
|
||||
this.key = key;
|
||||
this.secret = secret;
|
||||
}
|
||||
|
||||
private isSpotClient() {
|
||||
|
||||
@@ -1,4 +1,13 @@
|
||||
export interface RestClientOptions {
|
||||
/** Your API key */
|
||||
key?: string;
|
||||
|
||||
/** Your API secret */
|
||||
secret?: string;
|
||||
|
||||
/** Set to `true` to connect to testnet. Uses the live environment by default. */
|
||||
testnet?: boolean;
|
||||
|
||||
/** Override the max size of the request window (in ms) */
|
||||
recv_window?: number;
|
||||
|
||||
@@ -43,7 +52,7 @@ export function serializeParams(
|
||||
}
|
||||
|
||||
export function getRestBaseUrl(
|
||||
useLivenet: boolean,
|
||||
useTestnet: boolean,
|
||||
restInverseOptions: RestClientOptions
|
||||
): string {
|
||||
const exchangeBaseUrls = {
|
||||
@@ -55,10 +64,11 @@ export function getRestBaseUrl(
|
||||
return restInverseOptions.baseUrl;
|
||||
}
|
||||
|
||||
if (useLivenet === true) {
|
||||
return exchangeBaseUrls.livenet;
|
||||
if (useTestnet) {
|
||||
return exchangeBaseUrls.testnet;
|
||||
}
|
||||
return exchangeBaseUrls.testnet;
|
||||
|
||||
return exchangeBaseUrls.livenet;
|
||||
}
|
||||
|
||||
export function isWsPong(msg: any): boolean {
|
||||
|
||||
@@ -47,12 +47,19 @@ export type WsClientEvent =
|
||||
| 'response';
|
||||
|
||||
interface WebsocketClientEvents {
|
||||
/** Connection opened. If this connection was previously opened and reconnected, expect the reconnected event instead */
|
||||
open: (evt: { wsKey: WsKey; event: any }) => void;
|
||||
/** Reconnecting a dropped connection */
|
||||
reconnect: (evt: { wsKey: WsKey; event: any }) => void;
|
||||
/** Successfully reconnected a connection that dropped */
|
||||
reconnected: (evt: { wsKey: WsKey; event: any }) => void;
|
||||
/** Connection closed */
|
||||
close: (evt: { wsKey: WsKey; event: any }) => void;
|
||||
/** Received reply to websocket command (e.g. after subscribing to topics) */
|
||||
response: (response: any) => void;
|
||||
/** Received data for topic */
|
||||
update: (response: any) => void;
|
||||
/** Exception from ws client OR custom listeners */
|
||||
error: (response: any) => void;
|
||||
}
|
||||
|
||||
@@ -92,6 +99,10 @@ export class WebsocketClient extends EventEmitter {
|
||||
fetchTimeOffsetBeforeAuth: false,
|
||||
...options,
|
||||
};
|
||||
this.options.restOptions = {
|
||||
...this.options.restOptions,
|
||||
testnet: this.options.testnet,
|
||||
};
|
||||
|
||||
this.prepareRESTClient();
|
||||
}
|
||||
@@ -105,9 +116,6 @@ export class WebsocketClient extends EventEmitter {
|
||||
switch (this.options.market) {
|
||||
case 'inverse': {
|
||||
this.restClient = new InverseClient(
|
||||
undefined,
|
||||
undefined,
|
||||
!this.isTestnet(),
|
||||
this.options.restOptions,
|
||||
this.options.requestOptions
|
||||
);
|
||||
@@ -115,9 +123,6 @@ export class WebsocketClient extends EventEmitter {
|
||||
}
|
||||
case 'linear': {
|
||||
this.restClient = new LinearClient(
|
||||
undefined,
|
||||
undefined,
|
||||
!this.isTestnet(),
|
||||
this.options.restOptions,
|
||||
this.options.requestOptions
|
||||
);
|
||||
@@ -125,9 +130,6 @@ export class WebsocketClient extends EventEmitter {
|
||||
}
|
||||
case 'spot': {
|
||||
this.restClient = new SpotClient(
|
||||
undefined,
|
||||
undefined,
|
||||
!this.isTestnet(),
|
||||
this.options.restOptions,
|
||||
this.options.requestOptions
|
||||
);
|
||||
@@ -136,9 +138,6 @@ export class WebsocketClient extends EventEmitter {
|
||||
}
|
||||
case 'spotv3': {
|
||||
this.restClient = new SpotClientV3(
|
||||
undefined,
|
||||
undefined,
|
||||
!this.isTestnet(),
|
||||
this.options.restOptions,
|
||||
this.options.requestOptions
|
||||
);
|
||||
@@ -146,9 +145,6 @@ export class WebsocketClient extends EventEmitter {
|
||||
}
|
||||
case 'usdcOption': {
|
||||
this.restClient = new USDCOptionClient(
|
||||
undefined,
|
||||
undefined,
|
||||
!this.isTestnet(),
|
||||
this.options.restOptions,
|
||||
this.options.requestOptions
|
||||
);
|
||||
@@ -156,9 +152,6 @@ export class WebsocketClient extends EventEmitter {
|
||||
}
|
||||
case 'usdcPerp': {
|
||||
this.restClient = new USDCPerpetualClient(
|
||||
undefined,
|
||||
undefined,
|
||||
!this.isTestnet(),
|
||||
this.options.restOptions,
|
||||
this.options.requestOptions
|
||||
);
|
||||
@@ -167,9 +160,6 @@ export class WebsocketClient extends EventEmitter {
|
||||
case 'unifiedOption':
|
||||
case 'unifiedPerp': {
|
||||
this.restClient = new UnifiedMarginClient(
|
||||
undefined,
|
||||
undefined,
|
||||
!this.isTestnet(),
|
||||
this.options.restOptions,
|
||||
this.options.requestOptions
|
||||
);
|
||||
|
||||
Reference in New Issue
Block a user