@vkontakte/vk-miniapps-deploy 0.1.5 → 0.1.6

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.
@@ -21,6 +21,10 @@ async function run() {
21
21
  const deploy = require('../index');
22
22
  const cfg = configJSON || {};
23
23
 
24
+ if (cfg.debug) {
25
+ console.log('\nDebug mode is enabled. It will log all client requests!\n');
26
+ }
27
+
24
28
  let confirmation = {
25
29
  result: true,
26
30
  }
package/index.js CHANGED
@@ -1,7 +1,8 @@
1
+ const crypto = require('crypto');
1
2
  const packageJson = require('./package.json');
2
3
  const chalk = require('chalk');
3
4
  const prompt = require('prompts');
4
- const fetch = require('node-fetch');
5
+ const nodeFetch = require('node-fetch');
5
6
  const { zip } = require('zip-a-folder');
6
7
  const fs = require('fs-extra');
7
8
  const FormData = require('form-data');
@@ -14,6 +15,8 @@ var cfg = configJSON || {};
14
15
  prompt.message = "vk-mini-apps-deploy".grey;
15
16
  prompt.delimiter = "=>".grey;
16
17
 
18
+ const DEBUG_MODE = !!cfg.debug;
19
+
17
20
  const API_HOST = cfg.api_host || 'https://api.vk.com/method/';
18
21
  const OAUTH_HOST = cfg.oauth_host || 'https://oauth.vk.com/';
19
22
 
@@ -63,6 +66,41 @@ const URL_NAMES_MAP = {
63
66
  [URL_NAMES.MOBILE_WEB]: PLATFORMS.MOBILE_WEB,
64
67
  };
65
68
 
69
+ function getTraceId() {
70
+ return crypto.randomBytes(4).toString("hex");
71
+ }
72
+
73
+ /**
74
+ * @param {nodeFetch.RequestInfo} url
75
+ * @param {nodeFetch.RequestInit} options
76
+ * @returns {Promise<nodeFetch.Response>}
77
+ */
78
+ async function fetch(url, options) {
79
+ if (!DEBUG_MODE) {
80
+ return nodeFetch(url, options);
81
+ }
82
+
83
+ const traceId = chalk.hex(`#${(Math.random() * 0xFFFFFF << 0).toString(16)}`)(getTraceId());
84
+ const logLine = (line, type) => console.log(`[${traceId}][${type}]`, chalk.cyan(line));
85
+ const logError = (error) => console.error(`[${traceId}][ERROR]`, chalk.red(error));
86
+
87
+ try {
88
+ logLine(url, 'REQ');
89
+ options && logLine(JSON.stringify(options), 'REQ');
90
+
91
+ const response = await nodeFetch(url, options);
92
+ const body = await response.clone().text();
93
+
94
+ logLine(`${response.status} ${response.statusText}`, 'RESP');
95
+ logLine(body, 'RESP');
96
+
97
+ return response
98
+ } catch (e) {
99
+ logError(e);
100
+ throw e;
101
+ }
102
+ }
103
+
66
104
  async function auth() {
67
105
  const get_auth_code = await fetch(OAUTH_HOST + 'get_auth_code?scope=offline&client_id=' + DEPLOY_APP_ID);
68
106
  const get_auth_code_res = await get_auth_code.json();
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@vkontakte/vk-miniapps-deploy",
3
- "version": "0.1.5",
3
+ "version": "0.1.6",
4
4
  "description": "Deploy to VK Mini Apps hosting with one simple command",
5
5
  "main": "index.js",
6
6
  "scripts": {
@@ -5,5 +5,6 @@
5
5
  "mvk": "index.html",
6
6
  "web": "index.html",
7
7
  "mobile": "index.html"
8
- }
8
+ },
9
+ debug: false
9
10
  }