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 CHANGED
@@ -130,6 +130,8 @@ Mock 服务器已启动:
130
130
 
131
131
  ## 📖 配置指南
132
132
 
133
+ 详细配置请参考 [CONFIG.ch.md](./CONFIG.ch.md)。
134
+
133
135
  ### 服务器配置
134
136
 
135
137
  `server` 部分配置基本的服务器设置:
package/README.md CHANGED
@@ -130,6 +130,8 @@ Available APIs:
130
130
 
131
131
  ## 📖 Configuration Guide
132
132
 
133
+ For detailed configuration options, please refer to [CONFIG.md](./CONFIG.md).
134
+
133
135
  ### Server Configuration
134
136
 
135
137
  The `server` section configures basic server settings:
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
- this.app.post(fullPath, this.handleRequest(config));
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
@@ -9,7 +9,7 @@ export interface PaginationConfig {
9
9
  }
10
10
  export interface MockConfig {
11
11
  enabled: boolean;
12
- total: number;
12
+ total?: number;
13
13
  template: Record<string, any>;
14
14
  }
15
15
  export interface MethodConfig {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "json-api-mocker",
3
- "version": "1.2.5",
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"