1 | "use strict";
|
2 | Object.defineProperty(exports, "__esModule", { value: true });
|
3 | const axios_1 = require("axios");
|
4 | const cli_ux_1 = require("cli-ux");
|
5 | const inquirer = require("inquirer");
|
6 | const link_1 = require("../actions/link");
|
7 | const createIntegration_1 = require("../actions/createIntegration");
|
8 | const login_1 = require("../actions/login");
|
9 | const constants_1 = require("./constants");
|
10 | function skipIfNoViews(displayError = false) {
|
11 | return function (_target, _propertyKey, descriptor) {
|
12 | const originalMethod = descriptor.value;
|
13 | descriptor.value = async function () {
|
14 | if (this.hasViews) {
|
15 | await originalMethod.apply(this, arguments);
|
16 | }
|
17 | else {
|
18 | if (displayError) {
|
19 | this.error(
|
20 |
|
21 | 'This integration does not contain any views. If you want to use this command please generate a new integration with --withViews flag');
|
22 | }
|
23 | else {
|
24 | this.debug('No views present, skipping');
|
25 | }
|
26 | }
|
27 | };
|
28 | return descriptor;
|
29 | };
|
30 | }
|
31 | exports.skipIfNoViews = skipIfNoViews;
|
32 |
|
33 | function RequireIntegrationFolder() {
|
34 | return function (_target, _propertyKey, descriptor) {
|
35 | const originalMethod = descriptor.value;
|
36 | descriptor.value = async function () {
|
37 | if (this.isIntegrationLocation) {
|
38 | await originalMethod.apply(this, arguments);
|
39 | }
|
40 | else {
|
41 | this.warn(
|
42 |
|
43 | `We couldn't find any auth.config.json file, please make sure this file exists at the root of your integration`);
|
44 | this.error('This command must be run within a integration folder.');
|
45 | }
|
46 | };
|
47 | return descriptor;
|
48 | };
|
49 | }
|
50 | exports.RequireIntegrationFolder = RequireIntegrationFolder;
|
51 |
|
52 | function RequireLinkedIntegration(prompt = true) {
|
53 | return function (_target, _propertyKey, descriptor) {
|
54 | const originalMethod = descriptor.value;
|
55 | descriptor.value = async function () {
|
56 | if (!this.bearerConfig.hasIntegrationLinked) {
|
57 | if (!prompt) {
|
58 | this.error('Can not run this command, please run link command before');
|
59 | }
|
60 | const { choice } = await inquirer.prompt([
|
61 | {
|
62 | name: 'choice',
|
63 | message: "Your integration isn't linked, what would you like to do?",
|
64 | type: 'list',
|
65 | choices: [
|
66 | {
|
67 | name: 'Create a new integration',
|
68 | value: 'create'
|
69 | },
|
70 | {
|
71 | name: 'Select an integration from my list',
|
72 | value: 'select'
|
73 | }
|
74 | ]
|
75 | }
|
76 | ]);
|
77 | switch (choice) {
|
78 | case 'create':
|
79 | await createIntegration_1.default(this, { link: true });
|
80 | break;
|
81 | case 'select':
|
82 | await link_1.default(this);
|
83 | default:
|
84 | break;
|
85 | }
|
86 | }
|
87 | await originalMethod.apply(this, arguments);
|
88 | };
|
89 | return descriptor;
|
90 | };
|
91 | }
|
92 | exports.RequireLinkedIntegration = RequireLinkedIntegration;
|
93 | function ensureFreshToken() {
|
94 | return function (_target, _propertyKey, descriptor) {
|
95 | const originalMethod = descriptor.value;
|
96 | descriptor.value = async function () {
|
97 | const { expires_at, refresh_token } = (await this.bearerConfig.getToken()) || {
|
98 | expires_at: null,
|
99 | refresh_token: null
|
100 | };
|
101 | if (expires_at && refresh_token) {
|
102 | try {
|
103 | if (expires_at < Date.now()) {
|
104 | cli_ux_1.default.action.start('Refreshing token');
|
105 | await refreshMyToken(this, refresh_token);
|
106 | cli_ux_1.default.action.stop();
|
107 | }
|
108 | }
|
109 | catch (error) {
|
110 | cli_ux_1.default.action.stop(`Failed`);
|
111 | this.error(error.message);
|
112 | }
|
113 | }
|
114 | else {
|
115 | await login_1.promptToLogin(this);
|
116 | }
|
117 | return await originalMethod.apply(this, arguments);
|
118 | };
|
119 | return descriptor;
|
120 | };
|
121 | }
|
122 | exports.ensureFreshToken = ensureFreshToken;
|
123 |
|
124 | async function refreshMyToken(command, refresh_token) {
|
125 |
|
126 | const response = await axios_1.default.post(`${command.constants.LoginDomain}/oauth/token`, {
|
127 | refresh_token,
|
128 | grant_type: 'refresh_token',
|
129 | client_id: constants_1.LOGIN_CLIENT_ID
|
130 | });
|
131 | await command.bearerConfig.storeToken(Object.assign({}, response.data, { refresh_token }));
|
132 | return true;
|
133 | }
|