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.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 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
-
12
- ## ✨ 特性
13
-
14
- - 🚀 通过 JSON 配置快速搭建
15
- - 🔄 支持 GET、POST、PUT、DELETE 方法
16
- - 📝 自动数据持久化
17
- - 🔍 内置分页支持
18
- - 🛠 可自定义响应结构
19
- - 💡 集成 Mock.js 实现强大的数据模拟
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
- ### 2. 启动服务器
74
-
75
- 有多种方式可以启动 Mock 服务器:
76
-
77
- ```bash
78
- # 方式一:使用 npx(推荐)
79
- npx json-api-mocker
80
-
81
- # 方式二:使用 npx 并指定配置文件
82
- npx json-api-mocker ./custom-config.json
83
-
84
- # 方式三:如果全局安装了包
85
- json-api-mocker
86
-
87
- # 方式四:如果作为项目依赖安装
88
- # 在 package.json 的 scripts 中添加:
89
- {
90
- "scripts": {
91
- "mock": "json-api-mocker"
92
- }
93
- }
94
- # 然后运行:
95
- npm run mock
96
- ```
97
-
98
- 现在你的 Mock 服务器已经在 `http://localhost:8080` 运行了!
99
-
100
- 你会看到类似这样的输出:
101
- ```bash
102
- Mock 服务器已启动:
103
- - 地址:http://localhost:8080
104
- - 基础路径:/api
105
- 可用的接口:
106
- GET http://localhost:8080/api/users
107
- POST http://localhost:8080/api/users
108
- PUT http://localhost:8080/api/users/:id
109
- DELETE http://localhost:8080/api/users/:id
110
- ```
111
-
112
- ## 📖 配置指南
113
-
114
- ### 服务器配置
115
-
116
- `server` 部分配置基本的服务器设置:
117
-
118
- ```json
119
- {
120
- "server": {
121
- "port": 8080, // 服务器端口号
122
- "baseProxy": "/api" // 所有路由的基础路径
123
- }
124
- }
125
- ```
126
-
127
- ### 路由配置
128
-
129
- 每个路由可以支持多个 HTTP 方法:
130
-
131
- ```json
132
- {
133
- "path": "/users", // 路由路径
134
- "methods": {
135
- "get": {
136
- "type": "array", // 响应类型:array 或 object
137
- "pagination": { // 可选的分页设置
138
- "enabled": true,
139
- "pageSize": 10,
140
- "totalCount": 100
141
- },
142
- "response": [] // 响应数据
143
- },
144
- "post": {
145
- "requestSchema": { // 请求体验证模式
146
- "name": "string",
147
- "age": "number"
148
- },
149
- "response": {
150
- "success": true
151
- }
152
- }
153
- }
154
- }
155
- ```
156
-
157
- ### Mock.js 集成
158
-
159
- 你可以使用 Mock.js 模板来生成动态数据:
160
-
161
- ```json
162
- {
163
- "path": "/users",
164
- "methods": {
165
- "get": {
166
- "type": "array",
167
- "mock": {
168
- "enabled": true,
169
- "total": 200,
170
- "template": {
171
- "id|+1": 1,
172
- "name": "@cname",
173
- "email": "@email",
174
- "age|18-60": 1,
175
- "address": "@city(true)",
176
- "avatar": "@image('200x200')",
177
- "createTime": "@datetime"
178
- }
179
- },
180
- "pagination": {
181
- "enabled": true,
182
- "pageSize": 10
183
- }
184
- }
185
- }
186
- }
187
- ```
188
-
189
- #### 可用的 Mock.js 模板
190
-
191
- - `@cname` - 生成中文姓名
192
- - `@name` - 生成英文姓名
193
- - `@email` - 生成邮箱地址
194
- - `@datetime` - 生成日期时间
195
- - `@image` - 生成图片链接
196
- - `@city` - 生成城市名
197
- - `@id` - 生成随机 ID
198
- - `@guid` - 生成 GUID
199
- - `@title` - 生成标题
200
- - `@paragraph` - 生成段落
201
- - `|+1` - 自增数字
202
-
203
- 更多 Mock.js 模板请访问 [Mock.js 文档](http://mockjs.com/examples.html)
204
-
205
- #### Mock.js 使用示例
206
-
207
- 1. 生成用户列表:
208
- ```json
209
- {
210
- "mock": {
211
- "enabled": true,
212
- "total": 100,
213
- "template": {
214
- "id|+1": 1,
215
- "name": "@cname",
216
- "email": "@email"
217
- }
218
- }
219
- }
220
- ```
221
-
222
- 2. 生成文章列表:
223
- ```json
224
- {
225
- "mock": {
226
- "enabled": true,
227
- "total": 50,
228
- "template": {
229
- "id|+1": 1,
230
- "title": "@ctitle",
231
- "content": "@cparagraph",
232
- "author": "@cname",
233
- "publishDate": "@datetime"
234
- }
235
- }
236
- }
237
- ```
238
-
239
- 3. 生成商品列表:
240
- ```json
241
- {
242
- "mock": {
243
- "enabled": true,
244
- "total": 30,
245
- "template": {
246
- "id|+1": 1,
247
- "name": "@ctitle(3, 5)",
248
- "price|100-1000.2": 1,
249
- "category": "@pick(['电子产品', '图书', '服装'])",
250
- "image": "@image('200x200', '#50B347', '#FFF', 'Mock.js')"
251
- }
252
- }
253
- }
254
- ```
255
-
256
- ## 🎯 API 示例
257
-
258
- ### 基本的 CRUD 操作
259
-
260
- #### 获取用户列表
261
- ```bash
262
- curl http://localhost:8080/api/users
263
- ```
264
-
265
- #### 获取单个用户
266
- ```bash
267
- curl http://localhost:8080/api/users/1
268
- ```
269
-
270
- #### 创建用户
271
- ```bash
272
- curl -X POST http://localhost:8080/api/users \
273
- -H "Content-Type: application/json" \
274
- -d '{"name":"李四","age":25,"city":"上海"}'
275
- ```
276
-
277
- #### 更新用户
278
- ```bash
279
- curl -X PUT http://localhost:8080/api/users/1 \
280
- -H "Content-Type: application/json" \
281
- -d '{"name":"李四","age":26,"city":"上海"}'
282
- ```
283
-
284
- #### 删除用户
285
- ```bash
286
- curl -X DELETE http://localhost:8080/api/users/1
287
- ```
288
-
289
- ### 高级用法
290
-
291
- #### 分页
292
- ```bash
293
- # 获取第2页,每页10条数据
294
- curl http://localhost:8080/api/users?page=2&pageSize=10
295
- ```
296
-
297
- #### 自定义响应头
298
- 服务器自动添加以下响应头:
299
- - `X-Total-Count`:数据总条数(用于分页响应)
300
-
301
- ## 🔧 高级配置
302
-
303
- ### 动态路由
304
-
305
- 你可以在路由中使用 URL 参数:
306
-
307
- ```json
308
- {
309
- "path": "/users/:id/posts",
310
- "methods": {
311
- "get": {
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
  - 感谢所有贡献者和用户