initial commit, add bitget rest api and websockets connector
This commit is contained in:
92
src/util/requestUtils.ts
Normal file
92
src/util/requestUtils.ts
Normal file
@@ -0,0 +1,92 @@
|
||||
export interface RestClientOptions {
|
||||
/** Your API key */
|
||||
apiKey?: string;
|
||||
|
||||
/** Your API secret */
|
||||
apiSecret?: string;
|
||||
|
||||
/** The passphrase you set when creating the API Key (NOT your account password) */
|
||||
apiPass?: 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) */
|
||||
recvWindow?: number;
|
||||
|
||||
/** Default: false. If true, we'll throw errors if any params are undefined */
|
||||
strictParamValidation?: boolean;
|
||||
|
||||
/**
|
||||
* Optionally override API protocol + domain
|
||||
* e.g baseUrl: 'https://api.bitget.com'
|
||||
**/
|
||||
baseUrl?: string;
|
||||
|
||||
/** Default: true. whether to try and post-process request exceptions (and throw them). */
|
||||
parseExceptions?: boolean;
|
||||
}
|
||||
|
||||
export function serializeParams<T extends object | undefined = {}>(
|
||||
params: T,
|
||||
strict_validation = false,
|
||||
prefixWith: string = ''
|
||||
): string {
|
||||
if (!params) {
|
||||
return '';
|
||||
}
|
||||
|
||||
const queryString = 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('&');
|
||||
|
||||
// Only prefix if there's a value
|
||||
return queryString ? prefixWith + queryString : queryString;
|
||||
}
|
||||
|
||||
export function getRestBaseUrl(
|
||||
useTestnet: boolean,
|
||||
restInverseOptions: RestClientOptions
|
||||
): string {
|
||||
const exchangeBaseUrls = {
|
||||
livenet: 'https://api.bitget.com',
|
||||
livenet2: 'https://capi.bitget.com',
|
||||
testnet: 'https://noTestnet',
|
||||
};
|
||||
|
||||
if (restInverseOptions.baseUrl) {
|
||||
return restInverseOptions.baseUrl;
|
||||
}
|
||||
|
||||
if (useTestnet) {
|
||||
return exchangeBaseUrls.testnet;
|
||||
}
|
||||
|
||||
return exchangeBaseUrls.livenet;
|
||||
}
|
||||
|
||||
export function isWsPong(msg: any): boolean {
|
||||
// bitget
|
||||
if (msg?.data === 'pong') {
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* Used to switch how authentication/requests work under the hood (primarily for SPOT since it's different there)
|
||||
*/
|
||||
export const REST_CLIENT_TYPE_ENUM = {
|
||||
spot: 'spot',
|
||||
futures: 'futures',
|
||||
broker: 'broker',
|
||||
} as const;
|
||||
Reference in New Issue
Block a user