json-api-mocker 2.3.0 → 3.0.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/DESIGN.md +227 -0
- package/README.ch.md +95 -224
- package/README.md +98 -227
- package/dist/cli.js +37 -2
- package/dist/index.d.ts +5 -1
- package/dist/index.js +7 -2
- package/dist/server.d.ts +17 -14
- package/dist/server.js +290 -100
- package/dist/types.d.ts +26 -21
- package/package.json +21 -11
- package/web/assets/index-C4eZNDku.js +25 -0
- package/web/assets/index-CGhPNH5w.css +1 -0
- package/web/index.html +15 -0
- package/web/monaco-editor-worker-loader.js +8 -0
- package/web/vite.svg +1 -0
package/README.md
CHANGED
@@ -1,6 +1,6 @@
|
|
1
1
|
# JSON API Mocker
|
2
2
|
|
3
|
-
A lightweight and flexible mock server
|
3
|
+
A lightweight and flexible mock server with JSON configuration and visual management interface.
|
4
4
|
|
5
5
|
<p align="center">
|
6
6
|
<img src="https://img.shields.io/npm/v/json-api-mocker" alt="npm version" />
|
@@ -10,33 +10,57 @@ A lightweight and flexible mock server that uses JSON configuration to quickly c
|
|
10
10
|
|
11
11
|
## ✨ Features
|
12
12
|
|
13
|
-
- 🚀
|
14
|
-
-
|
13
|
+
- 🚀 Brand new visual management interface
|
14
|
+
- 🚀 Support both configuration file and UI management
|
15
|
+
- 🔄 Support all common HTTP methods
|
15
16
|
- 📝 Automatic data persistence
|
16
17
|
- 🔍 Built-in pagination support
|
17
|
-
- 🛠 Customizable response
|
18
|
-
- 🎭
|
19
|
-
-
|
18
|
+
- 🛠 Customizable response structure
|
19
|
+
- 🎭 Powerful data mocking with Mock.js
|
20
|
+
- 📊 Real-time request logs and statistics
|
20
21
|
- 💡 TypeScript support
|
21
22
|
|
22
23
|
## 📦 Installation
|
23
24
|
|
24
25
|
```bash
|
25
|
-
|
26
|
-
|
26
|
+
npm install -g json-api-mocker
|
27
|
+
```
|
28
|
+
|
29
|
+
## 🚀 Quick Start
|
30
|
+
|
31
|
+
The server uses fixed ports:
|
32
|
+
- API Server: 35728
|
33
|
+
- Web UI: 35729
|
34
|
+
- WebSocket: 35730
|
35
|
+
|
36
|
+
### Method 1: Using Visual Interface (Recommended)
|
27
37
|
|
28
|
-
|
29
|
-
|
38
|
+
1. Create a `data.json` file:
|
39
|
+
```json
|
40
|
+
{
|
41
|
+
"server": {
|
42
|
+
"port": 35728,
|
43
|
+
"baseProxy": "/api"
|
44
|
+
},
|
45
|
+
"routes": []
|
46
|
+
}
|
47
|
+
```
|
30
48
|
|
31
|
-
|
32
|
-
|
49
|
+
2. Start the server:
|
50
|
+
```bash
|
51
|
+
# Start server and open browser
|
52
|
+
json-api-mocker -o
|
33
53
|
```
|
34
54
|
|
35
|
-
|
55
|
+
After starting, the browser will automatically open the management interface, where you can:
|
56
|
+
1. Visually create and manage APIs
|
57
|
+
2. View real-time request logs
|
58
|
+
3. Monitor API call statistics
|
59
|
+
4. Debug mock data online
|
36
60
|
|
37
|
-
###
|
61
|
+
### Method 2: Using Configuration File (Compatible with old version)
|
38
62
|
|
39
|
-
Create a `data.json` file
|
63
|
+
Create a `data.json` file:
|
40
64
|
|
41
65
|
```json
|
42
66
|
{
|
@@ -46,40 +70,21 @@ Create a `data.json` file in your project root:
|
|
46
70
|
},
|
47
71
|
"routes": [
|
48
72
|
{
|
49
|
-
"
|
50
|
-
"
|
51
|
-
"
|
52
|
-
|
53
|
-
"
|
54
|
-
"
|
55
|
-
"
|
56
|
-
|
57
|
-
|
58
|
-
"response": [
|
59
|
-
{
|
60
|
-
"id": 1,
|
61
|
-
"name": "John",
|
62
|
-
"age": 30,
|
63
|
-
"city": "New York"
|
64
|
-
}
|
65
|
-
]
|
66
|
-
}
|
67
|
-
}
|
68
|
-
},
|
69
|
-
{
|
70
|
-
"path": "/upload/avatar",
|
71
|
-
"methods": {
|
72
|
-
"post": {
|
73
|
-
"type": "object",
|
74
|
-
"mock": {
|
75
|
-
"enabled": true,
|
76
|
-
"template": {
|
77
|
-
"success": true,
|
78
|
-
"message": "Upload successful",
|
73
|
+
"id": "user-api",
|
74
|
+
"route": {
|
75
|
+
"path": "/users",
|
76
|
+
"methods": {
|
77
|
+
"get": {
|
78
|
+
"status": 200,
|
79
|
+
"response": {
|
80
|
+
"code": 200,
|
81
|
+
"message": "success",
|
79
82
|
"data": {
|
80
|
-
"
|
81
|
-
|
82
|
-
|
83
|
+
"list|10": [{
|
84
|
+
"id": "@id",
|
85
|
+
"name": "@name",
|
86
|
+
"email": "@email"
|
87
|
+
}]
|
83
88
|
}
|
84
89
|
}
|
85
90
|
}
|
@@ -90,110 +95,45 @@ Create a `data.json` file in your project root:
|
|
90
95
|
}
|
91
96
|
```
|
92
97
|
|
93
|
-
|
94
|
-
|
95
|
-
There are several ways to start the mock server:
|
98
|
+
Then start the server:
|
96
99
|
|
97
100
|
```bash
|
98
|
-
# Method 1: Using npx (Recommended)
|
99
|
-
npx json-api-mocker
|
100
|
-
|
101
|
-
# Method 2: Using npx with a custom config file
|
102
|
-
npx json-api-mocker ./custom-config.json
|
103
|
-
|
104
|
-
# Method 3: If installed globally
|
105
101
|
json-api-mocker
|
106
|
-
|
107
|
-
# Method 4: If installed as a project dependency
|
108
|
-
# Add this to your package.json scripts:
|
109
|
-
{
|
110
|
-
"scripts": {
|
111
|
-
"mock": "json-api-mocker"
|
112
|
-
}
|
113
|
-
}
|
114
|
-
# Then run:
|
115
|
-
npm run mock
|
116
102
|
```
|
117
103
|
|
118
|
-
|
119
|
-
|
120
|
-
You'll see output like this:
|
121
|
-
```bash
|
122
|
-
Mock Server is running:
|
123
|
-
- Address: http://localhost:8080
|
124
|
-
- Base Path: /api
|
125
|
-
Available APIs:
|
126
|
-
GET http://localhost:8080/api/users
|
127
|
-
POST http://localhost:8080/api/users
|
128
|
-
POST http://localhost:8080/api/upload/avatar
|
129
|
-
```
|
130
|
-
|
131
|
-
## 📖 Configuration Guide
|
132
|
-
|
133
|
-
For detailed configuration options, please refer to [CONFIG.md](./CONFIG.md).
|
104
|
+
## 📖 Configuration
|
134
105
|
|
135
106
|
### Server Configuration
|
136
107
|
|
137
|
-
The `server` section configures basic server settings:
|
138
|
-
|
139
108
|
```json
|
140
109
|
{
|
141
110
|
"server": {
|
142
|
-
"port": 8080, // Server port
|
143
|
-
"baseProxy": "/api" //
|
111
|
+
"port": 8080, // Server port
|
112
|
+
"baseProxy": "/api" // API base path
|
144
113
|
}
|
145
114
|
}
|
146
115
|
```
|
147
116
|
|
148
|
-
###
|
117
|
+
### API Configuration
|
149
118
|
|
150
|
-
Each
|
119
|
+
Each API configuration includes:
|
151
120
|
|
152
121
|
```json
|
153
122
|
{
|
154
|
-
"
|
155
|
-
"
|
156
|
-
"
|
157
|
-
|
158
|
-
"
|
159
|
-
"
|
160
|
-
"
|
161
|
-
|
162
|
-
|
163
|
-
|
164
|
-
|
165
|
-
"post": {
|
166
|
-
"requestSchema": { // Request body validation schema
|
167
|
-
"name": "string",
|
168
|
-
"age": "number"
|
169
|
-
},
|
170
|
-
"response": {
|
171
|
-
"success": true
|
172
|
-
}
|
173
|
-
}
|
174
|
-
}
|
175
|
-
}
|
176
|
-
```
|
177
|
-
|
178
|
-
### File Upload Support
|
179
|
-
|
180
|
-
You can configure file upload endpoints in your `data.json`:
|
181
|
-
|
182
|
-
```json
|
183
|
-
{
|
184
|
-
"path": "/upload/avatar",
|
185
|
-
"methods": {
|
186
|
-
"post": {
|
187
|
-
"type": "object",
|
188
|
-
"mock": {
|
189
|
-
"enabled": true,
|
190
|
-
"template": {
|
191
|
-
"success": true,
|
192
|
-
"message": "Upload successful",
|
123
|
+
"id": "unique-id", // API unique identifier
|
124
|
+
"route": {
|
125
|
+
"path": "/users", // API path (without base path)
|
126
|
+
"methods": { // Supported HTTP methods
|
127
|
+
"get": {
|
128
|
+
"status": 200, // Response status code
|
129
|
+
"headers": { // Custom response headers
|
130
|
+
"Content-Type": "application/json"
|
131
|
+
},
|
132
|
+
"response": { // Response data (supports Mock.js syntax)
|
133
|
+
"code": 200,
|
193
134
|
"data": {
|
194
|
-
"
|
195
|
-
"
|
196
|
-
"size": "@integer(1000, 1000000)"
|
135
|
+
"name": "@name",
|
136
|
+
"age": "@integer(18, 60)"
|
197
137
|
}
|
198
138
|
}
|
199
139
|
}
|
@@ -202,113 +142,44 @@ You can configure file upload endpoints in your `data.json`:
|
|
202
142
|
}
|
203
143
|
```
|
204
144
|
|
205
|
-
|
206
|
-
|
207
|
-
```bash
|
208
|
-
# Upload single file
|
209
|
-
curl -X POST http://localhost:8080/api/upload/avatar \
|
210
|
-
-H "Content-Type: multipart/form-data" \
|
211
|
-
-F "avatar=@/path/to/your/image.jpg"
|
212
|
-
|
213
|
-
# Upload multiple files
|
214
|
-
curl -X POST http://localhost:8080/api/upload/images \
|
215
|
-
-H "Content-Type: multipart/form-data" \
|
216
|
-
-F "images=@/path/to/image1.jpg" \
|
217
|
-
-F "images=@/path/to/image2.jpg"
|
218
|
-
```
|
219
|
-
|
220
|
-
For detailed configuration options, please refer to [CONFIG.md](./CONFIG.md#file-upload-configuration).
|
145
|
+
## 🎮 Visual Interface Features
|
221
146
|
|
222
|
-
|
147
|
+
### 1. API Management
|
148
|
+
- Create, edit, and delete APIs
|
149
|
+
- Support multiple HTTP methods
|
150
|
+
- Visual response data editor
|
151
|
+
- Mock.js syntax hints
|
223
152
|
|
224
|
-
###
|
225
|
-
|
226
|
-
|
227
|
-
|
228
|
-
|
229
|
-
```
|
230
|
-
|
231
|
-
#### Get Single User
|
232
|
-
```bash
|
233
|
-
curl http://localhost:8080/api/users/1
|
234
|
-
```
|
153
|
+
### 2. Real-time Logs
|
154
|
+
- Request path and method
|
155
|
+
- Response status and duration
|
156
|
+
- Request parameters recording
|
157
|
+
- Response data viewing
|
235
158
|
|
236
|
-
|
237
|
-
|
238
|
-
|
239
|
-
|
240
|
-
|
241
|
-
```
|
159
|
+
### 3. Statistics Dashboard
|
160
|
+
- Total API count
|
161
|
+
- Request volume monitoring
|
162
|
+
- Average response time
|
163
|
+
- Success rate statistics
|
242
164
|
|
243
|
-
|
244
|
-
```bash
|
245
|
-
curl -X PUT http://localhost:8080/api/users/1 \
|
246
|
-
-H "Content-Type: application/json" \
|
247
|
-
-d '{"name":"Alice","age":26,"city":"Boston"}'
|
248
|
-
```
|
165
|
+
## 🔧 Command Line Options
|
249
166
|
|
250
|
-
#### Delete User
|
251
167
|
```bash
|
252
|
-
|
253
|
-
```
|
254
|
-
|
255
|
-
### Advanced Usage
|
168
|
+
json-api-mocker [options]
|
256
169
|
|
257
|
-
|
258
|
-
|
259
|
-
|
260
|
-
|
170
|
+
Options:
|
171
|
+
-p, --port <number> Specify server port (default: 8080)
|
172
|
+
-c, --config <path> Specify config file path (default: data.json)
|
173
|
+
-o, --open Auto open management interface
|
174
|
+
-h, --help Show help information
|
175
|
+
-v, --version Show version number
|
261
176
|
```
|
262
177
|
|
263
|
-
#### Custom Response Headers
|
264
|
-
The server automatically adds these headers:
|
265
|
-
- `X-Total-Count`: Total number of items (for paginated responses)
|
266
|
-
|
267
|
-
## 🔧 Advanced Configuration
|
268
|
-
|
269
|
-
### Dynamic Routes
|
270
|
-
|
271
|
-
You can use URL parameters in routes:
|
272
|
-
|
273
|
-
```json
|
274
|
-
{
|
275
|
-
"path": "/users/:id/posts",
|
276
|
-
"methods": {
|
277
|
-
"get": {
|
278
|
-
"type": "array",
|
279
|
-
"response": []
|
280
|
-
}
|
281
|
-
}
|
282
|
-
}
|
283
|
-
```
|
284
|
-
|
285
|
-
### Request Validation
|
286
|
-
|
287
|
-
Add schema validation for POST/PUT requests:
|
288
|
-
|
289
|
-
```json
|
290
|
-
{
|
291
|
-
"requestSchema": {
|
292
|
-
"name": "string",
|
293
|
-
"age": "number",
|
294
|
-
"email": "string"
|
295
|
-
}
|
296
|
-
}
|
297
|
-
```
|
298
|
-
|
299
|
-
## 🤝 Contributing
|
300
|
-
|
301
|
-
1. Fork the repository
|
302
|
-
2. Create your feature branch (`git checkout -b feature/amazing-feature`)
|
303
|
-
3. Commit your changes (`git commit -m 'Add some amazing feature'`)
|
304
|
-
4. Push to the branch (`git push origin feature/amazing-feature`)
|
305
|
-
5. Open a Pull Request
|
306
|
-
|
307
178
|
## 📄 License
|
308
179
|
|
309
180
|
MIT © [Xiong Haiyin]
|
310
181
|
|
311
182
|
## 🙏 Acknowledgments
|
312
183
|
|
313
|
-
-
|
314
|
-
-
|
184
|
+
- Thanks to all contributors and users
|
185
|
+
- Special thanks to Mock.js for data mocking support
|
package/dist/cli.js
CHANGED
@@ -1,6 +1,41 @@
|
|
1
1
|
#!/usr/bin/env node
|
2
2
|
"use strict";
|
3
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
4
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
5
|
+
};
|
3
6
|
Object.defineProperty(exports, "__esModule", { value: true });
|
7
|
+
const commander_1 = require("commander");
|
4
8
|
const index_1 = require("./index");
|
5
|
-
const
|
6
|
-
|
9
|
+
const path_1 = require("path");
|
10
|
+
const express_1 = __importDefault(require("express"));
|
11
|
+
const http_1 = require("http");
|
12
|
+
const open_1 = __importDefault(require("open"));
|
13
|
+
const WEB_PORT = 35729; // 前端页面端口
|
14
|
+
const WS_PORT = 35730; // WebSocket 端口
|
15
|
+
commander_1.program
|
16
|
+
.version(require('../package.json').version)
|
17
|
+
.option('-c, --config <path>', '配置文件路径', 'data.json')
|
18
|
+
.option('-o, --open', '自动打开浏览器', false)
|
19
|
+
.parse(process.argv);
|
20
|
+
const options = commander_1.program.opts();
|
21
|
+
// 启动 API Mock 服务器,不再传入端口参数,使用配置文件中的端口
|
22
|
+
(0, index_1.startServer)({
|
23
|
+
config: options.config,
|
24
|
+
wsPort: WS_PORT
|
25
|
+
});
|
26
|
+
// 创建静态文件服务器来托管前端页面
|
27
|
+
const app = (0, express_1.default)();
|
28
|
+
const httpServer = (0, http_1.createServer)(app);
|
29
|
+
// 使用打包后的前端文件
|
30
|
+
app.use(express_1.default.static((0, path_1.join)(__dirname, '../web')));
|
31
|
+
// 所有路由都返回 index.html
|
32
|
+
app.get('*', (req, res) => {
|
33
|
+
res.sendFile((0, path_1.join)(__dirname, '../web/index.html'));
|
34
|
+
});
|
35
|
+
// 启动前端服务器(使用固定端口)
|
36
|
+
httpServer.listen(WEB_PORT, () => {
|
37
|
+
console.log(`Web UI is running on http://localhost:${WEB_PORT}`);
|
38
|
+
if (options.open) {
|
39
|
+
(0, open_1.default)(`http://localhost:${WEB_PORT}`);
|
40
|
+
}
|
41
|
+
});
|
package/dist/index.d.ts
CHANGED
@@ -1,3 +1,7 @@
|
|
1
|
-
|
1
|
+
interface StartOptions {
|
2
|
+
config?: string;
|
3
|
+
wsPort?: number;
|
4
|
+
}
|
5
|
+
export declare function startServer(options?: StartOptions): void;
|
2
6
|
export { MockServer } from './server';
|
3
7
|
export * from './types';
|
package/dist/index.js
CHANGED
@@ -21,12 +21,17 @@ exports.MockServer = exports.startServer = void 0;
|
|
21
21
|
const fs_1 = __importDefault(require("fs"));
|
22
22
|
const path_1 = __importDefault(require("path"));
|
23
23
|
const server_1 = require("./server");
|
24
|
-
function startServer(
|
24
|
+
function startServer(options = {}) {
|
25
|
+
const configPath = options.config || 'data.json';
|
25
26
|
const fullPath = path_1.default.resolve(process.cwd(), configPath);
|
26
27
|
try {
|
27
28
|
const configContent = fs_1.default.readFileSync(fullPath, 'utf-8');
|
28
29
|
const config = JSON.parse(configContent);
|
29
|
-
const
|
30
|
+
const serverConfig = {
|
31
|
+
...config.server,
|
32
|
+
wsPort: options.wsPort
|
33
|
+
};
|
34
|
+
const server = new server_1.MockServer(serverConfig, configPath);
|
30
35
|
server.start();
|
31
36
|
}
|
32
37
|
catch (error) {
|
package/dist/server.d.ts
CHANGED
@@ -1,18 +1,21 @@
|
|
1
|
-
import {
|
2
|
-
import { Config } from './types';
|
1
|
+
import type { Config } from './types';
|
3
2
|
export declare class MockServer {
|
4
3
|
private app;
|
5
|
-
private config;
|
6
4
|
private configPath;
|
7
|
-
|
8
|
-
|
9
|
-
private
|
10
|
-
private
|
11
|
-
private
|
12
|
-
private
|
13
|
-
private
|
14
|
-
|
15
|
-
private
|
16
|
-
private
|
17
|
-
|
5
|
+
private config;
|
6
|
+
private serverConfig;
|
7
|
+
private logs;
|
8
|
+
private readonly MAX_LOGS;
|
9
|
+
private upload;
|
10
|
+
private wss;
|
11
|
+
private clients;
|
12
|
+
constructor(serverConfig?: Config, configPath?: string);
|
13
|
+
private loadConfig;
|
14
|
+
private saveConfig;
|
15
|
+
private setupConfigRoutes;
|
16
|
+
private setupMockRoutes;
|
17
|
+
private sendResponse;
|
18
|
+
private setupUploadRoute;
|
19
|
+
start(port?: number): void;
|
20
|
+
private broadcastLog;
|
18
21
|
}
|