json-api-mocker 3.0.0 → 3.1.0

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