json-api-mocker 1.2.3 → 1.2.5
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 -305
- package/CONFIG.md +304 -271
- package/README.ch.md +311 -347
- package/README.md +311 -345
- package/package.json +2 -2
package/CONFIG.md
CHANGED
@@ -1,271 +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
|
-
|
112
|
-
|
113
|
-
|
114
|
-
|
115
|
-
```json
|
116
|
-
{
|
117
|
-
"
|
118
|
-
|
119
|
-
"
|
120
|
-
|
121
|
-
|
122
|
-
|
123
|
-
|
124
|
-
|
125
|
-
|
126
|
-
"
|
127
|
-
|
128
|
-
"
|
129
|
-
"
|
130
|
-
|
131
|
-
|
132
|
-
|
133
|
-
|
134
|
-
|
135
|
-
|
136
|
-
|
137
|
-
|
138
|
-
|
139
|
-
|
140
|
-
|
141
|
-
|
142
|
-
|
143
|
-
|
144
|
-
|
145
|
-
|
146
|
-
|
147
|
-
|
148
|
-
|
149
|
-
|
150
|
-
|
151
|
-
|
152
|
-
|
153
|
-
|
154
|
-
|
155
|
-
|
156
|
-
|
157
|
-
|
158
|
-
|
159
|
-
|
160
|
-
|
161
|
-
|
162
|
-
|
163
|
-
{
|
164
|
-
|
165
|
-
|
166
|
-
|
167
|
-
|
168
|
-
|
169
|
-
|
170
|
-
|
171
|
-
|
172
|
-
|
173
|
-
|
174
|
-
|
175
|
-
|
176
|
-
|
177
|
-
|
178
|
-
"
|
179
|
-
|
180
|
-
|
181
|
-
|
182
|
-
|
183
|
-
"
|
184
|
-
"
|
185
|
-
}
|
186
|
-
}
|
187
|
-
}
|
188
|
-
}
|
189
|
-
|
190
|
-
}
|
191
|
-
```
|
192
|
-
|
193
|
-
###
|
194
|
-
|
195
|
-
```json
|
196
|
-
{
|
197
|
-
"path": "/
|
198
|
-
"methods": {
|
199
|
-
"get": {
|
200
|
-
"type": "array",
|
201
|
-
"mock": {
|
202
|
-
"enabled": true,
|
203
|
-
"total":
|
204
|
-
"template": {
|
205
|
-
"id|+1": 1,
|
206
|
-
"title": "@title",
|
207
|
-
"content": "@paragraph"
|
208
|
-
|
209
|
-
|
210
|
-
|
211
|
-
|
212
|
-
}
|
213
|
-
|
214
|
-
|
215
|
-
|
216
|
-
|
217
|
-
|
218
|
-
|
219
|
-
|
220
|
-
|
221
|
-
|
222
|
-
|
223
|
-
|
224
|
-
|
225
|
-
|
226
|
-
|
227
|
-
|
228
|
-
|
229
|
-
|
230
|
-
|
231
|
-
|
232
|
-
|
233
|
-
|
234
|
-
|
235
|
-
|
236
|
-
|
237
|
-
|
238
|
-
|
239
|
-
|
240
|
-
|
241
|
-
|
242
|
-
|
243
|
-
|
244
|
-
|
245
|
-
|
246
|
-
|
247
|
-
|
248
|
-
|
249
|
-
|
250
|
-
|
251
|
-
|
252
|
-
|
253
|
-
|
254
|
-
|
255
|
-
|
256
|
-
|
257
|
-
|
258
|
-
|
259
|
-
|
260
|
-
|
261
|
-
|
262
|
-
|
263
|
-
|
264
|
-
|
265
|
-
|
266
|
-
|
267
|
-
|
268
|
-
|
269
|
-
|
270
|
-
|
271
|
-
|
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
|
+
```
|