ironwing
Version:
Ironwing is a lightweight front-end data library for model like data representations
132 lines (109 loc) • 3.73 kB
JavaScript
/**
* XHR is a wrapper over the XMLHttpRequest object
* @return {Object}
*/
'use strict';
Object.defineProperty(exports, '__esModule', {
value: true
});
var _createClass = (function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ('value' in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; })();
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError('Cannot call a class as a function'); } }
var _coreUtils = require('../../core/utils');
var XHRJson = (function () {
function XHRJson() {
_classCallCheck(this, XHRJson);
this.xhr = typeof XMLHttpRequest === 'function' ? new XMLHttpRequest() : null;
this.apiUrl = null;
this.done = null;
this.fail = null;
}
_createClass(XHRJson, [{
key: 'setUrl',
value: function setUrl(url) {
this.apiUrl = (0, _coreUtils.checkURL)(url);
}
/**
* Constructor
* @param {String} url The API URL
*/
}, {
key: 'init',
value: function init(url) {
this.apiUrl = (0, _coreUtils.checkURL)(url);
this.done = null;
this.fail = null;
this.xhr.onreadystatechange = (function () {
var responseObj;
if (this.xhr.readyState === 4 && this.xhr.status === 200 && typeof this.done === 'function') {
responseObj = JSON.parse(this.xhr.responseText);
this.done.call(null, responseObj);
} else if (this.xhr.readyState === 4 && this.xhr.status === 404 && typeof this.fail === 'function') {
this.fail.call(null);
}
}).bind(this);
}
/**
* Perform an AJAX request
* @param {String} method HTTP method
* @param {String} url URL string
* @param {Boolean} async
* @param {Object} data POST/PUT data
*/
}, {
key: 'ajax',
value: function ajax(method, url, async, data) {
method = method.toUpperCase();
if (this.xhr) {
this.xhr.open(method, this.apiUrl + url, async);
if (method === 'POST' || method === 'PUT') {
this.xhr.setRequestHeader('Content-Type', 'application/json;charset=UTF-8');
this.xhr.send(JSON.stringify(data));
} else {
this.xhr.send();
}
} else {
var response = require(this.apiUrl + url);
if (method === 'POST') {
data.attr.id = 1000;
this.done.call(null, data.attr);
} else {
this.done.call(null, response);
}
}
}
/**
* URL getter
* @return {String} URL string
*/
}, {
key: 'getAPIURL',
value: function getAPIURL() {
return this.apiUrl;
}
/**
* Callback which is if the request is done
* @param {Function} callback Callback function
* @return {Object} Mjs
*/
}, {
key: 'onDone',
value: function onDone(callback) {
this.done = callback;
return this;
}
/**
* Callback which is if the request failed
* @param {Function} callback Callback function
* @return {Object} Mjs
*/
}, {
key: 'onFail',
value: function onFail(callback) {
this.fail = callback;
return this;
}
}]);
return XHRJson;
})();
exports['default'] = XHRJson;
module.exports = exports['default'];