json-api-mocker 1.2.1 → 1.2.3
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/dist/server.d.ts +3 -0
- package/dist/server.js +25 -0
- package/package.json +4 -2
package/dist/server.d.ts
CHANGED
package/dist/server.js
CHANGED
@@ -7,6 +7,7 @@ exports.MockServer = void 0;
|
|
7
7
|
const mockjs_1 = __importDefault(require("mockjs"));
|
8
8
|
const express_1 = __importDefault(require("express"));
|
9
9
|
const cors_1 = __importDefault(require("cors"));
|
10
|
+
const multer_1 = __importDefault(require("multer"));
|
10
11
|
class MockServer {
|
11
12
|
constructor(config, configPath = 'data.json') {
|
12
13
|
this.app = (0, express_1.default)();
|
@@ -30,11 +31,13 @@ class MockServer {
|
|
30
31
|
this.config = config;
|
31
32
|
this.configPath = configPath;
|
32
33
|
this.setupMiddleware();
|
34
|
+
this.setupFileUpload();
|
33
35
|
this.setupRoutes();
|
34
36
|
}
|
35
37
|
setupMiddleware() {
|
36
38
|
this.app.use((0, cors_1.default)());
|
37
39
|
this.app.use(express_1.default.json());
|
40
|
+
this.app.use('/uploads', express_1.default.static('uploads'));
|
38
41
|
this.app.use(this.logRequest);
|
39
42
|
}
|
40
43
|
generateMockData(config) {
|
@@ -98,6 +101,28 @@ class MockServer {
|
|
98
101
|
break;
|
99
102
|
}
|
100
103
|
}
|
104
|
+
setupFileUpload() {
|
105
|
+
const upload = (0, multer_1.default)({ storage: multer_1.default.memoryStorage() });
|
106
|
+
this.app.post('/api/upload/avatar', upload.single('avatar'), (req, res) => {
|
107
|
+
const config = this.findRouteConfig('/upload/avatar', 'post');
|
108
|
+
if (!config) {
|
109
|
+
return res.status(404).json({ error: 'Route not found' });
|
110
|
+
}
|
111
|
+
const mockResponse = this.generateMockResponse(config);
|
112
|
+
res.json(mockResponse);
|
113
|
+
});
|
114
|
+
// 多文件上传的处理可以类似实现
|
115
|
+
}
|
116
|
+
findRouteConfig(path, method) {
|
117
|
+
const route = this.config.routes.find(r => r.path === path);
|
118
|
+
return route?.methods[method] || null;
|
119
|
+
}
|
120
|
+
generateMockResponse(config) {
|
121
|
+
if (config.mock?.enabled && config.mock.template) {
|
122
|
+
return mockjs_1.default.mock(config.mock.template);
|
123
|
+
}
|
124
|
+
return config.response;
|
125
|
+
}
|
101
126
|
start() {
|
102
127
|
this.app.listen(this.config.server.port, () => {
|
103
128
|
console.log(`Mock 服务器已启动:`);
|
package/package.json
CHANGED
@@ -1,6 +1,6 @@
|
|
1
1
|
{
|
2
2
|
"name": "json-api-mocker",
|
3
|
-
"version": "1.2.
|
3
|
+
"version": "1.2.3",
|
4
4
|
"description": "一个基于 JSON 配置的 Mock 服务器",
|
5
5
|
"main": "dist/index.js",
|
6
6
|
"types": "dist/index.d.ts",
|
@@ -44,13 +44,15 @@
|
|
44
44
|
"dependencies": {
|
45
45
|
"cors": "^2.8.5",
|
46
46
|
"express": "^4.17.1",
|
47
|
-
"mockjs": "^1.1.0"
|
47
|
+
"mockjs": "^1.1.0",
|
48
|
+
"multer": "^1.4.5-lts.1"
|
48
49
|
},
|
49
50
|
"devDependencies": {
|
50
51
|
"@types/cors": "^2.8.13",
|
51
52
|
"@types/express": "^4.17.13",
|
52
53
|
"@types/jest": "^27.5.2",
|
53
54
|
"@types/mockjs": "^1.0.10",
|
55
|
+
"@types/multer": "^1.4.12",
|
54
56
|
"@types/node": "^16.18.0",
|
55
57
|
"jest": "^27.5.1",
|
56
58
|
"nodemon": "^2.0.22",
|