Skip to content

Commit

Permalink
Pass dispatcher options to global fetch
Browse files Browse the repository at this point in the history
  • Loading branch information
Kruspe committed Jun 5, 2024
1 parent 23ff8bb commit cff79eb
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 1 deletion.
6 changes: 5 additions & 1 deletion sdk_contrib/fetch/lib/fetch_p.js
Original file line number Diff line number Diff line change
Expand Up @@ -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] : {};
Expand Down Expand Up @@ -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);
Expand Down
11 changes: 11 additions & 0 deletions sdk_contrib/fetch/test/unit/fetch_p.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -352,6 +352,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);
Expand Down

0 comments on commit cff79eb

Please sign in to comment.