| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140 |
- /**
- * @enum{string}
- **/
- var HttpRequestMethod = {
- GET: "GET",
- POST: "POST",
- PUT: "PUT",
- DELETE: "DELETE"
- };
- /**
- * @enum{string}
- **/
- var HttpRequestResponseType = {
- ARRAYBUFFER: "arraybuffer",
- BLOB: "blob",
- DOCUMENT: "document",
- JSON: "json",
- TEXT: "text"
- };
- /**
- * @constructor
- * @param {HttpRequestMethod|string} urlOrMethod url or method to use (default: GET)
- * @param {string=} url
- **/
- function HttpRequest(urlOrMethod, url) {
- /** @const @type {XMLHttpRequest} */
- this.xhr = new XMLHttpRequest();
- /** @const @type {string} */
- this.url = url || urlOrMethod;
- /** @const @type {HttpRequestMethod} */
- this.method = url ? /** @type {HttpRequestMethod} */ (urlOrMethod) : HttpRequestMethod.GET;
- /** @type {Array<function(number, string, (string|Object|null))>|undefined} */
- this._callback;
- /** @type {Array<function(number, string, (string|Object|null))>|undefined} */
- this._callbackError;
- /** @type {Array<function(number, string, (string|Object|null))>|undefined} */
- this._callbackSuccess;
- this.xhr.onreadystatechange = (function(e) {
- if (this.xhr.readyState === 4) {
- if (Math.floor(this.xhr.status / 100) === 2) {
- HttpRequest.fireCallbacks(this._callbackSuccess, this.xhr.status, this.xhr.statusText, this.xhr.response);
- } else {
- HttpRequest.fireCallbacks(this._callbackError, this.xhr.status, this.xhr.statusText, this.xhr.response);
- }
- HttpRequest.fireCallbacks(this._callback, this.xhr.status, this.xhr.statusText, this.xhr.response);
- }
- }).bind(this);
- }
- /**
- * @param {Array<function(number, string, (string|Object|null))>|undefined} callbacks
- * @param {number} statusCode
- * @param {string} statusText
- * @param {string|Object|null} response
- **/
- HttpRequest.fireCallbacks = function(callbacks, statusCode, statusText, response) {
- if (callbacks)
- callbacks.forEach(function(cb) {
- cb(statusCode, statusText, response);
- });
- };
- /**
- * @param {function(number, string, (string|Object|null))} cb callback (statusCode statusText responseText)
- * @param {Object=} ctx
- * @return {HttpRequest}
- **/
- HttpRequest.prototype.callback = function(cb, ctx) {
- if (!this._callback)
- this._callback = [];
- cb.bind(ctx || this);
- this._callback.push(cb);
- return this;
- };
- /**
- * @param {function(number, string, (string|Object|null))} cb callback (statusCode statusText responseText)
- * @param {Object=} ctx
- * @return {HttpRequest}
- **/
- HttpRequest.prototype.callbackSuccess = function(cb, ctx) {
- if (!this._callbackSuccess)
- this._callbackSuccess = [];
- cb.bind(ctx || this);
- this._callbackSuccess.push(cb);
- return this;
- };
- /**
- * @param {function(number, string, (string|Object|null))} cb callback (statusCode statusText responseText)
- * @param {Object=} ctx
- * @return {HttpRequest}
- **/
- HttpRequest.prototype.callbackError = function(cb, ctx) {
- if (!this._callbackError)
- this._callbackError = [];
- cb.bind(ctx || this);
- this._callbackError.push(cb);
- return this;
- };
- /**
- * @param {number} timeo
- * @return {HttpRequest}
- **/
- HttpRequest.prototype.setTimeout = function(timeo) {
- this.xhr.timeout = timeo;
- return this;
- };
- /**
- * @param {HttpRequestResponseType} respType
- * @return {HttpRequest}
- **/
- HttpRequest.prototype.setResponseType = function(respType) {
- this.xhr.responseType = respType;
- return this;
- };
- /**
- * @param {string|FormData=} payload
- * @return {HttpRequest}
- **/
- HttpRequest.prototype.send = function(payload) {
- this.xhr.open(this.method, this.url, true);
- this.xhr.send(payload);
- return this;
- };
|