api.js 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. 'use strict';
  2. import $ from 'jquery';
  3. /**
  4. * @param {String} message
  5. * @param {*} debug
  6. * @param {Number} code
  7. * @constructor
  8. */
  9. const ApiError = function (message, debug, code) {
  10. let temp = Error.call(this, message);
  11. temp.name = this.name = 'ApiError';
  12. this.stack = temp.stack;
  13. this.message = temp.message;
  14. this.debug = debug;
  15. this.code = code;
  16. };
  17. ApiError.prototype = Object.create(Error.prototype, {
  18. constructor: {
  19. value: ApiError,
  20. writable: true,
  21. configurable: true
  22. }
  23. });
  24. /**
  25. *
  26. * @param {String} verb
  27. * @param {String} path
  28. * @param {Object} [data]
  29. * @param {Object} [options]
  30. * @returns {Promise}
  31. */
  32. function fetch (verb, path, data, options) {
  33. options = options || {};
  34. return new Promise(function (resolve, reject) {
  35. let api_url = '/api/';
  36. let url = api_url + path;
  37. $.ajax({
  38. url: url,
  39. data: typeof data === 'object' && data !== null ? JSON.stringify(data) : data,
  40. type: verb,
  41. dataType: 'json',
  42. contentType: 'application/json; charset=UTF-8',
  43. crossDomain: true,
  44. timeout: (options.timeout ? options.timeout : 30000),
  45. xhrFields: {
  46. withCredentials: true
  47. },
  48. success: function (data, textStatus, response) {
  49. resolve(response);
  50. },
  51. error: function (xhr, status, error_thrown) {
  52. let code = 400;
  53. if (typeof xhr.responseJSON !== 'undefined' && typeof xhr.responseJSON.error !== 'undefined' && typeof xhr.responseJSON.error.message !== 'undefined') {
  54. error_thrown = xhr.responseJSON.error.message;
  55. code = xhr.responseJSON.error.code || 500;
  56. }
  57. reject(new ApiError(error_thrown, xhr.responseText, code));
  58. }
  59. });
  60. });
  61. }
  62. module.exports = {
  63. status: function () {
  64. return fetch('get', '');
  65. },
  66. Hosts: {
  67. /**
  68. * @returns {Promise}
  69. */
  70. getAll: function () {
  71. return fetch('get', 'hosts');
  72. },
  73. /**
  74. * @param {Object} data
  75. * @returns {Promise}
  76. */
  77. create: function (data) {
  78. return fetch('post', 'hosts', data);
  79. },
  80. /**
  81. * @param {Object} data
  82. * @param {String} data._id
  83. * @returns {Promise}
  84. */
  85. update: function (data) {
  86. let _id = data._id;
  87. delete data._id;
  88. return fetch('put', 'hosts/' + _id, data);
  89. },
  90. /**
  91. * @param {String} _id
  92. * @returns {Promise}
  93. */
  94. delete: function (_id) {
  95. return fetch('delete', 'hosts/' + _id);
  96. },
  97. /**
  98. * @param {String} _id
  99. * @returns {Promise}
  100. */
  101. reconfigure: function (_id) {
  102. return fetch('post', 'hosts/' + _id + '/reconfigure');
  103. },
  104. /**
  105. * @param {String} _id
  106. * @returns {Promise}
  107. */
  108. renew: function (_id) {
  109. return fetch('post', 'hosts/' + _id + '/renew');
  110. }
  111. },
  112. Access: {
  113. /**
  114. * @returns {Promise}
  115. */
  116. getAll: function () {
  117. return fetch('get', 'access');
  118. },
  119. /**
  120. * @param {Object} data
  121. * @returns {Promise}
  122. */
  123. create: function (data) {
  124. return fetch('post', 'access', data);
  125. },
  126. /**
  127. * @param {Object} data
  128. * @param {String} data._id
  129. * @returns {Promise}
  130. */
  131. update: function (data) {
  132. let _id = data._id;
  133. delete data._id;
  134. return fetch('put', 'access/' + _id, data);
  135. },
  136. /**
  137. * @param {String} _id
  138. * @returns {Promise}
  139. */
  140. delete: function (_id) {
  141. return fetch('delete', 'access/' + _id);
  142. }
  143. }
  144. };