mock-service-cli 3.1.2 → 3.1.3

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/CHANGELOG.md CHANGED
@@ -2,6 +2,13 @@
2
2
 
3
3
  All notable changes to this project will be documented in this file. See [standard-version](https://github.com/conventional-changelog/standard-version) for commit guidelines.
4
4
 
5
+ ### [3.1.3](https://github.com/chandq/mock-service-cli/compare/v3.1.2...v3.1.3) (2022-09-23)
6
+
7
+
8
+ ### Bug Fixes
9
+
10
+ * support rewrite proxy request url prefix ([621a2c5](https://github.com/chandq/mock-service-cli/commit/621a2c530f4003d7e29023a69bc22335e76a39c3))
11
+
5
12
  ### [3.1.2](https://github.com/chandq/mock-service-cli/compare/v3.1.1...v3.1.2) (2022-08-07)
6
13
 
7
14
  ### [3.1.1](https://github.com/chandq/mock-service-cli/compare/v3.1.0...v3.1.1) (2022-07-25)
package/README.md CHANGED
@@ -10,7 +10,7 @@
10
10
 
11
11
  ### 简介
12
12
 
13
- 内置 Mock Server、Web Server、Http request proxy 等功能集。
13
+ 内置 Mock Server、Web Server、Http?s request proxy 等功能集。
14
14
 
15
15
  **简易轻量**、**B/S 架构**、**0 秒启动**的本地命令行 Mock 服务套件, 支持热更新,对于开发调试 mock 数据很实用,能提高前端开发者的开发效率。支持 `GET`,`POST`,`PUT`,`DELETE`,`PATCH`,`OPTIONS`,`COPY`,`LINK`,`UNLINK`,`PURGE` 等常用请求类型,无需布署后端,可能是本地最好用的 Mock 工具之一。
16
16
 
@@ -32,6 +32,7 @@ if (argv.h || argv.help) {
32
32
  ' -H --cors-headers Optionally provide CORS headers list separated by commas. default *',
33
33
  '',
34
34
  ' -O --proxy-options Optionally provide proxy options list separated by commas.',
35
+ ' -r --rewrite rewrite http or https request url prefix of proxy, default false.',
35
36
  ' -P --web-port Web server port to use. If 0, look for open port. [9090]',
36
37
  ' -D --web-dir Enable web server, specify web directory, default [./dist] directory',
37
38
  ' -b --web-baseurl Specify website public path when enabled web server.',
@@ -51,6 +52,7 @@ var port = argv.p || argv.port,
51
52
  specifiedFile = argv.f || argv.file,
52
53
  version = argv.v || argv.version,
53
54
  isStartSocketServer = argv.S || false,
55
+ rewrite = argv.r || argv.rewrite || false,
54
56
  log = null;
55
57
 
56
58
  if (!argv.s && !argv.silent) {
@@ -74,7 +76,7 @@ if (version) {
74
76
  log.info('v' + require('../package.json').version);
75
77
  process.exit();
76
78
  }
77
-
79
+ injectEnvVariable('PREFIX_REWRITE', rewrite);
78
80
  injectEnvVariable('CORS_ORIGIN', argv.o || argv['cors-origin']);
79
81
  injectEnvVariable('CORS_HEADERS', argv.H || argv['cors-headers']);
80
82
 
package/lib/mockServer.js CHANGED
@@ -27,6 +27,7 @@ let socketServer = null; // Socket server instance
27
27
  const AsyncTaskQueue = require('./asyncTaskQueue');
28
28
  const saveDataAsyncTask = new AsyncTaskQueue();
29
29
 
30
+ const httpsRE = /^https:\/\//;
30
31
  let count = 0,
31
32
  fileCount = 0, // 记录mock api个数,mock file个数
32
33
  mockDirStat = {}, // 缓存mock目录的统计信息
@@ -172,10 +173,23 @@ if (process.env.WEB_ROOT) {
172
173
  if (process.env.PROXY_OPTIONS) {
173
174
  try {
174
175
  const options = JSON.parse(process.env.PROXY_OPTIONS);
175
- Object.keys(options).forEach(function (origin) {
176
- proxyTable[origin] = options[origin];
176
+ Object.keys(options).forEach(function (prefix) {
177
+ proxyTable[prefix] = options[prefix];
178
+ const opts = {
179
+ target: options[prefix],
180
+ changeOrigin: true,
181
+ ws: true
182
+ };
183
+ if (httpsRE.test(options[prefix])) {
184
+ // https is require secure=false
185
+ opts.secure = false;
186
+ }
187
+ if (process.env.PREFIX_REWRITE) {
188
+ opts.pathRewrite = path => path.replace(new RegExp(`^${prefix}`), '');
189
+ }
177
190
  // 接口代理
178
- webApp.use(origin, createProxyMiddleware({ target: options[origin], changeOrigin: true, ws: true }));
191
+ // https://github.com/http-party/node-http-proxy#options
192
+ webApp.use(prefix, createProxyMiddleware(opts));
179
193
  }, this);
180
194
  } catch (error) {
181
195
  console.warn(colors.yellow('\nEnable web proxy Failed:', error));
@@ -291,8 +305,8 @@ if (webApp && !process.env.RESTARTED) {
291
305
  });
292
306
  if (!isEmptyObj(proxyTable)) {
293
307
  console.info(colors.yellow(`\n Enable proxy at web server on:`));
294
- Object.keys(proxyTable).forEach(origin => {
295
- console.info(` ${origin} -> ${proxyTable[origin]}`);
308
+ Object.keys(proxyTable).forEach(prefix => {
309
+ console.info(` ${prefix} -> ${proxyTable[prefix]}`);
296
310
  });
297
311
  }
298
312
  });
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "mock-service-cli",
3
- "version": "3.1.2",
3
+ "version": "3.1.3",
4
4
  "description": "🦅 Local mock server",
5
5
  "main": "./lib/mockServer.js",
6
6
  "bin": {