Skip to content

Commit

Permalink
Fix: Handle Request objects in Segment fetch override
Browse files Browse the repository at this point in the history
  • Loading branch information
amirtds committed Nov 14, 2023
1 parent 97a3e4d commit e98d4ab
Showing 1 changed file with 36 additions and 9 deletions.
45 changes: 36 additions & 9 deletions lms/templates/widgets/segment-io.html
Original file line number Diff line number Diff line change
Expand Up @@ -9,18 +9,45 @@
!function(){
var originalAPI = '${settings.SEGMENT_ORIGINAL_API}';
var replicateAPI = '${settings.SEGMENT_REPLICATE_API}';
function replaceFetchResourceForSegmentSite(resource){
if (resource.substr(0, originalAPI.length) === originalAPI) {
resource = replicateAPI + resource.substr(originalAPI.length);
function replaceFetchResourceForSegmentSite(resource) {
// Helper function to replace the URL
function replaceUrl(url) {
if (url.substr(0, originalAPI.length) === originalAPI) {
return replicateAPI + url.substr(originalAPI.length);
}
return url;
}

// Check if resource is a string (a URL)
if (typeof resource === 'string') {
return replaceUrl(resource);
} else if (resource instanceof Request) {
// If resource is a Request object, create a new Request with a replaced URL
const newUrl = replaceUrl(resource.url);
return new Request(newUrl, {
method: resource.method,
headers: resource.headers,
body: resource.body,
mode: resource.mode,
credentials: resource.credentials,
cache: resource.cache,
redirect: resource.redirect,
referrer: resource.referrer,
integrity: resource.integrity,
keepalive: resource.keepalive,
signal: resource.signal
});
} else {
// If it's neither a string nor a Request object, log an error or handle as needed
console.error('replaceFetchResourceForSegmentSite was called with an unexpected argument type');
return resource;
}
return resource;
}
const { fetch: originalFetch } = window;
// Override the fetch function to use the replaceFetchResourceForSegmentSite function
const originalFetch = window.fetch;
window.fetch = async (...args) => {
let [resource, config ] = args;
resource = replaceFetchResourceForSegmentSite(resource);
const response = await originalFetch(resource, config);
return response;
args[0] = replaceFetchResourceForSegmentSite(args[0]);
return originalFetch.apply(window, args);
};
}();
</script>
Expand Down

0 comments on commit e98d4ab

Please sign in to comment.