har-gen-api 0.1.3 → 0.1.4
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/README.md +15 -6
- package/package.json +1 -4
- package/mockServer.mjs +0 -78
package/README.md
CHANGED
|
@@ -1,21 +1,30 @@
|
|
|
1
1
|
# har-gen-api
|
|
2
|
-
|
|
2
|
+
|
|
3
|
+
> 浏览器har文件转成自定义的接口文件,通过`vite`启动`mock`服务,实现接口模拟
|
|
3
4
|
|
|
4
5
|
## 使用
|
|
5
6
|
|
|
6
|
-
###
|
|
7
|
+
### 安装
|
|
7
8
|
```bash
|
|
8
|
-
npm install
|
|
9
|
+
npm install har-gen-api
|
|
10
|
+
# or
|
|
11
|
+
yarn add har-gen-api
|
|
9
12
|
```
|
|
10
13
|
|
|
14
|
+
### 导出har文件
|
|
15
|
+
1. 打开浏览器`chrome`
|
|
16
|
+
2. 打开`F12`切换到`Network`
|
|
17
|
+
3. 点击导出按钮,选择保存路径
|
|
18
|
+
4. 保存**har**文件到 `mock/har.local` 文件夹下
|
|
19
|
+
|
|
11
20
|
### 命令行使用
|
|
12
21
|
```bash
|
|
13
|
-
har-gen-api --input mock/har.local --output mock/api.local --baseURL /api --overwrite
|
|
22
|
+
npx har-gen-api --input mock/har.local --output mock/api.local --baseURL /api --overwrite
|
|
14
23
|
```
|
|
15
24
|
|
|
16
25
|
### 参数说明:
|
|
17
|
-
* input: 输入路径,默认为 mock/har.local (default: "mock/har.local")
|
|
18
|
-
* output: 输出路径,默认为 mock/api.local (default: "mock/api.local")
|
|
26
|
+
* input: 输入路径,默认为 `mock/har.local` (default: "mock/har.local")
|
|
27
|
+
* output: 输出路径,默认为 `mock/api.local` (default: "mock/api.local")
|
|
19
28
|
* baseURL: baseURL路径 (default: "")
|
|
20
29
|
* overwrite: 是否覆盖已存在的文件 (default: false)
|
|
21
30
|
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "har-gen-api",
|
|
3
3
|
"type": "module",
|
|
4
|
-
"version": "0.1.
|
|
4
|
+
"version": "0.1.4",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"repository": {
|
|
7
7
|
"type": "git",
|
|
@@ -13,9 +13,6 @@
|
|
|
13
13
|
},
|
|
14
14
|
"./vite": {
|
|
15
15
|
"import": "./mockServer.js"
|
|
16
|
-
},
|
|
17
|
-
"./vite/esm": {
|
|
18
|
-
"import": "./mockServer.mjs"
|
|
19
16
|
}
|
|
20
17
|
},
|
|
21
18
|
"bin": {
|
package/mockServer.mjs
DELETED
|
@@ -1,78 +0,0 @@
|
|
|
1
|
-
// mock/index.js
|
|
2
|
-
const Mock = require('mockjs');
|
|
3
|
-
const fs = require('fs');
|
|
4
|
-
const _ = require('lodash');
|
|
5
|
-
const JSON5 = require('json5');
|
|
6
|
-
|
|
7
|
-
const mockServer = ({ mockPath = 'mock', baseURL = '', enabled = false } = {}) => {
|
|
8
|
-
return {
|
|
9
|
-
name: 'mock-server',
|
|
10
|
-
configureServer(server) {
|
|
11
|
-
if (localEnabled) {
|
|
12
|
-
server.middlewares.use((req, res, next) => {
|
|
13
|
-
const url = new URL(req.url, `http://${req.headers.host}`);
|
|
14
|
-
let pathname = url.pathname;
|
|
15
|
-
|
|
16
|
-
if (pathname.startsWith(baseURL)) {
|
|
17
|
-
pathname = pathname.replace(new RegExp(`^${baseURL}`), '');
|
|
18
|
-
|
|
19
|
-
// console.log('路径', pathname);
|
|
20
|
-
|
|
21
|
-
// console.log(`${mockPath}${pathname}.${req.method.toLowerCase()}`)
|
|
22
|
-
|
|
23
|
-
fs.readFile(`${mockPath}${pathname}.${req.method.toLowerCase()}`, (err, data) => {
|
|
24
|
-
if (err) {
|
|
25
|
-
next();
|
|
26
|
-
return;
|
|
27
|
-
}
|
|
28
|
-
|
|
29
|
-
res.setHeader('X-Powered-By', 'mockjs');
|
|
30
|
-
res.setHeader('Content-Type', 'application/json; charset=utf-8');
|
|
31
|
-
|
|
32
|
-
try {
|
|
33
|
-
data = JSON5.parse(data);
|
|
34
|
-
const keys = Object.keys(data).filter(key => key.startsWith('?') || key === '');
|
|
35
|
-
|
|
36
|
-
keys.sort((a, b) => {
|
|
37
|
-
return a > b ? 1 : -1;
|
|
38
|
-
});
|
|
39
|
-
|
|
40
|
-
data = data[ url.search ] ?? data[ keys[0] ] ?? data;
|
|
41
|
-
|
|
42
|
-
if (_.isString(data)) {
|
|
43
|
-
// 模板解析
|
|
44
|
-
data = _.template(data)({
|
|
45
|
-
headers: req.headers,
|
|
46
|
-
query: req.query,
|
|
47
|
-
body: req.body,
|
|
48
|
-
});
|
|
49
|
-
|
|
50
|
-
data = JSON.parse(data || null);
|
|
51
|
-
}
|
|
52
|
-
|
|
53
|
-
data = Mock.mock(data);
|
|
54
|
-
|
|
55
|
-
// res.send(data);
|
|
56
|
-
res.end(JSON.stringify(data));
|
|
57
|
-
}
|
|
58
|
-
catch (e) {
|
|
59
|
-
console.error(e);
|
|
60
|
-
// res.status(500).send('Mock Error');
|
|
61
|
-
// res.send(500, 'Mock Error');
|
|
62
|
-
res.statusCode = 500;
|
|
63
|
-
res.end('Mock Error');
|
|
64
|
-
}
|
|
65
|
-
});
|
|
66
|
-
}
|
|
67
|
-
else {
|
|
68
|
-
next();
|
|
69
|
-
}
|
|
70
|
-
});
|
|
71
|
-
|
|
72
|
-
console.log('Mock server is running...');
|
|
73
|
-
}
|
|
74
|
-
},
|
|
75
|
-
};
|
|
76
|
-
};
|
|
77
|
-
|
|
78
|
-
exports.mockServer = mockServer;
|