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.
Files changed (5) hide show
  1. package/CONFIG.ch.md +322 -305
  2. package/CONFIG.md +304 -271
  3. package/README.ch.md +311 -347
  4. package/README.md +311 -345
  5. 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
- - ๐Ÿ’ก Integration with Mock.js for powerful data mocking
19
- - ๐Ÿ’ก TypeScript support
20
-
21
- ## ๐Ÿ“ฆ Installation
22
-
23
- ```bash
24
- # Using npm
25
- npm install json-api-mocker
26
-
27
- # Using yarn
28
- yarn add json-api-mocker
29
-
30
- # Using pnpm
31
- pnpm add json-api-mocker
32
- ```
33
-
34
- ## ๐Ÿš€ Quick Start
35
-
36
- ### 1. Create Configuration File
37
-
38
- Create a `data.json` file in your project root:
39
-
40
- ```json
41
- {
42
- "server": {
43
- "port": 8080,
44
- "baseProxy": "/api"
45
- },
46
- "routes": [
47
- {
48
- "path": "/users",
49
- "methods": {
50
- "get": {
51
- "type": "array",
52
- "pagination": {
53
- "enabled": true,
54
- "pageSize": 10,
55
- "totalCount": 100
56
- },
57
- "response": [
58
- {
59
- "id": 1,
60
- "name": "John",
61
- "age": 30,
62
- "city": "New York"
63
- }
64
- ]
65
- }
66
- }
67
- }
68
- ]
69
- }
70
- ```
71
-
72
- ### 2. Start the Server
73
-
74
- There are several ways to start the mock server:
75
-
76
- ```bash
77
- # Method 1: Using npx (Recommended)
78
- npx json-api-mocker
79
-
80
- # Method 2: Using npx with a custom config file
81
- npx json-api-mocker ./custom-config.json
82
-
83
- # Method 3: If installed globally
84
- json-api-mocker
85
-
86
- # Method 4: If installed as a project dependency
87
- # Add this to your package.json scripts:
88
- {
89
- "scripts": {
90
- "mock": "json-api-mocker"
91
- }
92
- }
93
- # Then run:
94
- npm run mock
95
- ```
96
-
97
- Now your mock server is running at `http://localhost:8080`!
98
-
99
- You'll see output like this:
100
- ```bash
101
- Mock Server is running:
102
- - Address: http://localhost:8080
103
- - Base Path: /api
104
- Available APIs:
105
- GET http://localhost:8080/api/users
106
- POST http://localhost:8080/api/users
107
- PUT http://localhost:8080/api/users/:id
108
- DELETE http://localhost:8080/api/users/:id
109
- ```
110
-
111
- ## ๐Ÿ“– Configuration Guide
112
-
113
- ### Server Configuration
114
-
115
- The `server` section configures basic server settings:
116
-
117
- ```json
118
- {
119
- "server": {
120
- "port": 8080, // Server port number
121
- "baseProxy": "/api" // Base path for all routes
122
- }
123
- }
124
- ```
125
-
126
- ### Route Configuration
127
-
128
- Each route can support multiple HTTP methods:
129
-
130
- ```json
131
- {
132
- "path": "/users", // Route path
133
- "methods": {
134
- "get": {
135
- "type": "array", // Response type: "array" or "object"
136
- "pagination": { // Optional pagination settings
137
- "enabled": true,
138
- "pageSize": 10,
139
- "totalCount": 100
140
- },
141
- "response": [] // Response data
142
- },
143
- "post": {
144
- "requestSchema": { // Request body validation schema
145
- "name": "string",
146
- "age": "number"
147
- },
148
- "response": {
149
- "success": true
150
- }
151
- }
152
- }
153
- }
154
- ```
155
-
156
- ### Mock.js Integration
157
-
158
- You can use Mock.js templates to generate dynamic data:
159
-
160
- ```json
161
- {
162
- "path": "/users",
163
- "methods": {
164
- "get": {
165
- "type": "array",
166
- "mock": {
167
- "enabled": true,
168
- "total": 200,
169
- "template": {
170
- "id|+1": 1,
171
- "name": "@name",
172
- "email": "@email",
173
- "age|18-60": 1,
174
- "address": "@city(true)",
175
- "avatar": "@image('200x200')",
176
- "createTime": "@datetime"
177
- }
178
- },
179
- "pagination": {
180
- "enabled": true,
181
- "pageSize": 10
182
- }
183
- }
184
- }
185
- }
186
- ```
187
-
188
- #### Available Mock.js Templates
189
-
190
- - `@name` - Generate random name
191
- - `@email` - Generate random email
192
- - `@datetime` - Generate random datetime
193
- - `@image` - Generate random image URL
194
- - `@city` - Generate random city name
195
- - `@id` - Generate random ID
196
- - `@guid` - Generate GUID
197
- - `@title` - Generate random title
198
- - `@paragraph` - Generate random paragraph
199
- - `|+1` - Auto increment number
200
-
201
- For more Mock.js templates, visit [Mock.js Documentation](http://mockjs.com/examples.html)
202
-
203
- #### Examples with Mock.js
204
-
205
- 1. Generate user list with random data:
206
- ```json
207
- {
208
- "mock": {
209
- "enabled": true,
210
- "total": 100,
211
- "template": {
212
- "id|+1": 1,
213
- "name": "@name",
214
- "email": "@email"
215
- }
216
- }
217
- }
218
- ```
219
-
220
- 2. Generate article list with random content:
221
- ```json
222
- {
223
- "mock": {
224
- "enabled": true,
225
- "total": 50,
226
- "template": {
227
- "id|+1": 1,
228
- "title": "@title",
229
- "content": "@paragraph",
230
- "author": "@name",
231
- "publishDate": "@datetime"
232
- }
233
- }
234
- }
235
- ```
236
-
237
- 3. Generate product list with random prices:
238
- ```json
239
- {
240
- "mock": {
241
- "enabled": true,
242
- "total": 30,
243
- "template": {
244
- "id|+1": 1,
245
- "name": "@title(3, 5)",
246
- "price|100-1000.2": 1,
247
- "category": "@pick(['Electronics', 'Books', 'Clothing'])",
248
- "image": "@image('200x200', '#50B347', '#FFF', 'Mock.js')"
249
- }
250
- }
251
- }
252
- ```
253
-
254
- ## ๐ŸŽฏ API Examples
255
-
256
- ### Basic CRUD Operations
257
-
258
- #### Get Users List
259
- ```bash
260
- curl http://localhost:8080/api/users
261
- ```
262
-
263
- #### Get Single User
264
- ```bash
265
- curl http://localhost:8080/api/users/1
266
- ```
267
-
268
- #### Create User
269
- ```bash
270
- curl -X POST http://localhost:8080/api/users \
271
- -H "Content-Type: application/json" \
272
- -d '{"name":"Alice","age":25,"city":"Boston"}'
273
- ```
274
-
275
- #### Update User
276
- ```bash
277
- curl -X PUT http://localhost:8080/api/users/1 \
278
- -H "Content-Type: application/json" \
279
- -d '{"name":"Alice","age":26,"city":"Boston"}'
280
- ```
281
-
282
- #### Delete User
283
- ```bash
284
- curl -X DELETE http://localhost:8080/api/users/1
285
- ```
286
-
287
- ### Advanced Usage
288
-
289
- #### Pagination
290
- ```bash
291
- # Get page 2 with 10 items per page
292
- curl http://localhost:8080/api/users?page=2&pageSize=10
293
- ```
294
-
295
- #### Custom Response Headers
296
- The server automatically adds these headers:
297
- - `X-Total-Count`: Total number of items (for paginated responses)
298
-
299
- ## ๐Ÿ”ง Advanced Configuration
300
-
301
- ### Dynamic Routes
302
-
303
- You can use URL parameters in routes:
304
-
305
- ```json
306
- {
307
- "path": "/users/:id/posts",
308
- "methods": {
309
- "get": {
310
- "type": "array",
311
- "response": []
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.3",
4
- "description": "ไธ€ไธชๅŸบไบŽ JSON ้…็ฝฎ็š„ Mock ๆœๅŠกๅ™จ",
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": {