Skip to content

Commit 8fe82b8

Browse files
fix(client): normalize method (#639)
1 parent 546cfb4 commit 8fe82b8

File tree

2 files changed

+28
-1
lines changed

2 files changed

+28
-1
lines changed

src/core.ts

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -536,9 +536,19 @@ export abstract class APIClient {
536536

537537
const timeout = setTimeout(() => controller.abort(), ms);
538538

539+
const fetchOptions = {
540+
signal: controller.signal as any,
541+
...options,
542+
};
543+
if (fetchOptions.method) {
544+
// Custom methods like 'patch' need to be uppercased
545+
// See https://github.com/nodejs/undici/issues/2294
546+
fetchOptions.method = fetchOptions.method.toUpperCase();
547+
}
548+
539549
return (
540550
// use undefined this binding; fetch errors if bound to something else in browser/cloudflare
541-
this.fetch.call(undefined, url, { signal: controller.signal as any, ...options }).finally(() => {
551+
this.fetch.call(undefined, url, fetchOptions).finally(() => {
542552
clearTimeout(timeout);
543553
})
544554
);

tests/index.test.ts

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,23 @@ 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 Anthropic({
133+
baseURL: 'http://localhost:5000/',
134+
apiKey: 'my-anthropic-api-key',
135+
fetch: testFetch,
136+
});
137+
138+
await client.patch('/foo');
139+
expect(capturedRequest?.method).toEqual('PATCH');
140+
});
141+
125142
describe('baseUrl', () => {
126143
test('trailing slash', () => {
127144
const client = new Anthropic({

0 commit comments

Comments
 (0)