@winches/auto-mock
约定式路由mock工具
⭐️ feature
✅ auto get mock route (自动获取mock路径)
✅ Hot Update 热更新支持(检测mock路径和配置文件)
🤖 easy to get params(更简易的获取请求参数)
⚡ Faster response build with fastify(更快的响应速度)
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"
},
Create a mock.config.c?js
file set some config
module.exports = {
// default option value
mockPath: '__mock__', // mock文件路径
watch: false, // 是否热更新
port: 5050, // 端口号
}
Run script
npm run mock
运行参数
热更新模式
auto-mock -w
设置端口
auto-mock -p 8080
创建一个本地__mock__
文件夹
For instance
└── 存放mock目录(__mock__)
│
└── hello
│
└── index.js
auto-mock会自动将上面mock路径转成请求 http://<host>:<port>/hello/index
index.js
暴露接受一个函数,并且携带三个参数,函数的返回值就是mock请求的结果
For instance
// hello/index.js
module.exports = function(req, query, body) {
// response result
return {
code: 0,
msg: 'success',
req,
query,
body
}
}
此时将代理设置到auto-mock启动的服务中
发起请求http://host:port/hello/index
将得到返回值
{
code: 0,
msg: 'success',
req,
query,
body
}