diff --git a/src/account-asset-client.ts b/src/account-asset-client.ts index f7330d7..0e2963d 100644 --- a/src/account-asset-client.ts +++ b/src/account-asset-client.ts @@ -1,4 +1,3 @@ -import { AxiosRequestConfig } from 'axios'; import { AccountAssetInformationRequest, APIResponseWithTime, @@ -12,39 +11,15 @@ import { WithdrawalRecordsRequest, WithdrawalRequest, } from './types'; -import { - RestClientOptions, - getRestBaseUrl, - REST_CLIENT_TYPE_ENUM, -} from './util'; +import { REST_CLIENT_TYPE_ENUM } from './util'; import BaseRestClient from './util/BaseRestClient'; +/** + * REST API client for Account Asset APIs + */ export class AccountAssetClient extends BaseRestClient { - /** - * @public Creates an instance of the Account Asset REST API 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( - 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; + getClientType() { + return REST_CLIENT_TYPE_ENUM.accountAsset; } async fetchServerTime(): Promise { diff --git a/src/inverse-client.ts b/src/inverse-client.ts index 491bbc3..1d3354f 100644 --- a/src/inverse-client.ts +++ b/src/inverse-client.ts @@ -1,9 +1,4 @@ -import { AxiosRequestConfig } from 'axios'; -import { - getRestBaseUrl, - RestClientOptions, - REST_CLIENT_TYPE_ENUM, -} from './util'; +import { REST_CLIENT_TYPE_ENUM } from './util'; import { APIResponseWithTime, AssetExchangeRecordsReq, @@ -18,32 +13,12 @@ import { } from './types'; import BaseRestClient from './util/BaseRestClient'; +/** + * REST API client for Inverse Perpetual Futures APIs (v2) + */ export class InverseClient extends BaseRestClient { - /** - * @public Creates an instance of the inverse REST API 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( - 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; + getClientType() { + return REST_CLIENT_TYPE_ENUM.inverse; } async fetchServerTime(): Promise { diff --git a/src/inverse-futures-client.ts b/src/inverse-futures-client.ts index 9892dc7..7411522 100644 --- a/src/inverse-futures-client.ts +++ b/src/inverse-futures-client.ts @@ -1,9 +1,4 @@ -import { AxiosRequestConfig } from 'axios'; -import { - getRestBaseUrl, - RestClientOptions, - REST_CLIENT_TYPE_ENUM, -} from './util/requestUtils'; +import { REST_CLIENT_TYPE_ENUM } from './util/requestUtils'; import { APIResponseWithTime, AssetExchangeRecordsReq, @@ -18,32 +13,12 @@ import { } from './types/shared'; import BaseRestClient from './util/BaseRestClient'; +/** + * REST API client for Inverse Futures APIs (e.g. quarterly futures) (v2) + */ export class InverseFuturesClient extends BaseRestClient { - /** - * @public Creates an instance of the inverse futures REST API 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( - 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; + getClientType() { + return REST_CLIENT_TYPE_ENUM.inverseFutures; } async fetchServerTime(): Promise { diff --git a/src/linear-client.ts b/src/linear-client.ts index 4a64ab3..32b6754 100644 --- a/src/linear-client.ts +++ b/src/linear-client.ts @@ -1,9 +1,4 @@ -import { AxiosRequestConfig } from 'axios'; -import { - getRestBaseUrl, - RestClientOptions, - REST_CLIENT_TYPE_ENUM, -} from './util/requestUtils'; +import { REST_CLIENT_TYPE_ENUM } from './util/requestUtils'; import { APIResponse, APIResponseWithTime, @@ -25,32 +20,12 @@ import { import { linearPositionModeEnum, positionTpSlModeEnum } from './constants/enum'; import BaseRestClient from './util/BaseRestClient'; +/** + * REST API client for linear/USD perpetual futures APIs (v2) + */ export class LinearClient extends BaseRestClient { - /** - * @public Creates an instance of the linear (USD Perps) REST API 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( - 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; + getClientType() { + return REST_CLIENT_TYPE_ENUM.linear; } async fetchServerTime(): Promise { diff --git a/src/spot-client.ts b/src/spot-client.ts index a31b467..7753949 100644 --- a/src/spot-client.ts +++ b/src/spot-client.ts @@ -1,4 +1,3 @@ -import { AxiosRequestConfig } from 'axios'; import { NewSpotOrder, APIResponse, @@ -11,40 +10,14 @@ import { SpotSymbolInfo, } from './types'; import BaseRestClient from './util/BaseRestClient'; -import { - agentSource, - getRestBaseUrl, - RestClientOptions, - REST_CLIENT_TYPE_ENUM, -} from './util/requestUtils'; +import { agentSource, REST_CLIENT_TYPE_ENUM } from './util/requestUtils'; +/** + * REST API client for Spot APIs (v1) + */ export class SpotClient extends BaseRestClient { - /** - * @public Creates an instance of the Spot REST API 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( - 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; + getClientType() { + return REST_CLIENT_TYPE_ENUM.spot; } fetchServerTime(): Promise { diff --git a/src/usdc-options-client.ts b/src/usdc-options-client.ts index edfa3df..0bb3f9a 100644 --- a/src/usdc-options-client.ts +++ b/src/usdc-options-client.ts @@ -1,38 +1,13 @@ -import { AxiosRequestConfig } from 'axios'; import { APIResponseWithTime } from './types'; -import { - RestClientOptions, - getRestBaseUrl, - REST_CLIENT_TYPE_ENUM, -} from './util'; +import { REST_CLIENT_TYPE_ENUM } from './util'; import BaseRestClient from './util/BaseRestClient'; +/** + * REST API client for USDC Options APIs + */ export class USDCOptionsClient extends BaseRestClient { - /** - * @public Creates an instance of the USDC Options REST API client. - * - * @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; + getClientType() { + return REST_CLIENT_TYPE_ENUM.usdcOptions; } async fetchServerTime(): Promise { diff --git a/src/util/BaseRestClient.ts b/src/util/BaseRestClient.ts index dd13600..850cbc5 100644 --- a/src/util/BaseRestClient.ts +++ b/src/util/BaseRestClient.ts @@ -7,6 +7,7 @@ import { RestClientType, REST_CLIENT_TYPE_ENUM, agentSource, + getRestBaseUrl, } from './requestUtils'; // axios.interceptors.request.use((request) => { @@ -53,21 +54,32 @@ export default abstract class BaseRestClient { private secret: string | undefined; 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; + /** 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( - key: string | undefined, - secret: string | undefined, - baseUrl: string, + key?: string | undefined, + secret?: string | undefined, + useLivenet: boolean = false, options: RestClientOptions = {}, - requestOptions: AxiosRequestConfig = {}, - clientType: RestClientType + requestOptions: AxiosRequestConfig = {} ) { + const baseUrl = getRestBaseUrl(useLivenet, options); this.timeOffset = null; this.syncTimePromise = null; - this.clientType = clientType; + this.clientType = this.getClientType(); this.options = { 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 ...requestOptions, headers: { - 'x-referer': 'bybitapinode', + 'x-referer': agentSource, }, };