Giter Club home page Giter Club logo

api-mocker's Introduction

接口管理系统 - API Mocker

Hi all,好久不见!API Mocker重新开始维护了,也许不会太快,但一直都在~

API Mocker,不仅仅是Mocker,它致力于解决前后端开发协作过程中出现的各类问题,提高开发效率,对接口做统一管理,同时也能为后续的迭代维护提供更便捷的方式。

目录

系统功能

接口约定

API Mocker提供操作简单但功能丰富的接口编辑,接口约定者能方便的设置接口的各类信息,其中:

  • 结构化的接口参数输入与输出
    • 支持不同维度的请求参数约定(querybodypath, header
    • 支持参数的类型约定(stringnumberbooleanobjectarray
    • 参数备注、示例
  • 支持Json数据逆向生成参数结构
  • 保留一定接口历史记录

Mock数据

API Mocker认为,mock不仅仅体现在返回数据中,mock请求本身也应该符合接口约定。因此,我们提供更加符合业务场景的mock服务。

  • Mock请求会根据接口约定,自动生成假数据。
  • Mock请求会根据接口约定,对请求参数做校验。(校验是否选填、参数类型是否正确)。
  • 根据接口约定与用户设置,能模拟不同的网络响应状态,如:200404500
  • 支持接口代理,代理mock请求到线上or测试地址,避免开发环境跨域,省下Charles代理过程。
  • 支持Mock.js语法。

接口文档

API Mocker会根据接口约定自动生成简洁优美、结构清晰的接口文档。同时,可以订阅接口,当接口发生变化时,及时收到邮件通知;也可以按组订阅接口,当组内接口发生修改、增加、删除时,能收到相应的邮件通知。

接口权限

API Mocker提供简单易用的权限控制。

  • 组级别、API级别控制
  • 可见性、可写性权限控制

其他功能点

  • 接口测试。开发人员可以在系统上直接测试接口,避免在postman等请求工具上又填写众多参数
  • 便捷的接口搜索。(可以按接口地址、管理员等维度搜索接口)
  • 数据呈现
  • 支持rap的导入

技术栈与第三方库

  • ES6
  • ESLint (Standard)
  • Ramda

Client

Server


安装与启动

安装依赖外部命令(mongod)

  • make install

同时该命令也会确保mongod的启动,如果未启动会在本地建立db目录,并启动mongod. 如果服务器新开机可重新执行make install确保数据库启动.

开发启动(dev)

Client

  • make client 或者 cd client && npm install && npm run dev

Server

  • make server 或者 cd server && npm install && npm run dev

发布启动(prods)

Client

  • make prod_client 或者 cd client && npm install && npm run build

Server

  • make prod_server 或者 cd server && npm install && npm start

默认端口号为7001


部署相关

环境要求

"node": ">= 6.0.0",
"npm": ">= 3.0.0"

Config - Server

需要https支持的话,证书路径的配置在入口 server/index.js 中。其他服务端的配置都在 server/config目录下。其中:

clientRoot: 客户端地址,目前仅在发送提示邮件中有作用;

transporter: 邮件推送的相关配置,请设置自己推送邮箱。有些邮件服务商安全策略比价高,会发生推送错误,请自行充分测试。如果未配置,则系统不推送提示邮件;

pushInterval: 邮件推送的时间间隔配置。目前仅只针对api修改这一项配置;

mongoose: mongoDB的相关配置;

bodyParser.jsonLimit: 接口请求信息最大限制。因为接口更新时,数据量比较大,所以得设置一下。

其他相关配置,请看文件注释以及egg.js文档中相关内容

Config - Client

客户端配置在 client/config/index.js 中。其中:

docsUrl: 文档地址,可自行配置到自己的git仓库;

serverRoot: 服务端接口根路径;

assetsPublicPath: 静态文件公共路径,所有的静态文件资源地址,以此路径开头。请根据自身发布需求配置。

其他相关配置,请参考vue-cli脚手架webpack模板

以nginx做部署步骤示例

假设api-mocker项目文件夹路径为 __api_mocker_path

  1. 发布启动(prods),请参考上述执行命令
  2. nginx添加配置项:
server {

        listen       80;
        server_name  localhost;
        
        location / {
            root __api_mocker_path/client/dist/;
            index index.html index.htm test-vue.html;
            try_files $uri $uri/ /index.html =404;
        }

        location /mock-api/ {
            proxy_pass http://127.0.0.1:7001/;
        }
}
  1. 访问http://localhost

api-mocker's People

Contributors

atian25 avatar dependabot[bot] avatar foxpsd avatar guchongxi avatar lndj avatar lusq010 avatar wangqiang0121 avatar wuomzfx avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

api-mocker's Issues

cd server && cnpm install && npm run dev

/home/app/api-mocker/server/node_modules/_egg-core@4.4.0@egg-core/lib/egg.js:239
const closeFunction = async () => {
^

SyntaxError: Unexpected token (
at createScript (vm.js:56:10)
at Object.runInThisContext (vm.js:97:10)
at Module._compile (module.js:542:28)
at Object.Module._extensions..js (module.js:579:10)
at Module.load (module.js:487:32)
at tryModuleLoad (module.js:446:12)
at Function.Module._load (module.js:438:3)
at Module.require (module.js:497:17)
at require (internal/module.js:20:19)
at Object. (/home/app/api-mocker/server/node_modules/_egg-core@4.4.0@egg-core/index.js:3:17)
⚠️ Error: /home/app/api-mocker/server/node_modules/_egg-bin@4.3.7@egg-bin/lib/start-cluster {"baseDir":"/home/app/api-mocker/server","framework":"/home/app/api-mocker/server/node_modules/egg","workers":1} exit with code 1
⚠️ Command Error, enable DEBUG=common-bin for detail

server工程启动不了?npm ERR! [email protected] dev: `egg-bin dev`

#大神,这个错遇到过么
2019-06-13 17:05:55,906 ERROR 30841 [master] agent_worker#1:30849 start fail, exiting with code:1
2019-06-13 17:05:55,906 ERROR 30841 [master] exit with code:1
{ Error: /usr/local/api-mocker-dev/server/node_modules/egg-bin/lib/start-cluster {"workers":1,"baseDir":"/usr/local/api-mocker-dev/server","framework":"/usr/local/api-mocker-dev/server/node_modules/egg"} exit with code 1
at ChildProcess.proc.once.code (/usr/local/api-mocker-dev/server/node_modules/common-bin/lib/helper.js:56:21)
at Object.onceWrapper (events.js:286:20)
at ChildProcess.emit (events.js:198:13)
at Process.ChildProcess._handle.onexit (internal/child_process.js:248:12) code: 1 }
npm ERR! code ELIFECYCLE
npm ERR! errno 1
npm ERR! [email protected] dev: egg-bin dev
npm ERR! Exit status 1
npm ERR!
npm ERR! Failed at the [email protected] dev script.
npm ERR! This is probably not a problem with npm. There is likely additional logging output above.

npm ERR! A complete log of this run can be found in:
npm ERR! /root/.npm/_logs/2019-06-13T09_05_55_924Z-debug.log

导入 RapJSON 提示格式错误

Rap 更新了,数据结构不匹配,导入 Rap JSON 出现格式错误。简单修改了下(FromRap.vue),可以导入API 描述,但是参数和响应都没有:

      moduleList = json.data.modules[0]
      const apis = []
      moduleList.interfaces.forEach(action => {
        window.console.log(action)
        const apiName = `${json.data.name}-${action.name}`
        const api = new ApiInit()
        api.name = apiName
        api.desc = action.description
        api.prodUrl = action.url
        api.group = this.group._id
        const requestType = Number(action.method)
        api.options.method = this.methods[requestType - 1]
        const requests = action.properties.filter(p => p.scope === 'request')
        const responses = action.properties.filter(p => p.scope === 'response')
        api.options.params = this.buildReqParams(api.options.params, requests, requestType)
        api.options.response = this.buildResponse(responses)
        apis.push(api)
      })

cd server && cnpm install && npm run dev

2018-11-21 15:51:30,743 ERROR 6868 nodejs.AppWorkerDiedError: [master] app_worker#2:6889 died (code: 0, signal: null, suicide: false, state: dead), current workers: ["1","3","4"]
at Master.onAppExit (/www/wwwroot/server.api.xxx.com/server/node_modules/egg-cluster/lib/master.js:398:21)
at Master.emit (events.js:182:13)
at Messenger.sendToMaster (/www/wwwroot/server.api.xxx.com/server/node_modules/egg-cluster/lib/utils/messenger.js:133:17)
at Messenger.send (/www/wwwroot/server.api.xxx.com/server/node_modules/egg-cluster/lib/utils/messenger.js:98:12)
at EventEmitter.cluster.on (/www/wwwroot/server.api.xxx.com/server/node_modules/egg-cluster/lib/master.js:267:22)
at EventEmitter.emit (events.js:187:15)
at ChildProcess.worker.process.once (internal/cluster/master.js:193:13)
at Object.onceWrapper (events.js:273:13)
at ChildProcess.emit (events.js:182:13)
at Process.ChildProcess._handle.onexit (internal/child_process.js:240:12)
name: "AppWorkerDiedError"
pid: 6868
hostname: VM_161_185_centos

2018-11-21 15:51:30,745 ERROR 6868 [master] app_worker#2:6889 start fail, exiting with code:1
2018-11-21 15:51:30,745 ERROR 6868 [master] exit with code:1
2018-11-21 15:51:30,752 ERROR 6877 [agent_worker] receive disconnect event on child_process fork mode, exiting with code:110

[root@VM_161_185_centos server]# node -v
v10.13.0
[root@VM_161_185_centos server]# npm -v
6.4.1

mongod安全模式问题

使用安全模式启动MongoDB(mongod --auth)后,修改server/config/config.default.js中的mongoose内容,原内容:

mongoose: {
     url: 'mongodb://127.0.0.1/apimock'
 }

修改后的内容:

mongoose: {
        url: 'mongodb://apimock:[email protected]/apimock'
 }

重新启动server,提示登录失败:undefined,是不能使用安全模式启动MongoDB么?

May the Api-Mocker list data could be sorted by name?

Such as the "接口文档" page, the list data are always sorted by the last modify time. It is too hard to find the API document which I badly needed. Please provide this new feature and make it works in a new version, thank you very much!

希望支持超级管理员用户以及可选关闭注册

这是我所能找到最适合我们需求的API管理工具
因为我们通过网络协作开发,所以该项目被我部署于公网上
但是目前而言,因为只要能访问到就可以随意注册带来了一些安全性问题

增加状态码 405

default
这个可以优化一下
后端这边一般是用 405 (405 Method Not Allowed)
default
可以更加直观了解问题在哪里出现的。

请求mocker时,一直返回验证不通过

image

如上图api接口的定义

通过postman打接口,如下图一直返回参数校验失败
image

最奇怪的是,如果把接口定义的必选都去掉,就正常了!

不知道问题出在哪,烦请指教。

接口列表、接口文档中组的排序优化问题

前提是这样的:进入系统后,经常需要进行搜索或者下拉列表才能找到自己想编辑的组
image
请问是否可以增加一个支持:比如对自己曾经更改过组内接口的组、自己关系的组(订阅功能)进行一个置顶,便于查找。

Respone Schema 类型同步问题

Schema已经编写保存,当再次更新时,只修改一个字段的类型,Schema to Example并不能更新Example中的字段类型,需要重新修改一下Schema中改字段的内容,即使内容一样。所以考虑更新字段的同时,保持Example同步更新。

关于 Mock 生成的 Url

感谢作者提供这么好的工具,有个问题想请教下:

mock 生成的url 是不确定,这个就导致我在开发的过程中不能和后端的api保持一致,
更倾向于 一个接口分组下的 生成的 mock 地址是可以 自己配置的:
group_basic_url/customize_url
这样就可以保证 之前代码的接口地址不做改动也能很好整合。

按group订阅功能

提供订阅整个group的功能
1.group内新增、删除、修改接口可以得到提醒

对依赖引入的建议

  "dependencies": {
    "blueimp-md5": "2.7.0",
    "egg": "1.8.0",
    "egg-cors": "1.0.0",
    "egg-mongoose": "1.1.1",
    "egg-redis": "1.0.1",
    "egg-validate": "1.0.0",
    "http-assert": "1.2.0",
    "lru-cache": "4.1.1",
    "mocker-dsl-core": "0.1.2",
    "moment": "2.18.1",
    "nodemailer": "4.0.1",
    "path-to-regexp": "1.7.0",
    "ramda": "0.23.0"
  },

建议 egg 的依赖都改为 ^ 的方式,我们会严格遵循 Semver 规则。
而且写死版本是没有用的,因为底层的依赖一样会用最新的。

如何进行接口测试

其他功能点
· 接口测试。开发人员可以在系统上直接测试接口,避免在postman等请求工具上又填写众多参数

创建接口后,没有地方进行接口测试啊,怎么测试接口?

Post 传值失败

通过body中设置参数
{
"shartoken": 50067
}
测试dev模式,发现这个参数不能传递到api服务端,通过postman工具同样的参数是可以请求到数据的

请求 Body 不能是数组吗?

可以理解 form 数据是 key-value 存储的;

但是 json 数据的话,是支持整个数据为一个 array 的;
例如:
jietu20171010-201131

优化Nginx配置

假设api-mocker项目文件夹路径为__api_mocker_path,网络域名为your-mocker.com
官方给的Nginx配置,需要访问http://your-mocker.com/mock,一般情况下,为了方便,都是直接访问域名,可以修改Nginx配置如下:
1、

server {
        listen       80;
        server_name  your-mocker.com;
        root   __api_mocker_path/client/dist;
        index  index.html;

        location /mock-api/ {
            proxy_pass http://127.0.0.1:7001/;
        }
}

2、修改client/config/index.js文件下的serverRoot,改为your-mocker.com/mock-api
3、访问your-mocker.com

希望予以采纳。

mongo导致启动失败

cd server
npm run dev报下面的错误
name: 'MongoError'
message: '[egg-mongoose]failed to connect to server [127.0.0.1:27017] on first connect [MongoError: connect ECONNREFUSED 127.0.0.1:27017]'

mock如何生成一个随机时间,或随机的中文等?

众所周知
// example值为:
{
"status|1-2": true,
"number|1-100": 100
}
// 则会生成
{
"status": false,
"number": 40
}
那么如何生成一个随机时间?
是这样
{
"time|Random.time()": ""
}
还是
{
"time": Random.time()
}
上面两种方法都不行,生成的都是“Random.time()”字符串,期望得到的应该是一个随机时间,如"10:58:21"

资源请求失败

问题描述
隔一段时间就会出现白屏一直加载的情况,查看network发现是一个vendor.js请求时间超长,一直请求到分钟级别而后控制台报错net::ERR_CONNECTION_RESET 200

尝试过的措施

  • 服务端nginx重启
  • 服务端pm2重启
  • 服务端项目根目录make install重新启动mongodb

最后一步有效,但是该问题周期性出现,不知道根源在哪。

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.