Skip to content

Commit 6363f01

Browse files
author
winches
committed
feat: 新增动态路由
1 parent de6f63d commit 6363f01

File tree

4 files changed

+53
-10
lines changed

4 files changed

+53
-10
lines changed

README.md

+32-5
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,16 @@
22

33
约定式路由mock工具
44

5-
## ⭐️ feature
6-
auto get mock route (自动获取mock路径)
5+
## ⭐️ Feature
6+
Auto get mock route (自动获取mock路径)
77

88
✅ Hot Update 热更新支持(检测mock路径和配置文件)
99

10-
🤖 easy to get params(更简易的获取请求参数)
10+
✅ Dynamics routes 支持动态路由
11+
12+
✅ Contractual routing 约定式路由(文件路径 -> 路由路径)
13+
14+
🤖 Easy to get params(更简易的获取请求参数)
1115

1216
⚡ Faster response build with fastify(更快的响应速度)
1317

@@ -21,7 +25,7 @@ pnpm add @winches/auto-mock -D
2125
```
2226

2327
## Usage
24-
### add script
28+
### Add script
2529
```json
2630
"scripts": {
2731
"mock": "auto-mock"
@@ -82,5 +86,28 @@ module.exports = function(req, query, body) {
8286
}
8387
}
8488
```
89+
### Dynamics Routes(动态路由)
90+
#### For instance
91+
```
92+
└── your mockPath(__mock__)
93+
94+
└── hello
95+
96+
└── test:id.js
97+
```
98+
the http request will be transform `http://<host>:<port>/hello/test:id`
99+
100+
and when send a request `http://<...>/hello/test/1`
101+
```js
102+
// hello/test:id.js
103+
module.exports = function(req, query, body) {
104+
// response result
105+
return {
106+
code: 0,
107+
msg: 'success',
108+
res: req.params // { id: 1 }
109+
}
110+
}
111+
```
85112

86-
> note: **node require 14+**
113+
> note: **node require 14+**

__mock__/test/dynamics:id.js

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
module.exports = function(req, query, body) {
2+
return {
3+
code: 0,
4+
msg: '你号',
5+
res: req.params
6+
}
7+
}

package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@winches/auto-mock",
3-
"version": "0.0.4",
3+
"version": "0.0.5",
44
"description": "约定式路由的mock工具",
55
"main": "dist/app.js",
66
"directories": {

routes/index.js

+13-4
Original file line numberDiff line numberDiff line change
@@ -36,11 +36,20 @@ async function routes (fastify, options) {
3636

3737
for (const item of mockList) {
3838
newItem = item.replace(/\\/g, '\/')
39-
fastify.get(newItem, async (request, reply) => {
40-
return require(join(mockDir, `${item}.js`))(request, request.query, request.body)
39+
// 判断是否为动态路径
40+
if (newItem.includes(':')) {
41+
const path = newItem.replace(':', '/:')
42+
return addRoutes(path, item)
43+
}
44+
addRoutes(newItem, item)
45+
}
46+
47+
function addRoutes(url, name) {
48+
fastify.get(url, async (request, reply) => {
49+
return require(join(mockDir, `${name}.js`))(request, request.query, request.body)
4150
})
42-
fastify.post(newItem, async (request, reply) => {
43-
return require(join(mockDir, `${item}.js`))(request, request.query, request.body)
51+
fastify.post(url, async (request, reply) => {
52+
return require(join(mockDir, `${name}.js`))(request, request.query, request.body)
4453
})
4554
}
4655
}

0 commit comments

Comments
 (0)