Skip to content

Commit

Permalink
omit mode option for CloudFlare Workers #96
Browse files Browse the repository at this point in the history
  • Loading branch information
adrai committed Sep 20, 2022
1 parent 1e75746 commit a2af2fa
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 20 deletions.
49 changes: 37 additions & 12 deletions i18nextHttpBackend.js
Original file line number Diff line number Diff line change
Expand Up @@ -261,6 +261,8 @@ if (typeof fetch === 'function') {
fetchApi = global.fetch;
} else if (typeof window !== 'undefined' && window.fetch) {
fetchApi = window.fetch;
} else {
fetchApi = fetch;
}
}

Expand Down Expand Up @@ -302,18 +304,8 @@ var addQueryString = function addQueryString(url, params) {
return url;
};

var requestWithFetch = function requestWithFetch(options, url, payload, callback) {
if (options.queryStringParams) {
url = addQueryString(url, options.queryStringParams);
}

var headers = (0, _utils.defaults)({}, typeof options.customHeaders === 'function' ? options.customHeaders() : options.customHeaders);
if (payload) headers['Content-Type'] = 'application/json';
fetchApi(url, (0, _utils.defaults)({
method: payload ? 'POST' : 'GET',
body: payload ? options.stringify(payload) : undefined,
headers: headers
}, typeof options.requestOptions === 'function' ? options.requestOptions(payload) : options.requestOptions)).then(function (response) {
var fetchIt = function fetchIt(url, fetchOptions, callback) {
fetchApi(url, fetchOptions).then(function (response) {
if (!response.ok) return callback(response.statusText || 'Error', {
status: response.status
});
Expand All @@ -326,6 +318,39 @@ var requestWithFetch = function requestWithFetch(options, url, payload, callback
}).catch(callback);
};

var omitFetchMode = false;

var requestWithFetch = function requestWithFetch(options, url, payload, callback) {
if (options.queryStringParams) {
url = addQueryString(url, options.queryStringParams);
}

var headers = (0, _utils.defaults)({}, typeof options.customHeaders === 'function' ? options.customHeaders() : options.customHeaders);
if (payload) headers['Content-Type'] = 'application/json';
var fetchOptions = (0, _utils.defaults)({
method: payload ? 'POST' : 'GET',
body: payload ? options.stringify(payload) : undefined,
headers: headers
}, typeof options.requestOptions === 'function' ? options.requestOptions(payload) : options.requestOptions);
if (omitFetchMode) delete fetchOptions.mode;

try {
fetchIt(url, fetchOptions, callback);
} catch (e) {
if (!fetchOptions.mode || !e.message || e.message.indexOf('mode') < 0 || e.message.indexOf('not implemented') < 0) {
return callback(e);
}

try {
delete fetchOptions.mode;
fetchIt(url, fetchOptions, callback);
omitFetchMode = true;
} catch (err) {
callback(err);
}
}
};

var requestWithXmlHttpRequest = function requestWithXmlHttpRequest(options, url, payload, callback) {
if (payload && _typeof(payload) === 'object') {
payload = addQueryString('', payload).slice(1);
Expand Down
Loading

0 comments on commit a2af2fa

Please sign in to comment.