Skip to content

Commit 4d16d77

Browse files
authored
Merge pull request #525 from PermanentOrg/522-retry-configuration-expansion
Support additional retry configuration
2 parents 1fb3ac2 + b0d6ab4 commit 4d16d77

5 files changed

Lines changed: 73 additions & 2 deletions

File tree

CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,11 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
77

88
## [Unreleased]
99

10+
### Added
11+
12+
- SDK clients can now be configured to specify how many `retry` attempts to make.
13+
- SDK clients can now be configured to specify a custom `retryDelay` function.
14+
1015
## [0.10.0] - 2025-04-02
1116

1217
### Added

src/types/sdk/ClientConfiguration.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,6 @@ export interface ClientConfiguration {
33
baseUrl?: string;
44
stelaBaseUrl?: string;
55
retryOn?: number[];
6+
retries?: number;
7+
retryDelay?: (attempt: number) => number;
68
}

src/utils/__tests__/makePermanentApiCall.test.ts

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,4 +82,39 @@ describe('makePermanentApiCall', () => {
8282
expect(responseBody).toEqual('it worked');
8383
expect(mock.isDone()).toBe(true); // All mocked responses were invoked
8484
});
85+
86+
it('should retry additional times than the default if configured to do so', async () => {
87+
const mock = nock('https://permanent.local', {
88+
reqheaders: {
89+
Authorization: 'Bearer 12345',
90+
'Request-Version': '2',
91+
'Content-Type': 'application/json',
92+
},
93+
})
94+
.get('/api/testing')
95+
.reply(404, 'it did not work')
96+
.get('/api/testing')
97+
.reply(404, 'it did not work')
98+
.get('/api/testing')
99+
.reply(404, 'it did not work')
100+
.get('/api/testing')
101+
.reply(404, 'it did not work')
102+
.get('/api/testing')
103+
.reply(200, 'it worked');
104+
105+
const response = await makePermanentApiCall(
106+
{
107+
bearerToken: '12345',
108+
baseUrl: 'https://permanent.local/api',
109+
retryOn: [404],
110+
retries: 4,
111+
retryDelay: (attempt) => attempt * 0.01,
112+
},
113+
'/testing',
114+
);
115+
const responseBody = await response.text();
116+
117+
expect(responseBody).toEqual('it worked');
118+
expect(mock.isDone()).toBe(true); // All mocked responses were invoked
119+
});
85120
});

src/utils/__tests__/makeStelaApiCall.test.ts

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,4 +92,33 @@ describe('makeStelaApiCall', () => {
9292
expect(responseBody).toEqual('it worked');
9393
expect(mock.isDone()).toBe(true); // All mocked responses were invoked
9494
});
95+
96+
it('should retry additional times than the default if configured to do so', async () => {
97+
const mock = nock('https://api.permanent.local')
98+
.get('/api/v2/testing')
99+
.reply(404, 'it did not work')
100+
.get('/api/v2/testing')
101+
.reply(404, 'it did not work')
102+
.get('/api/v2/testing')
103+
.reply(404, 'it did not work')
104+
.get('/api/v2/testing')
105+
.reply(404, 'it did not work')
106+
.get('/api/v2/testing')
107+
.reply(200, 'it worked');
108+
109+
const response = await makeStelaApiCall(
110+
{
111+
bearerToken: '12345',
112+
stelaBaseUrl: 'https://api.permanent.local/api/v2',
113+
retryOn: [404],
114+
retries: 4,
115+
retryDelay: (attempt) => attempt * 0.01,
116+
},
117+
'/testing',
118+
);
119+
const responseBody = await response.text();
120+
121+
expect(responseBody).toEqual('it worked');
122+
expect(mock.isDone()).toBe(true); // All mocked responses were invoked
123+
});
95124
});

src/utils/getFetch.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@ import fetchRetry from 'fetch-retry';
33
import type { ClientConfiguration } from '../types/sdk/ClientConfiguration';
44

55
const getFetch = (clientConfiguration: ClientConfiguration) => fetchRetry(originalFetch, {
6-
retries: 3,
7-
retryDelay: (attempt) => (2 ** attempt) * 1000,
6+
retries: clientConfiguration.retries ?? 3,
7+
retryDelay: clientConfiguration.retryDelay ?? ((attempt) => (2 ** attempt) * 1000),
88
retryOn: clientConfiguration.retryOn ?? [],
99
});
1010

0 commit comments

Comments
 (0)