har-gen-api 0.1.2 → 0.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/.editorconfig ADDED
@@ -0,0 +1,9 @@
1
+ root = true
2
+
3
+ [*]
4
+ charset = utf-8
5
+ indent_style = space
6
+ indent_size = 2
7
+ end_of_line = lf
8
+ insert_final_newline = true
9
+ trim_trailing_whitespace = true
package/README.md CHANGED
@@ -1,20 +1,35 @@
1
- # har-gen-api
2
- > 浏览器HAR转成接口文件
3
-
4
- ## 使用
5
-
6
- ### 全局安装
7
- ```bash
8
- npm install -g har-gen-api
9
- ```
10
-
11
- ### 命令行使用
12
- ```bash
13
- har-gen-api --input mock/har.local --output mock/api.local --baseURL /api --overwrite
14
- ```
15
-
16
- ### 参数说明:
17
- * input: 输入路径,默认为 mock/har.local (default: "mock/har.local")
18
- * output: 输出路径,默认为 mock/api.local (default: "mock/api.local")
19
- * baseURL: baseURL路径 (default: "")
20
- * overwrite: 是否覆盖已存在的文件 (default: false)
1
+ # har-gen-api
2
+ > 浏览器HAR转成接口文件
3
+
4
+ ## 使用
5
+
6
+ ### 全局安装
7
+ ```bash
8
+ npm install -g har-gen-api
9
+ ```
10
+
11
+ ### 命令行使用
12
+ ```bash
13
+ har-gen-api --input mock/har.local --output mock/api.local --baseURL /api --overwrite
14
+ ```
15
+
16
+ ### 参数说明:
17
+ * input: 输入路径,默认为 mock/har.local (default: "mock/har.local")
18
+ * output: 输出路径,默认为 mock/api.local (default: "mock/api.local")
19
+ * baseURL: baseURL路径 (default: "")
20
+ * overwrite: 是否覆盖已存在的文件 (default: false)
21
+
22
+ ### vite配置
23
+ ```js
24
+ import { mockServer } from 'har-gen-api/vite';
25
+
26
+ export default defineConfig({
27
+ plugins: [
28
+ mockServer({
29
+ mockPath: 'mock',
30
+ baseURL: '/api',
31
+ enabled: true
32
+ })
33
+ ]
34
+ })
35
+ ```
package/mockServer.mjs ADDED
@@ -0,0 +1,78 @@
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;
package/package.json CHANGED
@@ -1,29 +1,32 @@
1
- {
2
- "name": "har-gen-api",
3
- "type": "module",
4
- "version": "0.1.2",
5
- "license": "MIT",
6
- "repository": {
7
- "type": "git",
8
- "url": "git+https://github.com/lemonppai/har-gen-api.git"
9
- },
10
- "exports": {
11
- ".": {
12
- "import": "./index.js"
13
- },
14
- "./vite": {
15
- "import": "./mockServer.js"
16
- }
17
- },
18
- "bin": {
19
- "har-gen-api": "bin/cli.js"
20
- },
21
- "dependencies": {
22
- "cfonts": "^3.3.1",
23
- "chalk": "^5.6.2",
24
- "commander": "^14.0.3",
25
- "json5": "^2.2.3",
26
- "lodash": "^4.18.1",
27
- "mockjs": "^1.1.0"
28
- }
29
- }
1
+ {
2
+ "name": "har-gen-api",
3
+ "type": "module",
4
+ "version": "0.1.3",
5
+ "license": "MIT",
6
+ "repository": {
7
+ "type": "git",
8
+ "url": "git+https://github.com/lemonppai/har-gen-api.git"
9
+ },
10
+ "exports": {
11
+ ".": {
12
+ "import": "./index.js"
13
+ },
14
+ "./vite": {
15
+ "import": "./mockServer.js"
16
+ },
17
+ "./vite/esm": {
18
+ "import": "./mockServer.mjs"
19
+ }
20
+ },
21
+ "bin": {
22
+ "har-gen-api": "bin/cli.js"
23
+ },
24
+ "dependencies": {
25
+ "cfonts": "^3.3.1",
26
+ "chalk": "^5.6.2",
27
+ "commander": "^14.0.3",
28
+ "json5": "^2.2.3",
29
+ "lodash": "^4.18.1",
30
+ "mockjs": "^1.1.0"
31
+ }
32
+ }