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