Skip to content

Commit dc2191c

Browse files
authored
Merge pull request coinbase#105 from coinbase/feat/network-retry
Adding retry for failed GET requests
2 parents 1af65a4 + f5c01ed commit dc2191c

File tree

4 files changed

+31
-1
lines changed

4 files changed

+31
-1
lines changed

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
"@scure/bip32": "^1.4.0",
2929
"axios": "^1.6.8",
3030
"axios-mock-adapter": "^1.22.0",
31+
"axios-retry": "^4.4.1",
3132
"bip32": "^4.0.0",
3233
"bip39": "^3.1.0",
3334
"decimal.js": "^10.4.3",

src/coinbase/coinbase.ts

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
import globalAxios from "axios";
1+
import globalAxios, { AxiosError } from "axios";
2+
import axiosRetry from "axios-retry";
23
import * as fs from "fs";
34
import {
45
User as UserModel,
@@ -77,6 +78,7 @@ export class Coinbase {
7778
* @param options.useServerSigner - Whether to use a Server-Signer or not.
7879
* @param options.debugging - If true, logs API requests and responses to the console.
7980
* @param options.basePath - The base path for the API.
81+
* @param options.maxNetworkRetries - The maximum number of network retries for the API GET requests.
8082
* @throws {InternalError} If the configuration is invalid.
8183
* @throws {InvalidAPIKeyFormat} If not able to create JWT token.
8284
*/
@@ -86,6 +88,7 @@ export class Coinbase {
8688
useServerSigner = false,
8789
debugging = false,
8890
basePath = BASE_PATH,
91+
maxNetworkRetries = 3,
8992
}: CoinbaseOptions) {
9093
if (apiKeyName === "") {
9194
throw new InternalError("Invalid configuration: apiKeyName is empty");
@@ -98,6 +101,15 @@ export class Coinbase {
98101
basePath: basePath,
99102
});
100103
const axiosInstance = globalAxios.create();
104+
axiosRetry(axiosInstance, {
105+
retries: maxNetworkRetries,
106+
retryCondition: (error: AxiosError) => {
107+
return (
108+
error.config?.method?.toUpperCase() === "GET" &&
109+
(error.response?.status || 0) in [500, 502, 503, 504]
110+
);
111+
},
112+
});
101113
registerAxiosInterceptors(
102114
axiosInstance,
103115
config => coinbaseAuthenticator.authenticateRequest(config, debugging),

src/coinbase/types.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -662,6 +662,11 @@ export type CoinbaseOptions = {
662662
* The base path for the API.
663663
*/
664664
basePath?: string;
665+
666+
/**
667+
* The maximum number of network retries for the API GET requests.
668+
*/
669+
maxNetworkRetries?: number;
665670
};
666671

667672
/**

yarn.lock

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1052,6 +1052,13 @@ axios-mock-adapter@^1.22.0:
10521052
fast-deep-equal "^3.1.3"
10531053
is-buffer "^2.0.5"
10541054

1055+
axios-retry@^4.4.1:
1056+
version "4.4.1"
1057+
resolved "https://registry.yarnpkg.com/axios-retry/-/axios-retry-4.4.1.tgz#e5d0c851e635fd4de866aec2b9b422f05d661257"
1058+
integrity sha512-JGzNoglDHtHWIEvvAampB0P7jxQ/sT4COmW0FgSQkVg6o4KqNjNMBI6uFVOq517hkw/OAYYAG08ADtBlV8lvmQ==
1059+
dependencies:
1060+
is-retry-allowed "^2.2.0"
1061+
10551062
axios@^1.6.8:
10561063
version "1.6.8"
10571064
resolved "https://registry.yarnpkg.com/axios/-/axios-1.6.8.tgz#66d294951f5d988a00e87a0ffb955316a619ea66"
@@ -2057,6 +2064,11 @@ is-path-inside@^3.0.3:
20572064
resolved "https://registry.yarnpkg.com/is-path-inside/-/is-path-inside-3.0.3.tgz#d231362e53a07ff2b0e0ea7fed049161ffd16283"
20582065
integrity sha512-Fd4gABb+ycGAmKou8eMftCupSir5lRxqf4aD/vd0cD2qc4HL07OjCeuHMr8Ro4CoMaeCKDB0/ECBOVWjTwUvPQ==
20592066

2067+
is-retry-allowed@^2.2.0:
2068+
version "2.2.0"
2069+
resolved "https://registry.yarnpkg.com/is-retry-allowed/-/is-retry-allowed-2.2.0.tgz#88f34cbd236e043e71b6932d09b0c65fb7b4d71d"
2070+
integrity sha512-XVm7LOeLpTW4jV19QSH38vkswxoLud8sQ57YwJVTPWdiaI9I8keEhGFpBlslyVsgdQy4Opg8QOLb8YRgsyZiQg==
2071+
20602072
is-stream@^2.0.0:
20612073
version "2.0.1"
20622074
resolved "https://registry.yarnpkg.com/is-stream/-/is-stream-2.0.1.tgz#fac1e3d53b97ad5a9d0ae9cef2389f5810a5c077"

0 commit comments

Comments
 (0)