123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176 |
- "use strict";
- Object.defineProperty(exports, "__esModule", { value: true });
- exports.BaseApi = void 0;
- const OpenApiError_1 = require("../api/OpenApiError");
- class BaseApi {
- /** Die URL des Servers. Alle Pfadangaben sind relativ zu dieser URL. */
- url;
- /** Der Anmeldename beim Server */
- username;
- /** Der Default-RequestInit für einen Fetch */
- #requestinit = {
- cache: 'no-cache',
- credentials: 'same-origin'
- };
- /** Die Default-Header-Einträge */
- #headers = {};
- /**
- * Erstellt eine neue API mit der übergebenen Konfiguration.
- *
- * @param {string} url - die URL des Servers: Alle Pfadangaben sind relativ zu dieser URL
- * @param {string} username - der Benutzername für den API-Zugriff
- * @param {string} password - das Kennwort des Benutzers für den API-Zugriff
- */
- constructor(url, username, password) {
- this.url = url;
- this.username = username;
- this.#headers["Authorization"] = "Basic " + btoa(username + ":" + password);
- }
- #getURL(path) {
- return this.url + path;
- }
- async #getBinary(path, mimetype) {
- let requestInit = { ...this.#requestinit };
- requestInit.headers = { ...this.#headers };
- requestInit.headers["Accept"] = mimetype;
- requestInit.body = null;
- requestInit.method = 'GET';
- try {
- const response = await fetch(this.#getURL(path), requestInit);
- if (!response.ok)
- throw new OpenApiError_1.OpenApiError(response, 'Fetch failed for GET: ' + path);
- return await response.blob();
- }
- catch (e) {
- if (e instanceof Error)
- throw (e instanceof OpenApiError_1.OpenApiError) ? e : new OpenApiError_1.OpenApiError(e, 'Fetch failed for GET: ' + path);
- throw new Error("Unexpected Error: " + e);
- }
- }
- getPDF(path) {
- return this.#getBinary(path, 'application/pdf');
- }
- async #getTextBased(path, mimetype) {
- let requestInit = { ...this.#requestinit };
- requestInit.headers = { ...this.#headers };
- requestInit.headers["Accept"] = mimetype;
- requestInit.body = null;
- requestInit.method = 'GET';
- try {
- const response = await fetch(this.#getURL(path), requestInit);
- if (!response.ok)
- throw new OpenApiError_1.OpenApiError(response, 'Fetch failed for GET: ' + path);
- return await response.text();
- }
- catch (e) {
- if (e instanceof Error)
- throw (e instanceof OpenApiError_1.OpenApiError) ? e : new OpenApiError_1.OpenApiError(e, 'Fetch failed for GET: ' + path);
- throw new Error("Unexpected Error: " + e);
- }
- }
- getText(path) {
- return this.#getTextBased(path, 'text/plain');
- }
- getJSON(path) {
- return this.#getTextBased(path, 'application/json');
- }
- async #postTextBased(path, mimetype_send, mimetype_receive, body) {
- let requestInit = { ...this.#requestinit };
- requestInit.headers = { ...this.#headers };
- requestInit.headers["Content-Type"] = mimetype_send;
- requestInit.headers["Accept"] = mimetype_receive;
- requestInit.body = body;
- requestInit.method = 'POST';
- try {
- const response = await fetch(this.#getURL(path), requestInit);
- if (!response.ok)
- throw new OpenApiError_1.OpenApiError(response, 'Fetch failed for POST: ' + path);
- return await response.text();
- }
- catch (e) {
- if (e instanceof Error)
- throw (e instanceof OpenApiError_1.OpenApiError) ? e : new OpenApiError_1.OpenApiError(e, 'Fetch failed for POST: ' + path);
- throw new Error("Unexpected Error: " + e);
- }
- }
- postText(path, body) {
- return this.#postTextBased(path, 'text/plain', 'text/plain', body);
- }
- postJSON(path, body) {
- return this.#postTextBased(path, 'application/json', 'application/json', body);
- }
- async #patchTextBased(path, mimetype, body) {
- let requestInit = { ...this.#requestinit };
- requestInit.headers = { ...this.#headers };
- requestInit.headers["Content-Type"] = mimetype;
- requestInit.body = body;
- requestInit.method = 'PATCH';
- try {
- const response = await fetch(this.#getURL(path), requestInit);
- if (!response.ok)
- throw new OpenApiError_1.OpenApiError(response, 'Fetch failed for PATCH: ' + path);
- return;
- }
- catch (e) {
- if (e instanceof Error)
- throw (e instanceof OpenApiError_1.OpenApiError) ? e : new OpenApiError_1.OpenApiError(e, 'Fetch failed for PATCH: ' + path);
- throw new Error("Unexpected Error: " + e);
- }
- }
- patchText(path, body) {
- return this.#patchTextBased(path, 'text/plain', body);
- }
- patchJSON(path, body) {
- return this.#patchTextBased(path, 'application/json', body);
- }
- async #putTextBased(path, mimetype, body) {
- let requestInit = { ...this.#requestinit };
- requestInit.headers = { ...this.#headers };
- requestInit.headers["Content-Type"] = mimetype;
- requestInit.body = body;
- requestInit.method = 'PUT';
- try {
- const response = await fetch(this.#getURL(path), requestInit);
- if (!response.ok)
- throw new OpenApiError_1.OpenApiError(response, 'Fetch failed for PUT: ' + path);
- return;
- }
- catch (e) {
- if (e instanceof Error)
- throw (e instanceof OpenApiError_1.OpenApiError) ? e : new OpenApiError_1.OpenApiError(e, 'Fetch failed for PUT: ' + path);
- throw new Error("Unexpected Error: " + e);
- }
- }
- putText(path, body) {
- return this.#putTextBased(path, 'text/plain', body);
- }
- putJSON(path, body) {
- return this.#putTextBased(path, 'application/json', body);
- }
- async #deleteTextBased(path, mimetype) {
- let requestInit = { ...this.#requestinit };
- requestInit.headers = { ...this.#headers };
- requestInit.headers["Accept"] = mimetype;
- requestInit.body = null;
- requestInit.method = 'DELETE';
- try {
- const response = await fetch(this.#getURL(path), requestInit);
- if (!response.ok)
- throw new OpenApiError_1.OpenApiError(response, 'Fetch failed for DELETE: ' + path);
- return;
- }
- catch (e) {
- if (e instanceof Error)
- throw (e instanceof OpenApiError_1.OpenApiError) ? e : new OpenApiError_1.OpenApiError(e, 'Fetch failed for DELETE: ' + path);
- throw new Error("Unexpected Error: " + e);
- }
- }
- deleteText(path) {
- return this.#deleteTextBased(path, 'text/plain');
- }
- deleteJSON(path) {
- return this.#deleteTextBased(path, 'application/json');
- }
- }
- exports.BaseApi = BaseApi;
- //# sourceMappingURL=BaseApi.js.map
|