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 CHANGED
@@ -302,3 +302,41 @@ You can configure file upload endpoints in your `data.json`:
302
302
  }
303
303
  }
304
304
  ```
305
+
306
+ ### WebSocket Configuration
307
+
308
+ | Field | Type | Required | Description |
309
+ |-------|------|----------|-------------|
310
+ | enabled | boolean | Yes | Enable/disable WebSocket support |
311
+ | path | string | Yes | WebSocket endpoint path |
312
+ | events | object | No | Event configurations |
313
+
314
+ #### Event Configuration
315
+
316
+ | Field | Type | Required | Description |
317
+ |-------|------|----------|-------------|
318
+ | mock.enabled | boolean | Yes | Enable/disable mock data for this event |
319
+ | mock.interval | number | No | Interval (ms) for automatic data sending |
320
+ | mock.template | object | Yes | Mock.js template for response data |
321
+
322
+ Example:
323
+ ```json
324
+ {
325
+ "websocket": {
326
+ "enabled": true,
327
+ "path": "/ws",
328
+ "events": {
329
+ "event-name": {
330
+ "mock": {
331
+ "enabled": true,
332
+ "interval": 5000,
333
+ "template": {
334
+ "field1": "@value",
335
+ "field2|1-100": 1
336
+ }
337
+ }
338
+ }
339
+ }
340
+ }
341
+ }
342
+ ```
package/README.ch.md CHANGED
@@ -1,6 +1,6 @@
1
1
  # JSON API Mocker
2
2
 
3
- 一个轻量级且灵活的 Mock 服务器,支持 JSON 配置和可视化界面管理。
3
+ 一个轻量级且灵活的 Mock 服务器,通过 JSON 配置快速创建 RESTful API。
4
4
 
5
5
  <p align="center">
6
6
  <img src="https://img.shields.io/npm/v/json-api-mocker" alt="npm 版本" />
@@ -10,57 +10,34 @@
10
10
 
11
11
  ## ✨ 特性
12
12
 
13
- - 🚀 全新的可视化管理界面
14
- - 🚀 支持配置文件和 UI 两种使用方式
15
- - 🔄 支持所有常用 HTTP 方法
13
+ - 🚀 通过 JSON 配置快速搭建
14
+ - 🔄 支持 GET、POST、PUT、DELETE 方法
16
15
  - 📝 自动数据持久化
17
16
  - 🔍 内置分页支持
18
17
  - 🛠 可自定义响应结构
19
18
  - 🎭 集成 Mock.js 实现强大的数据模拟
20
- - 📊 实时请求日志和统计
19
+ - 📤 支持文件上传
20
+ - 🔌 支持 WebSocket 实时通信
21
21
  - 💡 TypeScript 支持
22
22
 
23
23
  ## 📦 安装
24
24
 
25
25
  ```bash
26
- npm install -g json-api-mocker
27
- ```
28
-
29
- ## 🚀 快速开始
26
+ # 使用 npm
27
+ npm install json-api-mocker
30
28
 
31
- 服务器使用固定端口:
32
- - API 服务器:35728
33
- - Web 界面:35729
34
- - WebSocket:35730
35
-
36
- ### 方式一:使用可视化界面(推荐)
37
-
38
- 1. 创建 `data.json` 文件:
39
- ```json
40
- {
41
- "server": {
42
- "port": 35728,
43
- "baseProxy": "/api"
44
- },
45
- "routes": []
46
- }
47
- ```
29
+ # 使用 yarn
30
+ yarn add json-api-mocker
48
31
 
49
- 2. 启动服务器:
50
- ```bash
51
- # 启动服务器并打开浏览器
52
- json-api-mocker -o
32
+ # 使用 pnpm
33
+ pnpm add json-api-mocker
53
34
  ```
54
35
 
55
- 启动后,浏览器会自动打开管理界面,你可以:
56
- 1. 可视化创建和管理 API
57
- 2. 实时查看请求日志
58
- 3. 监控 API 调用统计
59
- 4. 在线调试 Mock 数据
36
+ ## 🚀 快速开始
60
37
 
61
- ### 方式二:使用配置文件(兼容旧版本)
38
+ ### 1. 创建配置文件
62
39
 
63
- 创建 `data.json` 文件:
40
+ 在项目根目录创建 `data.json` 文件:
64
41
 
65
42
  ```json
66
43
  {
@@ -70,21 +47,40 @@ json-api-mocker -o
70
47
  },
71
48
  "routes": [
72
49
  {
73
- "id": "user-api",
74
- "route": {
75
- "path": "/users",
76
- "methods": {
77
- "get": {
78
- "status": 200,
79
- "response": {
80
- "code": 200,
81
- "message": "success",
50
+ "path": "/users",
51
+ "methods": {
52
+ "get": {
53
+ "type": "array",
54
+ "pagination": {
55
+ "enabled": true,
56
+ "pageSize": 10,
57
+ "totalCount": 100
58
+ },
59
+ "response": [
60
+ {
61
+ "id": 1,
62
+ "name": "张三",
63
+ "age": 30,
64
+ "city": "北京"
65
+ }
66
+ ]
67
+ }
68
+ }
69
+ },
70
+ {
71
+ "path": "/upload/avatar",
72
+ "methods": {
73
+ "post": {
74
+ "type": "object",
75
+ "mock": {
76
+ "enabled": true,
77
+ "template": {
78
+ "success": true,
79
+ "message": "上传成功",
82
80
  "data": {
83
- "list|10": [{
84
- "id": "@id",
85
- "name": "@cname",
86
- "email": "@email"
87
- }]
81
+ "url": "@image('200x200')",
82
+ "filename": "@string(10).jpg",
83
+ "size": "@integer(1000, 1000000)"
88
84
  }
89
85
  }
90
86
  }
@@ -95,45 +91,110 @@ json-api-mocker -o
95
91
  }
96
92
  ```
97
93
 
98
- 然后启动服务器:
94
+ ### 2. 启动服务器
95
+
96
+ 有多种方式可以启动 Mock 服务器:
99
97
 
100
98
  ```bash
99
+ # 方式一:使用 npx(推荐)
100
+ npx json-api-mocker
101
+
102
+ # 方式二:使用 npx 并指定配置文件
103
+ npx json-api-mocker ./custom-config.json
104
+
105
+ # 方式三:如果全局安装了包
101
106
  json-api-mocker
107
+
108
+ # 方式四:如果作为项目依赖安装
109
+ # 在 package.json 的 scripts 中添加:
110
+ {
111
+ "scripts": {
112
+ "mock": "json-api-mocker"
113
+ }
114
+ }
115
+ # 然后运行:
116
+ npm run mock
117
+ ```
118
+
119
+ 现在你的 Mock 服务器已经在 `http://localhost:8080` 运行了!
120
+
121
+ 你会看到类似这样的输出:
122
+ ```bash
123
+ Mock 服务器已启动:
124
+ - 地址:http://localhost:8080
125
+ - 基础路径:/api
126
+ 可用的接口:
127
+ GET http://localhost:8080/api/users
128
+ POST http://localhost:8080/api/users
129
+ POST http://localhost:8080/api/upload/avatar
102
130
  ```
103
131
 
104
- ## 📖 配置说明
132
+ ## 📖 配置指南
133
+
134
+ 详细配置请参考 [CONFIG.ch.md](./CONFIG.ch.md)。
105
135
 
106
136
  ### 服务器配置
107
137
 
138
+ `server` 部分配置基本的服务器设置:
139
+
108
140
  ```json
109
141
  {
110
142
  "server": {
111
143
  "port": 8080, // 服务器端口号
112
- "baseProxy": "/api" // API 基础路径
144
+ "baseProxy": "/api" // 所有路由的基础路径
145
+ }
146
+ }
147
+ ```
148
+
149
+ ### 路由配置
150
+
151
+ 每个路由可以支持多个 HTTP 方法:
152
+
153
+ ```json
154
+ {
155
+ "path": "/users", // 路由路径
156
+ "methods": {
157
+ "get": {
158
+ "type": "array", // 响应类型:array 或 object
159
+ "pagination": { // 可选的分页设置
160
+ "enabled": true,
161
+ "pageSize": 10,
162
+ "totalCount": 100
163
+ },
164
+ "response": [] // 响应数据
165
+ },
166
+ "post": {
167
+ "requestSchema": { // 请求体验证模式
168
+ "name": "string",
169
+ "age": "number"
170
+ },
171
+ "response": {
172
+ "success": true
173
+ }
174
+ }
113
175
  }
114
176
  }
115
177
  ```
116
178
 
117
- ### API 配置
179
+ ### 文件上传支持
118
180
 
119
- 每个 API 配置包含:
181
+ 你可以在 `data.json` 中配置文件上传接口:
120
182
 
121
183
  ```json
122
184
  {
123
- "id": "unique-id", // API 唯一标识
124
- "route": {
125
- "path": "/users", // API 路径(不含基础路径)
126
- "methods": { // 支持的 HTTP 方法
127
- "get": {
128
- "status": 200, // 响应状态码
129
- "headers": { // 自定义响应头
130
- "Content-Type": "application/json"
131
- },
132
- "response": { // 响应数据(支持 Mock.js 语法)
133
- "code": 200,
185
+ "path": "/upload/avatar",
186
+ "methods": {
187
+ "post": {
188
+ "type": "object",
189
+ "mock": {
190
+ "enabled": true,
191
+ "template": {
192
+ "success": true,
193
+ "message": "上传成功",
134
194
  "data": {
135
- "name": "@name",
136
- "age": "@integer(18, 60)"
195
+ "url": "@image('200x200')",
196
+ "filename": "@string(10).jpg",
197
+ "size": "@integer(1000, 1000000)"
137
198
  }
138
199
  }
139
200
  }
@@ -142,44 +203,196 @@ json-api-mocker
142
203
  }
143
204
  ```
144
205
 
145
- ## 🎮 可视化界面功能
206
+ #### 使用示例:
207
+
208
+ ```bash
209
+ # 上传单个文件
210
+ curl -X POST http://localhost:8080/api/upload/avatar \
211
+ -H "Content-Type: multipart/form-data" \
212
+ -F "avatar=@/path/to/your/image.jpg"
213
+
214
+ # 上传多个文件
215
+ curl -X POST http://localhost:8080/api/upload/images \
216
+ -H "Content-Type: multipart/form-data" \
217
+ -F "images=@/path/to/image1.jpg" \
218
+ -F "images=@/path/to/image2.jpg"
219
+ ```
220
+
221
+ 详细配置选项请参考 [CONFIG.ch.md](./CONFIG.ch.md#文件上传配置)。
222
+
223
+ ## 🎯 API 示例
224
+
225
+ ### 基本的 CRUD 操作
226
+
227
+ #### 获取用户列表
228
+ ```bash
229
+ curl http://localhost:8080/api/users
230
+ ```
231
+
232
+ #### 获取单个用户
233
+ ```bash
234
+ curl http://localhost:8080/api/users/1
235
+ ```
146
236
 
147
- ### 1. API 管理
148
- - 创建、编辑、删除 API
149
- - 支持多种 HTTP 方法
150
- - 可视化编辑响应数据
151
- - Mock.js 语法提示
237
+ #### 创建用户
238
+ ```bash
239
+ curl -X POST http://localhost:8080/api/users \
240
+ -H "Content-Type: application/json" \
241
+ -d '{"name":"李四","age":25,"city":"上海"}'
242
+ ```
152
243
 
153
- ### 2. 实时日志
154
- - 请求路径和方法
155
- - 响应状态和耗时
156
- - 请求参数记录
157
- - 响应数据查看
244
+ #### 更新用户
245
+ ```bash
246
+ curl -X PUT http://localhost:8080/api/users/1 \
247
+ -H "Content-Type: application/json" \
248
+ -d '{"name":"李四","age":26,"city":"上海"}'
249
+ ```
158
250
 
159
- ### 3. 统计面板
160
- - API 总数统计
161
- - 请求量监控
162
- - 平均响应时间
163
- - 成功率统计
251
+ #### 删除用户
252
+ ```bash
253
+ curl -X DELETE http://localhost:8080/api/users/1
254
+ ```
164
255
 
165
- ## 🔧 命令行参数
256
+ ### 高级用法
166
257
 
258
+ #### 分页
167
259
  ```bash
168
- json-api-mocker [options]
260
+ # 获取第2页,每页10条数据
261
+ curl http://localhost:8080/api/users?page=2&pageSize=10
262
+ ```
263
+
264
+ #### 自定义响应头
265
+ 服务器自动添加以下响应头:
266
+ - `X-Total-Count`:数据总条数(用于分页响应)
267
+
268
+ ## 🔧 高级配置
269
+
270
+ ### 动态路由
271
+
272
+ 你可以在路由中使用 URL 参数:
273
+
274
+ ```json
275
+ {
276
+ "path": "/users/:id/posts",
277
+ "methods": {
278
+ "get": {
279
+ "type": "array",
280
+ "response": []
281
+ }
282
+ }
283
+ }
284
+ ```
285
+
286
+ ### 请求验证
169
287
 
170
- 选项:
171
- -p, --port <number> 指定服务器端口号(默认:8080)
172
- -c, --config <path> 指定配置文件路径(默认:data.json
173
- -o, --open 自动打开管理界面
174
- -h, --help 显示帮助信息
175
- -v, --version 显示版本号
288
+ 为 POST/PUT 请求添加模式验证:
289
+
290
+ ```json
291
+ {
292
+ "requestSchema": {
293
+ "name": "string",
294
+ "age": "number",
295
+ "email": "string"
296
+ }
297
+ }
298
+ ```
299
+
300
+ ### WebSocket 支持
301
+
302
+ 你可以在 `data.json` 中配置 WebSocket 接口:
303
+
304
+ ```json
305
+ {
306
+ "websocket": {
307
+ "enabled": true,
308
+ "path": "/ws",
309
+ "events": {
310
+ "realtime-data": {
311
+ "mock": {
312
+ "enabled": true,
313
+ "interval": 5000, // 每5秒发送一次数据
314
+ "template": {
315
+ "timestamp": "@datetime",
316
+ "value|1-100": 1,
317
+ "status|1": ["normal", "warning", "error"]
318
+ }
319
+ }
320
+ },
321
+ "user-status": {
322
+ "mock": {
323
+ "enabled": true,
324
+ "template": {
325
+ "userId|+1": 1,
326
+ "status|1": ["online", "offline", "away"],
327
+ "lastActive": "@datetime"
328
+ }
329
+ }
330
+ }
331
+ }
332
+ }
333
+ }
334
+ ```
335
+
336
+ #### 客户端使用示例:
337
+
338
+ ```javascript
339
+ // 连接 WebSocket 服务器
340
+ const ws = new WebSocket('ws://localhost:8080/ws');
341
+
342
+ // 处理连接打开
343
+ ws.onopen = () => {
344
+ console.log('已连接到 WebSocket 服务器');
345
+
346
+ // 请求实时数据
347
+ ws.send(JSON.stringify({
348
+ event: 'realtime-data'
349
+ }));
350
+ };
351
+
352
+ // 处理接收到的消息
353
+ ws.onmessage = (event) => {
354
+ const data = JSON.parse(event.data);
355
+ console.log('收到数据:', data);
356
+ // {
357
+ // event: 'realtime-data',
358
+ // data: {
359
+ // timestamp: '2024-01-01 12:00:00',
360
+ // value: 75,
361
+ // status: 'normal'
362
+ // }
363
+ // }
364
+ };
365
+
366
+ // 处理错误
367
+ ws.onerror = (error) => {
368
+ console.error('WebSocket 错误:', error);
369
+ };
370
+
371
+ // 处理连接关闭
372
+ ws.onclose = () => {
373
+ console.log('与 WebSocket 服务器断开连接');
374
+ };
176
375
  ```
177
376
 
377
+ #### 特性:
378
+ - 基于事件的通信
379
+ - 支持指定间隔自动发送数据
380
+ - 支持 Mock.js 模板生成动态数据
381
+ - 支持多个事件处理器
382
+
383
+ ## 🤝 贡献指南
384
+
385
+ 1. Fork 本仓库
386
+ 2. 创建你的特性分支 (`git checkout -b feature/amazing-feature`)
387
+ 3. 提交你的改动 (`git commit -m '添加一些很棒的特性'`)
388
+ 4. 推送到分支 (`git push origin feature/amazing-feature`)
389
+ 5. 开启一个 Pull Request
390
+
178
391
  ## 📄 许可证
179
392
 
180
393
  MIT © [熊海银]
181
394
 
182
395
  ## 🙏 致谢
183
396
 
184
- - 感谢所有贡献者和用户
185
- - 特别感谢 Mock.js 提供数据模拟支持
397
+ - 感谢 Express.js 提供出色的 Web 框架
398
+ - 感谢所有贡献者和用户