@winches/auto-mock
约定式路由mock工具
⭐️ Feature
Install
npm i @winches/auto-mock -D
# or
yarn add @winches/auto-mock -D
# or
pnpm add @winches/auto-mock -D
Usage
Add script
"scripts": {
"mock": "auto-mock"
},
mock.config.c?js
file set some config
Create a module.exports = {
// default option value
mockPath: '__mock__', // mock文件路径
watch: false, // 是否热更新
port: 5050, // 端口号
}
Run script
npm run mock
Run script with options
Watch mode
auto-mock -w
Set port
auto-mock -p 8080
__mock__
folder
Create a file under the For instance
└── your mockPath(__mock__)
│
└── hello
│
└── index.js
and the http request will be transform http://<host>:<port>/hello/index
index.js exports a function it receive three parameters and the fn result is response
For instance
// hello/index.js
module.exports = function(req, query, body) {
// response result
return {
code: 0,
msg: 'success',
req,
query,
body
}
}
Dynamics Routes(动态路由)
For instance
└── your mockPath(__mock__)
│
└── hello
│
└── test:id.js
the http request will be transform http://<host>:<port>/hello/test:id
and when send a request http://<...>/hello/test/1
// hello/test:id.js
module.exports = function(req, query, body) {
// response result
return {
code: 0,
msg: 'success',
res: req.params // { id: 1 }
}
}
note: node require 14+