deprecate redundant repeated constructors

This commit is contained in:
tiagosiebler
2022-09-08 14:31:03 +01:00
parent 88bd4fd9af
commit 666720b27d
7 changed files with 56 additions and 196 deletions

View File

@@ -1,4 +1,3 @@
import { AxiosRequestConfig } from 'axios';
import { import {
AccountAssetInformationRequest, AccountAssetInformationRequest,
APIResponseWithTime, APIResponseWithTime,
@@ -12,39 +11,15 @@ import {
WithdrawalRecordsRequest, WithdrawalRecordsRequest,
WithdrawalRequest, WithdrawalRequest,
} from './types'; } from './types';
import { import { REST_CLIENT_TYPE_ENUM } from './util';
RestClientOptions,
getRestBaseUrl,
REST_CLIENT_TYPE_ENUM,
} from './util';
import BaseRestClient from './util/BaseRestClient'; import BaseRestClient from './util/BaseRestClient';
/**
* REST API client for Account Asset APIs
*/
export class AccountAssetClient extends BaseRestClient { export class AccountAssetClient extends BaseRestClient {
/** getClientType() {
* @public Creates an instance of the Account Asset REST API client. return REST_CLIENT_TYPE_ENUM.accountAsset;
*
* @param {string} key - your API key
* @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
*/
constructor(
key?: string | undefined,
secret?: string | undefined,
useLivenet: boolean = false,
restClientOptions: RestClientOptions = {},
requestOptions: AxiosRequestConfig = {}
) {
super(
key,
secret,
getRestBaseUrl(useLivenet, restClientOptions),
restClientOptions,
requestOptions,
REST_CLIENT_TYPE_ENUM.accountAsset
);
return this;
} }
async fetchServerTime(): Promise<number> { async fetchServerTime(): Promise<number> {

View File

@@ -1,9 +1,4 @@
import { AxiosRequestConfig } from 'axios'; import { REST_CLIENT_TYPE_ENUM } from './util';
import {
getRestBaseUrl,
RestClientOptions,
REST_CLIENT_TYPE_ENUM,
} from './util';
import { import {
APIResponseWithTime, APIResponseWithTime,
AssetExchangeRecordsReq, AssetExchangeRecordsReq,
@@ -18,32 +13,12 @@ import {
} from './types'; } from './types';
import BaseRestClient from './util/BaseRestClient'; import BaseRestClient from './util/BaseRestClient';
/**
* REST API client for Inverse Perpetual Futures APIs (v2)
*/
export class InverseClient extends BaseRestClient { export class InverseClient extends BaseRestClient {
/** getClientType() {
* @public Creates an instance of the inverse REST API client. return REST_CLIENT_TYPE_ENUM.inverse;
*
* @param {string} key - your API key
* @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
*/
constructor(
key?: string | undefined,
secret?: string | undefined,
useLivenet: boolean = false,
restClientOptions: RestClientOptions = {},
requestOptions: AxiosRequestConfig = {}
) {
super(
key,
secret,
getRestBaseUrl(useLivenet, restClientOptions),
restClientOptions,
requestOptions,
REST_CLIENT_TYPE_ENUM.inverse
);
return this;
} }
async fetchServerTime(): Promise<number> { async fetchServerTime(): Promise<number> {

View File

@@ -1,9 +1,4 @@
import { AxiosRequestConfig } from 'axios'; import { REST_CLIENT_TYPE_ENUM } from './util/requestUtils';
import {
getRestBaseUrl,
RestClientOptions,
REST_CLIENT_TYPE_ENUM,
} from './util/requestUtils';
import { import {
APIResponseWithTime, APIResponseWithTime,
AssetExchangeRecordsReq, AssetExchangeRecordsReq,
@@ -18,32 +13,12 @@ import {
} from './types/shared'; } from './types/shared';
import BaseRestClient from './util/BaseRestClient'; import BaseRestClient from './util/BaseRestClient';
/**
* REST API client for Inverse Futures APIs (e.g. quarterly futures) (v2)
*/
export class InverseFuturesClient extends BaseRestClient { export class InverseFuturesClient extends BaseRestClient {
/** getClientType() {
* @public Creates an instance of the inverse futures REST API client. return REST_CLIENT_TYPE_ENUM.inverseFutures;
*
* @param {string} key - your API key
* @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
*/
constructor(
key?: string | undefined,
secret?: string | undefined,
useLivenet: boolean = false,
restClientOptions: RestClientOptions = {},
requestOptions: AxiosRequestConfig = {}
) {
super(
key,
secret,
getRestBaseUrl(useLivenet, restClientOptions),
restClientOptions,
requestOptions,
REST_CLIENT_TYPE_ENUM.inverseFutures
);
return this;
} }
async fetchServerTime(): Promise<number> { async fetchServerTime(): Promise<number> {

View File

@@ -1,9 +1,4 @@
import { AxiosRequestConfig } from 'axios'; import { REST_CLIENT_TYPE_ENUM } from './util/requestUtils';
import {
getRestBaseUrl,
RestClientOptions,
REST_CLIENT_TYPE_ENUM,
} from './util/requestUtils';
import { import {
APIResponse, APIResponse,
APIResponseWithTime, APIResponseWithTime,
@@ -25,32 +20,12 @@ import {
import { linearPositionModeEnum, positionTpSlModeEnum } from './constants/enum'; import { linearPositionModeEnum, positionTpSlModeEnum } from './constants/enum';
import BaseRestClient from './util/BaseRestClient'; import BaseRestClient from './util/BaseRestClient';
/**
* REST API client for linear/USD perpetual futures APIs (v2)
*/
export class LinearClient extends BaseRestClient { export class LinearClient extends BaseRestClient {
/** getClientType() {
* @public Creates an instance of the linear (USD Perps) REST API client. return REST_CLIENT_TYPE_ENUM.linear;
*
* @param {string} key - your API key
* @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
*/
constructor(
key?: string | undefined,
secret?: string | undefined,
useLivenet: boolean = false,
restClientOptions: RestClientOptions = {},
requestOptions: AxiosRequestConfig = {}
) {
super(
key,
secret,
getRestBaseUrl(useLivenet, restClientOptions),
restClientOptions,
requestOptions,
REST_CLIENT_TYPE_ENUM.linear
);
return this;
} }
async fetchServerTime(): Promise<number> { async fetchServerTime(): Promise<number> {

View File

@@ -1,4 +1,3 @@
import { AxiosRequestConfig } from 'axios';
import { import {
NewSpotOrder, NewSpotOrder,
APIResponse, APIResponse,
@@ -11,40 +10,14 @@ import {
SpotSymbolInfo, SpotSymbolInfo,
} from './types'; } from './types';
import BaseRestClient from './util/BaseRestClient'; import BaseRestClient from './util/BaseRestClient';
import { import { agentSource, REST_CLIENT_TYPE_ENUM } from './util/requestUtils';
agentSource,
getRestBaseUrl,
RestClientOptions,
REST_CLIENT_TYPE_ENUM,
} from './util/requestUtils';
/**
* REST API client for Spot APIs (v1)
*/
export class SpotClient extends BaseRestClient { export class SpotClient extends BaseRestClient {
/** getClientType() {
* @public Creates an instance of the Spot REST API client. return REST_CLIENT_TYPE_ENUM.spot;
*
* @param {string} key - your API key
* @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
*/
constructor(
key?: string | undefined,
secret?: string | undefined,
useLivenet: boolean = false,
restClientOptions: RestClientOptions = {},
requestOptions: AxiosRequestConfig = {}
) {
super(
key,
secret,
getRestBaseUrl(useLivenet, restClientOptions),
restClientOptions,
requestOptions,
REST_CLIENT_TYPE_ENUM.spot
);
return this;
} }
fetchServerTime(): Promise<number> { fetchServerTime(): Promise<number> {

View File

@@ -1,38 +1,13 @@
import { AxiosRequestConfig } from 'axios';
import { APIResponseWithTime } from './types'; import { APIResponseWithTime } from './types';
import { import { REST_CLIENT_TYPE_ENUM } from './util';
RestClientOptions,
getRestBaseUrl,
REST_CLIENT_TYPE_ENUM,
} from './util';
import BaseRestClient from './util/BaseRestClient'; import BaseRestClient from './util/BaseRestClient';
/**
* REST API client for USDC Options APIs
*/
export class USDCOptionsClient extends BaseRestClient { export class USDCOptionsClient extends BaseRestClient {
/** getClientType() {
* @public Creates an instance of the USDC Options REST API client. return REST_CLIENT_TYPE_ENUM.usdcOptions;
*
* @param {string} key - your API key
* @param {string} secret - your API secret
* @param {boolean} [useLivenet=false] uses testnet by default
* @param {RestClientOptions} [restClientOptions={}] options to configure REST API connectivity
* @param {AxiosRequestConfig} [requestOptions={}] HTTP networking options for axios
*/
constructor(
key?: string | undefined,
secret?: string | undefined,
useLivenet: boolean = false,
restClientOptions: RestClientOptions = {},
requestOptions: AxiosRequestConfig = {}
) {
super(
key,
secret,
getRestBaseUrl(useLivenet, restClientOptions),
restClientOptions,
requestOptions,
REST_CLIENT_TYPE_ENUM.usdcOptions
);
return this;
} }
async fetchServerTime(): Promise<number> { async fetchServerTime(): Promise<number> {

View File

@@ -7,6 +7,7 @@ import {
RestClientType, RestClientType,
REST_CLIENT_TYPE_ENUM, REST_CLIENT_TYPE_ENUM,
agentSource, agentSource,
getRestBaseUrl,
} from './requestUtils'; } from './requestUtils';
// axios.interceptors.request.use((request) => { // axios.interceptors.request.use((request) => {
@@ -53,21 +54,32 @@ export default abstract class BaseRestClient {
private secret: string | undefined; private secret: string | undefined;
private clientType: RestClientType; private clientType: RestClientType;
/** Function that calls exchange API to query & resolve server time, used by time sync */ /** Function that calls exchange API to query & resolve server time, used by time sync, disabled by default */
abstract fetchServerTime(): Promise<number>; abstract fetchServerTime(): Promise<number>;
/** Defines the client type (affecting how requests & signatures behave) */
abstract getClientType(): RestClientType;
/**
* Create an instance of the REST client
* @param {string} key - your API key
* @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
*/
constructor( constructor(
key: string | undefined, key?: string | undefined,
secret: string | undefined, secret?: string | undefined,
baseUrl: string, useLivenet: boolean = false,
options: RestClientOptions = {}, options: RestClientOptions = {},
requestOptions: AxiosRequestConfig = {}, requestOptions: AxiosRequestConfig = {}
clientType: RestClientType
) { ) {
const baseUrl = getRestBaseUrl(useLivenet, options);
this.timeOffset = null; this.timeOffset = null;
this.syncTimePromise = null; this.syncTimePromise = null;
this.clientType = clientType; this.clientType = this.getClientType();
this.options = { this.options = {
recv_window: 5000, recv_window: 5000,
@@ -86,7 +98,7 @@ export default abstract class BaseRestClient {
// custom request options based on axios specs - see: https://github.com/axios/axios#request-config // custom request options based on axios specs - see: https://github.com/axios/axios#request-config
...requestOptions, ...requestOptions,
headers: { headers: {
'x-referer': 'bybitapinode', 'x-referer': agentSource,
}, },
}; };