UNPKG

@mopinion/deploy

Version:

Deploy your Mopinion feedback forms with ease

51 lines (39 loc) 1.5 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports["default"] = request; var _storage = require("./storage"); var getFromStorage = function getFromStorage(useCache, cachePath) { var _getSessionStorage; return useCache && cachePath && Object.keys((_getSessionStorage = (0, _storage.getSessionStorage)(cachePath)) !== null && _getSessionStorage !== void 0 ? _getSessionStorage : {}).length > 0; }; function request(_ref) { var url = _ref.url, cachePath = _ref.cachePath, _ref$useCache = _ref.useCache, useCache = _ref$useCache === void 0 ? true : _ref$useCache, shouldCache = _ref.shouldCache, _ref$onSuccess = _ref.onSuccess, onSuccess = _ref$onSuccess === void 0 ? function () {} : _ref$onSuccess; if (getFromStorage(useCache, cachePath)) { return onSuccess((0, _storage.getSessionStorage)(cachePath)); } var _request = new XMLHttpRequest(); _request.onreadystatechange = function () { if (_request.readyState === XMLHttpRequest.DONE || _request.readyState === 4) { if (_request.status === 200) { try { var parsed = JSON.parse(_request.responseText); if (shouldCache(parsed)) { (0, _storage.setSessionStorage)(cachePath, parsed); } onSuccess(JSON.parse(_request.responseText), false, true); } catch (e) {} } } }; _request.open('get', url, true); _request.send(); } //# sourceMappingURL=request.js.map