BaseApi.js 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", { value: true });
  3. exports.BaseApi = void 0;
  4. const OpenApiError_1 = require("../api/OpenApiError");
  5. class BaseApi {
  6. /** Die URL des Servers. Alle Pfadangaben sind relativ zu dieser URL. */
  7. url;
  8. /** Der Anmeldename beim Server */
  9. username;
  10. /** Der Default-RequestInit für einen Fetch */
  11. #requestinit = {
  12. cache: 'no-cache',
  13. credentials: 'same-origin'
  14. };
  15. /** Die Default-Header-Einträge */
  16. #headers = {};
  17. /**
  18. * Erstellt eine neue API mit der übergebenen Konfiguration.
  19. *
  20. * @param {string} url - die URL des Servers: Alle Pfadangaben sind relativ zu dieser URL
  21. * @param {string} username - der Benutzername für den API-Zugriff
  22. * @param {string} password - das Kennwort des Benutzers für den API-Zugriff
  23. */
  24. constructor(url, username, password) {
  25. this.url = url;
  26. this.username = username;
  27. this.#headers["Authorization"] = "Basic " + btoa(username + ":" + password);
  28. }
  29. #getURL(path) {
  30. return this.url + path;
  31. }
  32. async #getBinary(path, mimetype) {
  33. let requestInit = { ...this.#requestinit };
  34. requestInit.headers = { ...this.#headers };
  35. requestInit.headers["Accept"] = mimetype;
  36. requestInit.body = null;
  37. requestInit.method = 'GET';
  38. try {
  39. const response = await fetch(this.#getURL(path), requestInit);
  40. if (!response.ok)
  41. throw new OpenApiError_1.OpenApiError(response, 'Fetch failed for GET: ' + path);
  42. return await response.blob();
  43. }
  44. catch (e) {
  45. if (e instanceof Error)
  46. throw (e instanceof OpenApiError_1.OpenApiError) ? e : new OpenApiError_1.OpenApiError(e, 'Fetch failed for GET: ' + path);
  47. throw new Error("Unexpected Error: " + e);
  48. }
  49. }
  50. getPDF(path) {
  51. return this.#getBinary(path, 'application/pdf');
  52. }
  53. async #getTextBased(path, mimetype) {
  54. let requestInit = { ...this.#requestinit };
  55. requestInit.headers = { ...this.#headers };
  56. requestInit.headers["Accept"] = mimetype;
  57. requestInit.body = null;
  58. requestInit.method = 'GET';
  59. try {
  60. const response = await fetch(this.#getURL(path), requestInit);
  61. if (!response.ok)
  62. throw new OpenApiError_1.OpenApiError(response, 'Fetch failed for GET: ' + path);
  63. return await response.text();
  64. }
  65. catch (e) {
  66. if (e instanceof Error)
  67. throw (e instanceof OpenApiError_1.OpenApiError) ? e : new OpenApiError_1.OpenApiError(e, 'Fetch failed for GET: ' + path);
  68. throw new Error("Unexpected Error: " + e);
  69. }
  70. }
  71. getText(path) {
  72. return this.#getTextBased(path, 'text/plain');
  73. }
  74. getJSON(path) {
  75. return this.#getTextBased(path, 'application/json');
  76. }
  77. async #postTextBased(path, mimetype_send, mimetype_receive, body) {
  78. let requestInit = { ...this.#requestinit };
  79. requestInit.headers = { ...this.#headers };
  80. requestInit.headers["Content-Type"] = mimetype_send;
  81. requestInit.headers["Accept"] = mimetype_receive;
  82. requestInit.body = body;
  83. requestInit.method = 'POST';
  84. try {
  85. const response = await fetch(this.#getURL(path), requestInit);
  86. if (!response.ok)
  87. throw new OpenApiError_1.OpenApiError(response, 'Fetch failed for POST: ' + path);
  88. return await response.text();
  89. }
  90. catch (e) {
  91. if (e instanceof Error)
  92. throw (e instanceof OpenApiError_1.OpenApiError) ? e : new OpenApiError_1.OpenApiError(e, 'Fetch failed for POST: ' + path);
  93. throw new Error("Unexpected Error: " + e);
  94. }
  95. }
  96. postText(path, body) {
  97. return this.#postTextBased(path, 'text/plain', 'text/plain', body);
  98. }
  99. postJSON(path, body) {
  100. return this.#postTextBased(path, 'application/json', 'application/json', body);
  101. }
  102. async #patchTextBased(path, mimetype, body) {
  103. let requestInit = { ...this.#requestinit };
  104. requestInit.headers = { ...this.#headers };
  105. requestInit.headers["Content-Type"] = mimetype;
  106. requestInit.body = body;
  107. requestInit.method = 'PATCH';
  108. try {
  109. const response = await fetch(this.#getURL(path), requestInit);
  110. if (!response.ok)
  111. throw new OpenApiError_1.OpenApiError(response, 'Fetch failed for PATCH: ' + path);
  112. return;
  113. }
  114. catch (e) {
  115. if (e instanceof Error)
  116. throw (e instanceof OpenApiError_1.OpenApiError) ? e : new OpenApiError_1.OpenApiError(e, 'Fetch failed for PATCH: ' + path);
  117. throw new Error("Unexpected Error: " + e);
  118. }
  119. }
  120. patchText(path, body) {
  121. return this.#patchTextBased(path, 'text/plain', body);
  122. }
  123. patchJSON(path, body) {
  124. return this.#patchTextBased(path, 'application/json', body);
  125. }
  126. async #putTextBased(path, mimetype, body) {
  127. let requestInit = { ...this.#requestinit };
  128. requestInit.headers = { ...this.#headers };
  129. requestInit.headers["Content-Type"] = mimetype;
  130. requestInit.body = body;
  131. requestInit.method = 'PUT';
  132. try {
  133. const response = await fetch(this.#getURL(path), requestInit);
  134. if (!response.ok)
  135. throw new OpenApiError_1.OpenApiError(response, 'Fetch failed for PUT: ' + path);
  136. return;
  137. }
  138. catch (e) {
  139. if (e instanceof Error)
  140. throw (e instanceof OpenApiError_1.OpenApiError) ? e : new OpenApiError_1.OpenApiError(e, 'Fetch failed for PUT: ' + path);
  141. throw new Error("Unexpected Error: " + e);
  142. }
  143. }
  144. putText(path, body) {
  145. return this.#putTextBased(path, 'text/plain', body);
  146. }
  147. putJSON(path, body) {
  148. return this.#putTextBased(path, 'application/json', body);
  149. }
  150. async #deleteTextBased(path, mimetype) {
  151. let requestInit = { ...this.#requestinit };
  152. requestInit.headers = { ...this.#headers };
  153. requestInit.headers["Accept"] = mimetype;
  154. requestInit.body = null;
  155. requestInit.method = 'DELETE';
  156. try {
  157. const response = await fetch(this.#getURL(path), requestInit);
  158. if (!response.ok)
  159. throw new OpenApiError_1.OpenApiError(response, 'Fetch failed for DELETE: ' + path);
  160. return;
  161. }
  162. catch (e) {
  163. if (e instanceof Error)
  164. throw (e instanceof OpenApiError_1.OpenApiError) ? e : new OpenApiError_1.OpenApiError(e, 'Fetch failed for DELETE: ' + path);
  165. throw new Error("Unexpected Error: " + e);
  166. }
  167. }
  168. deleteText(path) {
  169. return this.#deleteTextBased(path, 'text/plain');
  170. }
  171. deleteJSON(path) {
  172. return this.#deleteTextBased(path, 'application/json');
  173. }
  174. }
  175. exports.BaseApi = BaseApi;
  176. //# sourceMappingURL=BaseApi.js.map