Skip to content
Open
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 src/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -892,6 +892,7 @@ export class OpenAI {
return await this.fetch.call(undefined, url, fetchOptions);
} finally {
clearTimeout(timeout);
if (signal) signal.removeEventListener('abort', abort);
}
}

Expand Down
30 changes: 30 additions & 0 deletions tests/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -276,6 +276,36 @@ describe('instantiate client', () => {
expect(spy).toHaveBeenCalledTimes(1);
});

test('fetchWithTimeout removes custom abort listener after success', async () => {
const testFetch = async (): Promise<Response> => {
return new Response(JSON.stringify({ ok: true }), {
headers: { 'Content-Type': 'application/json' },
});
};

const client = new OpenAI({
baseURL: 'http://localhost:5000/',
apiKey: 'My API Key',
fetch: testFetch,
});

const signal = {
addEventListener: jest.fn(),
removeEventListener: jest.fn(),
} as unknown as AbortSignal;

await client.fetchWithTimeout(
'http://localhost:5000/foo',
{ signal },
10,
new AbortController(),
);

expect(signal.addEventListener).toHaveBeenCalledWith('abort', expect.any(Function), { once: true });
const abort = (signal.addEventListener as jest.Mock).mock.calls[0][1];
expect(signal.removeEventListener).toHaveBeenCalledWith('abort', abort);
});

test('normalized method', async () => {
let capturedRequest: RequestInit | undefined;
const testFetch = async (url: string | URL | Request, init: RequestInit = {}): Promise<Response> => {
Expand Down