json-api-mocker 2.0.0 → 2.0.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.ch.md +322 -0
- package/CONFIG.md +304 -0
- package/DESIGN.md +227 -0
- package/README.ch.md +16 -7
- package/README.md +16 -7
- package/dist/cli.js +1 -1
- package/dist/server.js +3 -3
- package/package.json +7 -3
- package/web/assets/{index-D7ix-gXo.css → index-3Av1b8pt.css} +1 -1
- package/web/assets/{index-Cs1AgF5r.js → index-BSwj3jUK.js} +6 -6
- package/web/index.html +2 -2
package/CONFIG.ch.md
ADDED
@@ -0,0 +1,322 @@
|
|
1
|
+
# JSON API Mocker 配置指南
|
2
|
+
|
3
|
+
本文档将帮助你理解和编写 `data.json` 配置文件。
|
4
|
+
|
5
|
+
## 配置文件结构
|
6
|
+
|
7
|
+
```json
|
8
|
+
{
|
9
|
+
"server": {
|
10
|
+
"port": 8080,
|
11
|
+
"baseProxy": "/api"
|
12
|
+
},
|
13
|
+
"routes": []
|
14
|
+
}
|
15
|
+
```
|
16
|
+
|
17
|
+
## 配置项详解
|
18
|
+
|
19
|
+
### 1. 服务器配置 (server)
|
20
|
+
|
21
|
+
| 字段 | 类型 | 必填 | 默认值 | 说明 |
|
22
|
+
|------|------|------|--------|------|
|
23
|
+
| port | number | 否 | 8080 | 服务器端口号 |
|
24
|
+
| baseProxy | string | 否 | "/api" | API 的基础路径 |
|
25
|
+
|
26
|
+
### 2. 路由配置 (routes)
|
27
|
+
|
28
|
+
routes 是一个数组,每个元素都是一个路由配置对象:
|
29
|
+
|
30
|
+
```json
|
31
|
+
{
|
32
|
+
"path": "/users",
|
33
|
+
"methods": {
|
34
|
+
"get": {},
|
35
|
+
"post": {},
|
36
|
+
"put": {},
|
37
|
+
"delete": {}
|
38
|
+
}
|
39
|
+
}
|
40
|
+
```
|
41
|
+
|
42
|
+
#### 2.1 路由基础配置
|
43
|
+
|
44
|
+
| 字段 | 类型 | 必填 | 说明 |
|
45
|
+
|------|------|------|------|
|
46
|
+
| path | string | 是 | 路由路径,支持参数如 `/users/:id` |
|
47
|
+
| methods | object | 是 | HTTP 方法配置对象 |
|
48
|
+
|
49
|
+
#### 2.2 方法配置 (methods)
|
50
|
+
|
51
|
+
每个 HTTP 方法可以包含以下配置:
|
52
|
+
|
53
|
+
| 字段 | 类型 | 必填 | 说明 |
|
54
|
+
|------|------|------|------|
|
55
|
+
| type | string | 否 | 响应类型:"array" 或 "object" |
|
56
|
+
| response | any | 否 | 静态响应数据 |
|
57
|
+
| mock | object | 否 | Mock.js 配置 |
|
58
|
+
| pagination | object | 否 | 分页配置 |
|
59
|
+
| requestSchema | object | 否 | 请求体验证模式 |
|
60
|
+
|
61
|
+
### 3. Mock 数据配置 (mock)
|
62
|
+
|
63
|
+
```json
|
64
|
+
{
|
65
|
+
"mock": {
|
66
|
+
"enabled": true,
|
67
|
+
"total": 100,
|
68
|
+
"template": {
|
69
|
+
"id|+1": 1,
|
70
|
+
"name": "@cname",
|
71
|
+
"email": "@email"
|
72
|
+
}
|
73
|
+
}
|
74
|
+
}
|
75
|
+
```
|
76
|
+
|
77
|
+
| 字段 | 类型 | 必填 | 说明 |
|
78
|
+
|------|------|------|------|
|
79
|
+
| enabled | boolean | 是 | 是否启用 mock 数据 |
|
80
|
+
| total | number | 是 | 生成数据的总条数 |
|
81
|
+
| template | object | 是 | Mock.js 的数据模板 |
|
82
|
+
|
83
|
+
### 4. 分页配置 (pagination)
|
84
|
+
|
85
|
+
```json
|
86
|
+
{
|
87
|
+
"pagination": {
|
88
|
+
"enabled": true,
|
89
|
+
"pageSize": 10
|
90
|
+
}
|
91
|
+
}
|
92
|
+
```
|
93
|
+
|
94
|
+
| 字段 | 类型 | 必填 | 默认值 | 说明 |
|
95
|
+
|------|------|------|--------|------|
|
96
|
+
| enabled | boolean | 否 | false | 是否启用分页 |
|
97
|
+
| pageSize | number | 否 | 10 | 每页数据条数 |
|
98
|
+
|
99
|
+
### 5. 请求验证配置 (requestSchema)
|
100
|
+
|
101
|
+
```json
|
102
|
+
{
|
103
|
+
"requestSchema": {
|
104
|
+
"name": "string",
|
105
|
+
"age": "number",
|
106
|
+
"email": "string"
|
107
|
+
}
|
108
|
+
}
|
109
|
+
```
|
110
|
+
|
111
|
+
### 6. 文件上传配置
|
112
|
+
|
113
|
+
你可以在 `data.json` 中配置文件上传接口:
|
114
|
+
|
115
|
+
```json
|
116
|
+
{
|
117
|
+
"path": "/upload/avatar",
|
118
|
+
"methods": {
|
119
|
+
"post": {
|
120
|
+
"type": "object",
|
121
|
+
"mock": {
|
122
|
+
"enabled": true,
|
123
|
+
"template": {
|
124
|
+
"success": true,
|
125
|
+
"message": "上传成功",
|
126
|
+
"data": {
|
127
|
+
"url": "@image('200x200')",
|
128
|
+
"filename": "@string(10).jpg",
|
129
|
+
"size": "@integer(1000, 1000000)"
|
130
|
+
}
|
131
|
+
}
|
132
|
+
}
|
133
|
+
}
|
134
|
+
}
|
135
|
+
}
|
136
|
+
```
|
137
|
+
|
138
|
+
| 字段 | 类型 | 必填 | 说明 |
|
139
|
+
|------|------|------|------|
|
140
|
+
| type | string | 是 | 文件上传必须为 "object" |
|
141
|
+
| mock.enabled | boolean | 是 | 启用模拟响应 |
|
142
|
+
| mock.template | object | 是 | 使用 Mock.js 语法的响应模板 |
|
143
|
+
|
144
|
+
## 完整配置示例
|
145
|
+
|
146
|
+
### 1. 用户管理接口
|
147
|
+
|
148
|
+
```json
|
149
|
+
{
|
150
|
+
"server": {
|
151
|
+
"port": 8080,
|
152
|
+
"baseProxy": "/api"
|
153
|
+
},
|
154
|
+
"routes": [
|
155
|
+
{
|
156
|
+
"path": "/users",
|
157
|
+
"methods": {
|
158
|
+
"get": {
|
159
|
+
"type": "array",
|
160
|
+
"mock": {
|
161
|
+
"enabled": true,
|
162
|
+
"total": 100,
|
163
|
+
"template": {
|
164
|
+
"id|+1": 1,
|
165
|
+
"name": "@cname",
|
166
|
+
"age|18-60": 1,
|
167
|
+
"email": "@email",
|
168
|
+
"address": "@city(true)"
|
169
|
+
}
|
170
|
+
},
|
171
|
+
"pagination": {
|
172
|
+
"enabled": true,
|
173
|
+
"pageSize": 10
|
174
|
+
}
|
175
|
+
},
|
176
|
+
"post": {
|
177
|
+
"requestSchema": {
|
178
|
+
"name": "string",
|
179
|
+
"age": "number",
|
180
|
+
"email": "string"
|
181
|
+
},
|
182
|
+
"response": {
|
183
|
+
"success": true,
|
184
|
+
"message": "创建成功"
|
185
|
+
}
|
186
|
+
}
|
187
|
+
}
|
188
|
+
}
|
189
|
+
]
|
190
|
+
}
|
191
|
+
```
|
192
|
+
|
193
|
+
### 2. 文章管理接口(带嵌套数据)
|
194
|
+
|
195
|
+
```json
|
196
|
+
{
|
197
|
+
"path": "/articles",
|
198
|
+
"methods": {
|
199
|
+
"get": {
|
200
|
+
"type": "array",
|
201
|
+
"mock": {
|
202
|
+
"enabled": true,
|
203
|
+
"total": 50,
|
204
|
+
"template": {
|
205
|
+
"id|+1": 1,
|
206
|
+
"title": "@ctitle",
|
207
|
+
"content": "@cparagraph",
|
208
|
+
"author": {
|
209
|
+
"id|+1": 100,
|
210
|
+
"name": "@cname",
|
211
|
+
"avatar": "@image('100x100')"
|
212
|
+
},
|
213
|
+
"comments|0-10": [{
|
214
|
+
"id|+1": 1,
|
215
|
+
"content": "@csentence",
|
216
|
+
"user": "@cname",
|
217
|
+
"createTime": "@datetime"
|
218
|
+
}]
|
219
|
+
}
|
220
|
+
}
|
221
|
+
}
|
222
|
+
}
|
223
|
+
}
|
224
|
+
```
|
225
|
+
|
226
|
+
## 使用技巧
|
227
|
+
|
228
|
+
### 1. 动态路由参数
|
229
|
+
```json
|
230
|
+
{
|
231
|
+
"path": "/users/:id/posts",
|
232
|
+
"methods": {
|
233
|
+
"get": {
|
234
|
+
"type": "array",
|
235
|
+
"mock": {
|
236
|
+
"enabled": true,
|
237
|
+
"total": 10,
|
238
|
+
"template": {
|
239
|
+
"id|+1": 1,
|
240
|
+
"title": "@ctitle",
|
241
|
+
"content": "@cparagraph"
|
242
|
+
}
|
243
|
+
}
|
244
|
+
}
|
245
|
+
}
|
246
|
+
}
|
247
|
+
```
|
248
|
+
|
249
|
+
### 2. 分页查询
|
250
|
+
```bash
|
251
|
+
# 获取第二页,每页10条数据
|
252
|
+
curl http://localhost:8080/api/users?page=2&pageSize=10
|
253
|
+
```
|
254
|
+
|
255
|
+
### 3. 数据范围生成
|
256
|
+
```json
|
257
|
+
{
|
258
|
+
"template": {
|
259
|
+
"age|18-60": 1, // 生成18-60之间的数字
|
260
|
+
"score|1-100.1": 1, // 生成1-100之间的浮点数,保留1位小数
|
261
|
+
"items|1-5": ["item"] // 生成1-5个重复项
|
262
|
+
}
|
263
|
+
}
|
264
|
+
```
|
265
|
+
|
266
|
+
## 注意事项
|
267
|
+
|
268
|
+
1. **路径参数**
|
269
|
+
- 动态路由参数使用 `:参数名` 格式
|
270
|
+
- 例如:`/users/:id/posts/:postId`
|
271
|
+
|
272
|
+
2. **Mock 数据**
|
273
|
+
- `enabled` 为 `true` 时才会生成 mock 数据
|
274
|
+
- `template` 中可以使用所有 Mock.js 支持的语法
|
275
|
+
|
276
|
+
3. **分页**
|
277
|
+
- 启用分页后,可通过 `?page=1&pageSize=10` 访问分页数据
|
278
|
+
- 响应头会包含 `X-Total-Count` 表示总数据量
|
279
|
+
|
280
|
+
4. **数据持久化**
|
281
|
+
- POST、PUT、DELETE 操作会自动保存到 data.json 文件
|
282
|
+
- 重启服务器后数据仍然保留
|
283
|
+
|
284
|
+
## 最佳实践
|
285
|
+
|
286
|
+
1. **合理使用 Mock 数据**
|
287
|
+
```json
|
288
|
+
{
|
289
|
+
"mock": {
|
290
|
+
"enabled": true,
|
291
|
+
"total": 100,
|
292
|
+
"template": {
|
293
|
+
// 使用自增ID避免重复
|
294
|
+
"id|+1": 1,
|
295
|
+
// 使用合适的数据类型
|
296
|
+
"age|18-60": 1
|
297
|
+
}
|
298
|
+
}
|
299
|
+
}
|
300
|
+
```
|
301
|
+
|
302
|
+
2. **适当的分页配置**
|
303
|
+
```json
|
304
|
+
{
|
305
|
+
"pagination": {
|
306
|
+
"enabled": true,
|
307
|
+
"pageSize": 10 // 根据数据量设置合适的页大小
|
308
|
+
}
|
309
|
+
}
|
310
|
+
```
|
311
|
+
|
312
|
+
3. **请求验证**
|
313
|
+
```json
|
314
|
+
{
|
315
|
+
"requestSchema": {
|
316
|
+
// 确保必要的字段都有类型验证
|
317
|
+
"name": "string",
|
318
|
+
"age": "number",
|
319
|
+
"email": "string"
|
320
|
+
}
|
321
|
+
}
|
322
|
+
```
|
package/CONFIG.md
ADDED
@@ -0,0 +1,304 @@
|
|
1
|
+
# JSON API Mocker Configuration Guide
|
2
|
+
|
3
|
+
A comprehensive guide to help you understand and write the `data.json` configuration file.
|
4
|
+
|
5
|
+
## Configuration Structure
|
6
|
+
|
7
|
+
```json
|
8
|
+
{
|
9
|
+
"server": {
|
10
|
+
"port": 8080,
|
11
|
+
"baseProxy": "/api"
|
12
|
+
},
|
13
|
+
"routes": []
|
14
|
+
}
|
15
|
+
```
|
16
|
+
|
17
|
+
## Configuration Details
|
18
|
+
|
19
|
+
### 1. Server Configuration (server)
|
20
|
+
|
21
|
+
| Field | Type | Required | Default | Description |
|
22
|
+
|-------|------|----------|---------|-------------|
|
23
|
+
| port | number | No | 8080 | Server port number |
|
24
|
+
| baseProxy | string | No | "/api" | Base path for all APIs |
|
25
|
+
|
26
|
+
### 2. Routes Configuration (routes)
|
27
|
+
|
28
|
+
Routes is an array where each element is a route configuration object:
|
29
|
+
|
30
|
+
```json
|
31
|
+
{
|
32
|
+
"path": "/users",
|
33
|
+
"methods": {
|
34
|
+
"get": {},
|
35
|
+
"post": {},
|
36
|
+
"put": {},
|
37
|
+
"delete": {}
|
38
|
+
}
|
39
|
+
}
|
40
|
+
```
|
41
|
+
|
42
|
+
#### 2.1 Route Basic Configuration
|
43
|
+
|
44
|
+
| Field | Type | Required | Description |
|
45
|
+
|-------|------|----------|-------------|
|
46
|
+
| path | string | Yes | Route path, supports parameters like `/users/:id` |
|
47
|
+
| methods | object | Yes | HTTP methods configuration object |
|
48
|
+
|
49
|
+
#### 2.2 Method Configuration (methods)
|
50
|
+
|
51
|
+
Each HTTP method can include the following configurations:
|
52
|
+
|
53
|
+
| Field | Type | Required | Description |
|
54
|
+
|-------|------|----------|-------------|
|
55
|
+
| type | string | No | Response type: "array" or "object" |
|
56
|
+
| response | any | No | Static response data |
|
57
|
+
| mock | object | No | Mock.js configuration |
|
58
|
+
| pagination | object | No | Pagination configuration |
|
59
|
+
| requestSchema | object | No | Request body validation schema |
|
60
|
+
|
61
|
+
### 3. Mock Data Configuration (mock)
|
62
|
+
|
63
|
+
```json
|
64
|
+
{
|
65
|
+
"mock": {
|
66
|
+
"enabled": true,
|
67
|
+
"total": 100,
|
68
|
+
"template": {
|
69
|
+
"id|+1": 1,
|
70
|
+
"name": "@name",
|
71
|
+
"email": "@email"
|
72
|
+
}
|
73
|
+
}
|
74
|
+
}
|
75
|
+
```
|
76
|
+
|
77
|
+
| Field | Type | Required | Description |
|
78
|
+
|-------|------|----------|-------------|
|
79
|
+
| enabled | boolean | Yes | Enable/disable mock data |
|
80
|
+
| total | number | Yes | Total number of records to generate |
|
81
|
+
| template | object | Yes | Mock.js data template |
|
82
|
+
|
83
|
+
### 4. Pagination Configuration (pagination)
|
84
|
+
|
85
|
+
```json
|
86
|
+
{
|
87
|
+
"pagination": {
|
88
|
+
"enabled": true,
|
89
|
+
"pageSize": 10
|
90
|
+
}
|
91
|
+
}
|
92
|
+
```
|
93
|
+
|
94
|
+
| Field | Type | Required | Default | Description |
|
95
|
+
|-------|------|----------|---------|-------------|
|
96
|
+
| enabled | boolean | No | false | Enable/disable pagination |
|
97
|
+
| pageSize | number | No | 10 | Number of items per page |
|
98
|
+
|
99
|
+
### 5. Request Validation Configuration (requestSchema)
|
100
|
+
|
101
|
+
```json
|
102
|
+
{
|
103
|
+
"requestSchema": {
|
104
|
+
"name": "string",
|
105
|
+
"age": "number",
|
106
|
+
"email": "string"
|
107
|
+
}
|
108
|
+
}
|
109
|
+
```
|
110
|
+
|
111
|
+
### 6. File Upload Configuration
|
112
|
+
|
113
|
+
You can configure file upload endpoints in your `data.json`:
|
114
|
+
|
115
|
+
```json
|
116
|
+
{
|
117
|
+
"path": "/upload/avatar",
|
118
|
+
"methods": {
|
119
|
+
"post": {
|
120
|
+
"type": "object",
|
121
|
+
"mock": {
|
122
|
+
"enabled": true,
|
123
|
+
"template": {
|
124
|
+
"success": true,
|
125
|
+
"message": "Upload successful",
|
126
|
+
"data": {
|
127
|
+
"url": "@image('200x200')",
|
128
|
+
"filename": "@string(10).jpg",
|
129
|
+
"size": "@integer(1000, 1000000)"
|
130
|
+
}
|
131
|
+
}
|
132
|
+
}
|
133
|
+
}
|
134
|
+
}
|
135
|
+
}
|
136
|
+
```
|
137
|
+
|
138
|
+
| Field | Type | Required | Description |
|
139
|
+
|-------|------|----------|-------------|
|
140
|
+
| type | string | Yes | Must be "object" for file uploads |
|
141
|
+
| mock.enabled | boolean | Yes | Enable mock response |
|
142
|
+
| mock.template | object | Yes | Response template using Mock.js syntax |
|
143
|
+
|
144
|
+
## Complete Configuration Examples
|
145
|
+
|
146
|
+
### 1. Basic User CRUD API
|
147
|
+
|
148
|
+
```json
|
149
|
+
{
|
150
|
+
"server": {
|
151
|
+
"port": 8080,
|
152
|
+
"baseProxy": "/api"
|
153
|
+
},
|
154
|
+
"routes": [
|
155
|
+
{
|
156
|
+
"path": "/users",
|
157
|
+
"methods": {
|
158
|
+
"get": {
|
159
|
+
"type": "array",
|
160
|
+
"mock": {
|
161
|
+
"enabled": true,
|
162
|
+
"total": 100,
|
163
|
+
"template": {
|
164
|
+
"id|+1": 1,
|
165
|
+
"name": "@name",
|
166
|
+
"age|18-60": 1,
|
167
|
+
"email": "@email",
|
168
|
+
"address": "@city"
|
169
|
+
}
|
170
|
+
},
|
171
|
+
"pagination": {
|
172
|
+
"enabled": true,
|
173
|
+
"pageSize": 10
|
174
|
+
}
|
175
|
+
},
|
176
|
+
"post": {
|
177
|
+
"requestSchema": {
|
178
|
+
"name": "string",
|
179
|
+
"age": "number",
|
180
|
+
"email": "string"
|
181
|
+
},
|
182
|
+
"response": {
|
183
|
+
"success": true,
|
184
|
+
"message": "Created successfully"
|
185
|
+
}
|
186
|
+
}
|
187
|
+
}
|
188
|
+
}
|
189
|
+
]
|
190
|
+
}
|
191
|
+
```
|
192
|
+
|
193
|
+
### 2. Articles API with Nested Data
|
194
|
+
|
195
|
+
```json
|
196
|
+
{
|
197
|
+
"path": "/articles",
|
198
|
+
"methods": {
|
199
|
+
"get": {
|
200
|
+
"type": "array",
|
201
|
+
"mock": {
|
202
|
+
"enabled": true,
|
203
|
+
"total": 50,
|
204
|
+
"template": {
|
205
|
+
"id|+1": 1,
|
206
|
+
"title": "@title",
|
207
|
+
"content": "@paragraph",
|
208
|
+
"author": {
|
209
|
+
"id|+1": 100,
|
210
|
+
"name": "@name",
|
211
|
+
"avatar": "@image('100x100')"
|
212
|
+
},
|
213
|
+
"comments|0-10": [{
|
214
|
+
"id|+1": 1,
|
215
|
+
"content": "@sentence",
|
216
|
+
"user": "@name",
|
217
|
+
"createTime": "@datetime"
|
218
|
+
}]
|
219
|
+
}
|
220
|
+
}
|
221
|
+
}
|
222
|
+
}
|
223
|
+
}
|
224
|
+
```
|
225
|
+
|
226
|
+
### 3. Dynamic Route Parameters Example
|
227
|
+
|
228
|
+
```json
|
229
|
+
{
|
230
|
+
"path": "/users/:id/posts",
|
231
|
+
"methods": {
|
232
|
+
"get": {
|
233
|
+
"type": "array",
|
234
|
+
"mock": {
|
235
|
+
"enabled": true,
|
236
|
+
"total": 10,
|
237
|
+
"template": {
|
238
|
+
"id|+1": 1,
|
239
|
+
"title": "@title",
|
240
|
+
"content": "@paragraph"
|
241
|
+
}
|
242
|
+
}
|
243
|
+
}
|
244
|
+
}
|
245
|
+
}
|
246
|
+
```
|
247
|
+
|
248
|
+
## Important Notes
|
249
|
+
|
250
|
+
1. **Route Parameters**
|
251
|
+
- Dynamic route parameters use `:paramName` format
|
252
|
+
- Example: `/users/:id/posts/:postId`
|
253
|
+
|
254
|
+
2. **Mock Data**
|
255
|
+
- Mock data generation only works when `enabled` is `true`
|
256
|
+
- `template` supports all Mock.js syntax
|
257
|
+
|
258
|
+
3. **Pagination**
|
259
|
+
- When enabled, use `?page=1&pageSize=10` to access paginated data
|
260
|
+
- Response headers include `X-Total-Count` for total count
|
261
|
+
|
262
|
+
4. **Data Persistence**
|
263
|
+
- POST, PUT, DELETE operations automatically save to data.json
|
264
|
+
- Data persists after server restart
|
265
|
+
|
266
|
+
## Best Practices
|
267
|
+
|
268
|
+
1. **Effective Use of Mock Data**
|
269
|
+
```json
|
270
|
+
{
|
271
|
+
"mock": {
|
272
|
+
"enabled": true,
|
273
|
+
"total": 100,
|
274
|
+
"template": {
|
275
|
+
// Use auto-increment for IDs
|
276
|
+
"id|+1": 1,
|
277
|
+
// Use appropriate data types
|
278
|
+
"age|18-60": 1
|
279
|
+
}
|
280
|
+
}
|
281
|
+
}
|
282
|
+
```
|
283
|
+
|
284
|
+
2. **Proper Pagination Settings**
|
285
|
+
```json
|
286
|
+
{
|
287
|
+
"pagination": {
|
288
|
+
"enabled": true,
|
289
|
+
"pageSize": 10 // Set appropriate page size based on data volume
|
290
|
+
}
|
291
|
+
}
|
292
|
+
```
|
293
|
+
|
294
|
+
3. **Request Validation**
|
295
|
+
```json
|
296
|
+
{
|
297
|
+
"requestSchema": {
|
298
|
+
// Ensure all required fields have type validation
|
299
|
+
"name": "string",
|
300
|
+
"age": "number",
|
301
|
+
"email": "string"
|
302
|
+
}
|
303
|
+
}
|
304
|
+
```
|