api.client.js 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. "use strict";
  2. var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
  3. function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
  4. return new (P || (P = Promise))(function (resolve, reject) {
  5. function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
  6. function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
  7. function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
  8. step((generator = generator.apply(thisArg, _arguments || [])).next());
  9. });
  10. };
  11. var __importDefault = (this && this.__importDefault) || function (mod) {
  12. return (mod && mod.__esModule) ? mod : { "default": mod };
  13. };
  14. Object.defineProperty(exports, "__esModule", { value: true });
  15. exports.HttpClient = void 0;
  16. const axios_1 = __importDefault(require("axios"));
  17. class HttpClient {
  18. constructor(backendUrl) {
  19. this.backendUrl = backendUrl;
  20. this.axiosClient = axios_1.default.create({
  21. baseURL: backendUrl + '/v1',
  22. });
  23. }
  24. setAuthorizationToken(token) {
  25. this.axiosClient.defaults.headers.common.Authorization = `Bearer ${token}`;
  26. }
  27. disposeAuthorizationToken() {
  28. delete this.axiosClient.defaults.headers.common.Authorization;
  29. }
  30. getFullResponse(url, params) {
  31. return __awaiter(this, void 0, void 0, function* () {
  32. return yield this.axiosClient
  33. .get(url, {
  34. params,
  35. })
  36. .then((response) => response.data);
  37. });
  38. }
  39. get(url, params) {
  40. return __awaiter(this, void 0, void 0, function* () {
  41. return yield this.axiosClient
  42. .get(url, {
  43. params,
  44. })
  45. .then((response) => response.data.data);
  46. });
  47. }
  48. post(url, body = {}) {
  49. return __awaiter(this, void 0, void 0, function* () {
  50. return yield this.axiosClient.post(url, body).then((response) => response.data.data);
  51. });
  52. }
  53. patch(url, body = {}) {
  54. return __awaiter(this, void 0, void 0, function* () {
  55. return yield this.axiosClient.patch(url, body).then((response) => response.data.data);
  56. });
  57. }
  58. delete(url, body = {}) {
  59. return __awaiter(this, void 0, void 0, function* () {
  60. return yield this.axiosClient.delete(url, body).then((response) => response.data.data);
  61. });
  62. }
  63. }
  64. exports.HttpClient = HttpClient;