In my case, I needed to monkey-patch window.fetch
for both options - url as string and also Request object.
This answer https://mcmap.net/q/671713/-how-do-i-copy-a-request-object-with-a-different-url covers how you can change url in the Request
object by duplicating the existing Request
object.
I myself implemented a solution where the Request
is actually changed into simple window.fetch
with url & options.
(function (window) {
const original = window.fetch;
window.fetch = async function () {
let [url, config] = arguments;
if (typeof url === 'string') {
const newUrl = customChangeUrl(url);
return original.apply(window, [newUrl, config]);
}
if (typeof url === 'object') {
const request = url;
const blob = await request.blob();
const body = blob.size > 0 ? blob : undefined;
config = {
body,
cache: request.cache,
credentials: request.credentials,
headers: request.headers,
integrity: request.integrity,
keepalive: request.keepalive,
method: request.method,
mode: request.mode,
redirect: request.redirect,
referrer: request.referrer,
referrerPolicy: request.referrerPolicy,
signal: request.signal,
}
const newUrl = customChangeUrl(request.url);
return original.apply(window, [newUrl, config]);
}
};
})(window);
priority
of the request, since that is not exposed as a property. All other options seem to be exposed as a property, so I think they'll be copied over correctly. Though I'm not sure if this api was intended to be used this way. – Calcifuge