npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

sw-mock-server

v2.0.3

Published

**sw-mock-server** [![npm version](https://img.shields.io/static/v1?label=npm&message=v2.0.0&color=blue)](https://www.npmjs.com/package/sw-mock-server) ### 简介

Downloads

1

Readme

sw-mock-server npm version

简介

根据swagger文档地址生成mock数据,简化前端mock的工作量

安装

快速上手

1.基础用法:

跟目录创建文件 "sw.js"

  const sw = require('sw-mock-server');
  
  sw({
    "port": "8089", // 程序监听端口
    "swaggerOptions": {
      // swagger文档页面,比如http://xxxx/swagger-ui.html
      "url": "http://xxxx/swagger-ui.html"
    }
  });

node sw --clean --pull --server 等待几秒后,访问localhost:8089 + [接口路径]可访问到mock数据

演示项目:https://github.com/jmx164491960/bg-fe-server

2.进阶用法

命令

以下命令:

node sw --clean --pull --server

等价于

node sw -c -p -s // 简写版本

也可以等价于分别执行:

node sw --clean // 清除swagger生成的mock数据
node sw --pull // 从swagger文档上遍历,生成mock数据
node sw --server // 监听指定端口,开启服务

等价于

node sw -c
node sw -p
node sw -s

命令参数可以按需使用,clean和pull并不是每次启动都需要;比如我已经生成过mock数据,那么只需要node sw --server

配置

上述的简化配置,需要在代码中把使用mock的接口域名改为localhost:8089,但现实中如果可以不修改会更方便,即mock接口和正常接口共用一个域名。使用fail回调函数可以做到

另外,对整个swagger文档进行遍历,对生成出我们本不需要的mock数据,这时候我们可以加入白名单filterReg

sw.js:

  const run = require('./index.js');
  const httpProxy = require('http-proxy');
  const proxy = httpProxy.createProxyServer({});

  run({
    "port": "8089", // 程序监听端口
    // 请求没有被mock拦截,就转发到开发服务的url,这使得mock和非mock的接口可以使用同一个域名
    "fail": (req, res) => {
      proxy.web(req, res, {
        target: `http://127.0.0.1:8088`
      });
    },
    "swaggerOptions": {
      // swagger文档页面,比如http://xxxx/swagger-ui.html
      "url": "http://xxxx/swagger-ui.html"
      "filterReg": /getOverallBroadcastingData/, // 可以是URL正则或者为空,用于匹配swagger上的接口,如果空则匹配swagger上所有。
    }
  });

3.更多用法 除此之外,还有更多配置的字段。做到适合的配置,可以最大提高mock的效率。

配置说明

字段 | 类型 | 用法
-|-|- port | string | 程序监控端口 | fail | function | 请求没有被mock拦截时的回调函数 | swaggerOptions | Object | 从swagger上生成mock过程的相关配置 | swaggerOptions.url | string | swagger文档地址,如http://xxxx/swagger-ui.html | swaggerOptions.filterReg | regexp | 白名单规则,推荐接口量多的时候使用 | swaggerOptions.arrayMockNum | Object | 数组型数据的模拟长度,根据属性min-max,在他们之间随机 | swaggerOptions.arrayMockNum.min | number | 数组模拟长度最小值 | swaggerOptions.arrayMockNum.max | Object | 从swagger上生成mock过程的相关配置 | swaggerOptions.formatter | function | 格式化函数,用于对数据做一些统一特殊处理 |