UNPKG

5.99 kBJavaScriptView Raw
1"use strict";
2Object.defineProperty(exports, "__esModule", { value: true });
3const axios_1 = require("axios");
4const login_1 = require("../commands/login");
5const constants_1 = require("./constants");
6function 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 // tslint:disable-next-line
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}
27exports.skipIfNoViews = skipIfNoViews;
28// tslint:disable-next-line:function-name
29function 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 // tslint:disable-next-line: max-line-length
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}
46exports.RequireIntegrationFolder = RequireIntegrationFolder;
47// tslint:disable-next-line:function-name
48function 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}
88exports.RequireLinkedIntegration = RequireLinkedIntegration;
89function 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}
134exports.ensureFreshToken = ensureFreshToken;
135// tslint:disable-next-line variable-name
136async function refreshMyToken(command, refresh_token) {
137 // TODO: rework refresh mechanism
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// note: moving this line here, since link require RequireIntegrationFolder to be defined because it produces
147// this error: decorators_1.RequireIntegrationFolder is not a function
148const link_1 = require("../commands/link");
149const create_1 = require("../commands/integrations/create");