UNPKG

fedapay-cli

Version:
150 lines (149 loc) 5.09 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); const tslib_1 = require("tslib"); const command_1 = require("@oclif/command"); const cli_ux_1 = tslib_1.__importDefault(require("cli-ux")); const inquirer = tslib_1.__importStar(require("inquirer")); const axios_1 = tslib_1.__importDefault(require("axios")); const os_1 = tslib_1.__importDefault(require("os")); const user_config_1 = tslib_1.__importDefault(require("../helpers/user-config")); /** * Login Class extending superClass Command */ class Login extends command_1.Command { /** * Send Link request * @param {string} device_name The device name * @param {string} environment The environment * @return {login_url: string; poll_url: string} */ async sendLinksRequest(device_name, environment) { try { const { data } = await axios_1.default.post('https://cli.fedapay.com/links', { device_name, environment }); return data; } catch (e) { return null; } } /** * Send Poll request * @param {string} url * * @return {any} */ async sendPollRequest(url) { try { const response = await axios_1.default.get(url); return response.data; } catch (e) { return null; } } /** * Wrap the poll request with a promise. * Resolve it when secret key is defined. * Reject it when max try is reached * @param {string} poll_url The poll url * @return Promise<any> */ async checkSecretKey(poll_url) { return new Promise((resolve, reject) => { const maxTries = 10; let tries = 0; const pollIntervalId = setInterval(async () => { const login = await this.sendPollRequest(poll_url); tries++; if (login && login.secret_key) { clearInterval(pollIntervalId); resolve(login); } if (tries > maxTries) { clearInterval(pollIntervalId); reject(new Error('Unabled to login. Try interactive mode.')); } }, 5000); }); } /** * The command flags * @var Object * */ async run() { const { flags } = this.parse(Login); const inquirerPrompts = [{ name: 'environment', message: 'Select your environment', type: 'list', choices: [{ name: 'development' }, { name: 'sandbox' }, { name: 'live' }], }]; let account_name = 'default'; let environment; let secret_key; let public_key; if (flags.interactive) { let responses = await inquirer.prompt(inquirerPrompts); environment = responses.environment; secret_key = await cli_ux_1.default.prompt('Enter your secret_key'); public_key = await cli_ux_1.default.prompt('Enter your public_key'); } else { environment = flags.environment; if (environment.trim() === '') { // Ask the user to select the environment let responses = await inquirer.prompt(inquirerPrompts); environment = responses.environment; } try { const links = await this.sendLinksRequest(os_1.default.hostname(), environment); if (links === null) { this.error('Hostname and environnement are required'); return; } this.log(`Authenticate URL : ${links.login_url}`); await cli_ux_1.default.open(links.login_url); cli_ux_1.default.action.start('Waiting'); const login = await this.checkSecretKey(links.poll_url); account_name = login.account_name; secret_key = login.secret_key; public_key = login.public_key; } catch (error) { this.error(error.message); return; } } const userConfig = new user_config_1.default(this.config.configDir); userConfig.write({ account_name, environment, secret_key, public_key }); this.log('Login successfull'); } } exports.default = Login; /** * @param string * Description of the login command */ Login.description = 'Connect to Fedapay account'; /** * @param string * login usage */ Login.usage = 'login [parameters...]'; Login.flags = { environment: command_1.flags.string({ description: 'FedaPay Api environment', char: 'e', default: '', options: ['development', 'sandbox', 'live'] }), interactive: command_1.flags.boolean({ description: 'Login in interactive mode', char: 'i', default: false, }), help: command_1.flags.help({ char: 'h', description: 'Help for the login command' }), };