mock-service-cli 2.4.3 → 2.5.1

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,21 @@
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
+ ### [2.5.1](https://github.com/chandq/mock-service-cli/compare/v2.5.0...v2.5.1) (2022-04-25)
6
+
7
+ ## [2.5.0](https://github.com/chandq/mock-service-cli/compare/v2.4.0...v2.5.0) (2022-04-25)
8
+
9
+
10
+ ### Features
11
+
12
+ * **cors:** 支持cors-origin,cors-headers跨域配置 ([c2d2872](https://github.com/chandq/mock-service-cli/commit/c2d28723d047139a5165190b4ba197f46470f72f))
13
+ * 缓存mock统计信息 ([1ee87cd](https://github.com/chandq/mock-service-cli/commit/1ee87cd041e68b9f17b64e9e3cb906681cb855fc))
14
+
15
+
16
+ ### Bug Fixes
17
+
18
+ * 修复无法同时保留相同url不同请求类型的响应数据 ([90326dc](https://github.com/chandq/mock-service-cli/commit/90326dc556760eb81efadd76b031366452c95d2b))
19
+
5
20
  ### [2.4.3](https://github.com/chandq/mock-service-cli/compare/v2.4.2...v2.4.3) (2022-01-20)
6
21
 
7
22
  ### [2.4.2](https://github.com/chandq/mock-service-cli/compare/v2.4.1...v2.4.2) (2022-01-07)
package/README.md CHANGED
@@ -45,17 +45,19 @@ This will install `mock-service-cli` globally so that it may be run from the com
45
45
 
46
46
  `[path]` defaults to `./mock` .
47
47
 
48
- | Command | Description | Defaults |
49
- | ------------------------- | -------------------------------------------------------------------------------------------------------------- | -------- |
50
- | `-p` or `--port` | Port to use. Use `-p 0` to look for an open port, starting at 8090. It will also read from `process.env.PORT`. | 8090 |
51
- | `-d` | Specify mock directory | ./mock |
52
- | `-f` | Specify mock file | |
53
- | `-s` or `--silent` | Suppress log messages from output | |
54
- | `-h` or `--help` | Print this list and exit. | |
55
- | `-v` or `--version` | Print the version and exit. | |
56
- | `-S` or `--socket-server` | Start socket server which used to save api response data for future mock. | false |
57
- | `-a` or `--api-stat` | Whether print api url and file path info or not, default false. | false |
58
- | `-l` or `--log` | Whether record operation info by write file, default false. | false |
48
+ | Command | Description | Defaults |
49
+ | ------------------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | -------- |
50
+ | `-h` or `--help` | Print this list and exit. | |
51
+ | `-p` or `--port` | Port to use. Use `-p 0` to look for an open port, starting at 8090. It will also read from `process.env.PORT`. | 8090 |
52
+ | `-d` | Specify mock directory | ./mock |
53
+ | `-f` | Specify mock file | |
54
+ | `-s` or `--silent` | Suppress log messages from output | |
55
+ | `-v` or `--version` | Print the version and exit. | |
56
+ | `-S` or `--socket-server` | Start socket server which used to save api response data for future mock. | false |
57
+ | `-a` or `--api-stat` | Whether print api url and file path info or not, default false. | false |
58
+ | `-l` or `--log` | Whether record operation info by write file, default false. | false |
59
+ | `-o` or `--cors-origin` | Allow origin by cors, list separated by commas, must not be \* when withCredential is true .If specified, cors-headers will be `Authorization,Content-Type,Accept,Origin,User-Agent,DNT,Cache-Control,X-Mx-ReqToken,X-Data-Type,X-Requested-With,X-Data-Type,X-Auth-Token,Token` | \* |
60
+ | `-H` or `--cors-headers` | Optionally provide CORS headers list separated by commas | \* |
59
61
 
60
62
  ## Example
61
63
 
@@ -63,15 +65,19 @@ This will install `mock-service-cli` globally so that it may be run from the com
63
65
 
64
66
  `mock-service-cli -p 8085`
65
67
 
66
- 2. Specify mock directory.
68
+ 2. Enable cors, specify origin and headers.
69
+
70
+ `mock-service-cli -o "http://192.168.0.8:38200,http://10.30.30.3" -H "yy"`
71
+
72
+ 3. Specify mock directory.
67
73
 
68
74
  `mock-service-cli -d ./mock`
69
75
 
70
- 3. Specify mock file.
76
+ 4. Specify mock file.
71
77
 
72
78
  `mock-service-cli -f ./mock/test.js`
73
79
 
74
- 4. Start socket server for used to save api response data.
80
+ 5. Start socket server for used to save api response data.
75
81
 
76
82
  `mock-service-cli -S`
77
83
 
@@ -25,6 +25,10 @@ if (argv.h || argv.help) {
25
25
  ' -S --socket-server Whether start socket server or not, default false.',
26
26
  ' -a --api-stat Whether print api url and file path or not, default false.',
27
27
  ' -l --log Whether record operation info by write file, default false.',
28
+ ' -o --cors-origin Allow origin by cors, list separated by commas, must not be * when withCredential is true',
29
+ ' .If specified, cors-headers will be "Authorization,Content-Type,Accept,Origin,User-Agent,DNT',
30
+ ' ,Cache-Control,X-Mx-ReqToken,X-Data-Type,X-Requested-With,X-Data-Type,X-Auth-Token,Token", default *.',
31
+ ' -H --cors-headers Optionally provide CORS headers list separated by commas. default *',
28
32
  '',
29
33
  ' -h --help Print this list and exit.',
30
34
  ' -v --version Print the version and exit.'
@@ -59,6 +63,14 @@ if (version) {
59
63
  process.exit();
60
64
  }
61
65
 
66
+ if (argv.o || argv['cors-origin']) {
67
+ process.env.CORS_ORIGIN = argv.o || argv.origin;
68
+ }
69
+
70
+ if (argv.H || argv['cors-headers']) {
71
+ process.env.CORS_HEADERS = argv.H || argv['cors-headers'];
72
+ }
73
+
62
74
  if (!port) {
63
75
  portfinder.basePort = 8090;
64
76
  portfinder.getPort(function (err, port) {
package/lib/mockServer.js CHANGED
@@ -40,6 +40,20 @@ if (!process.env.PORT) {
40
40
  require('deasync').loopWhile(function () {
41
41
  return !done;
42
42
  });
43
+ let corsOrigin = [],
44
+ corsHeaders = '';
45
+ if (process.env.CORS_ORIGIN) {
46
+ process.env.CORS_ORIGIN.split(/\s*,\s*/).forEach(function (h) {
47
+ corsOrigin.push(h);
48
+ }, this);
49
+ corsHeaders =
50
+ 'Authorization,Content-Type,Accept,Origin,User-Agent,DNT,Cache-Control,X-Mx-ReqToken,X-Data-Type,X-Requested-With,X-Data-Type,X-Auth-Token,Token';
51
+ }
52
+ if (process.env.CORS_HEADERS) {
53
+ process.env.CORS_HEADERS.split(/\s*,\s*/).forEach(function (h) {
54
+ corsHeaders += ', ' + h;
55
+ }, this);
56
+ }
43
57
  /**
44
58
  * @description: 跨域设置
45
59
  * @param {*}
@@ -48,14 +62,12 @@ require('deasync').loopWhile(function () {
48
62
  const crossDomain = () => (req, res, next) => {
49
63
  // 设置withCredentials: true时,需设置以下两项
50
64
  res.header('Access-Control-Allow-Credentials', true);
51
- // res.header('Access-Control-Allow-Origin', 'http://localhost:8081');
52
- res.header('Access-Control-Allow-Origin', '*');
65
+ // withCredentials, must be specified value instead of *
66
+ res.header('Access-Control-Allow-Origin', corsOrigin.includes(req.headers.origin) ? req.headers.origin : '*');
67
+ // res.header('Access-Control-Allow-Origin', '*');
53
68
  res.header('Access-Control-Allow-Methods', SupportMethods.join(','));
54
- // res.header(
55
- // 'Access-Control-Allow-Headers',
56
- // 'Origin, X-Requested-With, Content-Type, Accept, Authorization'
57
- // );
58
- res.header('Access-Control-Allow-Headers', '*');
69
+ res.header('Access-Control-Allow-Headers', corsHeaders ? corsHeaders : '*');
70
+ // res.header('Access-Control-Allow-Headers', '*');
59
71
  if (req.method === 'OPTIONS') res.status(200); // 让OPTIONS快速返回
60
72
  next();
61
73
  };
@@ -149,10 +161,13 @@ if (process.env.SOCKET_SERVER) {
149
161
  colors.green(`Socket client ${clientIp} has connected. --- ${dateFormat('YYYY-mm-dd HH:MM:SS', new Date())}`)
150
162
  );
151
163
  socket.on('mock-dir-stat', function (dir) {
152
- socket.emit('mock-dir-stat', isEmptyObj(mockDirStat) ? mockDirStat = getMockStatFromDir(dir) : mockDirStat);
164
+ socket.emit('mock-dir-stat', isEmptyObj(mockDirStat) ? (mockDirStat = getMockStatFromDir(dir)) : mockDirStat);
153
165
  });
154
166
  socket.on('mock-file-stat', function (filePath) {
155
- socket.emit('mock-file-stat', isEmptyObj(mockFileStat) ? mockFileStat = getMockStatFromFile(filePath) : mockFileStat);
167
+ socket.emit(
168
+ 'mock-file-stat',
169
+ isEmptyObj(mockFileStat) ? (mockFileStat = getMockStatFromFile(filePath)) : mockFileStat
170
+ );
156
171
  });
157
172
  const async = args => {
158
173
  return async next => {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "mock-service-cli",
3
- "version": "2.4.3",
3
+ "version": "2.5.1",
4
4
  "description": "🦅 Local mock server",
5
5
  "main": "./lib/mockServer.js",
6
6
  "bin": {