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.
118 lines (117 loc) • 5.62 kB
JavaScript
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.default = void 0;
function _getRequireWildcardCache(e) { if ("function" != typeof WeakMap) return null; var r = new WeakMap(), t = new WeakMap(); return (_getRequireWildcardCache = function _getRequireWildcardCache(e) { return e ? t : r; })(e); }
function _interopRequireWildcard(e, r) { if (!r && e && e.__esModule) return e; if (null === e || "object" != _typeof(e) && "function" != typeof e) return { default: e }; var t = _getRequireWildcardCache(r); if (t && t.has(e)) return t.get(e); var n = { __proto__: null }, a = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var u in e) if ("default" !== u && {}.hasOwnProperty.call(e, u)) { var i = a ? Object.getOwnPropertyDescriptor(e, u) : null; i && (i.get || i.set) ? Object.defineProperty(n, u, i) : n[u] = e[u]; } return n.default = e, t && t.set(e, n), n; }
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 {
fetchApi = require('cross-fetch');
} 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!'));
};
var _default = exports.default = request;
module.exports = exports.default;
;