kucoin-api
Version:
Complete & robust Node.js SDK for Kucoin's REST APIs and WebSockets, with TypeScript & strong end to end tests.
85 lines • 3.25 kB
JavaScript
import { neverGuard } from './misc-util.js';
/**
* Used to switch how authentication/requests work under the hood
*/
export const REST_CLIENT_TYPE_ENUM = {
/** Global & AU: Spot & Margin */
main: 'main',
/** Global & AU: Futures */
futures: 'futures',
/** Global: Broker */
broker: 'broker',
/** Global: Unified Trading Account */
unifiedTradingAccount: 'unifiedTradingAccount',
/** Dedicated EU: main (Spot & Margin). No futures at this time. */
mainEU: 'mainEU',
};
const kucoinURLMap = {
[REST_CLIENT_TYPE_ENUM.main]: 'https://api.kucoin.com',
// https://www.kucoin.com/en-eu/docs-new/introduction/eu
[REST_CLIENT_TYPE_ENUM.mainEU]: 'https://api.kucoin.eu',
[REST_CLIENT_TYPE_ENUM.futures]: 'https://api-futures.kucoin.com',
[REST_CLIENT_TYPE_ENUM.broker]: 'https://api-broker.kucoin.com',
[REST_CLIENT_TYPE_ENUM.unifiedTradingAccount]: 'https://api.kucoin.com',
};
export function serializeParams(params, strict_validation, encodeValues, prefixWith) {
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');
}
const encodedValue = encodeValues ? encodeURIComponent(value) : value;
return `${key}=${encodedValue}`;
})
.join('&');
// Only prefix if there's a value
return queryString ? prefixWith + queryString : queryString;
}
export const APIIDMain = 'NODESDK';
export const APIIDMainSign = 'd28f5b4a-179d-4fcb-9c00-c8319c0bb82c';
export const APIIDFutures = 'NODESDKFUTURES';
export const APIIDFuturesSign = '7f7fb0d6-e600-4ef4-8fe3-41e6aea9af84';
export function getRestBaseUrl(useTestnet, restOptions, restClientType) {
let resolvedClientType = restClientType;
if (restOptions.baseUrl) {
return restOptions.baseUrl;
}
// Override to EU URL for EU regional users
if (restOptions.apiRegion === 'EU') {
switch (restClientType) {
case REST_CLIENT_TYPE_ENUM.main:
case REST_CLIENT_TYPE_ENUM.mainEU: {
resolvedClientType = REST_CLIENT_TYPE_ENUM.mainEU;
break;
}
case REST_CLIENT_TYPE_ENUM.futures: {
console.warn('Futures market is not available for EU users at this time. API requests may not function as expected');
break;
}
case REST_CLIENT_TYPE_ENUM.broker: {
break;
}
case REST_CLIENT_TYPE_ENUM.unifiedTradingAccount: {
resolvedClientType = REST_CLIENT_TYPE_ENUM.mainEU;
break;
}
default: {
throw neverGuard(restClientType, `Unhandled REST Client Type "${restClientType}"`);
}
}
}
const exchangeBaseUrls = {
livenet: kucoinURLMap[resolvedClientType],
testnet: 'https://noTestnet',
};
if (useTestnet) {
return exchangeBaseUrls.testnet;
}
return exchangeBaseUrls.livenet;
}
//# sourceMappingURL=requestUtils.js.map