UNPKG

translate-maker

Version:

Lightweight translation module. Internationalize your great project.

84 lines (70 loc) 2.17 kB
import _objectSpread from "@babel/runtime/helpers/objectSpread"; import _asyncToGenerator from "@babel/runtime/helpers/asyncToGenerator"; import Adapter from './Adapter'; import { defaultResolvePath } from './File'; function defaultGetData(_x, _x2) { return _defaultGetData.apply(this, arguments); } function _defaultGetData() { _defaultGetData = _asyncToGenerator(function* (url, parse) { return new Promise((resolve, reject) => { try { const XHRClass = window.XMLHttpRequest || window.ActiveXObject; const xhr = new XHRClass('MSXML2.XMLHTTP.3.0'); xhr.onreadystatechange = () => { if (xhr.readyState > 3) { resolve(parse(xhr.responseText)); } }; xhr.open('GET', url, true); xhr.setRequestHeader('X-Requested-With', 'XMLHttpRequest'); xhr.setRequestHeader('Content-type', 'application/x-www-form-urlencoded'); xhr.send(); } catch (e) { reject(e); } }); }); return _defaultGetData.apply(this, arguments); } export default class XHR extends Adapter { constructor(options = {}) { if (!options.path) { throw new Error('You need to set path'); } super(_objectSpread({ path: undefined, resolvePath: defaultResolvePath, parse: JSON.parse, ext: '.json', getData: defaultGetData, setData: undefined }, options)); } get(locale, namespace) { var _this = this; return _asyncToGenerator(function* () { if (typeof namespace === 'function') { return _this.get(locale, null, namespace); } const options = _this.getOptions(); const { resolvePath, getData, parse } = options; const path = resolvePath(locale, namespace, options); return getData(path, parse); })(); } set(locale, value, namespace) { var _this2 = this; return _asyncToGenerator(function* () { if (typeof namespace === 'function') { return _this2.set(locale, value, null, namespace); } throw new Error('XHR adapter is read only'); })(); } } //# sourceMappingURL=XHR.js.map