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

dev-mock-cli

v0.0.7

Published

a react-cli and create react app

Downloads

317

Readme

说明:前端的工作中,mock api是比较麻烦的事情,特别是API交付节点太晚导致前端项目延期背锅。所以构建了当前项目,用来配合前端项目快速生成mock API。

支持能力/价值:

  1. Mock API
  2. 读取Swagger文档,并生成API接口
  3. 无需担心跨域问题
  4. 安装方便:node环境一行指令安装/Docker启动
  5. API生成读取顺序:本地mock -> swagger -> remote api

快速开始

要求node环境:node > 18,后续考虑降低成本

1、安装dev-mock-cli

yarn add -D dev-mock-cli

2、在项目根目录下新建mock.config.json

{
    "port": "9000",
    "type": "action",
    "proxyApiUrl": "http://localhost:9000",
    "cors": {
      "allowedHeaders": ["Origin", "X-Requested-With", "Content-Type", "Accept", "X-Accesstoken"],
      "allowCredentials": true,  
      "maxAge": 86400
    },
    "swaggerApi": [
      {
        "title": "swagger",
        "type": "action",
        "url": "http://subscri.xxx.com/swagger/doc.json"
      }
    ],
    "mockFields": {
        "RetCode": {
            "fixedValue": 0
        }
    }
}

配置文件解读

| 字段 | 说明 | | --------------- | --------------------------------------------------------- | | port | 启动本地服务的端口号,默认9000 | | type | api风格,默认restful | | proxyApiUrl | 读取远程API服务,只需要写入远程服务的请求地址 | | cors | 跨域字段设置 | | swaggerAPI | 需要生成API的swagger文档 | | requestPriority | API读取的优先级,匹配到后将直接返回 | | mockFields | 特殊字段的配置,比如swagger中code是number类型,但是在实践中我们需要配置code字段为0,代表成功 |

swaggerAPI

| 字段 | 说明 | | ----- | -------------------------- | | title | swagger文档说明 | | type | api风格,默认restful,可以选择action | | url | swagger文档的json url地址 |

API数据来源配置

1、本地mock API

启动后,我们内置了一个api作为参考,我们这里来看下如何在本地文件mock api

原理:我们以目录为api请求路径,以文件名为请求方法。

不存在路由参数的请求:

  • 比如/v1/list/get.json,当前文件匹配的请求为Get方法,路径为/v1/list
  • 比如/v1/list/post.json,当前文件匹配的请求为Post方法,路径为/v1/list

存在路由参数的请求:

  • 比如/v1/list/{id}/get.json, 当前文件匹配的请求为Get请求,id可以被替换成任意数字或字符串,匹配路径有:/v1/list/1 /v1/list/abc

2、对接swagger

swagger文档生成后,只需要拿到当前swagger的json文件,写入配置文件即可, 然后访问对应的路由,即可生成返回参数

3、代理远程服务器API

在开发项目时,除了本地和swagger模拟的api,我们一般会有远程的api服务。

此时我们只需要在mock.config.json中配置proxyApiUrl, 即可进行反向代理,在本地mock和swagger都无法匹配到路由的时候,将会请求到proxyApiUrl代理的地址。

在项目中使用

在项目中安装:yarn add -D dev-mock-cli

我们可以在项目根目录下新建mock.config.json

{
    "port": "9000",
    "type": "restful",
    "proxyApiUrl": "",
    "cors": {
      "allowedHeaders": ["Origin", "X-Requested-With", "Content-Type", "Accept", "X-Accesstoken"],
      "allowCredentials": true,  
      "maxAge": 86400
    },
    "swaggerApi": [],
    "requestPriority": ["local", "swagger", "proxy"],
    "mockFields": {
        "RetCode": {
            "fixedValue": 0
        }
    }
}

启动服务yarn dev-mock-cli mock,会自动在项目根目录下新建mock文件夹,并生成list/get.json,作为/listAPI的请求数据源。

当然也可以全局安装,然后随时启动yarn global add dev-mock-cli

通过docker使用

上面我们通过docker启动,只有本地的一个API,那么如何新增API,如何配置swagger和代理远程的服务,我们继续

新建目录/data/api,在当前目录下维护本地mock数据和配置文件

docker run -d -p 9000:9000 -v /data/api/mock:/app/mock -v data/api/mock.config.json:/app/mock.config.json lvpf/dev-mock-cli

只需要更新mock文件夹下的json数据即可操作API