@rc-lib/swagger-mock

1.0.9 • Public • Published

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配置文件

Versions

Current Tags

VersionDownloads (Last 7 Days)Tag
1.0.90latest

Version History

VersionDownloads (Last 7 Days)Published
1.0.90
1.0.80
1.0.70
1.0.60
1.0.50
1.0.40
1.0.30
1.0.20
1.0.10
1.0.00

Package Sidebar

Install

npm i @rc-lib/swagger-mock

Weekly Downloads

0

Version

1.0.9

License

MIT

Unpacked Size

23.9 kB

Total Files

7

Last publish

Collaborators

  • luoxy518
  • liushurui
  • shubinwang