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