json-api-mocker 1.2.3 โ†’ 1.2.6

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 +313 -347
  4. package/README.md +313 -345
  5. package/package.json +2 -2
package/README.md CHANGED
@@ -1,346 +1,314 @@
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
+ For detailed configuration options, please refer to [CONFIG.md](./CONFIG.md).
134
+
135
+ ### Server Configuration
136
+
137
+ The `server` section configures basic server settings:
138
+
139
+ ```json
140
+ {
141
+ "server": {
142
+ "port": 8080, // Server port number
143
+ "baseProxy": "/api" // Base path for all routes
144
+ }
145
+ }
146
+ ```
147
+
148
+ ### Route Configuration
149
+
150
+ Each route can support multiple HTTP methods:
151
+
152
+ ```json
153
+ {
154
+ "path": "/users", // Route path
155
+ "methods": {
156
+ "get": {
157
+ "type": "array", // Response type: "array" or "object"
158
+ "pagination": { // Optional pagination settings
159
+ "enabled": true,
160
+ "pageSize": 10,
161
+ "totalCount": 100
162
+ },
163
+ "response": [] // Response data
164
+ },
165
+ "post": {
166
+ "requestSchema": { // Request body validation schema
167
+ "name": "string",
168
+ "age": "number"
169
+ },
170
+ "response": {
171
+ "success": true
172
+ }
173
+ }
174
+ }
175
+ }
176
+ ```
177
+
178
+ ### File Upload Support
179
+
180
+ You can configure file upload endpoints in your `data.json`:
181
+
182
+ ```json
183
+ {
184
+ "path": "/upload/avatar",
185
+ "methods": {
186
+ "post": {
187
+ "type": "object",
188
+ "mock": {
189
+ "enabled": true,
190
+ "template": {
191
+ "success": true,
192
+ "message": "Upload successful",
193
+ "data": {
194
+ "url": "@image('200x200')",
195
+ "filename": "@string(10).jpg",
196
+ "size": "@integer(1000, 1000000)"
197
+ }
198
+ }
199
+ }
200
+ }
201
+ }
202
+ }
203
+ ```
204
+
205
+ #### Example Usage:
206
+
207
+ ```bash
208
+ # Upload single file
209
+ curl -X POST http://localhost:8080/api/upload/avatar \
210
+ -H "Content-Type: multipart/form-data" \
211
+ -F "avatar=@/path/to/your/image.jpg"
212
+
213
+ # Upload multiple files
214
+ curl -X POST http://localhost:8080/api/upload/images \
215
+ -H "Content-Type: multipart/form-data" \
216
+ -F "images=@/path/to/image1.jpg" \
217
+ -F "images=@/path/to/image2.jpg"
218
+ ```
219
+
220
+ For detailed configuration options, please refer to [CONFIG.md](./CONFIG.md#file-upload-configuration).
221
+
222
+ ## ๐ŸŽฏ API Examples
223
+
224
+ ### Basic CRUD Operations
225
+
226
+ #### Get Users List
227
+ ```bash
228
+ curl http://localhost:8080/api/users
229
+ ```
230
+
231
+ #### Get Single User
232
+ ```bash
233
+ curl http://localhost:8080/api/users/1
234
+ ```
235
+
236
+ #### Create User
237
+ ```bash
238
+ curl -X POST http://localhost:8080/api/users \
239
+ -H "Content-Type: application/json" \
240
+ -d '{"name":"Alice","age":25,"city":"Boston"}'
241
+ ```
242
+
243
+ #### Update User
244
+ ```bash
245
+ curl -X PUT http://localhost:8080/api/users/1 \
246
+ -H "Content-Type: application/json" \
247
+ -d '{"name":"Alice","age":26,"city":"Boston"}'
248
+ ```
249
+
250
+ #### Delete User
251
+ ```bash
252
+ curl -X DELETE http://localhost:8080/api/users/1
253
+ ```
254
+
255
+ ### Advanced Usage
256
+
257
+ #### Pagination
258
+ ```bash
259
+ # Get page 2 with 10 items per page
260
+ curl http://localhost:8080/api/users?page=2&pageSize=10
261
+ ```
262
+
263
+ #### Custom Response Headers
264
+ The server automatically adds these headers:
265
+ - `X-Total-Count`: Total number of items (for paginated responses)
266
+
267
+ ## ๐Ÿ”ง Advanced Configuration
268
+
269
+ ### Dynamic Routes
270
+
271
+ You can use URL parameters in routes:
272
+
273
+ ```json
274
+ {
275
+ "path": "/users/:id/posts",
276
+ "methods": {
277
+ "get": {
278
+ "type": "array",
279
+ "response": []
280
+ }
281
+ }
282
+ }
283
+ ```
284
+
285
+ ### Request Validation
286
+
287
+ Add schema validation for POST/PUT requests:
288
+
289
+ ```json
290
+ {
291
+ "requestSchema": {
292
+ "name": "string",
293
+ "age": "number",
294
+ "email": "string"
295
+ }
296
+ }
297
+ ```
298
+
299
+ ## ๐Ÿค Contributing
300
+
301
+ 1. Fork the repository
302
+ 2. Create your feature branch (`git checkout -b feature/amazing-feature`)
303
+ 3. Commit your changes (`git commit -m 'Add some amazing feature'`)
304
+ 4. Push to the branch (`git push origin feature/amazing-feature`)
305
+ 5. Open a Pull Request
306
+
307
+ ## ๐Ÿ“„ License
308
+
309
+ MIT ยฉ [Xiong Haiyin]
310
+
311
+ ## ๐Ÿ™ Acknowledgments
312
+
313
+ - Express.js for the excellent web framework
346
314
  - 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.6",
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": {