Skip to content
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
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
"@scure/bip32": "^1.4.0",
"axios": "^1.6.8",
"axios-mock-adapter": "^1.22.0",
"axios-retry": "^4.4.1",
"bip32": "^4.0.0",
"bip39": "^3.1.0",
"decimal.js": "^10.4.3",
Expand Down
14 changes: 13 additions & 1 deletion src/coinbase/coinbase.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import globalAxios from "axios";
import globalAxios, { AxiosError } from "axios";
import axiosRetry from "axios-retry";
import * as fs from "fs";
import {
User as UserModel,
Expand Down Expand Up @@ -74,6 +75,7 @@ export class Coinbase {
* @param options.useServerSigner - Whether to use a Server-Signer or not.
* @param options.debugging - If true, logs API requests and responses to the console.
* @param options.basePath - The base path for the API.
* @param options.maxNetworkRetries - The maximum number of network retries for the API GET requests.
* @throws {InternalError} If the configuration is invalid.
* @throws {InvalidAPIKeyFormat} If not able to create JWT token.
*/
Expand All @@ -83,6 +85,7 @@ export class Coinbase {
useServerSigner = false,
debugging = false,
basePath = BASE_PATH,
maxNetworkRetries = 3,
}: CoinbaseOptions) {
if (apiKeyName === "") {
throw new InternalError("Invalid configuration: apiKeyName is empty");
Expand All @@ -95,6 +98,15 @@ export class Coinbase {
basePath: basePath,
});
const axiosInstance = globalAxios.create();
axiosRetry(axiosInstance, {
retries: maxNetworkRetries,
retryCondition: (error: AxiosError) => {
return (
error.config?.method?.toUpperCase() === "GET" &&
(error.response?.status || 0) in [500, 502, 503, 504]
);
},
});
registerAxiosInterceptors(
axiosInstance,
config => coinbaseAuthenticator.authenticateRequest(config, debugging),
Expand Down
5 changes: 5 additions & 0 deletions src/coinbase/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -607,6 +607,11 @@ export type CoinbaseOptions = {
* The base path for the API.
*/
basePath?: string;

/**
* The maximum number of network retries for the API GET requests.
*/
maxNetworkRetries?: number;
};

/**
Expand Down
12 changes: 12 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -1052,6 +1052,13 @@ axios-mock-adapter@^1.22.0:
fast-deep-equal "^3.1.3"
is-buffer "^2.0.5"

axios-retry@^4.4.1:
version "4.4.1"
resolved "https://registry.yarnpkg.com/axios-retry/-/axios-retry-4.4.1.tgz#e5d0c851e635fd4de866aec2b9b422f05d661257"
integrity sha512-JGzNoglDHtHWIEvvAampB0P7jxQ/sT4COmW0FgSQkVg6o4KqNjNMBI6uFVOq517hkw/OAYYAG08ADtBlV8lvmQ==
dependencies:
is-retry-allowed "^2.2.0"

axios@^1.6.8:
version "1.6.8"
resolved "https://registry.yarnpkg.com/axios/-/axios-1.6.8.tgz#66d294951f5d988a00e87a0ffb955316a619ea66"
Expand Down Expand Up @@ -2057,6 +2064,11 @@ is-path-inside@^3.0.3:
resolved "https://registry.yarnpkg.com/is-path-inside/-/is-path-inside-3.0.3.tgz#d231362e53a07ff2b0e0ea7fed049161ffd16283"
integrity sha512-Fd4gABb+ycGAmKou8eMftCupSir5lRxqf4aD/vd0cD2qc4HL07OjCeuHMr8Ro4CoMaeCKDB0/ECBOVWjTwUvPQ==

is-retry-allowed@^2.2.0:
version "2.2.0"
resolved "https://registry.yarnpkg.com/is-retry-allowed/-/is-retry-allowed-2.2.0.tgz#88f34cbd236e043e71b6932d09b0c65fb7b4d71d"
integrity sha512-XVm7LOeLpTW4jV19QSH38vkswxoLud8sQ57YwJVTPWdiaI9I8keEhGFpBlslyVsgdQy4Opg8QOLb8YRgsyZiQg==

is-stream@^2.0.0:
version "2.0.1"
resolved "https://registry.yarnpkg.com/is-stream/-/is-stream-2.0.1.tgz#fac1e3d53b97ad5a9d0ae9cef2389f5810a5c077"
Expand Down