mock-service-cli 1.1.1 → 1.2.0

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
@@ -10,7 +10,7 @@
10
10
 
11
11
  ### 简介
12
12
 
13
- 简单、零配置的命令行 Mock 服务器, 支持热更新,对于前端开发调试很实用,能提高前端开发者的开发效率。
13
+ **使用简单**、**零配置**、**0 秒启动**的本地命令行 Mock 服务器, 支持热更新,对于开发调试 mock 数据很实用,能提高前端开发者的开发效率。支持 `GET`,`POST`,`PUT`,`DELETE`,`PATCH`,`OPTIONS`,`COPY`,`LINK`,`UNLINK`,`PURGE` 等常用请求类型,对工程代码无任何侵入性,可能是本地最好用的 Mock 工具。
14
14
 
15
15
  ## Installation:
16
16
 
@@ -69,7 +69,17 @@ This will install `mock-service-cli` globally so that it may be run from the com
69
69
 
70
70
  ```javascript
71
71
  module.exports = {
72
- '/mock/:id/test': { aa: 1, bb: '西西小飞龙' },
72
+ '/mock/:id/test': { aa: 1, bb: '默认GET请求' },
73
+ 'GET /mock/:id/test': { aa: 1, bb: '指定GET 方法' },
74
+ 'POST /mock/:id/test': { aa: 1, bb: 'POST 方法' },
75
+ 'DELETE /mock/:id/test': { aa: 1, bb: 'DELETE 方法' },
76
+ 'PUT /mock/:id/test': { aa: 1, bb: 'PUT 方法' },
77
+ 'PATCH /mock/:id/test': { aa: 1, bb: 'PATCH 方法' },
78
+ 'OPTIONS /mock/:id/test': { aa: 1, bb: 'OPTIONS 方法' },
79
+ 'COPY /mock/:id/test': { aa: 1, bb: 'COPY 方法' },
80
+ 'LINK /mock/:id/test': { aa: 1, bb: 'LINK 方法' },
81
+ 'UNLINK /mock/:id/test': { aa: 1, bb: 'UNLINK 方法' },
82
+ 'PURGE /mock/:id/test': { aa: 1, bb: 'PURGE 方法' },
73
83
  '/mock/video/test': (req, res) => {
74
84
  res.json({ aa: 1, bb: 'asdf' });
75
85
  },
@@ -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.1.1",
3
+ "version": "1.2.0",
4
4
  "description": "🦅 Local mock server",
5
5
  "main": "./lib/mockServer.js",
6
6
  "bin": {