From 627b6df9a4b254abb05ed57dc1058f0512729452 Mon Sep 17 00:00:00 2001 From: Maximilian Prusch Date: Wed, 5 Jun 2024 23:53:31 +0200 Subject: [PATCH] Pass dispatcher options to global fetch --- sdk_contrib/fetch/lib/fetch_p.js | 6 +++++- sdk_contrib/fetch/test/unit/fetch_p.test.js | 11 +++++++++++ 2 files changed, 16 insertions(+), 1 deletion(-) diff --git a/sdk_contrib/fetch/lib/fetch_p.js b/sdk_contrib/fetch/lib/fetch_p.js index ba1a2761..e099cfb7 100644 --- a/sdk_contrib/fetch/lib/fetch_p.js +++ b/sdk_contrib/fetch/lib/fetch_p.js @@ -63,6 +63,10 @@ const enableCapture = function enableCapture(baseFetchFunction, requestClass, do const request = typeof args[0] === 'object' ? args[0] : new requestClass(...args); + let fetchOptions = undefined; + if (args[1] && 'dispatcher' in args[1]) { + fetchOptions = { dispatcher: args[1].dispatcher }; + } // Facilitate the addition of Segment information via the request arguments const params = args.length > 1 ? args[1] : {}; @@ -116,7 +120,7 @@ const enableCapture = function enableCapture(baseFetchFunction, requestClass, do const requestClone = request.clone(); let response; try { - response = await baseFetchFunction(requestClone); + response = await baseFetchFunction(requestClone, fetchOptions); if (thisSubsegmentCallback) { thisSubsegmentCallback(subsegment, requestClone, response); diff --git a/sdk_contrib/fetch/test/unit/fetch_p.test.js b/sdk_contrib/fetch/test/unit/fetch_p.test.js index 4dde8d52..85436f49 100644 --- a/sdk_contrib/fetch/test/unit/fetch_p.test.js +++ b/sdk_contrib/fetch/test/unit/fetch_p.test.js @@ -339,6 +339,17 @@ describe('Unit tests', function () { response.should.equal(stubValidResponse); }); + it('resolves to response through proxy when fetch options are supplied', async function() { + const activeFetch = captureFetch(true); + const proxyStub = sinon.stub(); + const request = new FetchRequest('https://www.foo.com/test'); + const response = await activeFetch(request, { + dispatcher: proxyStub + }); + stubFetch.should.have.been.calledOnceWith(request, {dispatcher: proxyStub}); + response.should.equal(stubValidResponse); + }); + it('calls subsegmentCallback with error upon fetch throwing', async function () { const spyCallback = sandbox.spy(); const activeFetch = captureFetch(true, spyCallback);