UNPKG

fedapay-cli

Version:
116 lines (115 loc) 4.1 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 fedapay_1 = require("fedapay"); const url_1 = tslib_1.__importDefault(require("url")); const chalk_1 = tslib_1.__importDefault(require("chalk")); const ngrok_1 = tslib_1.__importDefault(require("ngrok")); const webhooks_1 = tslib_1.__importDefault(require("../webhooks")); /* * WebhookListen Class extending the superClass Webhooks */ class WebhooksListen extends webhooks_1.default { constructor() { super(...arguments); this.webhook = null; } async deleteWebhook() { if (this.webhook) { await this.webhook.delete(); } } isLocalNetwork(hostname) { return ((['localhost', '127.0.0.1', '', '::1'].includes(hostname)) || (hostname.startsWith('192.168.')) || (hostname.startsWith('10.0.')) || (hostname.endsWith('.local'))); } async run() { /** * @param object * get flags value */ const { flags } = this.parse(WebhooksListen); /** * @param String * your api's key */ const apiKey = this.userConfig.read('secret_key', flags['api-key']); /** * @param String * sandbox or live */ const environment = this.userConfig.read('environment', flags.environment); /** * @param string * The data obtained after transformation */ const url = flags.url; /** * Set Apikey and environment to connect to fedapay */ fedapay_1.FedaPay.setApiKey(apiKey); fedapay_1.FedaPay.setEnvironment(environment); try { const parsedUrl = url_1.default.parse(url); // We only process local url if (!this.isLocalNetwork(parsedUrl.hostname || '')) { throw new Error('Please use a local url'); } const port = parseInt(parsedUrl.port || '8080'); let proxyUrl = await ngrok_1.default.connect({ port, onStatusChange: status => { if (status === 'closed') { this.deleteWebhook(); } }, onLogEvent: data => { if (flags.log) { console.log(data); } }, }); proxyUrl += parsedUrl.path; cli_ux_1.default.action.start('Creating webhook'); this.webhook = await fedapay_1.Webhook.create({ url: proxyUrl }); this.log(chalk_1.default.green('Webhook created successfully! ') + chalk_1.default.bold(`Webhook ID: ${this.webhook.id}`)); this.log(chalk_1.default.green(`Proxying ${url} ---> ${proxyUrl}`)); } catch (error) { this.log('Oups something occured'); this.error(error.message); } cli_ux_1.default.action.stop(); } } exports.default = WebhooksListen; /** * @params String * Description of the command webhooks:create */ WebhooksListen.description = 'Listen a new webhook and forward event to a local url'; /** * The command usage * @var string */ WebhooksListen.usage = 'webhooks:listen [options]'; /** * @params Object * Insertion of the different commands flags */ WebhooksListen.flags = Object.assign(Object.assign({}, webhooks_1.default.flags), { url: command_1.flags.string({ char: 'u', description: 'Forward url', required: true }), log: command_1.flags.boolean({ description: 'Log request output', default: false }), help: command_1.flags.help({ char: 'h', description: 'Help for webhooks:create.' }) }); /** * @param Sting[] * Some example of use of the webhook:create command */ WebhooksListen.examples = [ 'webhooks:listen --api-key=[API-KEY] --environment=[env] --url=http://localhost:8080/webhooks', ];