@vkontakte/vk-miniapps-deploy 0.1.8-rc.0 → 0.1.8
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/index.js +32 -22
- package/package.json +1 -1
package/index.js
CHANGED
|
@@ -100,8 +100,13 @@ async function fetch(url, options) {
|
|
|
100
100
|
}
|
|
101
101
|
}
|
|
102
102
|
|
|
103
|
-
|
|
104
|
-
|
|
103
|
+
/**
|
|
104
|
+
* @param {number} app_id
|
|
105
|
+
* @returns {Promise<{access_token: string, expires_in: string}>}
|
|
106
|
+
*/
|
|
107
|
+
async function auth(app_id) {
|
|
108
|
+
const get_auth_code_url = `${OAUTH_HOST}get_auth_code?scope=offline&client_id=${DEPLOY_APP_ID}&mini_app_id=${app_id}`;
|
|
109
|
+
const get_auth_code = await fetch(get_auth_code_url);
|
|
105
110
|
const get_auth_code_res = await get_auth_code.json();
|
|
106
111
|
|
|
107
112
|
if (get_auth_code_res.error !== void 0) {
|
|
@@ -116,7 +121,7 @@ async function auth() {
|
|
|
116
121
|
if (get_auth_code_res.auth_code) {
|
|
117
122
|
const {auth_code, device_id} = get_auth_code_res;
|
|
118
123
|
|
|
119
|
-
const
|
|
124
|
+
const code_auth_url = `${OAUTH_HOST}code_auth?stage=check&code=${auth_code}&revoke=1`;
|
|
120
125
|
|
|
121
126
|
let handled = false;
|
|
122
127
|
do {
|
|
@@ -124,14 +129,15 @@ async function auth() {
|
|
|
124
129
|
type: 'confirm',
|
|
125
130
|
name: 'result',
|
|
126
131
|
initial: true,
|
|
127
|
-
message: chalk.yellow('Please open this url in browser',
|
|
132
|
+
message: chalk.yellow('Please open this url in browser', code_auth_url)
|
|
128
133
|
});
|
|
129
134
|
|
|
130
135
|
if (!prompt_question.result) {
|
|
131
136
|
return Promise.reject("empty response " + prompt_question.result);
|
|
132
137
|
}
|
|
133
138
|
|
|
134
|
-
const
|
|
139
|
+
const code_auth_token_url = `${OAUTH_HOST}code_auth_token?device_id=${device_id}&client_id=${DEPLOY_APP_ID}&mini_app_id=${app_id}`;
|
|
140
|
+
const code_auth_token = await fetch(code_auth_token_url);
|
|
135
141
|
const code_auth_token_json = await code_auth_token.json();
|
|
136
142
|
|
|
137
143
|
if (code_auth_token.status !== CODE_SUCCESS) {
|
|
@@ -139,12 +145,14 @@ async function auth() {
|
|
|
139
145
|
continue;
|
|
140
146
|
}
|
|
141
147
|
|
|
142
|
-
const {access_token} = code_auth_token_json;
|
|
148
|
+
const {access_token, expires_in} = code_auth_token_json;
|
|
143
149
|
if (access_token || access_token === null) {
|
|
144
150
|
handled = true;
|
|
145
151
|
}
|
|
146
|
-
|
|
147
|
-
|
|
152
|
+
return Promise.resolve({
|
|
153
|
+
access_token,
|
|
154
|
+
expires_in,
|
|
155
|
+
});
|
|
148
156
|
|
|
149
157
|
} while (handled === false);
|
|
150
158
|
}
|
|
@@ -351,35 +359,37 @@ async function run(cfg) {
|
|
|
351
359
|
? (environmentMapping[process.env.MINI_APPS_ENVIRONMENT] || defaultEnvironment)
|
|
352
360
|
: defaultEnvironment;
|
|
353
361
|
|
|
362
|
+
if (process.env.MINI_APPS_APP_ID) {
|
|
363
|
+
const appId = parseInt(process.env.MINI_APPS_APP_ID, 10);
|
|
364
|
+
if (isNaN(appId)) {
|
|
365
|
+
throw new Error('env MINI_APPS_APP_ID is not valid number');
|
|
366
|
+
}
|
|
367
|
+
cfg.app_id = appId;
|
|
368
|
+
}
|
|
369
|
+
|
|
370
|
+
if (!cfg.app_id) {
|
|
371
|
+
throw new Error('Please provide "app_id" to vk-hosting-config.json or env MINI_APPS_APP_ID');
|
|
372
|
+
}
|
|
373
|
+
|
|
354
374
|
if (process.env.MINI_APPS_ACCESS_TOKEN) {
|
|
355
375
|
cfg.access_token = process.env.MINI_APPS_ACCESS_TOKEN;
|
|
356
376
|
}
|
|
357
377
|
|
|
358
378
|
if (!cfg.access_token && vault.get('access_token')) {
|
|
359
379
|
cfg.access_token = vault.get('access_token');
|
|
380
|
+
cfg.expires_in = vault.get('expires_in') || 0;
|
|
360
381
|
}
|
|
361
382
|
|
|
362
|
-
if (!cfg.access_token) {
|
|
383
|
+
if (!cfg.access_token || (cfg.expires_in > 0 && cfg.expires_in * 1000 < Date.now())) {
|
|
363
384
|
console.log('Try to retrieve access token...');
|
|
364
|
-
const access_token = await auth();
|
|
385
|
+
const { access_token, expires_in } = await auth(cfg.app_id);
|
|
365
386
|
cfg.access_token = access_token;
|
|
366
387
|
vault.set('access_token', access_token);
|
|
388
|
+
vault.set('expires_in', expires_in);
|
|
367
389
|
console.log(chalk.cyan('Token is saved in config store!'));
|
|
368
390
|
console.log(chalk.cyan('\nFor your CI, you can use \n > $ env MINI_APPS_ACCESS_TOKEN=' + access_token + ' yarn deploy'));
|
|
369
391
|
}
|
|
370
392
|
|
|
371
|
-
if (process.env.MINI_APPS_APP_ID) {
|
|
372
|
-
const appId = parseInt(process.env.MINI_APPS_APP_ID, 10);
|
|
373
|
-
if (isNaN(appId)) {
|
|
374
|
-
throw new Error('env MINI_APPS_APP_ID is not valid number');
|
|
375
|
-
}
|
|
376
|
-
cfg.app_id = appId;
|
|
377
|
-
}
|
|
378
|
-
|
|
379
|
-
if (!cfg.app_id) {
|
|
380
|
-
throw new Error('Please provide "app_id" to vk-hosting-config.json or env MINI_APPS_APP_ID');
|
|
381
|
-
}
|
|
382
|
-
|
|
383
393
|
const params = {
|
|
384
394
|
app_id: cfg.app_id,
|
|
385
395
|
environment: environment,
|