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/README.md
CHANGED
@@ -1,346 +1,312 @@
|
|
1
|
-
# JSON API Mocker
|
2
|
-
|
3
|
-
A lightweight and flexible mock server that uses JSON configuration to quickly create RESTful APIs.
|
4
|
-
|
5
|
-
<p align="center">
|
6
|
-
<img src="https://img.shields.io/npm/v/json-api-mocker" alt="npm version" />
|
7
|
-
<img src="https://img.shields.io/npm/l/json-api-mocker" alt="license" />
|
8
|
-
<img src="https://img.shields.io/npm/dt/json-api-mocker" alt="downloads" />
|
9
|
-
</p>
|
10
|
-
|
11
|
-
## โจ Features
|
12
|
-
|
13
|
-
- ๐ Quick setup with JSON configuration
|
14
|
-
- ๐ Support for GET, POST, PUT, DELETE methods
|
15
|
-
- ๐ Automatic data persistence
|
16
|
-
- ๐ Built-in pagination support
|
17
|
-
- ๐ Customizable response schemas
|
18
|
-
-
|
19
|
-
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
|
44
|
-
"
|
45
|
-
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
-
"
|
50
|
-
|
51
|
-
|
52
|
-
"
|
53
|
-
|
54
|
-
"
|
55
|
-
"
|
56
|
-
|
57
|
-
|
58
|
-
|
59
|
-
|
60
|
-
"
|
61
|
-
"
|
62
|
-
"
|
63
|
-
|
64
|
-
|
65
|
-
|
66
|
-
|
67
|
-
|
68
|
-
|
69
|
-
|
70
|
-
|
71
|
-
|
72
|
-
|
73
|
-
|
74
|
-
|
75
|
-
|
76
|
-
|
77
|
-
|
78
|
-
|
79
|
-
|
80
|
-
|
81
|
-
|
82
|
-
|
83
|
-
|
84
|
-
|
85
|
-
|
86
|
-
|
87
|
-
|
88
|
-
|
89
|
-
|
90
|
-
|
91
|
-
|
92
|
-
|
93
|
-
|
94
|
-
|
95
|
-
|
96
|
-
|
97
|
-
|
98
|
-
|
99
|
-
|
100
|
-
|
101
|
-
|
102
|
-
-
|
103
|
-
|
104
|
-
|
105
|
-
|
106
|
-
|
107
|
-
|
108
|
-
|
109
|
-
|
110
|
-
|
111
|
-
|
112
|
-
|
113
|
-
|
114
|
-
|
115
|
-
|
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
|
-
|
196
|
-
|
197
|
-
|
198
|
-
|
199
|
-
|
200
|
-
|
201
|
-
|
202
|
-
|
203
|
-
####
|
204
|
-
|
205
|
-
|
206
|
-
|
207
|
-
|
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
|
-
|
272
|
-
|
273
|
-
|
274
|
-
|
275
|
-
|
276
|
-
|
277
|
-
|
278
|
-
|
279
|
-
|
280
|
-
|
281
|
-
|
282
|
-
|
283
|
-
|
284
|
-
|
285
|
-
|
286
|
-
|
287
|
-
|
288
|
-
|
289
|
-
|
290
|
-
|
291
|
-
|
292
|
-
|
293
|
-
|
294
|
-
|
295
|
-
|
296
|
-
|
297
|
-
|
298
|
-
|
299
|
-
|
300
|
-
|
301
|
-
|
302
|
-
|
303
|
-
|
304
|
-
|
305
|
-
|
306
|
-
|
307
|
-
|
308
|
-
|
309
|
-
|
310
|
-
|
311
|
-
|
312
|
-
}
|
313
|
-
}
|
314
|
-
}
|
315
|
-
```
|
316
|
-
|
317
|
-
### Request Validation
|
318
|
-
|
319
|
-
Add schema validation for POST/PUT requests:
|
320
|
-
|
321
|
-
```json
|
322
|
-
{
|
323
|
-
"requestSchema": {
|
324
|
-
"name": "string",
|
325
|
-
"age": "number",
|
326
|
-
"email": "string"
|
327
|
-
}
|
328
|
-
}
|
329
|
-
```
|
330
|
-
|
331
|
-
## ๐ค Contributing
|
332
|
-
|
333
|
-
1. Fork the repository
|
334
|
-
2. Create your feature branch (`git checkout -b feature/amazing-feature`)
|
335
|
-
3. Commit your changes (`git commit -m 'Add some amazing feature'`)
|
336
|
-
4. Push to the branch (`git push origin feature/amazing-feature`)
|
337
|
-
5. Open a Pull Request
|
338
|
-
|
339
|
-
## ๐ License
|
340
|
-
|
341
|
-
MIT ยฉ [Xiong Haiyin]
|
342
|
-
|
343
|
-
## ๐ Acknowledgments
|
344
|
-
|
345
|
-
- Express.js for the excellent web framework
|
1
|
+
# JSON API Mocker
|
2
|
+
|
3
|
+
A lightweight and flexible mock server that uses JSON configuration to quickly create RESTful APIs.
|
4
|
+
|
5
|
+
<p align="center">
|
6
|
+
<img src="https://img.shields.io/npm/v/json-api-mocker" alt="npm version" />
|
7
|
+
<img src="https://img.shields.io/npm/l/json-api-mocker" alt="license" />
|
8
|
+
<img src="https://img.shields.io/npm/dt/json-api-mocker" alt="downloads" />
|
9
|
+
</p>
|
10
|
+
|
11
|
+
## โจ Features
|
12
|
+
|
13
|
+
- ๐ Quick setup with JSON configuration
|
14
|
+
- ๐ Support for GET, POST, PUT, DELETE methods
|
15
|
+
- ๐ Automatic data persistence
|
16
|
+
- ๐ Built-in pagination support
|
17
|
+
- ๐ Customizable response schemas
|
18
|
+
- ๐ญ Integration with Mock.js for powerful data mocking
|
19
|
+
- ๐ค File upload support
|
20
|
+
- ๐ก TypeScript support
|
21
|
+
|
22
|
+
## ๐ฆ Installation
|
23
|
+
|
24
|
+
```bash
|
25
|
+
# Using npm
|
26
|
+
npm install json-api-mocker
|
27
|
+
|
28
|
+
# Using yarn
|
29
|
+
yarn add json-api-mocker
|
30
|
+
|
31
|
+
# Using pnpm
|
32
|
+
pnpm add json-api-mocker
|
33
|
+
```
|
34
|
+
|
35
|
+
## ๐ Quick Start
|
36
|
+
|
37
|
+
### 1. Create Configuration File
|
38
|
+
|
39
|
+
Create a `data.json` file in your project root:
|
40
|
+
|
41
|
+
```json
|
42
|
+
{
|
43
|
+
"server": {
|
44
|
+
"port": 8080,
|
45
|
+
"baseProxy": "/api"
|
46
|
+
},
|
47
|
+
"routes": [
|
48
|
+
{
|
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": "John",
|
62
|
+
"age": 30,
|
63
|
+
"city": "New York"
|
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": "Upload successful",
|
79
|
+
"data": {
|
80
|
+
"url": "@image('200x200')",
|
81
|
+
"filename": "@string(10).jpg",
|
82
|
+
"size": "@integer(1000, 1000000)"
|
83
|
+
}
|
84
|
+
}
|
85
|
+
}
|
86
|
+
}
|
87
|
+
}
|
88
|
+
}
|
89
|
+
]
|
90
|
+
}
|
91
|
+
```
|
92
|
+
|
93
|
+
### 2. Start the Server
|
94
|
+
|
95
|
+
There are several ways to start the mock server:
|
96
|
+
|
97
|
+
```bash
|
98
|
+
# Method 1: Using npx (Recommended)
|
99
|
+
npx json-api-mocker
|
100
|
+
|
101
|
+
# Method 2: Using npx with a custom config file
|
102
|
+
npx json-api-mocker ./custom-config.json
|
103
|
+
|
104
|
+
# Method 3: If installed globally
|
105
|
+
json-api-mocker
|
106
|
+
|
107
|
+
# Method 4: If installed as a project dependency
|
108
|
+
# Add this to your package.json scripts:
|
109
|
+
{
|
110
|
+
"scripts": {
|
111
|
+
"mock": "json-api-mocker"
|
112
|
+
}
|
113
|
+
}
|
114
|
+
# Then run:
|
115
|
+
npm run mock
|
116
|
+
```
|
117
|
+
|
118
|
+
Now your mock server is running at `http://localhost:8080`!
|
119
|
+
|
120
|
+
You'll see output like this:
|
121
|
+
```bash
|
122
|
+
Mock Server is running:
|
123
|
+
- Address: http://localhost:8080
|
124
|
+
- Base Path: /api
|
125
|
+
Available APIs:
|
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
|
+
## ๐ Configuration Guide
|
132
|
+
|
133
|
+
### Server Configuration
|
134
|
+
|
135
|
+
The `server` section configures basic server settings:
|
136
|
+
|
137
|
+
```json
|
138
|
+
{
|
139
|
+
"server": {
|
140
|
+
"port": 8080, // Server port number
|
141
|
+
"baseProxy": "/api" // Base path for all routes
|
142
|
+
}
|
143
|
+
}
|
144
|
+
```
|
145
|
+
|
146
|
+
### Route Configuration
|
147
|
+
|
148
|
+
Each route can support multiple HTTP methods:
|
149
|
+
|
150
|
+
```json
|
151
|
+
{
|
152
|
+
"path": "/users", // Route path
|
153
|
+
"methods": {
|
154
|
+
"get": {
|
155
|
+
"type": "array", // Response type: "array" or "object"
|
156
|
+
"pagination": { // Optional pagination settings
|
157
|
+
"enabled": true,
|
158
|
+
"pageSize": 10,
|
159
|
+
"totalCount": 100
|
160
|
+
},
|
161
|
+
"response": [] // Response data
|
162
|
+
},
|
163
|
+
"post": {
|
164
|
+
"requestSchema": { // Request body validation schema
|
165
|
+
"name": "string",
|
166
|
+
"age": "number"
|
167
|
+
},
|
168
|
+
"response": {
|
169
|
+
"success": true
|
170
|
+
}
|
171
|
+
}
|
172
|
+
}
|
173
|
+
}
|
174
|
+
```
|
175
|
+
|
176
|
+
### File Upload Support
|
177
|
+
|
178
|
+
You can configure file upload endpoints in your `data.json`:
|
179
|
+
|
180
|
+
```json
|
181
|
+
{
|
182
|
+
"path": "/upload/avatar",
|
183
|
+
"methods": {
|
184
|
+
"post": {
|
185
|
+
"type": "object",
|
186
|
+
"mock": {
|
187
|
+
"enabled": true,
|
188
|
+
"template": {
|
189
|
+
"success": true,
|
190
|
+
"message": "Upload successful",
|
191
|
+
"data": {
|
192
|
+
"url": "@image('200x200')",
|
193
|
+
"filename": "@string(10).jpg",
|
194
|
+
"size": "@integer(1000, 1000000)"
|
195
|
+
}
|
196
|
+
}
|
197
|
+
}
|
198
|
+
}
|
199
|
+
}
|
200
|
+
}
|
201
|
+
```
|
202
|
+
|
203
|
+
#### Example Usage:
|
204
|
+
|
205
|
+
```bash
|
206
|
+
# Upload single file
|
207
|
+
curl -X POST http://localhost:8080/api/upload/avatar \
|
208
|
+
-H "Content-Type: multipart/form-data" \
|
209
|
+
-F "avatar=@/path/to/your/image.jpg"
|
210
|
+
|
211
|
+
# Upload multiple files
|
212
|
+
curl -X POST http://localhost:8080/api/upload/images \
|
213
|
+
-H "Content-Type: multipart/form-data" \
|
214
|
+
-F "images=@/path/to/image1.jpg" \
|
215
|
+
-F "images=@/path/to/image2.jpg"
|
216
|
+
```
|
217
|
+
|
218
|
+
For detailed configuration options, please refer to [CONFIG.md](./CONFIG.md#file-upload-configuration).
|
219
|
+
|
220
|
+
## ๐ฏ API Examples
|
221
|
+
|
222
|
+
### Basic CRUD Operations
|
223
|
+
|
224
|
+
#### Get Users List
|
225
|
+
```bash
|
226
|
+
curl http://localhost:8080/api/users
|
227
|
+
```
|
228
|
+
|
229
|
+
#### Get Single User
|
230
|
+
```bash
|
231
|
+
curl http://localhost:8080/api/users/1
|
232
|
+
```
|
233
|
+
|
234
|
+
#### Create User
|
235
|
+
```bash
|
236
|
+
curl -X POST http://localhost:8080/api/users \
|
237
|
+
-H "Content-Type: application/json" \
|
238
|
+
-d '{"name":"Alice","age":25,"city":"Boston"}'
|
239
|
+
```
|
240
|
+
|
241
|
+
#### Update User
|
242
|
+
```bash
|
243
|
+
curl -X PUT http://localhost:8080/api/users/1 \
|
244
|
+
-H "Content-Type: application/json" \
|
245
|
+
-d '{"name":"Alice","age":26,"city":"Boston"}'
|
246
|
+
```
|
247
|
+
|
248
|
+
#### Delete User
|
249
|
+
```bash
|
250
|
+
curl -X DELETE http://localhost:8080/api/users/1
|
251
|
+
```
|
252
|
+
|
253
|
+
### Advanced Usage
|
254
|
+
|
255
|
+
#### Pagination
|
256
|
+
```bash
|
257
|
+
# Get page 2 with 10 items per page
|
258
|
+
curl http://localhost:8080/api/users?page=2&pageSize=10
|
259
|
+
```
|
260
|
+
|
261
|
+
#### Custom Response Headers
|
262
|
+
The server automatically adds these headers:
|
263
|
+
- `X-Total-Count`: Total number of items (for paginated responses)
|
264
|
+
|
265
|
+
## ๐ง Advanced Configuration
|
266
|
+
|
267
|
+
### Dynamic Routes
|
268
|
+
|
269
|
+
You can use URL parameters in routes:
|
270
|
+
|
271
|
+
```json
|
272
|
+
{
|
273
|
+
"path": "/users/:id/posts",
|
274
|
+
"methods": {
|
275
|
+
"get": {
|
276
|
+
"type": "array",
|
277
|
+
"response": []
|
278
|
+
}
|
279
|
+
}
|
280
|
+
}
|
281
|
+
```
|
282
|
+
|
283
|
+
### Request Validation
|
284
|
+
|
285
|
+
Add schema validation for POST/PUT requests:
|
286
|
+
|
287
|
+
```json
|
288
|
+
{
|
289
|
+
"requestSchema": {
|
290
|
+
"name": "string",
|
291
|
+
"age": "number",
|
292
|
+
"email": "string"
|
293
|
+
}
|
294
|
+
}
|
295
|
+
```
|
296
|
+
|
297
|
+
## ๐ค Contributing
|
298
|
+
|
299
|
+
1. Fork the repository
|
300
|
+
2. Create your feature branch (`git checkout -b feature/amazing-feature`)
|
301
|
+
3. Commit your changes (`git commit -m 'Add some amazing feature'`)
|
302
|
+
4. Push to the branch (`git push origin feature/amazing-feature`)
|
303
|
+
5. Open a Pull Request
|
304
|
+
|
305
|
+
## ๐ License
|
306
|
+
|
307
|
+
MIT ยฉ [Xiong Haiyin]
|
308
|
+
|
309
|
+
## ๐ Acknowledgments
|
310
|
+
|
311
|
+
- Express.js for the excellent web framework
|
346
312
|
- All our contributors and users
|
package/package.json
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
{
|
2
2
|
"name": "json-api-mocker",
|
3
|
-
"version": "1.2.
|
4
|
-
"description": "
|
3
|
+
"version": "1.2.5",
|
4
|
+
"description": "A mock server based on JSON configuration",
|
5
5
|
"main": "dist/index.js",
|
6
6
|
"types": "dist/index.d.ts",
|
7
7
|
"bin": {
|