UNPKG

i18next-locize-backend

Version:

i18next-locize-backend is a backend layer for i18next to use locize service which can be used in node.js, in the browser and for deno.

111 lines 4.72 kB
function _typeof(o) { "@babel/helpers - typeof"; return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function (o) { return typeof o; } : function (o) { return o && "function" == typeof Symbol && o.constructor === Symbol && o !== Symbol.prototype ? "symbol" : typeof o; }, _typeof(o); } var fetchApi = typeof fetch === 'function' ? fetch : undefined; if (typeof global !== 'undefined' && global.fetch) { fetchApi = global.fetch; } else if (typeof window !== 'undefined' && window.fetch) { fetchApi = window.fetch; } var XmlHttpRequestApi; if (typeof XMLHttpRequest === 'function' || (typeof XMLHttpRequest === "undefined" ? "undefined" : _typeof(XMLHttpRequest)) === 'object') { if (typeof global !== 'undefined' && global.XMLHttpRequest) { XmlHttpRequestApi = global.XMLHttpRequest; } else if (typeof window !== 'undefined' && window.XMLHttpRequest) { XmlHttpRequestApi = window.XMLHttpRequest; } } var ActiveXObjectApi; if (typeof ActiveXObject === 'function') { if (typeof global !== 'undefined' && global.ActiveXObject) { ActiveXObjectApi = global.ActiveXObject; } else if (typeof window !== 'undefined' && window.ActiveXObject) { ActiveXObjectApi = window.ActiveXObject; } } if (typeof fetchApi !== 'function') fetchApi = undefined; if (!fetchApi && !XmlHttpRequestApi && !ActiveXObjectApi) { try { import('cross-fetch').then(function (mod) { fetchApi = mod.default; }).catch(function () {}); } catch (e) {} } var requestWithFetch = function requestWithFetch(options, url, payload, callback) { var headers = {}; if (typeof window === 'undefined' && typeof global !== 'undefined' && typeof global.process !== 'undefined' && global.process.versions && global.process.versions.node) { headers['User-Agent'] = "i18next-locize-backend (node/".concat(global.process.version, "; ").concat(global.process.platform, " ").concat(global.process.arch, ")"); } if (options.authorize && options.apiKey) { headers.Authorization = options.apiKey; } if (payload || options.setContentTypeJSON) { headers['Content-Type'] = 'application/json'; } var resolver = function resolver(response) { var resourceNotExisting = response.headers && response.headers.get('x-cache') === 'Error from cloudfront'; if (!response.ok) return callback(response.statusText || 'Error', { status: response.status, resourceNotExisting: resourceNotExisting }); response.text().then(function (data) { callback(null, { status: response.status, data: data, resourceNotExisting: resourceNotExisting }); }).catch(callback); }; if (typeof fetch === 'function') { fetch(url, { method: payload ? 'POST' : 'GET', body: payload ? JSON.stringify(payload) : undefined, headers: headers }).then(resolver).catch(callback); } else { fetchApi(url, { method: payload ? 'POST' : 'GET', body: payload ? JSON.stringify(payload) : undefined, headers: headers }).then(resolver).catch(callback); } }; var requestWithXmlHttpRequest = function requestWithXmlHttpRequest(options, url, payload, callback) { try { var x = XmlHttpRequestApi ? new XmlHttpRequestApi() : new ActiveXObjectApi('MSXML2.XMLHTTP.3.0'); x.open(payload ? 'POST' : 'GET', url, 1); if (!options.crossDomain) { x.setRequestHeader('X-Requested-With', 'XMLHttpRequest'); } if (options.authorize && options.apiKey) { x.setRequestHeader('Authorization', options.apiKey); } if (payload || options.setContentTypeJSON) { x.setRequestHeader('Content-Type', 'application/json'); } x.onreadystatechange = function () { var resourceNotExisting = x.getResponseHeader('x-cache') === 'Error from cloudfront'; x.readyState > 3 && callback(x.status >= 400 ? x.statusText : null, { status: x.status, data: x.responseText, resourceNotExisting: resourceNotExisting }); }; x.send(JSON.stringify(payload)); } catch (e) { console && console.log(e); } }; var request = function request(options, url, payload, callback) { if (typeof payload === 'function') { callback = payload; payload = undefined; } callback = callback || function () {}; if (fetchApi) { return requestWithFetch(options, url, payload, callback); } if (typeof XMLHttpRequest === 'function' || (typeof XMLHttpRequest === "undefined" ? "undefined" : _typeof(XMLHttpRequest)) === 'object' || typeof ActiveXObject === 'function') { return requestWithXmlHttpRequest(options, url, payload, callback); } callback(new Error('No fetch and no xhr implementation found!')); }; export default request;