Skip to content

Commit 7993276

Browse files
fix(client): normalize method (#535)
1 parent 8bddf3a commit 7993276

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
@@ -508,9 +508,19 @@ export abstract class APIClient {
508508

509509
const timeout = setTimeout(() => controller.abort(), ms);
510510

511+
const fetchOptions = {
512+
signal: controller.signal as any,
513+
...options,
514+
};
515+
if (fetchOptions.method) {
516+
// Custom methods like 'patch' need to be uppercased
517+
// See https://github.com/nodejs/undici/issues/2294
518+
fetchOptions.method = fetchOptions.method.toUpperCase();
519+
}
520+
511521
return (
512522
// use undefined this binding; fetch errors if bound to something else in browser/cloudflare
513-
this.fetch.call(undefined, url, { signal: controller.signal as any, ...options }).finally(() => {
523+
this.fetch.call(undefined, url, fetchOptions).finally(() => {
514524
clearTimeout(timeout);
515525
})
516526
);

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 Mux({
139+
baseURL: 'http://localhost:5000/',
140+
tokenId: 'my token id',
141+
tokenSecret: 'my secret',
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 Mux({

0 commit comments

Comments
 (0)