Skip to content

Commit fd67c19

Browse files
authored
fix(core): Parse method from Request object in fetch (#18453)
Fixes the case that the instrumentation defaulted to `GET` in case `undefined` was parsed as a second `fetch` argument. ```js const request = new Request("https://httpbin.org/post", { method: "POST" }); const response = await fetch(request, undefined); <-- will be GET (should be POST) ``` Closes #18455 (added automatically)
1 parent 8596086 commit fd67c19

File tree

2 files changed

+33
-3
lines changed

2 files changed

+33
-3
lines changed

packages/core/src/instrument/fetch.ts

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -240,11 +240,16 @@ export function parseFetchArgs(fetchArgs: unknown[]): { method: string; url: str
240240
}
241241

242242
if (fetchArgs.length === 2) {
243-
const [url, options] = fetchArgs as [FetchResource, object];
243+
const [resource, options] = fetchArgs as [FetchResource, object];
244244

245245
return {
246-
url: getUrlFromResource(url),
247-
method: hasProp(options, 'method') ? String(options.method).toUpperCase() : 'GET',
246+
url: getUrlFromResource(resource),
247+
method: hasProp(options, 'method')
248+
? String(options.method).toUpperCase()
249+
: // Request object as first argument
250+
isRequest(resource) && hasProp(resource, 'method')
251+
? String(resource.method).toUpperCase()
252+
: 'GET',
248253
};
249254
}
250255

packages/core/test/lib/instrument/fetch.test.ts

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,4 +27,29 @@ describe('instrument > parseFetchArgs', () => {
2727

2828
expect(actual).toEqual(expected);
2929
});
30+
31+
describe('fetch with Request object', () => {
32+
it.each([
33+
[
34+
'Request object (as only arg)',
35+
[new Request('http://example.com', { method: 'POST' })],
36+
{ method: 'POST', url: 'http://example.com/' },
37+
],
38+
[
39+
'Request object (with undefined options arg)',
40+
[new Request('http://example.com', { method: 'POST' }), undefined],
41+
{ method: 'POST', url: 'http://example.com/' },
42+
],
43+
[
44+
'Request object (with overwritten options arg)',
45+
[new Request('http://example.com', { method: 'POST' }), { method: 'DELETE' }],
46+
// fetch options overwrite Request object options
47+
{ method: 'DELETE', url: 'http://example.com/' },
48+
],
49+
])('%s', (_name, args, expected) => {
50+
const actual = parseFetchArgs(args as unknown[]);
51+
52+
expect(actual).toEqual(expected);
53+
});
54+
});
3055
});

0 commit comments

Comments
 (0)