Press n or j to go to the next uncovered block, b, p or k for the previous block.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 | 3x 3x 3x 3x 3x 3x 3x | "use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.NextcloudServer = void 0; const ConfigurationError_1 = __importDefault(require("../Exception/ConfigurationError")); class NextcloudServer { serverData; logger; persistence; static getRequestCachePrefix = 'cache-getJson-'; /** * Create NextcloudServer instance. * @param serverData * @param logger * @param persistence * @throws ConfigurationError */ constructor(serverData, logger, persistence) { this.serverData = serverData; this.logger = logger; this.persistence = persistence; if (!serverData.baseUrl.startsWith('https://') && !serverData.baseUrl.startsWith('http://')) { this.logger.onThrow(new ConfigurationError_1.default('Base URL (or protocol) is invalid')); } if (serverData.token.length < 3) { this.logger.onThrow(new ConfigurationError_1.default('Password or token is invalid')); } } getBaseUrl() { return this.serverData.baseUrl; } setBaseUrl(value) { if (!value.startsWith('https://') && !value.startsWith('http://')) { this.logger.onThrow(new ConfigurationError_1.default('Base URL (or protocol) is invalid')); } this.serverData.baseUrl = value; } getUser() { return this.serverData.user; } setUser(value) { this.serverData.user = value; } getToken() { return this.serverData.token; } setToken(value) { this.serverData.token = value; } getApiUrl() { return `${this.getBaseUrl()}/index.php/apps/passman/api/v2/`; } getEncodedLogin() { return btoa(this.getUser() + ":" + this.getToken()); } getJson = async (endpoint, errorCallback, getCachedIfPossible = false) => { if (getCachedIfPossible) { const cachedValue = await this.persistence.getRequestCacheHandler()?.get(NextcloudServer.getRequestCachePrefix + endpoint); if (cachedValue && cachedValue !== '') { try { return JSON.parse(cachedValue); } catch (_) { // ignore all exceptions, just continue with the non-cached request logic } } } const res = await fetch(this.getApiUrl() + endpoint, { headers: { Accept: 'application/json', Authorization: `Basic ${this.getEncodedLogin()}` }, credentials: 'omit' }).catch((err) => errorCallback(err)); if (!res) { return; } if (res.status >= 400) { const data = await res.json(); this.logger.onError(data.message); return; } const jsonResponse = await res.json(); const requestCacheHandler = this.persistence.getRequestCacheHandler(); if (requestCacheHandler) { await requestCacheHandler.set(NextcloudServer.getRequestCachePrefix + endpoint, JSON.stringify(jsonResponse)); } return (jsonResponse); }; deleteJson = async (endpoint, errorCallback) => { const res = await fetch(this.getApiUrl() + endpoint, { method: 'DELETE', headers: { Authorization: `Basic ${this.getEncodedLogin()}` }, credentials: 'omit' }).catch((err) => errorCallback(err)); if (!res) { return; } if (res.status >= 400) { const data = await res.json(); this.logger.onError(data.message); return; } return (await res.json()); }; /** * Do a post request. * * @param endpoint * @param data will be converted to a json string * @param errorCallback * @param method */ postJson = async (endpoint, data, errorCallback, method = 'POST') => { const res = await fetch(this.getApiUrl() + endpoint, { method: method, headers: { Accept: 'application/json', Authorization: `Basic ${this.getEncodedLogin()}`, "Content-Type": "application/json", }, credentials: 'omit', body: JSON.stringify(data), }).catch((err) => errorCallback(err)); if (!res) { return; } if (res.status >= 400) { const data = await res.json(); this.logger.onError(data.message); return; } return (await res.json()); }; } exports.NextcloudServer = NextcloudServer; |