json-api-mocker 1.2.2 → 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.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
- ## Complete Configuration Examples
112
-
113
- ### 1. Basic User CRUD API
114
-
115
- ```json
116
- {
117
- "server": {
118
- "port": 8080,
119
- "baseProxy": "/api"
120
- },
121
- "routes": [
122
- {
123
- "path": "/users",
124
- "methods": {
125
- "get": {
126
- "type": "array",
127
- "mock": {
128
- "enabled": true,
129
- "total": 100,
130
- "template": {
131
- "id|+1": 1,
132
- "name": "@name",
133
- "age|18-60": 1,
134
- "email": "@email",
135
- "address": "@city"
136
- }
137
- },
138
- "pagination": {
139
- "enabled": true,
140
- "pageSize": 10
141
- }
142
- },
143
- "post": {
144
- "requestSchema": {
145
- "name": "string",
146
- "age": "number",
147
- "email": "string"
148
- },
149
- "response": {
150
- "success": true,
151
- "message": "Created successfully"
152
- }
153
- }
154
- }
155
- }
156
- ]
157
- }
158
- ```
159
-
160
- ### 2. Articles API with Nested Data
161
-
162
- ```json
163
- {
164
- "path": "/articles",
165
- "methods": {
166
- "get": {
167
- "type": "array",
168
- "mock": {
169
- "enabled": true,
170
- "total": 50,
171
- "template": {
172
- "id|+1": 1,
173
- "title": "@title",
174
- "content": "@paragraph",
175
- "author": {
176
- "id|+1": 100,
177
- "name": "@name",
178
- "avatar": "@image('100x100')"
179
- },
180
- "comments|0-10": [{
181
- "id|+1": 1,
182
- "content": "@sentence",
183
- "user": "@name",
184
- "createTime": "@datetime"
185
- }]
186
- }
187
- }
188
- }
189
- }
190
- }
191
- ```
192
-
193
- ### 3. Dynamic Route Parameters Example
194
-
195
- ```json
196
- {
197
- "path": "/users/:id/posts",
198
- "methods": {
199
- "get": {
200
- "type": "array",
201
- "mock": {
202
- "enabled": true,
203
- "total": 10,
204
- "template": {
205
- "id|+1": 1,
206
- "title": "@title",
207
- "content": "@paragraph"
208
- }
209
- }
210
- }
211
- }
212
- }
213
- ```
214
-
215
- ## Important Notes
216
-
217
- 1. **Route Parameters**
218
- - Dynamic route parameters use `:paramName` format
219
- - Example: `/users/:id/posts/:postId`
220
-
221
- 2. **Mock Data**
222
- - Mock data generation only works when `enabled` is `true`
223
- - `template` supports all Mock.js syntax
224
-
225
- 3. **Pagination**
226
- - When enabled, use `?page=1&pageSize=10` to access paginated data
227
- - Response headers include `X-Total-Count` for total count
228
-
229
- 4. **Data Persistence**
230
- - POST, PUT, DELETE operations automatically save to data.json
231
- - Data persists after server restart
232
-
233
- ## Best Practices
234
-
235
- 1. **Effective Use of Mock Data**
236
- ```json
237
- {
238
- "mock": {
239
- "enabled": true,
240
- "total": 100,
241
- "template": {
242
- // Use auto-increment for IDs
243
- "id|+1": 1,
244
- // Use appropriate data types
245
- "age|18-60": 1
246
- }
247
- }
248
- }
249
- ```
250
-
251
- 2. **Proper Pagination Settings**
252
- ```json
253
- {
254
- "pagination": {
255
- "enabled": true,
256
- "pageSize": 10 // Set appropriate page size based on data volume
257
- }
258
- }
259
- ```
260
-
261
- 3. **Request Validation**
262
- ```json
263
- {
264
- "requestSchema": {
265
- // Ensure all required fields have type validation
266
- "name": "string",
267
- "age": "number",
268
- "email": "string"
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
+ ```