json-api-mocker 2.2.2 → 2.3.1
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/CONFIG.md +38 -0
- package/README.ch.md +308 -95
- package/README.md +311 -98
- package/dist/cli.d.ts +2 -2
- package/dist/cli.js +6 -41
- package/dist/index.d.ts +3 -7
- package/dist/index.js +43 -48
- package/dist/server.d.ts +22 -21
- package/dist/server.js +205 -325
- package/dist/types.d.ts +45 -36
- package/dist/types.js +2 -2
- package/package.json +11 -18
- 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/dist/server.d.ts
CHANGED
@@ -1,21 +1,22 @@
|
|
1
|
-
import
|
2
|
-
|
3
|
-
|
4
|
-
private
|
5
|
-
private
|
6
|
-
private
|
7
|
-
private
|
8
|
-
private
|
9
|
-
|
10
|
-
|
11
|
-
private
|
12
|
-
|
13
|
-
private
|
14
|
-
private
|
15
|
-
private
|
16
|
-
private
|
17
|
-
private
|
18
|
-
private
|
19
|
-
|
20
|
-
|
21
|
-
|
1
|
+
import { Express } from 'express';
|
2
|
+
import { Config } from './types';
|
3
|
+
export declare class MockServer {
|
4
|
+
private app;
|
5
|
+
private server;
|
6
|
+
private wss;
|
7
|
+
private config;
|
8
|
+
private configPath;
|
9
|
+
constructor(config: Config, configPath?: string);
|
10
|
+
getApp(): Express;
|
11
|
+
private setupMiddleware;
|
12
|
+
private logRequest;
|
13
|
+
private generateMockData;
|
14
|
+
private handleRequest;
|
15
|
+
private setupRoutes;
|
16
|
+
private createRoute;
|
17
|
+
private findRouteConfig;
|
18
|
+
private generateMockResponse;
|
19
|
+
private setupWebSocket;
|
20
|
+
start(): void;
|
21
|
+
close(): void;
|
22
|
+
}
|
package/dist/server.js
CHANGED
@@ -1,325 +1,205 @@
|
|
1
|
-
"use strict";
|
2
|
-
var __importDefault = (this && this.__importDefault) || function (mod) {
|
3
|
-
return (mod && mod.__esModule) ? mod : { "default": mod };
|
4
|
-
};
|
5
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
6
|
-
exports.MockServer = void 0;
|
7
|
-
const
|
8
|
-
const
|
9
|
-
const
|
10
|
-
const
|
11
|
-
const
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
this.
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
this.
|
48
|
-
this.
|
49
|
-
|
50
|
-
|
51
|
-
|
52
|
-
|
53
|
-
|
54
|
-
|
55
|
-
|
56
|
-
|
57
|
-
|
58
|
-
|
59
|
-
|
60
|
-
|
61
|
-
|
62
|
-
|
63
|
-
|
64
|
-
|
65
|
-
|
66
|
-
|
67
|
-
|
68
|
-
|
69
|
-
|
70
|
-
|
71
|
-
|
72
|
-
|
73
|
-
|
74
|
-
|
75
|
-
|
76
|
-
|
77
|
-
|
78
|
-
|
79
|
-
|
80
|
-
|
81
|
-
|
82
|
-
|
83
|
-
|
84
|
-
|
85
|
-
|
86
|
-
|
87
|
-
|
88
|
-
|
89
|
-
|
90
|
-
|
91
|
-
|
92
|
-
|
93
|
-
|
94
|
-
|
95
|
-
|
96
|
-
|
97
|
-
|
98
|
-
|
99
|
-
|
100
|
-
|
101
|
-
|
102
|
-
|
103
|
-
|
104
|
-
|
105
|
-
|
106
|
-
|
107
|
-
|
108
|
-
|
109
|
-
|
110
|
-
|
111
|
-
|
112
|
-
|
113
|
-
|
114
|
-
|
115
|
-
|
116
|
-
|
117
|
-
|
118
|
-
|
119
|
-
}
|
120
|
-
|
121
|
-
|
122
|
-
|
123
|
-
|
124
|
-
|
125
|
-
|
126
|
-
|
127
|
-
|
128
|
-
|
129
|
-
|
130
|
-
|
131
|
-
|
132
|
-
|
133
|
-
|
134
|
-
|
135
|
-
|
136
|
-
|
137
|
-
|
138
|
-
|
139
|
-
|
140
|
-
|
141
|
-
|
142
|
-
|
143
|
-
|
144
|
-
|
145
|
-
|
146
|
-
|
147
|
-
|
148
|
-
|
149
|
-
|
150
|
-
|
151
|
-
|
152
|
-
|
153
|
-
|
154
|
-
|
155
|
-
|
156
|
-
|
157
|
-
|
158
|
-
|
159
|
-
|
160
|
-
|
161
|
-
|
162
|
-
|
163
|
-
|
164
|
-
|
165
|
-
|
166
|
-
|
167
|
-
|
168
|
-
|
169
|
-
|
170
|
-
|
171
|
-
|
172
|
-
|
173
|
-
|
174
|
-
|
175
|
-
|
176
|
-
|
177
|
-
|
178
|
-
|
179
|
-
|
180
|
-
|
181
|
-
|
182
|
-
|
183
|
-
|
184
|
-
|
185
|
-
|
186
|
-
|
187
|
-
|
188
|
-
|
189
|
-
|
190
|
-
|
191
|
-
|
192
|
-
|
193
|
-
|
194
|
-
|
195
|
-
|
196
|
-
|
197
|
-
|
198
|
-
|
199
|
-
|
200
|
-
|
201
|
-
|
202
|
-
|
203
|
-
|
204
|
-
|
205
|
-
|
206
|
-
self.logs.pop();
|
207
|
-
}
|
208
|
-
// 广播新日志
|
209
|
-
self.broadcastLog(log);
|
210
|
-
return originalSend.call(this, body);
|
211
|
-
};
|
212
|
-
next();
|
213
|
-
});
|
214
|
-
// 处理所有mock请求,但排除配置接口
|
215
|
-
this.app.all('*', (req, res, next) => {
|
216
|
-
try {
|
217
|
-
// 如果是配置接口,跳过这个中间件
|
218
|
-
if (req.path === '/api/_config' || req.path.startsWith('/api/_config/')) {
|
219
|
-
return next();
|
220
|
-
}
|
221
|
-
console.log('Received request for path:', req.path);
|
222
|
-
console.log('Current config:', this.config);
|
223
|
-
if (!Array.isArray(this.config)) {
|
224
|
-
console.error('Config is not an array:', this.config);
|
225
|
-
res.status(500).json({ error: 'Internal server error' });
|
226
|
-
return;
|
227
|
-
}
|
228
|
-
const api = this.config.find(api => {
|
229
|
-
if (!api || !api.route || typeof api.route.path !== 'string') {
|
230
|
-
console.error('Invalid API config:', api);
|
231
|
-
return false;
|
232
|
-
}
|
233
|
-
// 移除 baseProxy 前缀再比较
|
234
|
-
const requestPath = req.path.replace(baseProxy, '');
|
235
|
-
return api.route.path === requestPath;
|
236
|
-
});
|
237
|
-
if (!api) {
|
238
|
-
res.status(404).json({ error: 'API not found' });
|
239
|
-
return;
|
240
|
-
}
|
241
|
-
if (!api.route || !api.route.methods) {
|
242
|
-
res.status(500).json({ error: 'Invalid API configuration' });
|
243
|
-
return;
|
244
|
-
}
|
245
|
-
const method = req.method.toLowerCase();
|
246
|
-
const methodConfig = api.route.methods[method];
|
247
|
-
if (!methodConfig) {
|
248
|
-
res.status(405).json({ error: 'Method not allowed' });
|
249
|
-
return;
|
250
|
-
}
|
251
|
-
if (methodConfig.delay) {
|
252
|
-
setTimeout(() => {
|
253
|
-
this.sendResponse(res, methodConfig);
|
254
|
-
}, methodConfig.delay);
|
255
|
-
}
|
256
|
-
else {
|
257
|
-
this.sendResponse(res, methodConfig);
|
258
|
-
}
|
259
|
-
}
|
260
|
-
catch (error) {
|
261
|
-
console.error('Error handling request:', error);
|
262
|
-
res.status(500).json({ error: 'Internal server error' });
|
263
|
-
}
|
264
|
-
});
|
265
|
-
}
|
266
|
-
sendResponse(res, config) {
|
267
|
-
try {
|
268
|
-
if (config.headers) {
|
269
|
-
Object.entries(config.headers).forEach(([key, value]) => {
|
270
|
-
res.setHeader(key, value);
|
271
|
-
});
|
272
|
-
}
|
273
|
-
// 解析存储的响应数据
|
274
|
-
let responseData = typeof config.response === 'string'
|
275
|
-
? JSON.parse(config.response.replace(/\\/g, '')) // 移除多余的反斜杠
|
276
|
-
: config.response;
|
277
|
-
// 使用 Mock.js 处理数据
|
278
|
-
const mockedData = mockjs_1.default.mock(responseData);
|
279
|
-
res.status(config.status || 200).json(mockedData);
|
280
|
-
}
|
281
|
-
catch (error) {
|
282
|
-
console.error('Error processing response:', error);
|
283
|
-
res.status(500).json({ error: 'Internal server error' });
|
284
|
-
}
|
285
|
-
}
|
286
|
-
setupUploadRoute() {
|
287
|
-
// 处理文件上传
|
288
|
-
this.app.post('/api/upload', this.upload.single('file'), (req, res) => {
|
289
|
-
if (!req.file) {
|
290
|
-
return res.status(400).json({ error: 'No file uploaded' });
|
291
|
-
}
|
292
|
-
// 返回文件URL
|
293
|
-
const fileUrl = `/uploads/${req.file.filename}`;
|
294
|
-
res.json({
|
295
|
-
success: true,
|
296
|
-
data: {
|
297
|
-
url: fileUrl,
|
298
|
-
filename: req.file.originalname,
|
299
|
-
size: req.file.size
|
300
|
-
}
|
301
|
-
});
|
302
|
-
});
|
303
|
-
// 提供静态文件访问
|
304
|
-
this.app.use('/uploads', express_1.default.static(path_2.default.join(process.cwd(), 'uploads')));
|
305
|
-
}
|
306
|
-
start(port) {
|
307
|
-
const serverPort = port || this.serverConfig.port || 3000;
|
308
|
-
this.app.listen(serverPort, () => {
|
309
|
-
console.log(`Mock server is running on http://localhost:${serverPort}`);
|
310
|
-
});
|
311
|
-
}
|
312
|
-
// 广播日志给所有客户端
|
313
|
-
broadcastLog(log) {
|
314
|
-
const message = JSON.stringify({
|
315
|
-
type: 'log',
|
316
|
-
data: log
|
317
|
-
});
|
318
|
-
this.clients.forEach(client => {
|
319
|
-
if (client.readyState === ws_1.WebSocket.OPEN) {
|
320
|
-
client.send(message);
|
321
|
-
}
|
322
|
-
});
|
323
|
-
}
|
324
|
-
}
|
325
|
-
exports.MockServer = MockServer;
|
1
|
+
"use strict";
|
2
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
3
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
4
|
+
};
|
5
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
6
|
+
exports.MockServer = void 0;
|
7
|
+
const mockjs_1 = __importDefault(require("mockjs"));
|
8
|
+
const express_1 = __importDefault(require("express"));
|
9
|
+
const cors_1 = __importDefault(require("cors"));
|
10
|
+
const ws_1 = require("ws");
|
11
|
+
const http_1 = __importDefault(require("http"));
|
12
|
+
class MockServer {
|
13
|
+
constructor(config, configPath = 'data.json') {
|
14
|
+
this.app = (0, express_1.default)();
|
15
|
+
this.wss = null;
|
16
|
+
this.logRequest = (req, res, next) => {
|
17
|
+
const startTime = Date.now();
|
18
|
+
const requestId = Math.random().toString(36).substring(7);
|
19
|
+
console.log(`[${new Date().toISOString()}] Request ${requestId}:`);
|
20
|
+
console.log(` Method: ${req.method}`);
|
21
|
+
console.log(` URL: ${req.url}`);
|
22
|
+
console.log(` Query Params: ${JSON.stringify(req.query)}`);
|
23
|
+
console.log(` Body: ${JSON.stringify(req.body)}`);
|
24
|
+
res.on('finish', () => {
|
25
|
+
const duration = Date.now() - startTime;
|
26
|
+
console.log(`[${new Date().toISOString()}] Response ${requestId}:`);
|
27
|
+
console.log(` Status: ${res.statusCode}`);
|
28
|
+
console.log(` Duration: ${duration}ms`);
|
29
|
+
console.log('----------------------------------------');
|
30
|
+
});
|
31
|
+
next();
|
32
|
+
};
|
33
|
+
this.config = config;
|
34
|
+
this.configPath = configPath;
|
35
|
+
this.server = http_1.default.createServer(this.app);
|
36
|
+
this.setupMiddleware();
|
37
|
+
this.setupRoutes();
|
38
|
+
if (config.websocket?.enabled) {
|
39
|
+
this.setupWebSocket();
|
40
|
+
}
|
41
|
+
}
|
42
|
+
getApp() {
|
43
|
+
return this.app;
|
44
|
+
}
|
45
|
+
setupMiddleware() {
|
46
|
+
this.app.use((0, cors_1.default)());
|
47
|
+
this.app.use(express_1.default.json());
|
48
|
+
this.app.use('/uploads', express_1.default.static('uploads'));
|
49
|
+
this.app.use(this.logRequest);
|
50
|
+
}
|
51
|
+
generateMockData(config) {
|
52
|
+
try {
|
53
|
+
if (config.mock?.enabled && config.mock.template) {
|
54
|
+
const { total, template } = config.mock;
|
55
|
+
return mockjs_1.default.mock({
|
56
|
+
[`data|${total}`]: [template]
|
57
|
+
}).data;
|
58
|
+
}
|
59
|
+
return config.response;
|
60
|
+
}
|
61
|
+
catch (error) {
|
62
|
+
console.error('Error generating mock data:', error);
|
63
|
+
return config.response;
|
64
|
+
}
|
65
|
+
}
|
66
|
+
handleRequest(config) {
|
67
|
+
return (req, res) => {
|
68
|
+
try {
|
69
|
+
let responseData = this.generateMockData(config);
|
70
|
+
if (config.pagination?.enabled && Array.isArray(responseData)) {
|
71
|
+
const page = parseInt(req.query.page) || 1;
|
72
|
+
const pageSize = parseInt(req.query.pageSize) || config.pagination.pageSize;
|
73
|
+
const startIndex = (page - 1) * pageSize;
|
74
|
+
const endIndex = startIndex + pageSize;
|
75
|
+
const paginatedData = responseData.slice(startIndex, endIndex);
|
76
|
+
res.header('X-Total-Count', responseData.length.toString());
|
77
|
+
responseData = paginatedData;
|
78
|
+
}
|
79
|
+
res.json(responseData);
|
80
|
+
}
|
81
|
+
catch (error) {
|
82
|
+
console.error('Error handling request:', error);
|
83
|
+
res.status(500).json({ error: 'Internal server error' });
|
84
|
+
}
|
85
|
+
};
|
86
|
+
}
|
87
|
+
setupRoutes() {
|
88
|
+
this.config.routes.forEach((route) => {
|
89
|
+
Object.entries(route.methods).forEach(([method, methodConfig]) => {
|
90
|
+
this.createRoute(route.path, method, methodConfig);
|
91
|
+
});
|
92
|
+
});
|
93
|
+
}
|
94
|
+
createRoute(path, method, config) {
|
95
|
+
const fullPath = `${this.config.server.baseProxy}${path}`;
|
96
|
+
console.log(`创建路由: ${method.toUpperCase()} ${fullPath}`);
|
97
|
+
switch (method.toLowerCase()) {
|
98
|
+
case 'get':
|
99
|
+
this.app.get(fullPath, this.handleRequest(config));
|
100
|
+
break;
|
101
|
+
case 'post':
|
102
|
+
if (path === '/upload/avatar') {
|
103
|
+
// 对于文件上传路由,使用特殊处理
|
104
|
+
this.app.post(fullPath, (req, res) => {
|
105
|
+
const mockResponse = this.generateMockData(config);
|
106
|
+
res.json(mockResponse);
|
107
|
+
});
|
108
|
+
}
|
109
|
+
else {
|
110
|
+
this.app.post(fullPath, this.handleRequest(config));
|
111
|
+
}
|
112
|
+
break;
|
113
|
+
case 'put':
|
114
|
+
this.app.put(`${fullPath}/:id`, this.handleRequest(config));
|
115
|
+
break;
|
116
|
+
case 'delete':
|
117
|
+
this.app.delete(`${fullPath}/:id`, this.handleRequest(config));
|
118
|
+
break;
|
119
|
+
}
|
120
|
+
}
|
121
|
+
findRouteConfig(path, method) {
|
122
|
+
const route = this.config.routes.find(r => r.path === path);
|
123
|
+
return route?.methods[method] || null;
|
124
|
+
}
|
125
|
+
generateMockResponse(config) {
|
126
|
+
if (config.mock?.enabled && config.mock.template) {
|
127
|
+
return mockjs_1.default.mock(config.mock.template);
|
128
|
+
}
|
129
|
+
return config.response;
|
130
|
+
}
|
131
|
+
setupWebSocket() {
|
132
|
+
if (!this.config.websocket)
|
133
|
+
return;
|
134
|
+
this.wss = new ws_1.Server({
|
135
|
+
server: this.server,
|
136
|
+
path: this.config.websocket.path
|
137
|
+
});
|
138
|
+
this.wss.on('connection', (ws) => {
|
139
|
+
console.log('WebSocket client connected');
|
140
|
+
// 处理客户端消息
|
141
|
+
ws.on('message', (message) => {
|
142
|
+
try {
|
143
|
+
const data = JSON.parse(message.toString());
|
144
|
+
const eventConfig = this.config.websocket?.events?.[data.event];
|
145
|
+
if (eventConfig?.mock.enabled) {
|
146
|
+
const response = mockjs_1.default.mock(eventConfig.mock.template);
|
147
|
+
ws.send(JSON.stringify({
|
148
|
+
event: data.event,
|
149
|
+
data: response
|
150
|
+
}));
|
151
|
+
}
|
152
|
+
}
|
153
|
+
catch (error) {
|
154
|
+
console.error('Error handling WebSocket message:', error);
|
155
|
+
}
|
156
|
+
});
|
157
|
+
// 设置自动发送数据的定时器
|
158
|
+
if (this.config.websocket && this.config.websocket.events) {
|
159
|
+
Object.entries(this.config.websocket.events).forEach(([event, config]) => {
|
160
|
+
if (config.mock.interval) {
|
161
|
+
setInterval(() => {
|
162
|
+
const response = mockjs_1.default.mock(config.mock.template);
|
163
|
+
ws.send(JSON.stringify({
|
164
|
+
event,
|
165
|
+
data: response
|
166
|
+
}));
|
167
|
+
}, config.mock.interval);
|
168
|
+
}
|
169
|
+
});
|
170
|
+
}
|
171
|
+
ws.on('close', () => {
|
172
|
+
// 移除日志,避免测试完成后的日志输出
|
173
|
+
// console.log('WebSocket client disconnected');
|
174
|
+
});
|
175
|
+
});
|
176
|
+
}
|
177
|
+
start() {
|
178
|
+
this.server.listen(this.config.server.port, () => {
|
179
|
+
console.log(`Mock 服务器已启动:`);
|
180
|
+
console.log(`- HTTP 地址: http://localhost:${this.config.server.port}`);
|
181
|
+
if (this.config.websocket?.enabled) {
|
182
|
+
console.log(`- WebSocket 地址: ws://localhost:${this.config.server.port}${this.config.websocket.path}`);
|
183
|
+
}
|
184
|
+
console.log(`- 基础路径: ${this.config.server.baseProxy}`);
|
185
|
+
console.log('可用的接口:');
|
186
|
+
this.config.routes.forEach(route => {
|
187
|
+
Object.keys(route.methods).forEach(method => {
|
188
|
+
console.log(` ${method.toUpperCase()} http://localhost:${this.config.server.port}${this.config.server.baseProxy}${route.path}`);
|
189
|
+
});
|
190
|
+
});
|
191
|
+
});
|
192
|
+
}
|
193
|
+
close() {
|
194
|
+
// 关闭所有 WebSocket 连接
|
195
|
+
if (this.wss) {
|
196
|
+
this.wss.clients.forEach(client => {
|
197
|
+
client.close();
|
198
|
+
});
|
199
|
+
this.wss.close();
|
200
|
+
}
|
201
|
+
// 关闭 HTTP 服务器
|
202
|
+
this.server.close();
|
203
|
+
}
|
204
|
+
}
|
205
|
+
exports.MockServer = MockServer;
|