mock-service-cli 3.1.1 → 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 +9 -0
- package/README.md +19 -2
- package/bin/mock-service-cli +3 -1
- package/lib/mockServer.js +19 -5
- package/package.json +1 -1
package/CHANGELOG.md
CHANGED
|
@@ -2,6 +2,15 @@
|
|
|
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
|
+
|
|
12
|
+
### [3.1.2](https://github.com/chandq/mock-service-cli/compare/v3.1.1...v3.1.2) (2022-08-07)
|
|
13
|
+
|
|
5
14
|
### [3.1.1](https://github.com/chandq/mock-service-cli/compare/v3.1.0...v3.1.1) (2022-07-25)
|
|
6
15
|
|
|
7
16
|
|
package/README.md
CHANGED
|
@@ -10,15 +10,32 @@
|
|
|
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
|
-
**简易轻量**、**B/S 架构**、**0 秒启动**的本地命令行 Mock 服务套件, 支持热更新,对于开发调试 mock 数据很实用,能提高前端开发者的开发效率。支持 `GET`,`POST`,`PUT`,`DELETE`,`PATCH`,`OPTIONS`,`COPY`,`LINK`,`UNLINK`,`PURGE` 等常用请求类型,无需布署后端,可能是本地最好用的 Mock
|
|
15
|
+
**简易轻量**、**B/S 架构**、**0 秒启动**的本地命令行 Mock 服务套件, 支持热更新,对于开发调试 mock 数据很实用,能提高前端开发者的开发效率。支持 `GET`,`POST`,`PUT`,`DELETE`,`PATCH`,`OPTIONS`,`COPY`,`LINK`,`UNLINK`,`PURGE` 等常用请求类型,无需布署后端,可能是本地最好用的 Mock 工具之一。
|
|
16
|
+
|
|
17
|
+
#### Mock Server - 本地 Mock 服务器
|
|
18
|
+
|
|
19
|
+
支持以下常见业务场景:
|
|
16
20
|
|
|
17
21
|
- [x] 无服务端演示项目的数据 Mock
|
|
18
22
|
- [x] 业务开发接口联调前的数据 Mock
|
|
19
23
|
- [x] 保留业务项目的所有或部分接口的响应数据(写入文件)
|
|
20
24
|
- [x] 保障前端开发调试不受后端服务影响(当后端服务挂掉或部分接口响应异常时启用 Mock)
|
|
21
25
|
|
|
26
|
+
#### Web Server - SPA Web 服务器
|
|
27
|
+
|
|
28
|
+
对 React、Vue 等单页应用项目构建生成的 dist 目录,启动本地 web 服务器(相当于使用本地安装的 nginx 服务)来模拟运行生产环境下的 web 服务
|
|
29
|
+
|
|
30
|
+
#### Http request proxy - 基于浏览器的本地代理服务
|
|
31
|
+
|
|
32
|
+
实现了本地接口代理的能力,一般接口测试工具无法查看到 request 和 response 中 headers 的更多详情,这里实现了接口代理功能,在 Network 中可查看接口调用的详细信息。
|
|
33
|
+
|
|
34
|
+
> 两种应用场景
|
|
35
|
+
|
|
36
|
+
- 项目 dev 模式下的接口代理请求
|
|
37
|
+
- Chrome 的 Console 执行 fetch 请求(用于接口测试)
|
|
38
|
+
|
|
22
39
|
## Installation:
|
|
23
40
|
|
|
24
41
|
#### Running on-demand:
|
package/bin/mock-service-cli
CHANGED
|
@@ -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 (
|
|
176
|
-
proxyTable[
|
|
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
|
-
|
|
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(
|
|
295
|
-
console.info(` ${
|
|
308
|
+
Object.keys(proxyTable).forEach(prefix => {
|
|
309
|
+
console.info(` ${prefix} -> ${proxyTable[prefix]}`);
|
|
296
310
|
});
|
|
297
311
|
}
|
|
298
312
|
});
|