UNPKG

5.63 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.bearerConfig.isIntegrationLocation) {
34 await originalMethod.apply(this, arguments);
35 }
36 else {
37 this.error('This command must be run within a integration folder.');
38 }
39 };
40 return descriptor;
41 };
42}
43exports.RequireIntegrationFolder = RequireIntegrationFolder;
44// tslint:disable-next-line:function-name
45function RequireLinkedIntegration() {
46 return function (_target, _propertyKey, descriptor) {
47 const originalMethod = descriptor.value;
48 descriptor.value = async function () {
49 if (!this.bearerConfig.hasIntegrationLinked) {
50 const { choice } = await this.inquirer.prompt([
51 {
52 name: 'choice',
53 message: "Your integration isn't linked, what would you like to do?",
54 type: 'list',
55 choices: [
56 {
57 name: 'Create a new integration',
58 value: 'create'
59 },
60 {
61 name: 'Select an integration from my list',
62 value: 'select'
63 }
64 ]
65 }
66 ]);
67 switch (choice) {
68 case 'create':
69 await create_1.default.run([]);
70 break;
71 case 'select':
72 await link_1.default.run([]);
73 default:
74 break;
75 }
76 }
77 await originalMethod.apply(this, arguments);
78 };
79 return descriptor;
80 };
81}
82exports.RequireLinkedIntegration = RequireLinkedIntegration;
83function ensureFreshToken() {
84 return function (_target, _propertyKey, descriptor) {
85 const originalMethod = descriptor.value;
86 descriptor.value = async function () {
87 const { expires_at, refresh_token } = (await this.bearerConfig.getToken()) || {
88 expires_at: null,
89 refresh_token: null
90 };
91 if (expires_at && refresh_token) {
92 try {
93 if (expires_at < Date.now()) {
94 this.ux.action.start('Refreshing token');
95 await refreshMyToken(this, refresh_token);
96 this.ux.action.stop();
97 }
98 }
99 catch (error) {
100 this.ux.action.stop(`Failed`);
101 this.error(error.message);
102 }
103 }
104 else {
105 const error = this.colors.bold('⚠️ It looks like you are not logged in');
106 this.log(error);
107 const { shoudlLogin } = await this.inquirer.prompt([
108 {
109 message: 'Would you like to login?',
110 name: 'shoudlLogin',
111 type: 'list',
112 choices: [{ name: 'Yes', value: true }, { name: 'No', value: false }]
113 }
114 ]);
115 if (shoudlLogin) {
116 await login_1.default.run([]);
117 }
118 else {
119 this.exit(0);
120 }
121 }
122 await originalMethod.apply(this, arguments);
123 return descriptor;
124 };
125 return descriptor;
126 };
127}
128exports.ensureFreshToken = ensureFreshToken;
129// tslint:disable-next-line variable-name
130async function refreshMyToken(command, refresh_token) {
131 // TODO: rework refresh mechanism
132 const response = await axios_1.default.post(`${command.constants.LoginDomain}/oauth/token`, {
133 refresh_token,
134 grant_type: 'refresh_token',
135 client_id: constants_1.LOGIN_CLIENT_ID
136 });
137 await command.bearerConfig.storeToken(Object.assign({}, response.data, { refresh_token }));
138 return true;
139}
140// note: moving this line here, since link require RequireIntegrationFolder to be defined because it produces
141// this error: decorators_1.RequireIntegrationFolder is not a function
142const link_1 = require("../commands/link");
143const create_1 = require("../commands/integrations/create");