@c8y/toolkit
Version:
Toolkit for Cumulocity applications, allows to e.g. deploy an application to an instance of Cumulocity.
33 lines • 1.22 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.createClient = createClient;
exports.ensureUserHasRequiredPermissions = ensureUserHasRequiredPermissions;
const client_1 = require("@c8y/client");
const constants_1 = require("../constants");
/**
* Creates client class instance for communicating with server (authentication, application upload).
*
* @param options - Authorization options.
* @returns A Promise that resolves to the created client.
*/
async function createClient({ user, password, url: baseUrl, tenant }) {
const basicAuth = {
user,
password,
tenant
};
try {
return await client_1.Client.authenticate(basicAuth, baseUrl);
}
catch (ex) {
console.log(ex);
throw new Error(constants_1.OPTIONS_CONSTANTS['TXT.INVALID_CREDENTIALS_BASEURL'](baseUrl));
}
}
async function ensureUserHasRequiredPermissions(client) {
const { data: currentUser } = await client.user.current();
if (!client.user.hasRole(currentUser, 'ROLE_APPLICATION_MANAGEMENT_ADMIN')) {
throw new Error(constants_1.OPTIONS_CONSTANTS['TXT.USER_MISSES_PERMISSION_TO_DEPLOY']);
}
}
//# sourceMappingURL=client.js.map