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.ch.md CHANGED
@@ -1,348 +1,314 @@
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
+ 详细配置请参考 [CONFIG.ch.md](./CONFIG.ch.md)。
134
+
135
+ ### 服务器配置
136
+
137
+ `server` 部分配置基本的服务器设置:
138
+
139
+ ```json
140
+ {
141
+ "server": {
142
+ "port": 8080, // 服务器端口号
143
+ "baseProxy": "/api" // 所有路由的基础路径
144
+ }
145
+ }
146
+ ```
147
+
148
+ ### 路由配置
149
+
150
+ 每个路由可以支持多个 HTTP 方法:
151
+
152
+ ```json
153
+ {
154
+ "path": "/users", // 路由路径
155
+ "methods": {
156
+ "get": {
157
+ "type": "array", // 响应类型:array 或 object
158
+ "pagination": { // 可选的分页设置
159
+ "enabled": true,
160
+ "pageSize": 10,
161
+ "totalCount": 100
162
+ },
163
+ "response": [] // 响应数据
164
+ },
165
+ "post": {
166
+ "requestSchema": { // 请求体验证模式
167
+ "name": "string",
168
+ "age": "number"
169
+ },
170
+ "response": {
171
+ "success": true
172
+ }
173
+ }
174
+ }
175
+ }
176
+ ```
177
+
178
+ ### 文件上传支持
179
+
180
+ 你可以在 `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": "上传成功",
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
+ #### 使用示例:
206
+
207
+ ```bash
208
+ # 上传单个文件
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
+ # 上传多个文件
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
+ 详细配置选项请参考 [CONFIG.ch.md](./CONFIG.ch.md#文件上传配置)。
221
+
222
+ ## 🎯 API 示例
223
+
224
+ ### 基本的 CRUD 操作
225
+
226
+ #### 获取用户列表
227
+ ```bash
228
+ curl http://localhost:8080/api/users
229
+ ```
230
+
231
+ #### 获取单个用户
232
+ ```bash
233
+ curl http://localhost:8080/api/users/1
234
+ ```
235
+
236
+ #### 创建用户
237
+ ```bash
238
+ curl -X POST http://localhost:8080/api/users \
239
+ -H "Content-Type: application/json" \
240
+ -d '{"name":"李四","age":25,"city":"上海"}'
241
+ ```
242
+
243
+ #### 更新用户
244
+ ```bash
245
+ curl -X PUT http://localhost:8080/api/users/1 \
246
+ -H "Content-Type: application/json" \
247
+ -d '{"name":"李四","age":26,"city":"上海"}'
248
+ ```
249
+
250
+ #### 删除用户
251
+ ```bash
252
+ curl -X DELETE http://localhost:8080/api/users/1
253
+ ```
254
+
255
+ ### 高级用法
256
+
257
+ #### 分页
258
+ ```bash
259
+ # 获取第2页,每页10条数据
260
+ curl http://localhost:8080/api/users?page=2&pageSize=10
261
+ ```
262
+
263
+ #### 自定义响应头
264
+ 服务器自动添加以下响应头:
265
+ - `X-Total-Count`:数据总条数(用于分页响应)
266
+
267
+ ## 🔧 高级配置
268
+
269
+ ### 动态路由
270
+
271
+ 你可以在路由中使用 URL 参数:
272
+
273
+ ```json
274
+ {
275
+ "path": "/users/:id/posts",
276
+ "methods": {
277
+ "get": {
278
+ "type": "array",
279
+ "response": []
280
+ }
281
+ }
282
+ }
283
+ ```
284
+
285
+ ### 请求验证
286
+
287
+ 为 POST/PUT 请求添加模式验证:
288
+
289
+ ```json
290
+ {
291
+ "requestSchema": {
292
+ "name": "string",
293
+ "age": "number",
294
+ "email": "string"
295
+ }
296
+ }
297
+ ```
298
+
299
+ ## 🤝 贡献指南
300
+
301
+ 1. Fork 本仓库
302
+ 2. 创建你的特性分支 (`git checkout -b feature/amazing-feature`)
303
+ 3. 提交你的改动 (`git commit -m '添加一些很棒的特性'`)
304
+ 4. 推送到分支 (`git push origin feature/amazing-feature`)
305
+ 5. 开启一个 Pull Request
306
+
307
+ ## 📄 许可证
308
+
309
+ MIT © [熊海银]
310
+
311
+ ## 🙏 致谢
312
+
313
+ - 感谢 Express.js 提供出色的 Web 框架
348
314
  - 感谢所有贡献者和用户