@rc-lib/swagger-mock
v1.0.9
Published
swagger文档添加mock功能的express中间件,可用于webpackDevServer,在后端接口不通得情况下,为前端提供mock数据
Downloads
4
Readme
Swagger-Mock
swagger文档添加mock功能的express中间件,可用于webpackDevServer,在后端接口不通得情况下,为前端提供mock数据
node v16 +
安装
yarn add @rc-lib/swagger-mock --registry=https://registry.npmmirror.com
使用
在webpackDevServer中添加中间件,比如 create-react-app项目config/webpackDevServer.config.js:
setupMiddlewares(middlewares, devServer) {
if (!devServer) {
throw new Error('webpack-dev-server is not defined');
}
devServer.app.use(require('@rc-lib/swagger-mock'));
...
}
配置代理
全部代理
app.use(
createProxyMiddleware('/api', {
target: 'http://127.0.0.1:3000', // 本地webpackDevServer启动的服务
pathRewrite: {
'^/api': '',
},
changeOrigin: true,
secure: false, // 是否验证证书
ws: true, // 启用websocket
}),
);
部分接口代理
Object.entries({
'/api/gateway': [
'/api/vcc/account/queryVccAccountBalance',
],
}).forEach(([base, urls]) => urls.forEach(url => {
app.use(
createProxyMiddleware(`${base}${url}`, {
target: 'http://localhost:4001',
pathRewrite: { [`^${base}`]: '/swagger-mock' },
}),
);
}));
配置
项目启动时,会创建swagger-mock.js文件,用于@rc-lib/swagger-mock配置文件