json-api-mocker 1.2.5 → 1.2.7
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 +2 -0
- package/README.md +2 -0
- package/dist/server.d.ts +2 -1
- package/dist/server.js +13 -15
- package/dist/types.d.ts +1 -1
- package/package.json +5 -1
package/README.ch.md
CHANGED
package/README.md
CHANGED
package/dist/server.d.ts
CHANGED
@@ -1,16 +1,17 @@
|
|
1
|
+
import { Express } from 'express';
|
1
2
|
import { Config } from './types';
|
2
3
|
export declare class MockServer {
|
3
4
|
private app;
|
4
5
|
private config;
|
5
6
|
private configPath;
|
6
7
|
constructor(config: Config, configPath?: string);
|
8
|
+
getApp(): Express;
|
7
9
|
private setupMiddleware;
|
8
10
|
private logRequest;
|
9
11
|
private generateMockData;
|
10
12
|
private handleRequest;
|
11
13
|
private setupRoutes;
|
12
14
|
private createRoute;
|
13
|
-
private setupFileUpload;
|
14
15
|
private findRouteConfig;
|
15
16
|
private generateMockResponse;
|
16
17
|
start(): void;
|
package/dist/server.js
CHANGED
@@ -7,7 +7,6 @@ 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"));
|
11
10
|
class MockServer {
|
12
11
|
constructor(config, configPath = 'data.json') {
|
13
12
|
this.app = (0, express_1.default)();
|
@@ -31,9 +30,11 @@ class MockServer {
|
|
31
30
|
this.config = config;
|
32
31
|
this.configPath = configPath;
|
33
32
|
this.setupMiddleware();
|
34
|
-
this.setupFileUpload();
|
35
33
|
this.setupRoutes();
|
36
34
|
}
|
35
|
+
getApp() {
|
36
|
+
return this.app;
|
37
|
+
}
|
37
38
|
setupMiddleware() {
|
38
39
|
this.app.use((0, cors_1.default)());
|
39
40
|
this.app.use(express_1.default.json());
|
@@ -91,7 +92,16 @@ class MockServer {
|
|
91
92
|
this.app.get(fullPath, this.handleRequest(config));
|
92
93
|
break;
|
93
94
|
case 'post':
|
94
|
-
|
95
|
+
if (path === '/upload/avatar') {
|
96
|
+
// 对于文件上传路由,使用特殊处理
|
97
|
+
this.app.post(fullPath, (req, res) => {
|
98
|
+
const mockResponse = this.generateMockData(config);
|
99
|
+
res.json(mockResponse);
|
100
|
+
});
|
101
|
+
}
|
102
|
+
else {
|
103
|
+
this.app.post(fullPath, this.handleRequest(config));
|
104
|
+
}
|
95
105
|
break;
|
96
106
|
case 'put':
|
97
107
|
this.app.put(`${fullPath}/:id`, this.handleRequest(config));
|
@@ -101,18 +111,6 @@ class MockServer {
|
|
101
111
|
break;
|
102
112
|
}
|
103
113
|
}
|
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
114
|
findRouteConfig(path, method) {
|
117
115
|
const route = this.config.routes.find(r => r.path === path);
|
118
116
|
return route?.methods[method] || null;
|
package/dist/types.d.ts
CHANGED
package/package.json
CHANGED
@@ -1,6 +1,6 @@
|
|
1
1
|
{
|
2
2
|
"name": "json-api-mocker",
|
3
|
-
"version": "1.2.
|
3
|
+
"version": "1.2.7",
|
4
4
|
"description": "A mock server based on JSON configuration",
|
5
5
|
"main": "dist/index.js",
|
6
6
|
"types": "dist/index.d.ts",
|
@@ -19,6 +19,8 @@
|
|
19
19
|
"build": "tsc",
|
20
20
|
"start": "node dist/index.js",
|
21
21
|
"test": "jest",
|
22
|
+
"test:watch": "jest --watch",
|
23
|
+
"test:coverage": "jest --coverage",
|
22
24
|
"prepublishOnly": "npm run build"
|
23
25
|
},
|
24
26
|
"keywords": [
|
@@ -54,8 +56,10 @@
|
|
54
56
|
"@types/mockjs": "^1.0.10",
|
55
57
|
"@types/multer": "^1.4.12",
|
56
58
|
"@types/node": "^16.18.0",
|
59
|
+
"@types/supertest": "^6.0.2",
|
57
60
|
"jest": "^27.5.1",
|
58
61
|
"nodemon": "^2.0.22",
|
62
|
+
"supertest": "^7.0.0",
|
59
63
|
"ts-jest": "^27.1.5",
|
60
64
|
"ts-node": "^10.9.1",
|
61
65
|
"typescript": "^4.9.5"
|