From aea11f1581512a8a6e314634d96e1e6036e6fb22 Mon Sep 17 00:00:00 2001 From: s1gr1d <32902192+s1gr1d@users.noreply.github.com> Date: Wed, 10 Dec 2025 10:20:22 +0100 Subject: [PATCH] fix(core): Parse method from Request object in fetch --- packages/core/src/instrument/fetch.ts | 11 +++++--- .../core/test/lib/instrument/fetch.test.ts | 25 +++++++++++++++++++ 2 files changed, 33 insertions(+), 3 deletions(-) diff --git a/packages/core/src/instrument/fetch.ts b/packages/core/src/instrument/fetch.ts index 0780b25bb29f..ef69ba8223e0 100644 --- a/packages/core/src/instrument/fetch.ts +++ b/packages/core/src/instrument/fetch.ts @@ -240,11 +240,16 @@ export function parseFetchArgs(fetchArgs: unknown[]): { method: string; url: str } if (fetchArgs.length === 2) { - const [url, options] = fetchArgs as [FetchResource, object]; + const [resource, options] = fetchArgs as [FetchResource, object]; return { - url: getUrlFromResource(url), - method: hasProp(options, 'method') ? String(options.method).toUpperCase() : 'GET', + url: getUrlFromResource(resource), + method: hasProp(options, 'method') + ? String(options.method).toUpperCase() + : // Request object as first argument + isRequest(resource) && hasProp(resource, 'method') + ? String(resource.method).toUpperCase() + : 'GET', }; } diff --git a/packages/core/test/lib/instrument/fetch.test.ts b/packages/core/test/lib/instrument/fetch.test.ts index 88d780a7dbad..215b0c513ee5 100644 --- a/packages/core/test/lib/instrument/fetch.test.ts +++ b/packages/core/test/lib/instrument/fetch.test.ts @@ -27,4 +27,29 @@ describe('instrument > parseFetchArgs', () => { expect(actual).toEqual(expected); }); + + describe('fetch with Request object', () => { + it.each([ + [ + 'Request object (as only arg)', + [new Request('http://example.com', { method: 'POST' })], + { method: 'POST', url: 'http://example.com/' }, + ], + [ + 'Request object (with undefined options arg)', + [new Request('http://example.com', { method: 'POST' }), undefined], + { method: 'POST', url: 'http://example.com/' }, + ], + [ + 'Request object (with overwritten options arg)', + [new Request('http://example.com', { method: 'POST' }), { method: 'DELETE' }], + // fetch options overwrite Request object options + { method: 'DELETE', url: 'http://example.com/' }, + ], + ])('%s', (_name, args, expected) => { + const actual = parseFetchArgs(args as unknown[]); + + expect(actual).toEqual(expected); + }); + }); });