mock-service-cli 1.0.2 → 1.2.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/LICENSE.md CHANGED
@@ -1,6 +1,6 @@
1
1
  MIT License
2
2
 
3
- Copyright (c) 2020 [chendq](https://github.com/chandq)
3
+ Copyright (c) 2021-present, [chendq](https://github.com/chandq)
4
4
 
5
5
  Permission is hereby granted, free of charge, to any person obtaining a copy
6
6
  of this software and associated documentation files (the "Software"), to deal
package/README.md CHANGED
@@ -1,14 +1,16 @@
1
- [![Build Status](https://travis-ci.org/chandq/mock-service-cli.svg?branch=master)](https://travis-ci.org/chandq/mock-service-cli)
1
+ [![GitHub Workflow Status (master)](https://img.shields.io/github/workflow/status/chandq/mock-service-cli/Node.js%20CI/master?style=flat-square)](https://github.com/chandq/mock-service-cli/actions)
2
2
  [![Coverage Status](https://coveralls.io/repos/github/chandq/mock-service-cli/badge.svg?branch=master)](https://coveralls.io/github/chandq/mock-service-cli?branch=master)
3
- [![mock-service-cli](https://img.shields.io/npm/v/mock-service-cli.svg)](https://www.npmjs.com/package/mock-service-cli)
4
- [![license:MIT](https://img.shields.io/npm/l/vue.svg?sanitize=true)](https://www.npmjs.com/package/mock-service-cli#license)
3
+ [![mock-service-cli](https://img.shields.io/github/package-json/v/chandq/mock-service-cli?style=flat-square)](https://github.com/chandq/mock-service-cli)
4
+ [![node](https://img.shields.io/badge/node-v14.0.0-blue)](https://nodejs.org/download/release/v14.0.0/)
5
+ [![node](https://img.shields.io/badge/language-node-orange.svg)](https://nodejs.org/download/release/v14.0.0/)
6
+ [![license:MIT](https://img.shields.io/npm/l/vue.svg?sanitize=true)](https://github.com/chandq/mock-service-cli/blob/master/LICENSE.md)
5
7
  [![Downloads:?](https://img.shields.io/npm/dm/mock-service-cli.svg?sanitize=true)](https://npmcharts.com/compare/mock-service-cli?minimal=true)
6
8
 
7
- # mock-service-cli: 🦅 一个基于 node 和 express 的 本地 Mock Server 命令行工具
9
+ # 🦅 一个基于 [node](https://nodejs.org/en/)[express](https://www.expressjs.com.cn/) 的 本地 Mock Server 命令行工具
8
10
 
9
11
  ### 简介
10
12
 
11
- 简单、零配置的命令行 Mock 服务器, 支持热更新,对于前端开发调试很实用,能提高前端开发者的开发效率。
13
+ **使用简单**、**零配置**、**0 秒启动**的本地命令行 Mock 服务器, 支持热更新,对于开发调试 mock 数据很实用,能提高前端开发者的开发效率。支持 `GET`,`POST`,`PUT`,`DELETE`,`PATCH`,`OPTIONS`,`COPY`,`LINK`,`UNLINK`,`PURGE` 等常用请求类型,对工程代码无任何侵入性,可能是本地最好用的 Mock 工具。
12
14
 
13
15
  ## Installation:
14
16
 
@@ -34,15 +36,15 @@ This will install `mock-service-cli` globally so that it may be run from the com
34
36
 
35
37
  ## Usage:
36
38
 
37
- mock-service-cli [path] [options]
39
+ mock-service-cli [options] [path]
38
40
 
39
41
  `[path]` defaults to `./mock` .
40
42
 
41
43
  | Command | Description | Defaults |
42
44
  | ------------------- | -------------------------------------------------------------------------------------------------------------- | -------- |
43
45
  | `-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 |
44
- | `-d` | Special mock directory | ./mock |
45
- | `-f` | Special mock file | |
46
+ | `-d` | Specify mock directory | ./mock |
47
+ | `-f` | Specify mock file | |
46
48
  | `-s` or `--silent` | Suppress log messages from output | |
47
49
  | `-h` or `--help` | Print this list and exit. | |
48
50
  | `-v` or `--version` | Print the version and exit. | |
@@ -61,17 +63,51 @@ This will install `mock-service-cli` globally so that it may be run from the com
61
63
 
62
64
  `mock-service-cli -f ./mock/test.js`
63
65
 
64
- ### mock file template
66
+ ### 编写 Mock 文件
67
+
68
+ > mock 请求的类型支持`GET`,`POST`,`PUT`,`DELETE`,`PATCH`,`OPTIONS`,`COPY`,`LINK`,`UNLINK`,`PURGE`
69
+
70
+ 如果 `./mock/test.js` 的内容如下,
65
71
 
66
- > e.g. `./mock/test.js`
67
72
  ```javascript
68
73
  module.exports = {
69
- '/mock/:id/test': { aa: 1, bb: '西西小飞龙' },
70
- '/mock/video/test': (req, res) => {
74
+ // GET 可忽略
75
+ '/mock/mock/api/test': { aa: 1, bb: '默认GET请求' },
76
+ 'GET /mock/api/:id/test': { aa: 1, bb: '使用id占位符' },
77
+ 'GET /mock/api/test1': { aa: 1, bb: '指定GET 方法' },
78
+ 'POST /mock/api/test': { aa: 1, bb: 'POST 方法' },
79
+ 'DELETE /mock/api/test': { aa: 1, bb: 'DELETE 方法' },
80
+ 'PUT /mock/api/test': { aa: 1, bb: 'PUT 方法' },
81
+ 'PATCH /mock/api/test': { aa: 1, bb: 'PATCH 方法' },
82
+ 'OPTIONS /mock/api/test': { aa: 1, bb: 'OPTIONS 方法' },
83
+ 'COPY /mock/api/test': { aa: 1, bb: 'COPY 方法' },
84
+ 'LINK /mock/api/test': { aa: 1, bb: 'LINK 方法' },
85
+ 'UNLINK /mock/api/test': { aa: 1, bb: 'UNLINK 方法' },
86
+ 'PURGE /mock/api/test': { aa: 1, bb: 'PURGE 方法' },
87
+ // 支持自定义函数,API 参考 express@4
88
+ '/mock/api/video/test': (req, res) => {
89
+ // 添加特定请求头token
90
+ res.setHeader('token', '5848778333359208');
71
91
  res.json({ aa: 1, bb: 'asdf' });
72
- },
73
- '/mock/image/test': (req, res) => {
74
- res.json({ aa: 1, bb: 'yyds' });
75
- },
92
+ }
93
+ };
94
+ ```
95
+
96
+ 然后访问 `本地代理地址` + `/mock/api/222/test` (例如`http://127.0.0.1:8090/mock/api/222/test`) 就能得到 { aa: 1, bb: '使用 id 占位符' } 的响应,其他以此类推。
97
+
98
+ ### 引入 Mock.js
99
+
100
+ [Mock.js](http://mockjs.com/) 是常用的辅助生成模拟数据的三方库,借助他可以提升我们的 mock 数据能力。
101
+
102
+ 比如:
103
+
104
+ ```js
105
+ const mockjs = require('mockjs');
106
+
107
+ module.exports = {
108
+ // 使用 mockjs 等三方库
109
+ 'GET /api/tags': mockjs.mock({
110
+ 'list|100': [{ name: '@city', 'value|1-100': 50, 'type|0-2': 1 }]
111
+ })
76
112
  };
77
113
  ```
@@ -11,13 +11,13 @@ process.title = 'mock-service-cli';
11
11
  if (argv.h || argv.help) {
12
12
  console.log(
13
13
  [
14
- 'usage: http-server [path] [options]',
14
+ 'usage: mock-service-cli [options]',
15
15
  '',
16
16
  'options:',
17
17
  ' -p --port Port to use. If 0, look for open port. [8090]',
18
- ' -d Show directory listings [true], default [/mock] directory,',
18
+ ' -d Specify mock directory, default [./mock] directory,',
19
19
  ' Also watch js files changes and support hot reload',
20
- ' -f --file Specify the input data source, support javascript or JSON file',
20
+ ' -f --file Specify the input data source, support javascript ',
21
21
  ' The priority is over the directory watches',
22
22
  ' -s --silent Suppress log messages from output',
23
23
  '',
@@ -79,7 +79,9 @@ const watchMockFiles = function (watchDir) {
79
79
  })
80
80
  .on('restart', function (files) {
81
81
  process.env.RESTARTED = true;
82
- log.info([colors.green('\nnodemon: mockServer restarted due to: '), files, '\n'].join(''));
82
+ log.info(
83
+ [colors.green('\nnodemon: mockServer restarted due to: '), files, colors.green(' have changed'), '\n'].join('')
84
+ );
83
85
  });
84
86
  };
85
87
 
package/lib/mockServer.js CHANGED
@@ -8,8 +8,13 @@ const express = require('express'), // 引入express
8
8
  const ifaces = os.networkInterfaces();
9
9
  const { dateFormat, logger } = require('./utils');
10
10
 
11
+ const methodRegExp = new RegExp('(GET|POST|PUT|DELETE|HEAD|PATCH|OPTIONS|COPY|LINK|UNLINK|PURGE) +(.*)', 'i');
12
+
11
13
  const app = express();
12
14
  const log = logger(process.env.SILENT);
15
+
16
+ let count = 0,
17
+ fileCount = 0; // 记录mock api个数,mock file个数
13
18
  let done = false;
14
19
  if (!process.env.PORT) {
15
20
  portfinder.basePort = 8090;
@@ -37,7 +42,7 @@ const crossDomain = () => (req, res, next) => {
37
42
  res.header('Access-Control-Allow-Credentials', true);
38
43
  // res.header('Access-Control-Allow-Origin', 'http://localhost:8081');
39
44
  res.header('Access-Control-Allow-Origin', '*');
40
- res.header('Access-Control-Allow-Methods', 'GET,HEAD,OPTIONS,POST,PUT');
45
+ res.header('Access-Control-Allow-Methods', 'GET,POST,PUT,DELETE,HEAD,PATCH,OPTIONS,COPY,LINK,UNLINK,PURGE');
41
46
  // res.header(
42
47
  // 'Access-Control-Allow-Headers',
43
48
  // 'Origin, X-Requested-With, Content-Type, Accept, Authorization'
@@ -57,13 +62,21 @@ const composeRoute = function (file) {
57
62
  delete require.cache[require.resolve(file)];
58
63
  const fileObject = require(path.resolve(__dirname, file));
59
64
 
60
- log.info(colors.green('Mockfile: '), file);
65
+ log.info(colors.green(`Mockfile ${++fileCount}: `), file, colors.yellow('all API URL is follows: '));
61
66
  Object.keys(fileObject).forEach(item => {
62
- log.info(colors.yellow('Traversal url: '), item, colors.blue(typeof fileObject[item]));
67
+ let reqMethod = 'get',
68
+ reqUrl = item;
69
+ // 支持(GET|POST|PUT|DELETE|HEAD|PATCH|OPTIONS|COPY|LINK|UNLINK|PURGE)常用方法, 默认GET请求
70
+ if (methodRegExp.exec(item)) {
71
+ const [, method, url] = methodRegExp.exec(item);
72
+ reqMethod = method.toLowerCase();
73
+ reqUrl = url;
74
+ }
75
+ log.info(colors.yellow(`path ${++count}: `), item, colors.blue(typeof fileObject[item]));
63
76
  if (typeof fileObject[item] === 'function') {
64
- app.use(item, fileObject[item]);
77
+ app[reqMethod](reqUrl, fileObject[item]);
65
78
  } else if (typeof fileObject[item] === 'object') {
66
- app.use(item, function (req, res) {
79
+ app[reqMethod](reqUrl, function (req, res) {
67
80
  res.json(fileObject[item]);
68
81
  });
69
82
  }
@@ -94,7 +107,6 @@ const parseMockFiles = function (specialDir = '../mock', isRootDir = true) {
94
107
  }
95
108
  };
96
109
  app.use(crossDomain());
97
-
98
110
  if (process.env.SPECIFIED_FILE) {
99
111
  composeRoute(process.env.SPECIFIED_FILE);
100
112
  } else {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "mock-service-cli",
3
- "version": "1.0.2",
3
+ "version": "1.2.1",
4
4
  "description": "🦅 Local mock server",
5
5
  "main": "./lib/mockServer.js",
6
6
  "bin": {
@@ -8,10 +8,10 @@
8
8
  },
9
9
  "scripts": {
10
10
  "start": "node ./bin/mock-service-cli",
11
- "unit-test": "tap --reporter=spec test/*.test.js",
11
+ "test:unit": "tap --reporter=spec test/*.test.js",
12
12
  "test": "tap --cov --coverage-report=lcov --reporter=spec test/*.test.js ",
13
- "test-watch": "tap --reporter=spec --watch test/*.test.js",
14
13
  "coverage": "cat ./coverage/lcov.info | ./node_modules/.bin/coveralls -v",
14
+ "test-watch": "tap --reporter=spec --watch test/*.test.js",
15
15
  "fix": "eslint --ext .js,.vue,.css --fix",
16
16
  "prettier": "prettier -c --write \"**/*.{ts,js,jsx,css,less,scss,json}\"",
17
17
  "release:prerelease": "standard-version --prerelease",
@@ -26,7 +26,7 @@
26
26
  "doc"
27
27
  ],
28
28
  "engines": {
29
- "node": ">=12"
29
+ "node": ">=14"
30
30
  },
31
31
  "author": "chendq <deqiaochen@gmail.com>",
32
32
  "keywords": [
@@ -45,16 +45,15 @@
45
45
  "dependencies": {
46
46
  "colors": "^1.4.0",
47
47
  "deasync": "^0.1.23",
48
+ "express": "^4.17.1",
48
49
  "minimist": "^1.2.5",
49
50
  "nodemon": "^2.0.14",
50
- "express": "^4.17.1",
51
51
  "portfinder": "^1.0.28"
52
52
  },
53
53
  "devDependencies": {
54
54
  "@commitlint/cli": "^8.3.5",
55
55
  "@commitlint/config-conventional": "^8.3.4",
56
56
  "babel-eslint": "^10.1.0",
57
- "coveralls": "^3.1.0",
58
57
  "eslint": "^7.0.0",
59
58
  "eslint-config-populist": "^4.2.0",
60
59
  "eslint-plugin-mocha": "^9.0.0",
@@ -64,6 +63,9 @@
64
63
  "standard-version": "^8.0.0",
65
64
  "tap": "^15.0.10"
66
65
  },
66
+ "tap": {
67
+ "check-coverage": false
68
+ },
67
69
  "husky": {
68
70
  "hooks": {
69
71
  "pre-commit": "lint-staged",