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
18 changes: 18 additions & 0 deletions tests/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -276,6 +276,24 @@ describe('instantiate client', () => {
expect(spy).toHaveBeenCalledTimes(1);
});

test('removes abort listener from caller signal after successful request', async () => {
const testFetch = async (_url: string | URL | Request, _init: RequestInit = {}): Promise<Response> => {
return new Response(JSON.stringify({ ok: true }), { headers: { 'Content-Type': 'application/json' } });
};

const client = new OpenAI({ apiKey: 'My API Key', fetch: testFetch });

const controller = new AbortController();
const addSpy = jest.spyOn(controller.signal, 'addEventListener');
const removeSpy = jest.spyOn(controller.signal, 'removeEventListener');

await client.get('/foo', { signal: controller.signal });

expect(addSpy).toHaveBeenCalledWith('abort', expect.any(Function), { once: true });
expect(removeSpy).toHaveBeenCalledWith('abort', expect.any(Function));
expect(addSpy.mock.calls[0]![1]).toBe(removeSpy.mock.calls[0]![1]);
});

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