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.ch.md
CHANGED
@@ -1,348 +1,312 @@
|
|
1
|
-
# JSON API Mocker
|
2
|
-
|
3
|
-
一个轻量级且灵活的 Mock 服务器,通过 JSON 配置快速创建 RESTful API。
|
4
|
-
|
5
|
-
<p align="center">
|
6
|
-
<img src="https://img.shields.io/npm/v/json-api-mocker" alt="npm
|
7
|
-
<img src="https://img.shields.io/npm/l/json-api-mocker" alt="
|
8
|
-
<img src="https://img.shields.io/npm/dt/json-api-mocker" alt="
|
9
|
-
</p>
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
-
|
15
|
-
-
|
16
|
-
-
|
17
|
-
-
|
18
|
-
-
|
19
|
-
-
|
20
|
-
- 💡 TypeScript 支持
|
21
|
-
|
22
|
-
## 📦 安装
|
23
|
-
|
24
|
-
```bash
|
25
|
-
# 使用 npm
|
26
|
-
npm install json-api-mocker
|
27
|
-
|
28
|
-
# 使用 yarn
|
29
|
-
yarn add json-api-mocker
|
30
|
-
|
31
|
-
# 使用 pnpm
|
32
|
-
pnpm add json-api-mocker
|
33
|
-
```
|
34
|
-
|
35
|
-
## 🚀 快速开始
|
36
|
-
|
37
|
-
### 1. 创建配置文件
|
38
|
-
|
39
|
-
在项目根目录创建 `data.json` 文件:
|
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": "张三",
|
62
|
-
"age": 30,
|
63
|
-
"city": "北京"
|
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
|
-
"type": "array",
|
313
|
-
"response": []
|
314
|
-
}
|
315
|
-
}
|
316
|
-
}
|
317
|
-
```
|
318
|
-
|
319
|
-
### 请求验证
|
320
|
-
|
321
|
-
为 POST/PUT 请求添加模式验证:
|
322
|
-
|
323
|
-
```json
|
324
|
-
{
|
325
|
-
"requestSchema": {
|
326
|
-
"name": "string",
|
327
|
-
"age": "number",
|
328
|
-
"email": "string"
|
329
|
-
}
|
330
|
-
}
|
331
|
-
```
|
332
|
-
|
333
|
-
## 🤝 贡献指南
|
334
|
-
|
335
|
-
1. Fork 本仓库
|
336
|
-
2. 创建你的特性分支 (`git checkout -b feature/amazing-feature`)
|
337
|
-
3. 提交你的改动 (`git commit -m '添加一些很棒的特性'`)
|
338
|
-
4. 推送到分支 (`git push origin feature/amazing-feature`)
|
339
|
-
5. 开启一个 Pull Request
|
340
|
-
|
341
|
-
## 📄 许可证
|
342
|
-
|
343
|
-
MIT © [熊海印]
|
344
|
-
|
345
|
-
## 🙏 致谢
|
346
|
-
|
347
|
-
- 感谢 Express.js 提供出色的 Web 框架
|
1
|
+
# JSON API Mocker
|
2
|
+
|
3
|
+
一个轻量级且灵活的 Mock 服务器,通过 JSON 配置快速创建 RESTful API。
|
4
|
+
|
5
|
+
<p align="center">
|
6
|
+
<img src="https://img.shields.io/npm/v/json-api-mocker" alt="npm 版本" />
|
7
|
+
<img src="https://img.shields.io/npm/l/json-api-mocker" alt="许可证" />
|
8
|
+
<img src="https://img.shields.io/npm/dt/json-api-mocker" alt="下载量" />
|
9
|
+
</p>
|
10
|
+
|
11
|
+
## ✨ 特性
|
12
|
+
|
13
|
+
- 🚀 通过 JSON 配置快速搭建
|
14
|
+
- 🔄 支持 GET、POST、PUT、DELETE 方法
|
15
|
+
- 📝 自动数据持久化
|
16
|
+
- 🔍 内置分页支持
|
17
|
+
- 🛠 可自定义响应结构
|
18
|
+
- 🎭 集成 Mock.js 实现强大的数据模拟
|
19
|
+
- 📤 支持文件上传
|
20
|
+
- 💡 TypeScript 支持
|
21
|
+
|
22
|
+
## 📦 安装
|
23
|
+
|
24
|
+
```bash
|
25
|
+
# 使用 npm
|
26
|
+
npm install json-api-mocker
|
27
|
+
|
28
|
+
# 使用 yarn
|
29
|
+
yarn add json-api-mocker
|
30
|
+
|
31
|
+
# 使用 pnpm
|
32
|
+
pnpm add json-api-mocker
|
33
|
+
```
|
34
|
+
|
35
|
+
## 🚀 快速开始
|
36
|
+
|
37
|
+
### 1. 创建配置文件
|
38
|
+
|
39
|
+
在项目根目录创建 `data.json` 文件:
|
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": "张三",
|
62
|
+
"age": 30,
|
63
|
+
"city": "北京"
|
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": "上传成功",
|
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. 启动服务器
|
94
|
+
|
95
|
+
有多种方式可以启动 Mock 服务器:
|
96
|
+
|
97
|
+
```bash
|
98
|
+
# 方式一:使用 npx(推荐)
|
99
|
+
npx json-api-mocker
|
100
|
+
|
101
|
+
# 方式二:使用 npx 并指定配置文件
|
102
|
+
npx json-api-mocker ./custom-config.json
|
103
|
+
|
104
|
+
# 方式三:如果全局安装了包
|
105
|
+
json-api-mocker
|
106
|
+
|
107
|
+
# 方式四:如果作为项目依赖安装
|
108
|
+
# 在 package.json 的 scripts 中添加:
|
109
|
+
{
|
110
|
+
"scripts": {
|
111
|
+
"mock": "json-api-mocker"
|
112
|
+
}
|
113
|
+
}
|
114
|
+
# 然后运行:
|
115
|
+
npm run mock
|
116
|
+
```
|
117
|
+
|
118
|
+
现在你的 Mock 服务器已经在 `http://localhost:8080` 运行了!
|
119
|
+
|
120
|
+
你会看到类似这样的输出:
|
121
|
+
```bash
|
122
|
+
Mock 服务器已启动:
|
123
|
+
- 地址:http://localhost:8080
|
124
|
+
- 基础路径:/api
|
125
|
+
可用的接口:
|
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
|
+
## 📖 配置指南
|
132
|
+
|
133
|
+
### 服务器配置
|
134
|
+
|
135
|
+
`server` 部分配置基本的服务器设置:
|
136
|
+
|
137
|
+
```json
|
138
|
+
{
|
139
|
+
"server": {
|
140
|
+
"port": 8080, // 服务器端口号
|
141
|
+
"baseProxy": "/api" // 所有路由的基础路径
|
142
|
+
}
|
143
|
+
}
|
144
|
+
```
|
145
|
+
|
146
|
+
### 路由配置
|
147
|
+
|
148
|
+
每个路由可以支持多个 HTTP 方法:
|
149
|
+
|
150
|
+
```json
|
151
|
+
{
|
152
|
+
"path": "/users", // 路由路径
|
153
|
+
"methods": {
|
154
|
+
"get": {
|
155
|
+
"type": "array", // 响应类型:array 或 object
|
156
|
+
"pagination": { // 可选的分页设置
|
157
|
+
"enabled": true,
|
158
|
+
"pageSize": 10,
|
159
|
+
"totalCount": 100
|
160
|
+
},
|
161
|
+
"response": [] // 响应数据
|
162
|
+
},
|
163
|
+
"post": {
|
164
|
+
"requestSchema": { // 请求体验证模式
|
165
|
+
"name": "string",
|
166
|
+
"age": "number"
|
167
|
+
},
|
168
|
+
"response": {
|
169
|
+
"success": true
|
170
|
+
}
|
171
|
+
}
|
172
|
+
}
|
173
|
+
}
|
174
|
+
```
|
175
|
+
|
176
|
+
### 文件上传支持
|
177
|
+
|
178
|
+
你可以在 `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": "上传成功",
|
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
|
+
#### 使用示例:
|
204
|
+
|
205
|
+
```bash
|
206
|
+
# 上传单个文件
|
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
|
+
# 上传多个文件
|
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
|
+
详细配置选项请参考 [CONFIG.ch.md](./CONFIG.ch.md#文件上传配置)。
|
219
|
+
|
220
|
+
## 🎯 API 示例
|
221
|
+
|
222
|
+
### 基本的 CRUD 操作
|
223
|
+
|
224
|
+
#### 获取用户列表
|
225
|
+
```bash
|
226
|
+
curl http://localhost:8080/api/users
|
227
|
+
```
|
228
|
+
|
229
|
+
#### 获取单个用户
|
230
|
+
```bash
|
231
|
+
curl http://localhost:8080/api/users/1
|
232
|
+
```
|
233
|
+
|
234
|
+
#### 创建用户
|
235
|
+
```bash
|
236
|
+
curl -X POST http://localhost:8080/api/users \
|
237
|
+
-H "Content-Type: application/json" \
|
238
|
+
-d '{"name":"李四","age":25,"city":"上海"}'
|
239
|
+
```
|
240
|
+
|
241
|
+
#### 更新用户
|
242
|
+
```bash
|
243
|
+
curl -X PUT http://localhost:8080/api/users/1 \
|
244
|
+
-H "Content-Type: application/json" \
|
245
|
+
-d '{"name":"李四","age":26,"city":"上海"}'
|
246
|
+
```
|
247
|
+
|
248
|
+
#### 删除用户
|
249
|
+
```bash
|
250
|
+
curl -X DELETE http://localhost:8080/api/users/1
|
251
|
+
```
|
252
|
+
|
253
|
+
### 高级用法
|
254
|
+
|
255
|
+
#### 分页
|
256
|
+
```bash
|
257
|
+
# 获取第2页,每页10条数据
|
258
|
+
curl http://localhost:8080/api/users?page=2&pageSize=10
|
259
|
+
```
|
260
|
+
|
261
|
+
#### 自定义响应头
|
262
|
+
服务器自动添加以下响应头:
|
263
|
+
- `X-Total-Count`:数据总条数(用于分页响应)
|
264
|
+
|
265
|
+
## 🔧 高级配置
|
266
|
+
|
267
|
+
### 动态路由
|
268
|
+
|
269
|
+
你可以在路由中使用 URL 参数:
|
270
|
+
|
271
|
+
```json
|
272
|
+
{
|
273
|
+
"path": "/users/:id/posts",
|
274
|
+
"methods": {
|
275
|
+
"get": {
|
276
|
+
"type": "array",
|
277
|
+
"response": []
|
278
|
+
}
|
279
|
+
}
|
280
|
+
}
|
281
|
+
```
|
282
|
+
|
283
|
+
### 请求验证
|
284
|
+
|
285
|
+
为 POST/PUT 请求添加模式验证:
|
286
|
+
|
287
|
+
```json
|
288
|
+
{
|
289
|
+
"requestSchema": {
|
290
|
+
"name": "string",
|
291
|
+
"age": "number",
|
292
|
+
"email": "string"
|
293
|
+
}
|
294
|
+
}
|
295
|
+
```
|
296
|
+
|
297
|
+
## 🤝 贡献指南
|
298
|
+
|
299
|
+
1. Fork 本仓库
|
300
|
+
2. 创建你的特性分支 (`git checkout -b feature/amazing-feature`)
|
301
|
+
3. 提交你的改动 (`git commit -m '添加一些很棒的特性'`)
|
302
|
+
4. 推送到分支 (`git push origin feature/amazing-feature`)
|
303
|
+
5. 开启一个 Pull Request
|
304
|
+
|
305
|
+
## 📄 许可证
|
306
|
+
|
307
|
+
MIT © [熊海银]
|
308
|
+
|
309
|
+
## 🙏 致谢
|
310
|
+
|
311
|
+
- 感谢 Express.js 提供出色的 Web 框架
|
348
312
|
- 感谢所有贡献者和用户
|