har-gen-api 0.1.1 → 0.1.2

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/bin/cli.js CHANGED
@@ -1,4 +1,4 @@
1
- #!/usr/bin/env node
1
+ #!/usr/bin/env node
2
2
 
3
3
  import chalk from 'chalk';
4
4
  import cfonts from 'cfonts';
package/mockServer.js ADDED
@@ -0,0 +1,75 @@
1
+ import Mock from 'mockjs';
2
+ import fs from 'fs';
3
+ import _ from 'lodash';
4
+ import JSON5 from 'json5';
5
+
6
+ export const mockServer = ({ mockPath = 'mock', baseURL = '', enabled = false } = {}) => {
7
+ return {
8
+ name: 'mock-server',
9
+ configureServer(server) {
10
+ if (enabled) {
11
+ server.middlewares.use((req, res, next) => {
12
+ const url = new URL(req.url, `http://${req.headers.host}`);
13
+ let pathname = url.pathname;
14
+
15
+ if (pathname.startsWith(baseURL)) {
16
+ pathname = pathname.replace(new RegExp(`^${baseURL}`), '');
17
+
18
+ // console.log('路径', pathname);
19
+
20
+ // console.log(`${mockPath}${pathname}.${req.method.toLowerCase()}`)
21
+
22
+ fs.readFile(`${mockPath}${pathname}.${req.method.toLowerCase()}`, (err, data) => {
23
+ if (err) {
24
+ next();
25
+ return;
26
+ }
27
+
28
+ res.setHeader('X-Powered-By', 'mockjs');
29
+ res.setHeader('Content-Type', 'application/json; charset=utf-8');
30
+
31
+ try {
32
+ data = JSON5.parse(data);
33
+ const keys = Object.keys(data).filter(key => key.startsWith('?') || key === '');
34
+
35
+ keys.sort((a, b) => {
36
+ return a > b ? 1 : -1;
37
+ });
38
+
39
+ data = data[ url.search ] ?? data[ keys[0] ] ?? data;
40
+
41
+ if (_.isString(data)) {
42
+ // 模板解析
43
+ data = _.template(data)({
44
+ headers: req.headers,
45
+ query: req.query,
46
+ body: req.body,
47
+ });
48
+
49
+ data = JSON.parse(data || null);
50
+ }
51
+
52
+ data = Mock.mock(data);
53
+
54
+ // res.send(data);
55
+ res.end(JSON.stringify(data));
56
+ }
57
+ catch (e) {
58
+ console.error(e);
59
+ // res.status(500).send('Mock Error');
60
+ // res.send(500, 'Mock Error');
61
+ res.statusCode = 500;
62
+ res.end('Mock Error');
63
+ }
64
+ });
65
+ }
66
+ else {
67
+ next();
68
+ }
69
+ });
70
+
71
+ console.log('Mock server is running...');
72
+ }
73
+ },
74
+ };
75
+ };
package/package.json CHANGED
@@ -1,13 +1,20 @@
1
1
  {
2
2
  "name": "har-gen-api",
3
3
  "type": "module",
4
- "version": "0.1.1",
4
+ "version": "0.1.2",
5
5
  "license": "MIT",
6
6
  "repository": {
7
7
  "type": "git",
8
8
  "url": "git+https://github.com/lemonppai/har-gen-api.git"
9
9
  },
10
- "main": "index.js",
10
+ "exports": {
11
+ ".": {
12
+ "import": "./index.js"
13
+ },
14
+ "./vite": {
15
+ "import": "./mockServer.js"
16
+ }
17
+ },
11
18
  "bin": {
12
19
  "har-gen-api": "bin/cli.js"
13
20
  },
@@ -15,6 +22,8 @@
15
22
  "cfonts": "^3.3.1",
16
23
  "chalk": "^5.6.2",
17
24
  "commander": "^14.0.3",
18
- "json5": "^2.2.3"
25
+ "json5": "^2.2.3",
26
+ "lodash": "^4.18.1",
27
+ "mockjs": "^1.1.0"
19
28
  }
20
29
  }