Skip to content

Commit

Permalink
fix(client): fix TypeError when a request gets retried (#201)
Browse files Browse the repository at this point in the history
  • Loading branch information
stainless-bot authored and cleb11 committed Aug 30, 2023
1 parent 1416150 commit ad59570
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 7 deletions.
12 changes: 6 additions & 6 deletions src/core.ts
Original file line number Diff line number Diff line change
Expand Up @@ -315,10 +315,10 @@ export abstract class APIClient {
return new APIPromise(this.makeRequest(options, remainingRetries));
}

private async makeRequest<T>(
private async makeRequest(
optionsInput: PromiseOrValue<FinalRequestOptions>,
retriesRemaining: number | null,
): Promise<{ response: Response; options: FinalRequestOptions; controller: AbortController }> {
): Promise<APIResponseProps> {
const options = await optionsInput;
if (retriesRemaining == null) {
retriesRemaining = options.maxRetries ?? this.maxRetries;
Expand Down Expand Up @@ -455,11 +455,11 @@ export abstract class APIClient {
return false;
}

private async retryRequest<Req extends {}, Rsp>(
options: FinalRequestOptions<Req>,
private async retryRequest(
options: FinalRequestOptions,
retriesRemaining: number,
responseHeaders?: Headers | undefined,
): Promise<Rsp> {
): Promise<APIResponseProps> {
retriesRemaining -= 1;

// About the Retry-After header: https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Retry-After
Expand All @@ -472,7 +472,7 @@ export abstract class APIClient {
const timeout = this.calculateRetryTimeoutSeconds(retriesRemaining, retryAfter, maxRetries) * 1000;
await sleep(timeout);

return this.request(options, retriesRemaining);
return this.makeRequest(options, retriesRemaining);
}

private calculateRetryTimeoutSeconds(
Expand Down
37 changes: 36 additions & 1 deletion tests/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,12 @@
import ModernTreasury from 'modern-treasury';
import { APIUserAbortError } from 'modern-treasury';
import { Headers } from 'modern-treasury/core';
import { Response, fetch as defaultFetch } from 'modern-treasury/_shims/fetch';
import {
Response,
fetch as defaultFetch,
type RequestInit,
type RequestInfo,
} from 'modern-treasury/_shims/fetch';

describe('instantiate client', () => {
const env = process.env;
Expand Down Expand Up @@ -218,3 +223,33 @@ describe('request building', () => {
});
});
});

describe('retries', () => {
test('single retry', async () => {
let count = 0;
const testFetch = async (url: RequestInfo, { signal }: RequestInit = {}): Promise<Response> => {
if (!count++)
return new Promise((resolve, reject) =>
signal?.addEventListener('abort', () => reject(new Error('timed out'))),
);
return new Response(JSON.stringify({ a: 1 }), { headers: { 'Content-Type': 'application/json' } });
};

const client = new ModernTreasury({
organizationId: 'my-organization-ID',
apiKey: 'my api key',
timeout: 2000,
fetch: testFetch,
});

expect(await client.request({ path: '/foo', method: 'get' })).toEqual({ a: 1 });
expect(count).toEqual(2);
expect(
await client
.request({ path: '/foo', method: 'get' })
.asResponse()
.then((r) => r.text()),
).toEqual(JSON.stringify({ a: 1 }));
expect(count).toEqual(3);
}, 10000);
});

0 comments on commit ad59570

Please sign in to comment.