Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(): injectable headers from constructor. feat() add sandbox urls #29

Merged
merged 1 commit into from
Sep 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion src/CBAppClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,13 @@ export class CBAppClient extends BaseRestClient {
restClientOptions: RestClientOptions = {},
requestOptions: AxiosRequestConfig = {},
) {
super(restClientOptions, requestOptions);
super(restClientOptions, {
...requestOptions,
headers: {
'CB-VERSION': '2024-09-13',
...requestOptions.headers,
},
});
return this;
}

Expand Down
7 changes: 2 additions & 5 deletions src/lib/BaseRestClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,7 @@ export abstract class BaseRestClient {
/** inject custom rquest options based on axios specs - see axios docs for more guidance on AxiosRequestConfig: https://github.com/axios/axios#request-config */
...networkOptions,
headers: {
...networkOptions.headers,
'Content-Type': 'application/json',
locale: 'en-US',
'User-Agent': USER_AGENT,
Expand All @@ -175,11 +176,7 @@ export abstract class BaseRestClient {
});
}

this.baseUrl = getRestBaseUrl(
false,
restClientOptions,
this.getClientType(),
);
this.baseUrl = getRestBaseUrl(restClientOptions, this.getClientType());

this.apiKey = this.options.apiKey;
this.apiSecret = this.options.apiSecret;
Expand Down
35 changes: 28 additions & 7 deletions src/lib/requestUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,22 @@ const exchangeBaseURLMap = {
[REST_CLIENT_TYPE_ENUM.advancedTrade]: 'https://api.coinbase.com',
[REST_CLIENT_TYPE_ENUM.coinbaseApp]: 'https://api.coinbase.com',
[REST_CLIENT_TYPE_ENUM.exchange]: 'https://api.exchange.coinbase.com',
[REST_CLIENT_TYPE_ENUM.prime]: 'https://api.prime.coinbase.com', // /v1
[REST_CLIENT_TYPE_ENUM.prime]: 'https://api.prime.coinbase.com',
[REST_CLIENT_TYPE_ENUM.international]:
'https://api.international.coinbase.com',
[REST_CLIENT_TYPE_ENUM.commerce]: 'https://api.commerce.coinbase.com',
} as const;

const exchangeSandboxURLMap = {
[REST_CLIENT_TYPE_ENUM.exchange]:
'https://api-public.sandbox.exchange.coinbase.com',
[REST_CLIENT_TYPE_ENUM.international]: 'https://api-n5e1.coinbase.com',
[REST_CLIENT_TYPE_ENUM.advancedTrade]: 'NoSandboxAvailable',
[REST_CLIENT_TYPE_ENUM.coinbaseApp]: 'NoSandboxAvailable',
[REST_CLIENT_TYPE_ENUM.prime]: 'NoSandboxAvailable',
[REST_CLIENT_TYPE_ENUM.commerce]: 'NoSandboxAvailable',
} as const;

export interface RestClientOptions {
/**
* Your API key name.
Expand Down Expand Up @@ -85,6 +95,18 @@ export interface RestClientOptions {
| 'pt'
| 'pt-br';

/**
* Connect to the sandbox for supported products
*
* - Coinbase Exchange: https://docs.cdp.coinbase.com/exchange/docs/sandbox
* - Coinbase International: https://docs.cdp.coinbase.com/intx/docs/sandbox
*
* - Coinbase App: No sandbox available.
* - Coinbase Advanced Trade: No sandbox available.
* - Coinbase Prime: No sandbox available.
*/
useSandbox?: boolean;

/**
* Enable keep alive for REST API requests (via axios).
* See: https://github.com/tiagosiebler/bybit-api/issues/368
Expand Down Expand Up @@ -159,20 +181,19 @@ export function logInvalidOrderId(
}

export function getRestBaseUrl(
useTestnet: boolean,
restInverseOptions: RestClientOptions,
restClientOptions: RestClientOptions,
restClientType: RestClientType,
): string {
const exchangeBaseUrls = {
livenet: exchangeBaseURLMap[restClientType],
testnet: 'https://noTestnet',
testnet: exchangeSandboxURLMap[restClientType],
};

if (restInverseOptions.baseUrl) {
return restInverseOptions.baseUrl;
if (restClientOptions.baseUrl) {
return restClientOptions.baseUrl;
}

if (useTestnet) {
if (restClientOptions.useSandbox === true) {
return exchangeBaseUrls.testnet;
}

Expand Down