base_collection.js 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", { value: true });
  3. exports.BaseCollection = void 0;
  4. const base_1 = require("../http_client/base");
  5. const paginated_result_1 = require("../models/paginated_result");
  6. class BaseCollection {
  7. clientData;
  8. static rootElementName;
  9. static rootElementNameSingular;
  10. static endpoint;
  11. static prefixURI;
  12. static elementClass;
  13. // Secondaries are used when an instance of a different class has to be created
  14. // For example, uploading a File may return a QueuedProcess
  15. static secondaryElementNameSingular;
  16. static secondaryElementClass;
  17. constructor(clientData) {
  18. this.clientData = clientData;
  19. }
  20. doList(params) {
  21. return this.createPromise("GET", params, this.populateArrayFromJson, this.handleReject, null);
  22. }
  23. doGet(id, params = {}) {
  24. params["id"] = id;
  25. return this.createPromise("GET", params, this.populateObjectFromJsonRoot, this.handleReject, null);
  26. }
  27. doDelete(id, params = {}) {
  28. params["id"] = id;
  29. return this.createPromise("DELETE", params, this.returnBareJSON, this.handleReject, null);
  30. }
  31. doCreate(body, params = {}, resolveFn = this.populateObjectFromJson) {
  32. return this.createPromise("POST", params, resolveFn, this.handleReject, body);
  33. }
  34. doUpdate(id, body, req_params, resolveFn = this.populateObjectFromJsonRoot) {
  35. const params = {
  36. ...req_params,
  37. ...{ id: id },
  38. };
  39. return this.createPromise("PUT", params, resolveFn, this.handleReject, body);
  40. }
  41. populateObjectFromJsonRoot(json, headers) {
  42. const childClass = this.constructor;
  43. if (childClass.rootElementNameSingular) {
  44. json = Object(json)[childClass.rootElementNameSingular];
  45. }
  46. return this.populateObjectFromJson(json, headers);
  47. }
  48. populateSecondaryObjectFromJsonRoot(json, headers) {
  49. const childClass = this.constructor;
  50. json = Object(json)[childClass.secondaryElementNameSingular];
  51. return this.populateObjectFromJson(json, headers, true);
  52. }
  53. populateObjectFromJson(json, _headers, secondary = false) {
  54. const childClass = this.constructor;
  55. if (secondary) {
  56. return new childClass.secondaryElementClass(json);
  57. }
  58. else {
  59. return new childClass.elementClass(json);
  60. }
  61. }
  62. populateArrayFromJsonBulk(json, headers) {
  63. const childClass = this.constructor;
  64. const arr = [];
  65. const jsonArray = json[childClass.rootElementName];
  66. for (const obj of jsonArray) {
  67. arr.push(this.populateObjectFromJson(obj, headers));
  68. }
  69. const result = {
  70. errors: json["errors"],
  71. items: arr,
  72. };
  73. return result;
  74. }
  75. populateArrayFromJson(json, headers) {
  76. const childClass = this.constructor;
  77. const arr = [];
  78. const jsonArray = json[childClass.rootElementName];
  79. for (const obj of jsonArray) {
  80. arr.push(this.populateObjectFromJson(obj, headers));
  81. }
  82. if (headers["x-pagination-total-count"] && headers["x-pagination-page"]) {
  83. const result = new paginated_result_1.PaginatedResult(arr, headers);
  84. return result;
  85. }
  86. else {
  87. return arr;
  88. }
  89. }
  90. populateApiErrorFromJson(json) {
  91. return json;
  92. }
  93. returnBareJSON(json) {
  94. return json;
  95. }
  96. handleReject(data) {
  97. return this.populateApiErrorFromJson(data);
  98. }
  99. async createPromise(method, params, resolveFn, rejectFn, body, uri = null) {
  100. const childClass = this.constructor;
  101. if (!uri) {
  102. uri = childClass.prefixURI;
  103. }
  104. const request = new base_1.ApiRequest(uri, method, body, params, this.clientData);
  105. try {
  106. const data = await request.promise;
  107. return Promise.resolve(resolveFn.call(this, data["json"], data["headers"]));
  108. }
  109. catch (err) {
  110. return Promise.reject(rejectFn.call(this, err));
  111. }
  112. }
  113. objToArray(raw_body) {
  114. if (!Array.isArray(raw_body)) {
  115. return Array(raw_body);
  116. }
  117. else {
  118. return raw_body;
  119. }
  120. }
  121. }
  122. exports.BaseCollection = BaseCollection;
  123. //# sourceMappingURL=base_collection.js.map