Skip to content

Commit

Permalink
Merge pull request #29 from tiagosiebler/appheader
Browse files Browse the repository at this point in the history
fix(): injectable headers from constructor. feat() add sandbox urls
  • Loading branch information
tiagosiebler authored Sep 19, 2024
2 parents d5a1c00 + f34c81e commit 7b7b77d
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 13 deletions.
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

0 comments on commit 7b7b77d

Please sign in to comment.