client.mjs 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. import axios from "axios";
  2. import logger from "./logger.mjs";
  3. const BackendApi = function (config, token) {
  4. this.config = config;
  5. this.token = token;
  6. this.axios = axios.create({
  7. baseURL: config.baseUrl,
  8. timeout: 90000,
  9. });
  10. };
  11. /**
  12. * @param {string} token
  13. */
  14. BackendApi.prototype.setToken = function (token) {
  15. this.token = token;
  16. };
  17. /**
  18. * @param {bool} returnOnError
  19. */
  20. BackendApi.prototype._prepareOptions = function (returnOnError) {
  21. const options = {
  22. headers: {
  23. Accept: "application/json",
  24. },
  25. };
  26. if (this.token) {
  27. options.headers.Authorization = `Bearer ${this.token}`;
  28. }
  29. if (returnOnError) {
  30. options.validateStatus = () => true;
  31. }
  32. return options;
  33. };
  34. /**
  35. * @param {*} response
  36. * @param {function} resolve
  37. * @param {function} reject
  38. * @param {bool} returnOnError
  39. */
  40. BackendApi.prototype._handleResponse = (
  41. response,
  42. resolve,
  43. reject,
  44. returnOnError,
  45. ) => {
  46. logger("Response data:", response.data);
  47. if (
  48. !returnOnError &&
  49. typeof response.data === "object" &&
  50. typeof response.data.error === "object"
  51. ) {
  52. if (
  53. typeof response.data === "object" &&
  54. typeof response.data.error === "object" &&
  55. typeof response.data.error.message !== "undefined"
  56. ) {
  57. reject(
  58. new Error(
  59. `${response.data.error.code}: ${response.data.error.message}`,
  60. ),
  61. );
  62. } else {
  63. reject(new Error(`Error ${response.status}`));
  64. }
  65. } else {
  66. resolve(response.data);
  67. }
  68. };
  69. /**
  70. * @param {*} err
  71. * @param {function} resolve
  72. * @param {function} reject
  73. * @param {bool} returnOnError
  74. */
  75. BackendApi.prototype._handleError = (err, resolve, reject, returnOnError) => {
  76. logger("Axios Error:", err);
  77. if (returnOnError) {
  78. resolve(typeof err.response.data !== "undefined" ? err.response.data : err);
  79. } else {
  80. reject(err);
  81. }
  82. };
  83. /**
  84. * @param {string} method
  85. * @param {string} path
  86. * @param {bool} [returnOnError]
  87. * @param {*} [data]
  88. * @returns {Promise<object>}
  89. */
  90. BackendApi.prototype.request = function (method, path, returnOnError, data) {
  91. logger(method.toUpperCase(), path);
  92. const options = this._prepareOptions(returnOnError);
  93. return new Promise((resolve, reject) => {
  94. const opts = {
  95. method: method,
  96. url: path,
  97. ...options,
  98. };
  99. if (data !== undefined && data !== null) {
  100. opts.data = data;
  101. }
  102. this.axios(opts)
  103. .then((response) => {
  104. this._handleResponse(response, resolve, reject, returnOnError);
  105. })
  106. .catch((err) => {
  107. this._handleError(err, resolve, reject, returnOnError);
  108. });
  109. });
  110. };
  111. /**
  112. * @param {string} path
  113. * @param {form} form
  114. * @param {bool} [returnOnError]
  115. * @returns {Promise<object>}
  116. */
  117. BackendApi.prototype.postForm = function (path, form, returnOnError) {
  118. logger("POST", this.config.baseUrl + path);
  119. const options = this._prepareOptions(returnOnError);
  120. return new Promise((resolve, reject) => {
  121. const opts = {
  122. ...options,
  123. ...form.getHeaders(),
  124. };
  125. this.axios
  126. .post(path, form, opts)
  127. .then((response) => {
  128. this._handleResponse(response, resolve, reject, returnOnError);
  129. })
  130. .catch((err) => {
  131. this._handleError(err, resolve, reject, returnOnError);
  132. });
  133. });
  134. };
  135. export default BackendApi;