@topgroup/diginext
Version:
A BUILD SERVER & CLI to deploy apps to any Kubernetes clusters.
84 lines (83 loc) • 3.65 kB
JavaScript
;
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.DIGINEXT_GITHUB_CLIENT_ID = void 0;
const axios_1 = __importDefault(require("axios"));
const class_validator_1 = require("class-validator");
const log_1 = require("diginext-utils/dist/xconsole/log");
const inquirer_1 = __importDefault(require("inquirer"));
const open_1 = __importDefault(require("open"));
const app_config_1 = require("../../app.config");
const config_1 = require("../../config/config");
const params_1 = require("../../plugins/params");
exports.DIGINEXT_GITHUB_CLIENT_ID = "Iv1.04ab669eff50438b";
const Github = {
loginWithPAT: async (personalAccessToken) => {
// get github profile
try {
const profile = await Github.profile(personalAccessToken);
(0, config_1.saveCliConfig)({ github_access_token: personalAccessToken });
(0, log_1.logSuccess)(`Congrats, ${profile.name}! Your github has been integrated to Diginext CLI successfully.`);
}
catch (e) {
(0, log_1.logError)(e.message);
}
},
loginWithApp: async () => {
const authCallbackURL = `${app_config_1.Config.DX_SITE_URL}/github/callback`;
(0, open_1.default)(`https://github.com/login/oauth/authorize?client_id=${exports.DIGINEXT_GITHUB_CLIENT_ID}&redirect_uri=${authCallbackURL}`);
const { access_token } = await inquirer_1.default.prompt({
type: "password",
name: "access_token",
message: `Github OAuth access token:`,
validate: function (value) {
return value.length ? true : "Github access token is required.";
},
});
// get github profile
try {
const profile = await Github.profile(access_token);
(0, config_1.saveCliConfig)({ github_access_token: access_token });
(0, log_1.logSuccess)(`Congrats, ${profile.name}! Your github has been integrated to Diginext CLI successfully.`);
}
catch (e) {
(0, log_1.logError)(e.message);
}
},
logout: async () => {
(0, config_1.saveCliConfig)({ github_access_token: "" });
(0, log_1.logSuccess)(`Logged out from Github account successfully.`);
},
profile: async (token) => {
const profile = (await Github.fetchApi({ url: "https://api.github.com/user", token }));
if (profile.message)
throw new Error(`${profile.message}: ${profile.documentation_url}`);
return profile;
},
fetchApi: async (options) => {
const { url, method = "GET", headers: inputHeaders = {}, data = {} } = options;
const { github_access_token = options.token || "" } = (0, config_1.getCliConfig)();
// headers
const headers = { Accept: "application/vnd.github+json", ...inputHeaders };
if (github_access_token)
headers.Authorization = `Bearer ${github_access_token}`;
// make a request
const res = await (0, axios_1.default)({ url, data, method, headers });
// response data
if ((0, class_validator_1.isJSON)(res.data)) {
return JSON.parse(res.data);
}
else {
if (res.data.toString().indexOf("&") > -1) {
return (0, params_1.paramsToObject)(new URLSearchParams(res.data));
}
else {
return res.data;
}
}
},
refreshToken: async () => { },
};
exports.default = Github;