| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278 |
- /**
- * @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"
- };
- /**
- * @abstract
- * @constructor
- * @param {HttpRequestMethod|string} urlOrMethod url or method to use (default: GET)
- * @param {string=} url
- **/
- function HttpRequest(urlOrMethod, url) {
- /** @const @type {string} */
- this.url = url || urlOrMethod;
- /** @const @type {HttpRequestMethod} */
- this.method = url ? /** @type {HttpRequestMethod} */ (urlOrMethod) : HttpRequestMethod.GET;
- /** @type {Array<function(number, string, *)>|undefined} */
- this._callback;
- /** @type {Array<function(number, string, *)>|undefined} */
- this._callbackError;
- /** @type {Array<function(number, string, *)>|undefined} */
- this._callbackSuccess;
- }
- /**
- * @param {Array<function(number, string, *)>|undefined} callbacks
- * @param {number} statusCode
- * @param {string} statusText
- * @param {*} 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;
- };
- /**
- * @abstract
- * @param {number} timeo
- * @return {HttpRequest}
- **/
- HttpRequest.prototype.setTimeout = function(timeo) {};
- /**
- * @abstract
- * @param {HttpRequestResponseType} respType
- * @return {HttpRequest}
- **/
- HttpRequest.prototype.setResponseType = function(respType) {};
- /**
- * @abstract
- * @param {string|FormData=} payload
- * @return {HttpRequest}
- **/
- HttpRequest.prototype.send = function(payload) {};
- /**
- * @constructor
- * @extends {HttpRequest}
- * @param {HttpRequestMethod|string} urlOrMethod url or method to use (default: GET)
- * @param {string=} url
- **/
- function XHRHttpRequest(urlOrMethod, url) {
- HttpRequest.call(this, urlOrMethod, url);
- /** @const @type {XMLHttpRequest} */
- this.xhr = new XMLHttpRequest();
- 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);
- }
- XHRHttpRequest.prototype = Object.create(HttpRequest.prototype);
- XHRHttpRequest.prototype.constructor = XHRHttpRequest;
- /**
- * @param {number} timeo
- * @return {HttpRequest}
- **/
- XHRHttpRequest.prototype.setTimeout = function(timeo) {
- this.xhr.timeout = timeo;
- return this;
- };
- /**
- * @param {HttpRequestResponseType} respType
- * @return {HttpRequest}
- **/
- XHRHttpRequest.prototype.setResponseType = function(respType) {
- this.xhr.responseType = respType;
- return this;
- };
- /**
- * @param {string|FormData=} payload
- * @return {HttpRequest}
- **/
- XHRHttpRequest.prototype.send = function(payload) {
- this.xhr.open(this.method, this.url, true);
- this.xhr.send(payload);
- return this;
- };
- /**
- * @constructor
- * @extends {HttpRequest}
- * @param {HttpRequestMethod|string} urlOrMethod url or method to use (default: GET)
- * @param {string=} url
- **/
- function NativeHttpRequest(urlOrMethod, url) {
- HttpRequest.call(this, urlOrMethod, url);
- /** @type {number} */
- this.timeo = 5000;
- /** @type {HttpRequestResponseType} */
- this.responseType = HttpRequestResponseType.TEXT;
- }
- NativeHttpRequest.prototype = Object.create(HttpRequest.prototype);
- NativeHttpRequest.prototype.constructor = XHRHttpRequest;
- /**
- * @param {number} timeo
- * @return {HttpRequest}
- **/
- NativeHttpRequest.prototype.setTimeout = function(timeo) {
- this.timeo = timeo;
- return this;
- };
- /**
- * @param {HttpRequestResponseType} respType
- * @return {HttpRequest}
- **/
- NativeHttpRequest.prototype.setResponseType = function(respType) {
- this.responseType = respType;
- return this;
- };
- /**
- * @param {string|FormData=} payload
- * @return {string|null}
- **/
- NativeHttpRequest.prototype.stringifyPayload = function(payload) {
- if (!payload) return null;
- if (typeof payload === "string") return payload;
- var obj = {};
- for (var i in payload)
- obj[i] = payload[i];
- return JSON.stringify(obj);
- };
- /** @type {function(string):string} */
- NativeHttpRequest.makeAbsoluteUrl = (function() {
- var a = document.createElement('a');
- return function(link) {
- a.href = link;
- return a.href;
- };
- })();
- /**
- * @param {string|FormData=} payload
- * @return {HttpRequest}
- **/
- NativeHttpRequest.prototype.send = function(payload) {
- __native.sendHttpRequest(this.method, NativeHttpRequest.makeAbsoluteUrl(this.url), this.timeo, this.responseType, this.stringifyPayload(payload), CALLBACK.makeCallback(function(statusCode, statusText, respText) {
- var resp;
- try {
- resp = this.responseType === HttpRequestResponseType.TEXT ? respText : JSON.parse(respText);
- } catch (e) {
- resp = respText;
- }
- if (__native.isDebug())
- console.log(this, {
- statusCode: statusCode,
- statusText: statusText,
- resp: respText
- });
- if (Math.floor(statusCode / 100) === 2) {
- HttpRequest.fireCallbacks(this._callbackSuccess, statusCode, statusText, resp);
- } else {
- HttpRequest.fireCallbacks(this._callbackError, statusCode, statusText, resp);
- }
- HttpRequest.fireCallbacks(this._callback, statusCode, statusText, resp);
- }, this));
- return this;
- };
- /**
- * @param {HttpRequestMethod|string} urlOrMethod
- * @param {string=} url
- * @return {HttpRequest}
- **/
- function HttpRequestWrapper(urlOrMethod, url) {
- return isNative() ? new NativeHttpRequest(urlOrMethod, url) : new XHRHttpRequest(urlOrMethod, url);
- }
- HttpRequestWrapper.encode = function(str) {
- return encodeURIComponnent(str);
- }
|