UNPKG

nextcloud-node-client

Version:

Nextcloud client API for node.js TypeScript applications

60 lines (59 loc) 3.06 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.Server = void 0; // tslint:disable-next-line:no-var-requires require("dotenv").config(); const debug_1 = __importDefault(require("debug")); const error_1 = __importDefault(require("./error")); const server_1 = __importDefault(require("./server")); exports.Server = server_1.default; const debug = debug_1.default("NCEnvironmentVcapServices"); /** * returns the nextcloud credentials that is defined in the * "user-provided" service section of the VCAP_SERVICES environment * instanceName: the name of the nextcloud user provided service instance */ class EnvironmentVcapServices { constructor(instanceName) { if (!process.env.VCAP_SERVICES) { throw new error_1.default("NCEnvironmentVcapServices: environment VCAP_SERVICES not found", "ERR_VCAP_SERVICES_NOT_FOUND"); } const vcapServices = require("vcap_services"); const cred = vcapServices.getCredentials("user-provided", undefined, instanceName); if (!cred || cred === undefined || (!cred.url && !cred.username && !cred.password)) { debug("NCEnvironmentVcapServices: error credentials not found or not fully specified %O", cred); throw new error_1.default(`NCEnvironmentVcapServices: nextcloud credentials not found in environment VCAP_SERVICES. Service section: "user-provided", service instance name: "${instanceName}" `, "ERR_VCAP_SERVICES_NOT_FOUND"); } if (!cred.url) { throw new error_1.default("NCEnvironmentVcapServices: VCAP_SERVICES url not defined in user provided services for nextcloud", "ERR_VCAP_SERVICES_URL_NOT_DEFINED", { credentials: cred }); } if (!cred.password) { throw new error_1.default("NCEnvironmentVcapServices: VCAP_SERVICES password not defined in user provided services for nextcloud", "ERR_VCAP_SERVICES_PASSWORD_NOT_DEFINED", { credentials: cred }); } if (!cred.username) { throw new error_1.default("NCEnvironmentVcapServices: VCAP_SERVICES username not defined in user provided services for nextcloud", "ERR_VCAP_SERVICES_USERNAME_NOT_DEFINED", { credentials: cred }); } this.url = cred.url; this.userName = cred.username; this.password = cred.password; } /** * returns the nextcloud credentials that is defined in the * "user-provided" service section of the VCAP_SERVICES environment * @param instanceName the name of the nextcloud user provided service instance * @returns credentials from the VCAP_SERVICES environment (user provided service) */ getServer() { return new server_1.default({ basicAuth: { password: this.password, username: this.userName, }, url: this.url, }); } } exports.default = EnvironmentVcapServices;