UNPKG

simpleitjs

Version:
139 lines (138 loc) 5.14 kB
"use strict"; /// <reference path="./module.ts" /> /// <reference path="./module-scope.ts" /> var __extends = (this && this.__extends) || (function () { var extendStatics = function (d, b) { extendStatics = Object.setPrototypeOf || ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) || function (d, b) { for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; }; return extendStatics(d, b); }; return function (d, b) { extendStatics(d, b); function __() { this.constructor = d; } d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __()); }; })(); var ModuleScopeClass; if (typeof window === "undefined") { ModuleScopeClass = require('./module-scope'); } else { var win = window; ModuleScopeClass = win.ModuleScope; } /** * Ajax module * Usful to create Ajax XHR requests. * @param {Ajax} Ajax this module * @returns {Ajax} */ var Ajax = /** @class */ (function (_super) { __extends(Ajax, _super); function Ajax() { return _super.call(this) || this; } /** * Creates an XHR Put request. * @static * @param {string} url string URL * @param {any} data Any type of data, mostly json object * @param {Function} onSuccess Optional, success callback function * @param {Function} onFail Optional, fail callback function * @returns {Promise<T>} Promise with data */ Ajax.prototype.put = function (url, data, onSuccess, onFail) { if (onSuccess === void 0) { onSuccess = function () { }; } if (onFail === void 0) { onFail = function () { }; } return new Promise(function (res, rej) { var xhr = new XMLHttpRequest(); xhr.open("PUT", url, true); xhr.setRequestHeader('Content-type', 'application/json; charset=utf-8'); xhr.onload = function () { var data = xhr.responseText; if (xhr.readyState == 4 && xhr.status == 200) { typeof onSuccess == "function" && onSuccess(data); res(data); } else { typeof onFail == "function" && onFail(data); rej(data); } }; xhr.send(JSON.stringify(data)); }); }; ; /** * Creates an XHR Get request. * @static * @param {string} url string URL * @param {Function} onSuccess Optional, success callback function * @param {Function} onFail Optional, fail callback function * @returns {Promise<T>} Promise with data */ Ajax.prototype.get = function (url, onSuccess, onFail) { if (onSuccess === void 0) { onSuccess = function () { }; } if (onFail === void 0) { onFail = function () { }; } return new Promise(function (res, rej) { var xhr = new XMLHttpRequest(); xhr.open('GET', url, true); xhr.onload = function () { var data = xhr.responseText; if (xhr.readyState == 4 && xhr.status == 200) { typeof onSuccess == "function" && onSuccess(data); res(data); } else { typeof onFail == "function" && onFail(data); rej(data); } }; xhr.send(null); }); }; /** * Creates an XHR Post request. * @static * @param {String} url string URL * @param {any} data Any type of data, mostly json object * @param {Function} onSuccess Optional, success callback function * @param {Function} onFail Optional, fail callback function * @returns {Promise<T>} Promise with data */ Ajax.prototype.post = function (url, data, onSuccess, onFail) { if (onSuccess === void 0) { onSuccess = function () { }; } if (onFail === void 0) { onFail = function () { }; } return new Promise(function (res, rej) { var xhr = new XMLHttpRequest(); xhr.open("POST", url, true); xhr.setRequestHeader('Content-type', 'application/json; charset=utf-8'); xhr.onload = function () { var data2 = xhr.responseText; if (xhr.readyState == 4 && xhr.status == 200) { typeof onSuccess == "function" && onSuccess(data2); res(data2); } else { typeof onFail == "function" && onFail(data2); rej(data2); } }; xhr.send(JSON.stringify(data)); }); }; ; return Ajax; }(ModuleScopeClass)); (function () { var ModuleClass; if (typeof window === "undefined") { ModuleClass = require('./module'); } else { var win = window; ModuleClass = win.SimpleJS.Module; } new ModuleClass('Ajax', Ajax); })();