json-api-mocker 2.2.2 → 2.3.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 +224 -95
- package/README.md +227 -98
- package/dist/cli.js +2 -37
- package/dist/index.d.ts +1 -5
- package/dist/index.js +2 -7
- package/dist/server.d.ts +14 -17
- package/dist/server.js +103 -290
- package/dist/types.d.ts +21 -26
- package/package.json +11 -20
- package/DESIGN.md +0 -227
- package/web/assets/index-Bkg4WfPW.js +0 -31
- package/web/assets/index-DiYdJ4Yt.css +0 -1
- package/web/index.html +0 -15
- package/web/monaco-editor-worker-loader.js +0 -8
- package/web/vite.svg +0 -1
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 that uses JSON configuration to quickly create RESTful APIs.
|
4
4
|
|
5
5
|
<p align="center">
|
6
6
|
<img src="https://img.shields.io/npm/v/json-api-mocker" alt="npm version" />
|
@@ -10,57 +10,33 @@ A lightweight and flexible mock server with JSON configuration and visual manage
|
|
10
10
|
|
11
11
|
## ✨ Features
|
12
12
|
|
13
|
-
- 🚀
|
14
|
-
-
|
15
|
-
- 🔄 Support all common HTTP methods
|
13
|
+
- 🚀 Quick setup with JSON configuration
|
14
|
+
- 🔄 Support for GET, POST, PUT, DELETE methods
|
16
15
|
- 📝 Automatic data persistence
|
17
16
|
- 🔍 Built-in pagination support
|
18
|
-
- 🛠 Customizable response
|
19
|
-
- 🎭
|
20
|
-
-
|
17
|
+
- 🛠 Customizable response schemas
|
18
|
+
- 🎭 Integration with Mock.js for powerful data mocking
|
19
|
+
- 📤 File upload support
|
21
20
|
- 💡 TypeScript support
|
22
21
|
|
23
22
|
## 📦 Installation
|
24
23
|
|
25
24
|
```bash
|
26
|
-
|
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)
|
25
|
+
# Using npm
|
26
|
+
npm install json-api-mocker
|
37
27
|
|
38
|
-
|
39
|
-
|
40
|
-
{
|
41
|
-
"server": {
|
42
|
-
"port": 35728,
|
43
|
-
"baseProxy": "/api"
|
44
|
-
},
|
45
|
-
"routes": []
|
46
|
-
}
|
47
|
-
```
|
28
|
+
# Using yarn
|
29
|
+
yarn add json-api-mocker
|
48
30
|
|
49
|
-
|
50
|
-
|
51
|
-
# Start server and open browser
|
52
|
-
json-api-mocker -o
|
31
|
+
# Using pnpm
|
32
|
+
pnpm add json-api-mocker
|
53
33
|
```
|
54
34
|
|
55
|
-
|
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
|
35
|
+
## 🚀 Quick Start
|
60
36
|
|
61
|
-
###
|
37
|
+
### 1. Create Configuration File
|
62
38
|
|
63
|
-
Create a `data.json` file:
|
39
|
+
Create a `data.json` file in your project root:
|
64
40
|
|
65
41
|
```json
|
66
42
|
{
|
@@ -70,21 +46,40 @@ Create a `data.json` file:
|
|
70
46
|
},
|
71
47
|
"routes": [
|
72
48
|
{
|
73
|
-
"
|
74
|
-
"
|
75
|
-
"
|
76
|
-
|
77
|
-
"
|
78
|
-
"
|
79
|
-
"
|
80
|
-
|
81
|
-
|
49
|
+
"path": "/users",
|
50
|
+
"methods": {
|
51
|
+
"get": {
|
52
|
+
"type": "array",
|
53
|
+
"pagination": {
|
54
|
+
"enabled": true,
|
55
|
+
"pageSize": 10,
|
56
|
+
"totalCount": 100
|
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",
|
82
79
|
"data": {
|
83
|
-
"
|
84
|
-
|
85
|
-
|
86
|
-
"email": "@email"
|
87
|
-
}]
|
80
|
+
"url": "@image('200x200')",
|
81
|
+
"filename": "@string(10).jpg",
|
82
|
+
"size": "@integer(1000, 1000000)"
|
88
83
|
}
|
89
84
|
}
|
90
85
|
}
|
@@ -95,45 +90,110 @@ Create a `data.json` file:
|
|
95
90
|
}
|
96
91
|
```
|
97
92
|
|
98
|
-
|
93
|
+
### 2. Start the Server
|
94
|
+
|
95
|
+
There are several ways to start the mock server:
|
99
96
|
|
100
97
|
```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
|
101
105
|
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
|
102
116
|
```
|
103
117
|
|
104
|
-
|
118
|
+
Now your mock server is running at `http://localhost:8080`!
|
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).
|
105
134
|
|
106
135
|
### Server Configuration
|
107
136
|
|
137
|
+
The `server` section configures basic server settings:
|
138
|
+
|
108
139
|
```json
|
109
140
|
{
|
110
141
|
"server": {
|
111
|
-
"port": 8080, // Server port
|
112
|
-
"baseProxy": "/api" //
|
142
|
+
"port": 8080, // Server port number
|
143
|
+
"baseProxy": "/api" // Base path for all routes
|
113
144
|
}
|
114
145
|
}
|
115
146
|
```
|
116
147
|
|
117
|
-
###
|
148
|
+
### Route Configuration
|
118
149
|
|
119
|
-
Each
|
150
|
+
Each route can support multiple HTTP methods:
|
120
151
|
|
121
152
|
```json
|
122
153
|
{
|
123
|
-
"
|
124
|
-
"
|
125
|
-
"
|
126
|
-
|
127
|
-
"
|
128
|
-
"
|
129
|
-
"
|
130
|
-
|
131
|
-
|
132
|
-
|
133
|
-
|
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
|
+
}
|
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",
|
134
193
|
"data": {
|
135
|
-
"
|
136
|
-
"
|
194
|
+
"url": "@image('200x200')",
|
195
|
+
"filename": "@string(10).jpg",
|
196
|
+
"size": "@integer(1000, 1000000)"
|
137
197
|
}
|
138
198
|
}
|
139
199
|
}
|
@@ -142,44 +202,113 @@ Each API configuration includes:
|
|
142
202
|
}
|
143
203
|
```
|
144
204
|
|
145
|
-
|
205
|
+
#### Example Usage:
|
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).
|
146
221
|
|
147
|
-
|
148
|
-
- Create, edit, and delete APIs
|
149
|
-
- Support multiple HTTP methods
|
150
|
-
- Visual response data editor
|
151
|
-
- Mock.js syntax hints
|
222
|
+
## 🎯 API Examples
|
152
223
|
|
153
|
-
###
|
154
|
-
|
155
|
-
|
156
|
-
|
157
|
-
|
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
|
+
```
|
158
235
|
|
159
|
-
|
160
|
-
|
161
|
-
-
|
162
|
-
-
|
163
|
-
-
|
236
|
+
#### Create User
|
237
|
+
```bash
|
238
|
+
curl -X POST http://localhost:8080/api/users \
|
239
|
+
-H "Content-Type: application/json" \
|
240
|
+
-d '{"name":"Alice","age":25,"city":"Boston"}'
|
241
|
+
```
|
164
242
|
|
165
|
-
|
243
|
+
#### Update User
|
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
|
+
```
|
166
249
|
|
250
|
+
#### Delete User
|
167
251
|
```bash
|
168
|
-
|
252
|
+
curl -X DELETE http://localhost:8080/api/users/1
|
253
|
+
```
|
254
|
+
|
255
|
+
### Advanced Usage
|
169
256
|
|
170
|
-
|
171
|
-
|
172
|
-
|
173
|
-
|
174
|
-
-h, --help Show help information
|
175
|
-
-v, --version Show version number
|
257
|
+
#### Pagination
|
258
|
+
```bash
|
259
|
+
# Get page 2 with 10 items per page
|
260
|
+
curl http://localhost:8080/api/users?page=2&pageSize=10
|
176
261
|
```
|
177
262
|
|
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
|
+
|
178
307
|
## 📄 License
|
179
308
|
|
180
309
|
MIT © [Xiong Haiyin]
|
181
310
|
|
182
311
|
## 🙏 Acknowledgments
|
183
312
|
|
184
|
-
-
|
185
|
-
-
|
313
|
+
- Express.js for the excellent web framework
|
314
|
+
- All our contributors and users
|
package/dist/cli.js
CHANGED
@@ -1,41 +1,6 @@
|
|
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
|
-
};
|
6
3
|
Object.defineProperty(exports, "__esModule", { value: true });
|
7
|
-
const commander_1 = require("commander");
|
8
4
|
const index_1 = require("./index");
|
9
|
-
const
|
10
|
-
|
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
|
-
});
|
5
|
+
const configPath = process.argv[2] || 'data.json';
|
6
|
+
(0, index_1.startServer)(configPath);
|
package/dist/index.d.ts
CHANGED
@@ -1,7 +1,3 @@
|
|
1
|
-
|
2
|
-
config?: string;
|
3
|
-
wsPort?: number;
|
4
|
-
}
|
5
|
-
export declare function startServer(options?: StartOptions): void;
|
1
|
+
export declare function startServer(configPath?: string): void;
|
6
2
|
export { MockServer } from './server';
|
7
3
|
export * from './types';
|
package/dist/index.js
CHANGED
@@ -21,17 +21,12 @@ 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(
|
25
|
-
const configPath = options.config || 'data.json';
|
24
|
+
function startServer(configPath = 'data.json') {
|
26
25
|
const fullPath = path_1.default.resolve(process.cwd(), configPath);
|
27
26
|
try {
|
28
27
|
const configContent = fs_1.default.readFileSync(fullPath, 'utf-8');
|
29
28
|
const config = JSON.parse(configContent);
|
30
|
-
const
|
31
|
-
...config.server,
|
32
|
-
wsPort: options.wsPort
|
33
|
-
};
|
34
|
-
const server = new server_1.MockServer(serverConfig, configPath);
|
29
|
+
const server = new server_1.MockServer(config, configPath);
|
35
30
|
server.start();
|
36
31
|
}
|
37
32
|
catch (error) {
|
package/dist/server.d.ts
CHANGED
@@ -1,21 +1,18 @@
|
|
1
|
-
import
|
1
|
+
import { Express } from 'express';
|
2
|
+
import { Config } from './types';
|
2
3
|
export declare class MockServer {
|
3
4
|
private app;
|
4
|
-
private configPath;
|
5
5
|
private config;
|
6
|
-
private
|
7
|
-
|
8
|
-
|
9
|
-
private
|
10
|
-
private
|
11
|
-
private
|
12
|
-
|
13
|
-
private
|
14
|
-
private
|
15
|
-
private
|
16
|
-
private
|
17
|
-
|
18
|
-
private setupUploadRoute;
|
19
|
-
start(port?: number): void;
|
20
|
-
private broadcastLog;
|
6
|
+
private configPath;
|
7
|
+
constructor(config: Config, configPath?: string);
|
8
|
+
getApp(): Express;
|
9
|
+
private setupMiddleware;
|
10
|
+
private logRequest;
|
11
|
+
private generateMockData;
|
12
|
+
private handleRequest;
|
13
|
+
private setupRoutes;
|
14
|
+
private createRoute;
|
15
|
+
private findRouteConfig;
|
16
|
+
private generateMockResponse;
|
17
|
+
start(): void;
|
21
18
|
}
|