httpRequest.js 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. /**
  2. * @enum{string}
  3. **/
  4. var HttpRequestMethod = {
  5. GET: "GET",
  6. POST: "POST",
  7. PUT: "PUT",
  8. DELETE: "DELETE"
  9. };
  10. /**
  11. * @enum{string}
  12. **/
  13. var HttpRequestResponseType = {
  14. ARRAYBUFFER: "arraybuffer",
  15. BLOB: "blob",
  16. DOCUMENT: "document",
  17. JSON: "json",
  18. TEXT: "text"
  19. };
  20. /**
  21. * @constructor
  22. * @param {HttpRequestMethod|string} urlOrMethod url or method to use (default: GET)
  23. * @param {string=} url
  24. **/
  25. function HttpRequest(urlOrMethod, url) {
  26. /** @const @type {XMLHttpRequest} */
  27. this.xhr = new XMLHttpRequest();
  28. /** @const @type {string} */
  29. this.url = url || urlOrMethod;
  30. /** @const @type {HttpRequestMethod} */
  31. this.method = url ? /** @type {HttpRequestMethod} */ (urlOrMethod) : HttpRequestMethod.GET;
  32. /** @type {Array<function(number, string, (string|Object|null))>|undefined} */
  33. this._callback;
  34. /** @type {Array<function(number, string, (string|Object|null))>|undefined} */
  35. this._callbackError;
  36. /** @type {Array<function(number, string, (string|Object|null))>|undefined} */
  37. this._callbackSuccess;
  38. this.xhr.onreadystatechange = (function(e) {
  39. if (this.xhr.readyState === 4) {
  40. if (Math.floor(this.xhr.status / 100) === 2) {
  41. HttpRequest.fireCallbacks(this._callbackSuccess, this.xhr.status, this.xhr.statusText, this.xhr.response);
  42. } else {
  43. HttpRequest.fireCallbacks(this._callbackError, this.xhr.status, this.xhr.statusText, this.xhr.response);
  44. }
  45. HttpRequest.fireCallbacks(this._callback, this.xhr.status, this.xhr.statusText, this.xhr.response);
  46. }
  47. }).bind(this);
  48. }
  49. /**
  50. * @param {Array<function(number, string, (string|Object|null))>|undefined} callbacks
  51. * @param {number} statusCode
  52. * @param {string} statusText
  53. * @param {string|Object|null} response
  54. **/
  55. HttpRequest.fireCallbacks = function(callbacks, statusCode, statusText, response) {
  56. if (callbacks)
  57. callbacks.forEach(function(cb) {
  58. cb(statusCode, statusText, response);
  59. });
  60. };
  61. /**
  62. * @param {function(number, string, (string|Object|null))} cb callback (statusCode statusText responseText)
  63. * @param {Object=} ctx
  64. * @return {HttpRequest}
  65. **/
  66. HttpRequest.prototype.callback = function(cb, ctx) {
  67. if (!this._callback)
  68. this._callback = [];
  69. cb.bind(ctx || this);
  70. this._callback.push(cb);
  71. return this;
  72. };
  73. /**
  74. * @param {function(number, string, (string|Object|null))} cb callback (statusCode statusText responseText)
  75. * @param {Object=} ctx
  76. * @return {HttpRequest}
  77. **/
  78. HttpRequest.prototype.callbackSuccess = function(cb, ctx) {
  79. if (!this._callbackSuccess)
  80. this._callbackSuccess = [];
  81. cb.bind(ctx || this);
  82. this._callbackSuccess.push(cb);
  83. return this;
  84. };
  85. /**
  86. * @param {function(number, string, (string|Object|null))} cb callback (statusCode statusText responseText)
  87. * @param {Object=} ctx
  88. * @return {HttpRequest}
  89. **/
  90. HttpRequest.prototype.callbackError = function(cb, ctx) {
  91. if (!this._callbackError)
  92. this._callbackError = [];
  93. cb.bind(ctx || this);
  94. this._callbackError.push(cb);
  95. return this;
  96. };
  97. /**
  98. * @param {number} timeo
  99. * @return {HttpRequest}
  100. **/
  101. HttpRequest.prototype.setTimeout = function(timeo) {
  102. this.xhr.timeout = timeo;
  103. return this;
  104. };
  105. /**
  106. * @param {HttpRequestResponseType} respType
  107. * @return {HttpRequest}
  108. **/
  109. HttpRequest.prototype.setResponseType = function(respType) {
  110. this.xhr.responseType = respType;
  111. return this;
  112. };
  113. /**
  114. * @param {string|FormData=} payload
  115. * @return {HttpRequest}
  116. **/
  117. HttpRequest.prototype.send = function(payload) {
  118. this.xhr.open(this.method, this.url, true);
  119. this.xhr.send(payload);
  120. return this;
  121. };