🌐 AI搜索 & 代理 主页
Skip to content

Commit 9cc7114

Browse files
stainless-app[bot]meorphis
authored andcommitted
fix(client): normalize method (#2265)
1 parent c30593b commit 9cc7114

File tree

2 files changed

+29
-1
lines changed

2 files changed

+29
-1
lines changed

src/core.ts

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

523523
const timeout = setTimeout(() => controller.abort(), ms);
524524

525+
const fetchOptions = {
526+
signal: controller.signal as any,
527+
...options,
528+
};
529+
if (fetchOptions.method) {
530+
// Custom methods like 'patch' need to be uppercased
531+
// See https://github.com/nodejs/undici/issues/2294
532+
fetchOptions.method = fetchOptions.method.toUpperCase();
533+
}
534+
525535
return (
526536
// use undefined this binding; fetch errors if bound to something else in browser/cloudflare
527-
this.fetch.call(undefined, url, { signal: controller.signal as any, ...options }).finally(() => {
537+
this.fetch.call(undefined, url, fetchOptions).finally(() => {
528538
clearTimeout(timeout);
529539
})
530540
);

tests/index.test.ts

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,24 @@ describe('instantiate client', () => {
128128
expect(spy).toHaveBeenCalledTimes(1);
129129
});
130130

131+
test('normalized method', async () => {
132+
let capturedRequest: RequestInit | undefined;
133+
const testFetch = async (url: RequestInfo, init: RequestInit = {}): Promise<Response> => {
134+
capturedRequest = init;
135+
return new Response(JSON.stringify({}), { headers: { 'Content-Type': 'application/json' } });
136+
};
137+
138+
const client = new Cloudflare({
139+
baseURL: 'http://localhost:5000/',
140+
apiKey: '144c9defac04969c7bfad8efaa8ea194',
141+
apiEmail: 'user@example.com',
142+
fetch: testFetch,
143+
});
144+
145+
await client.patch('/foo');
146+
expect(capturedRequest?.method).toEqual('PATCH');
147+
});
148+
131149
describe('baseUrl', () => {
132150
test('trailing slash', () => {
133151
const client = new Cloudflare({

0 commit comments

Comments
 (0)