json-api-mocker 1.2.6 โ 2.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/README.ch.md +84 -224
- package/README.md +87 -227
- package/dist/cli.d.ts +2 -2
- package/dist/cli.js +38 -5
- package/dist/index.d.ts +3 -3
- package/dist/index.js +43 -43
- package/dist/server.d.ts +21 -17
- package/dist/server.js +326 -140
- package/dist/types.d.ts +35 -31
- package/dist/types.js +2 -2
- package/package.json +20 -11
- package/web/assets/index-Cs1AgF5r.js +25 -0
- package/web/assets/index-D7ix-gXo.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/CONFIG.ch.md +0 -322
- package/CONFIG.md +0 -304
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,46 @@ 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
|
27
30
|
|
28
|
-
|
29
|
-
|
31
|
+
### Method 1: Using Visual Interface (Recommended)
|
32
|
+
|
33
|
+
```bash
|
34
|
+
# Start server and management interface
|
35
|
+
json-api-mocker -o # -o will automatically open browser
|
30
36
|
|
31
|
-
#
|
32
|
-
|
37
|
+
# Specify port
|
38
|
+
json-api-mocker -p 3000 -o
|
39
|
+
|
40
|
+
# Specify config file
|
41
|
+
json-api-mocker -c ./config.json -o
|
33
42
|
```
|
34
43
|
|
35
|
-
|
44
|
+
After starting, the browser will automatically open the management interface, where you can:
|
45
|
+
1. Visually create and manage APIs
|
46
|
+
2. View real-time request logs
|
47
|
+
3. Monitor API call statistics
|
48
|
+
4. Debug mock data online
|
36
49
|
|
37
|
-
###
|
50
|
+
### Method 2: Using Configuration File (Compatible with old version)
|
38
51
|
|
39
|
-
Create a `data.json` file
|
52
|
+
Create a `data.json` file:
|
40
53
|
|
41
54
|
```json
|
42
55
|
{
|
@@ -46,40 +59,21 @@ Create a `data.json` file in your project root:
|
|
46
59
|
},
|
47
60
|
"routes": [
|
48
61
|
{
|
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",
|
62
|
+
"id": "user-api",
|
63
|
+
"route": {
|
64
|
+
"path": "/users",
|
65
|
+
"methods": {
|
66
|
+
"get": {
|
67
|
+
"status": 200,
|
68
|
+
"response": {
|
69
|
+
"code": 200,
|
70
|
+
"message": "success",
|
79
71
|
"data": {
|
80
|
-
"
|
81
|
-
|
82
|
-
|
72
|
+
"list|10": [{
|
73
|
+
"id": "@id",
|
74
|
+
"name": "@name",
|
75
|
+
"email": "@email"
|
76
|
+
}]
|
83
77
|
}
|
84
78
|
}
|
85
79
|
}
|
@@ -90,110 +84,45 @@ Create a `data.json` file in your project root:
|
|
90
84
|
}
|
91
85
|
```
|
92
86
|
|
93
|
-
|
94
|
-
|
95
|
-
There are several ways to start the mock server:
|
87
|
+
Then start the server:
|
96
88
|
|
97
89
|
```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
90
|
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
91
|
```
|
117
92
|
|
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).
|
93
|
+
## ๐ Configuration
|
134
94
|
|
135
95
|
### Server Configuration
|
136
96
|
|
137
|
-
The `server` section configures basic server settings:
|
138
|
-
|
139
97
|
```json
|
140
98
|
{
|
141
99
|
"server": {
|
142
|
-
"port": 8080, // Server port
|
143
|
-
"baseProxy": "/api" //
|
144
|
-
}
|
145
|
-
}
|
146
|
-
```
|
147
|
-
|
148
|
-
### Route Configuration
|
149
|
-
|
150
|
-
Each route can support multiple HTTP methods:
|
151
|
-
|
152
|
-
```json
|
153
|
-
{
|
154
|
-
"path": "/users", // Route path
|
155
|
-
"methods": {
|
156
|
-
"get": {
|
157
|
-
"type": "array", // Response type: "array" or "object"
|
158
|
-
"pagination": { // Optional pagination settings
|
159
|
-
"enabled": true,
|
160
|
-
"pageSize": 10,
|
161
|
-
"totalCount": 100
|
162
|
-
},
|
163
|
-
"response": [] // Response data
|
164
|
-
},
|
165
|
-
"post": {
|
166
|
-
"requestSchema": { // Request body validation schema
|
167
|
-
"name": "string",
|
168
|
-
"age": "number"
|
169
|
-
},
|
170
|
-
"response": {
|
171
|
-
"success": true
|
172
|
-
}
|
173
|
-
}
|
100
|
+
"port": 8080, // Server port
|
101
|
+
"baseProxy": "/api" // API base path
|
174
102
|
}
|
175
103
|
}
|
176
104
|
```
|
177
105
|
|
178
|
-
###
|
106
|
+
### API Configuration
|
179
107
|
|
180
|
-
|
108
|
+
Each API configuration includes:
|
181
109
|
|
182
110
|
```json
|
183
111
|
{
|
184
|
-
"
|
185
|
-
"
|
186
|
-
"
|
187
|
-
|
188
|
-
"
|
189
|
-
"
|
190
|
-
"
|
191
|
-
"
|
192
|
-
|
112
|
+
"id": "unique-id", // API unique identifier
|
113
|
+
"route": {
|
114
|
+
"path": "/users", // API path (without base path)
|
115
|
+
"methods": { // Supported HTTP methods
|
116
|
+
"get": {
|
117
|
+
"status": 200, // Response status code
|
118
|
+
"headers": { // Custom response headers
|
119
|
+
"Content-Type": "application/json"
|
120
|
+
},
|
121
|
+
"response": { // Response data (supports Mock.js syntax)
|
122
|
+
"code": 200,
|
193
123
|
"data": {
|
194
|
-
"
|
195
|
-
"
|
196
|
-
"size": "@integer(1000, 1000000)"
|
124
|
+
"name": "@name",
|
125
|
+
"age": "@integer(18, 60)"
|
197
126
|
}
|
198
127
|
}
|
199
128
|
}
|
@@ -202,113 +131,44 @@ You can configure file upload endpoints in your `data.json`:
|
|
202
131
|
}
|
203
132
|
```
|
204
133
|
|
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).
|
221
|
-
|
222
|
-
## ๐ฏ API Examples
|
223
|
-
|
224
|
-
### Basic CRUD Operations
|
225
|
-
|
226
|
-
#### Get Users List
|
227
|
-
```bash
|
228
|
-
curl http://localhost:8080/api/users
|
229
|
-
```
|
230
|
-
|
231
|
-
#### Get Single User
|
232
|
-
```bash
|
233
|
-
curl http://localhost:8080/api/users/1
|
234
|
-
```
|
134
|
+
## ๐ฎ Visual Interface Features
|
235
135
|
|
236
|
-
|
237
|
-
|
238
|
-
|
239
|
-
|
240
|
-
|
241
|
-
```
|
136
|
+
### 1. API Management
|
137
|
+
- Create, edit, and delete APIs
|
138
|
+
- Support multiple HTTP methods
|
139
|
+
- Visual response data editor
|
140
|
+
- Mock.js syntax hints
|
242
141
|
|
243
|
-
|
244
|
-
|
245
|
-
|
246
|
-
|
247
|
-
|
248
|
-
```
|
142
|
+
### 2. Real-time Logs
|
143
|
+
- Request path and method
|
144
|
+
- Response status and duration
|
145
|
+
- Request parameters recording
|
146
|
+
- Response data viewing
|
249
147
|
|
250
|
-
|
251
|
-
|
252
|
-
|
253
|
-
|
148
|
+
### 3. Statistics Dashboard
|
149
|
+
- Total API count
|
150
|
+
- Request volume monitoring
|
151
|
+
- Average response time
|
152
|
+
- Success rate statistics
|
254
153
|
|
255
|
-
|
154
|
+
## ๐ง Command Line Options
|
256
155
|
|
257
|
-
#### Pagination
|
258
156
|
```bash
|
259
|
-
|
260
|
-
curl http://localhost:8080/api/users?page=2&pageSize=10
|
261
|
-
```
|
262
|
-
|
263
|
-
#### Custom Response Headers
|
264
|
-
The server automatically adds these headers:
|
265
|
-
- `X-Total-Count`: Total number of items (for paginated responses)
|
157
|
+
json-api-mocker [options]
|
266
158
|
|
267
|
-
|
268
|
-
|
269
|
-
|
270
|
-
|
271
|
-
|
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
|
-
}
|
159
|
+
Options:
|
160
|
+
-p, --port <number> Specify server port (default: 8080)
|
161
|
+
-c, --config <path> Specify config file path (default: data.json)
|
162
|
+
-o, --open Auto open management interface
|
163
|
+
-h, --help Show help information
|
164
|
+
-v, --version Show version number
|
297
165
|
```
|
298
166
|
|
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
167
|
## ๐ License
|
308
168
|
|
309
169
|
MIT ยฉ [Xiong Haiyin]
|
310
170
|
|
311
171
|
## ๐ Acknowledgments
|
312
172
|
|
313
|
-
-
|
314
|
-
-
|
173
|
+
- Thanks to all contributors and users
|
174
|
+
- Special thanks to Mock.js for data mocking support
|
package/dist/cli.d.ts
CHANGED
@@ -1,2 +1,2 @@
|
|
1
|
-
#!/usr/bin/env node
|
2
|
-
export {};
|
1
|
+
#!/usr/bin/env node
|
2
|
+
export {};
|
package/dist/cli.js
CHANGED
@@ -1,6 +1,39 @@
|
|
1
1
|
#!/usr/bin/env node
|
2
|
-
"use strict";
|
3
|
-
|
4
|
-
|
5
|
-
|
6
|
-
(
|
2
|
+
"use strict";
|
3
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
4
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
5
|
+
};
|
6
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
7
|
+
const commander_1 = require("commander");
|
8
|
+
const index_1 = require("./index");
|
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
|
+
commander_1.program
|
14
|
+
.version(require('../package.json').version)
|
15
|
+
.option('-c, --config <path>', '้
็ฝฎๆไปถ่ทฏๅพ', 'data.json')
|
16
|
+
.option('-p, --port <number>', 'ๆๅกๅจ็ซฏๅฃ', '8080')
|
17
|
+
.option('-o, --open', '่ชๅจๆๅผๆต่งๅจ', false)
|
18
|
+
.parse(process.argv);
|
19
|
+
const options = commander_1.program.opts();
|
20
|
+
// ๅฏๅจ API Mock ๆๅกๅจ
|
21
|
+
(0, index_1.startServer)(options.config);
|
22
|
+
// ๅๅปบ้ๆๆไปถๆๅกๅจๆฅๆ็ฎกๅ็ซฏ้กต้ข
|
23
|
+
const app = (0, express_1.default)();
|
24
|
+
const httpServer = (0, http_1.createServer)(app);
|
25
|
+
// ไฝฟ็จๆๅ
ๅ็ๅ็ซฏๆไปถ
|
26
|
+
app.use(express_1.default.static((0, path_1.join)(__dirname, '../web')));
|
27
|
+
// ๆๆ่ทฏ็ฑ้ฝ่ฟๅ index.html
|
28
|
+
app.get('*', (req, res) => {
|
29
|
+
res.sendFile((0, path_1.join)(__dirname, '../web/index.html'));
|
30
|
+
});
|
31
|
+
// ๅฏๅจๅ็ซฏๆๅกๅจ
|
32
|
+
const webPort = parseInt(options.port) + 1;
|
33
|
+
httpServer.listen(webPort, () => {
|
34
|
+
console.log(`Web UI is running on http://localhost:${webPort}`);
|
35
|
+
// ๅฆๆๆๅฎไบ --open ้้กน๏ผ่ชๅจๆๅผๆต่งๅจ
|
36
|
+
if (options.open) {
|
37
|
+
(0, open_1.default)(`http://localhost:${webPort}`);
|
38
|
+
}
|
39
|
+
});
|
package/dist/index.d.ts
CHANGED
@@ -1,3 +1,3 @@
|
|
1
|
-
export declare function startServer(configPath?: string): void;
|
2
|
-
export { MockServer } from './server';
|
3
|
-
export * from './types';
|
1
|
+
export declare function startServer(configPath?: string): void;
|
2
|
+
export { MockServer } from './server';
|
3
|
+
export * from './types';
|
package/dist/index.js
CHANGED
@@ -1,43 +1,43 @@
|
|
1
|
-
"use strict";
|
2
|
-
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
3
|
-
if (k2 === undefined) k2 = k;
|
4
|
-
var desc = Object.getOwnPropertyDescriptor(m, k);
|
5
|
-
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
6
|
-
desc = { enumerable: true, get: function() { return m[k]; } };
|
7
|
-
}
|
8
|
-
Object.defineProperty(o, k2, desc);
|
9
|
-
}) : (function(o, m, k, k2) {
|
10
|
-
if (k2 === undefined) k2 = k;
|
11
|
-
o[k2] = m[k];
|
12
|
-
}));
|
13
|
-
var __exportStar = (this && this.__exportStar) || function(m, exports) {
|
14
|
-
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
|
15
|
-
};
|
16
|
-
var __importDefault = (this && this.__importDefault) || function (mod) {
|
17
|
-
return (mod && mod.__esModule) ? mod : { "default": mod };
|
18
|
-
};
|
19
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
20
|
-
exports.MockServer = void 0;
|
21
|
-
|
22
|
-
const
|
23
|
-
const
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
const
|
29
|
-
const
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
if (require.main === module) {
|
39
|
-
startServer();
|
40
|
-
}
|
41
|
-
var server_2 = require("./server");
|
42
|
-
Object.defineProperty(exports, "MockServer", { enumerable: true, get: function () { return server_2.MockServer; } });
|
43
|
-
__exportStar(require("./types"), exports);
|
1
|
+
"use strict";
|
2
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
3
|
+
if (k2 === undefined) k2 = k;
|
4
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
5
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
6
|
+
desc = { enumerable: true, get: function() { return m[k]; } };
|
7
|
+
}
|
8
|
+
Object.defineProperty(o, k2, desc);
|
9
|
+
}) : (function(o, m, k, k2) {
|
10
|
+
if (k2 === undefined) k2 = k;
|
11
|
+
o[k2] = m[k];
|
12
|
+
}));
|
13
|
+
var __exportStar = (this && this.__exportStar) || function(m, exports) {
|
14
|
+
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
|
15
|
+
};
|
16
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
17
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
18
|
+
};
|
19
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
20
|
+
exports.MockServer = exports.startServer = void 0;
|
21
|
+
const fs_1 = __importDefault(require("fs"));
|
22
|
+
const path_1 = __importDefault(require("path"));
|
23
|
+
const server_1 = require("./server");
|
24
|
+
function startServer(configPath = 'data.json') {
|
25
|
+
const fullPath = path_1.default.resolve(process.cwd(), configPath);
|
26
|
+
try {
|
27
|
+
const configContent = fs_1.default.readFileSync(fullPath, 'utf-8');
|
28
|
+
const config = JSON.parse(configContent);
|
29
|
+
const server = new server_1.MockServer(config.server, configPath);
|
30
|
+
server.start();
|
31
|
+
}
|
32
|
+
catch (error) {
|
33
|
+
console.error('Failed to start server:', error);
|
34
|
+
process.exit(1);
|
35
|
+
}
|
36
|
+
}
|
37
|
+
exports.startServer = startServer;
|
38
|
+
if (require.main === module) {
|
39
|
+
startServer();
|
40
|
+
}
|
41
|
+
var server_2 = require("./server");
|
42
|
+
Object.defineProperty(exports, "MockServer", { enumerable: true, get: function () { return server_2.MockServer; } });
|
43
|
+
__exportStar(require("./types"), exports);
|
package/dist/server.d.ts
CHANGED
@@ -1,17 +1,21 @@
|
|
1
|
-
import { Config } from './types';
|
2
|
-
export declare class MockServer {
|
3
|
-
private app;
|
4
|
-
private
|
5
|
-
private
|
6
|
-
|
7
|
-
private
|
8
|
-
private
|
9
|
-
private
|
10
|
-
private
|
11
|
-
private
|
12
|
-
|
13
|
-
private
|
14
|
-
private
|
15
|
-
private
|
16
|
-
|
17
|
-
|
1
|
+
import type { Config } from './types';
|
2
|
+
export declare class MockServer {
|
3
|
+
private app;
|
4
|
+
private configPath;
|
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;
|
21
|
+
}
|