Skip to content

Commit 0f715f2

Browse files
fix(client): normalize method (#1235)
1 parent d70f6e8 commit 0f715f2

File tree

2 files changed

+24
-1
lines changed

2 files changed

+24
-1
lines changed

src/core.ts

+11-1
Original file line numberDiff line numberDiff line change
@@ -557,9 +557,19 @@ export abstract class APIClient {
557557

558558
const timeout = setTimeout(() => controller.abort(), ms);
559559

560+
const fetchOptions = {
561+
signal: controller.signal as any,
562+
...options,
563+
};
564+
if (fetchOptions.method) {
565+
// Custom methods like 'patch' need to be uppercased
566+
// See https://github.com/nodejs/undici/issues/2294
567+
fetchOptions.method = fetchOptions.method.toUpperCase();
568+
}
569+
560570
return (
561571
// use undefined this binding; fetch errors if bound to something else in browser/cloudflare
562-
this.fetch.call(undefined, url, { signal: controller.signal as any, ...options }).finally(() => {
572+
this.fetch.call(undefined, url, fetchOptions).finally(() => {
563573
clearTimeout(timeout);
564574
})
565575
);

tests/index.test.ts

+13
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,19 @@ describe('instantiate client', () => {
122122
expect(spy).toHaveBeenCalledTimes(1);
123123
});
124124

125+
test('normalized method', async () => {
126+
let capturedRequest: RequestInit | undefined;
127+
const testFetch = async (url: RequestInfo, init: RequestInit = {}): Promise<Response> => {
128+
capturedRequest = init;
129+
return new Response(JSON.stringify({}), { headers: { 'Content-Type': 'application/json' } });
130+
};
131+
132+
const client = new OpenAI({ baseURL: 'http://localhost:5000/', apiKey: 'My API Key', fetch: testFetch });
133+
134+
await client.patch('/foo');
135+
expect(capturedRequest?.method).toEqual('PATCH');
136+
});
137+
125138
describe('baseUrl', () => {
126139
test('trailing slash', () => {
127140
const client = new OpenAI({ baseURL: 'http://localhost:5000/custom/path/', apiKey: 'My API Key' });

0 commit comments

Comments
 (0)