UNPKG

3.04 kBJavaScriptView Raw
1"use strict";
2var __importDefault = (this && this.__importDefault) || function (mod) {
3 return (mod && mod.__esModule) ? mod : { "default": mod };
4};
5Object.defineProperty(exports, "__esModule", { value: true });
6// tslint:disable-next-line:no-var-requires
7require("dotenv").config();
8const debug_1 = __importDefault(require("debug"));
9const error_1 = __importDefault(require("./error"));
10const server_1 = __importDefault(require("./server"));
11exports.Server = server_1.default;
12const debug = debug_1.default("NCEnvironmentVcapServices");
13/**
14 * returns the nextcloud credentials that is defined in the
15 * "user-provided" service section of the VCAP_SERVICES environment
16 * instanceName: the name of the nextcloud user provided service instance
17 */
18class EnvironmentVcapServices {
19 constructor(instanceName) {
20 if (!process.env.VCAP_SERVICES) {
21 throw new error_1.default("NCEnvironmentVcapServices: environment VCAP_SERVICES not found", "ERR_VCAP_SERVICES_NOT_FOUND");
22 }
23 const vcapServices = require("vcap_services");
24 const cred = vcapServices.getCredentials("user-provided", undefined, instanceName);
25 if (!cred || cred === undefined || (!cred.url && !cred.username && !cred.password)) {
26 debug("NCEnvironmentVcapServices: error credentials not found or not fully specified %O", cred);
27 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");
28 }
29 if (!cred.url) {
30 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 });
31 }
32 if (!cred.password) {
33 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 });
34 }
35 if (!cred.username) {
36 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 });
37 }
38 this.url = cred.url;
39 this.userName = cred.username;
40 this.password = cred.password;
41 }
42 /**
43 * returns the nextcloud credentials that is defined in the
44 * "user-provided" service section of the VCAP_SERVICES environment
45 * @param instanceName the name of the nextcloud user provided service instance
46 * @returns credentials from the VCAP_SERVICES environment (user provided service)
47 */
48 getServer() {
49 return new server_1.default({
50 basicAuth: {
51 password: this.password,
52 username: this.userName,
53 },
54 url: this.url,
55 });
56 }
57}
58exports.default = EnvironmentVcapServices;