-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
杨磊
committed
Feb 1, 2019
1 parent
52ca59f
commit 3aa87c0
Showing
17 changed files
with
1,146 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
.vscode/ | ||
node_modules/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
<div align="center"> | ||
<h1>smock-core</h1> | ||
<p>SMock的核心代码,smock run的主要逻辑</p> | ||
</div> | ||
|
||
### 安装 | ||
|
||
```bash | ||
npm install smock-core --save-dev | ||
``` | ||
|
||
### 调用 | ||
|
||
```bash | ||
let smockCore = require('smock-core'); | ||
smockCore.init({ | ||
host:'xxx.xxx.xx.xx', | ||
domain:'xxx.com', | ||
projectName:'test' | ||
}) | ||
``` | ||
|
||
### 参数说明 | ||
|
||
|Attributes|forma|describe | ||
|---|---|---| | ||
|host| string| 需要mock的文档地址ip或者域名 | ||
|domain|string| 需要mock的文档访问域名。一般和host配合使用,如果文档是IP不能直接访问的形式,那么此处需要传入相应的值 | ||
|path|string| 需要mock的文档数据请求路径,在swagger文档页面可以找到,如:/v2/api-docs | ||
|port| integer| 需要mock的文档地址端口号, 默认80,如果协议配置为https,此参数则变为443 | ||
|projectName| string| 项目名,默认值swaggermock | ||
|mockPort| string| 本地mock服务启动后的端口,默认为3000 | ||
|customProtocol| string| swagger文档支持的协议请求 http/https |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
/** | ||
* 数据字典 | ||
* author: liaoyanli | ||
*/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
/** | ||
* 文件 | ||
* author: liaoyanli | ||
*/ | ||
let fs = require('fs'); | ||
let utils = require('./utils'); | ||
//文件是否存在 | ||
function existsSync(url) { | ||
return fs.existsSync(url) | ||
} | ||
|
||
//读文件 | ||
function readFileSync(url) { | ||
return fs.readFileSync(url); | ||
} | ||
|
||
//创建目录 | ||
function createDir(dir) { | ||
var stat = fs.existsSync(dir); | ||
if (!stat) { //为true的话那么存在,如果为false不存在 | ||
fs.mkdirSync(dir); | ||
} | ||
} | ||
|
||
//创建文件并写入内容(异步) | ||
function makeFile(filePath, content) { | ||
return new Promise((resolve, reject) => { | ||
var stat = existsSync(filePath); | ||
if (stat) { //为true的话那么存在,如果为false不存在 | ||
// utils.log(`${filePath} 已存在,内容已覆盖`); | ||
} | ||
fs.writeFile(filePath, content, (err) => { | ||
if (!err) { | ||
resolve(filePath); | ||
} else { | ||
reject(err); | ||
} | ||
}); | ||
}); | ||
} | ||
|
||
//创建文件并写入内容(同步无回调) | ||
function makeFileSync(filePath, content, resolve, reject) { | ||
var stat = existsSync(filePath); | ||
if (stat) { //为true的话那么存在,如果为false不存在 | ||
utils.log(`${filePath} 已存在,内容已覆盖`); | ||
} | ||
// else { | ||
fs.writeFileSync(filePath, content); | ||
} | ||
|
||
module.exports = { | ||
existsSync, | ||
readFileSync, | ||
makeFileSync, | ||
makeFile, | ||
createDir | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
/** | ||
* 模拟数据动态化,引入mockjs.js来处理 | ||
* author: liaoyanli | ||
*/ | ||
let Mock = require('mockjs'); | ||
var Random = Mock.Random; | ||
function setString() { | ||
Random.word(3, 8); | ||
return Mock.mock('@word(3, 8)'); | ||
} | ||
|
||
function setBoolean() { | ||
Random.boolean(); | ||
return Mock.mock('@boolean'); | ||
} | ||
|
||
function setInteger() { | ||
Random.integer(1, 100); | ||
return Mock.mock('@integer(1, 100)') | ||
} | ||
|
||
module.exports = { | ||
setString, | ||
setBoolean, | ||
setInteger | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
/** | ||
* 命名空间 | ||
* author: liaoyanli | ||
*/ | ||
module.exports = { | ||
configName: 'SMock.json', //参数文件名 | ||
urlsRealName: 'urlsReal', //真实路径文件名 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
/** | ||
* path | ||
* author: liaoyanli | ||
*/ | ||
let path = require('path'); | ||
|
||
function join(a, b) { | ||
return path.resolve(a, b); | ||
} | ||
|
||
function join2(a, b, c) { | ||
return path.join(a, b, c); | ||
} | ||
module.exports = { | ||
join, | ||
join2 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,94 @@ | ||
/** | ||
* 服务相关express | ||
* author: liaoyanli | ||
*/ | ||
let utils = require('./utils'); | ||
let paramsNumRule = require('../swagger/param-num-rule'); | ||
let paramsTypeRule = require('../swagger/param-type-rule'); | ||
let express = require('express'); | ||
var bodyParser = require("body-parser"); | ||
let app = express(); | ||
app.use(bodyParser.urlencoded({ extended: false })); | ||
|
||
//待所有mock的json文件创建或者修改完成后,再启动server | ||
function runServer(port, cal) { | ||
app.listen(port, () => { | ||
if (cal) cal(); //启动成功 | ||
}); | ||
} | ||
|
||
//创建server链接 | ||
function createServer(url, mockDir, type, typecontent, GlobalDefinitions) { | ||
let requreMockJson = require(mockDir); | ||
let realUrl = utils.dealUrl(url); | ||
|
||
app[type](realUrl, function(req, res) { | ||
utils.log(' '); | ||
utils.log('--------------------------'); | ||
utils.log('请求时间:' + new Date()); | ||
utils.log('请求路径:' + url); | ||
utils.log('请求方式:' + type); | ||
let params = getParamByType(type, req); | ||
utils.log('请求参数:' + JSON.stringify(params)); | ||
// res.header('Content-type', 'application/json'); | ||
// res.header('Charset', 'utf8'); | ||
res.header("Access-Control-Allow-Origin", req.headers.origin); | ||
res.header('Access-Control-Allow-Credentials', true); //告诉客户端可以在HTTP请求中带上Cookie | ||
res.header("Access-Control-Allow-Headers", "Origin, No-Cache, X-Requested-With, If-Modified-Since, Pragma, " + | ||
"Last-Modified, Cache-Control, Expires, Content-Type, Content-Language, Cache-Control, X-E4M-With,X_FILENAME"); | ||
res.header("Access-Control-Allow-Methods", "PUT,POST,GET,DELETE,OPTIONS"); | ||
res.header("X-Powered-By", ' 3.2.1') | ||
res.header("Content-Type", "application/json;charset=utf-8"); | ||
// res.send(req.query.callback + '(' + JSON.stringify(requreMockJson) + ');'); | ||
|
||
//验证参数个数 | ||
typecontent.parameters = typecontent.parameters || []; | ||
|
||
let parametersArr = typecontent.parameters, | ||
urlParameArr = Object.keys(params), | ||
requiredParamsArr = []; | ||
|
||
let isNotBody = parametersArr.some(function(item) { | ||
return item.in !== 'body'; | ||
}) | ||
|
||
parametersArr.forEach(item => { | ||
item.required && requiredParamsArr.push(item.name); | ||
}); | ||
|
||
if (isNotBody) { | ||
if (!paramsNumRule.isInclude(requiredParamsArr, urlParameArr)) { | ||
res.send('缺少必输参数,请查看服务台日志'); | ||
return; | ||
} | ||
} | ||
|
||
if (!paramsTypeRule.validate(params, typecontent.parameters, GlobalDefinitions)) { | ||
res.send('有参数类型不正确,请查看服务台日志'); | ||
return; | ||
} | ||
res.send(JSON.stringify(requreMockJson)); | ||
}); | ||
} | ||
|
||
//根据类型取参数 | ||
function getParamByType(type, req) { | ||
let params = {}; | ||
type = type.toLowerCase(); | ||
switch (type) { | ||
case 'get': | ||
params = req.query; | ||
break; | ||
case 'post': | ||
params = req.body; | ||
break; | ||
default: | ||
break; | ||
} | ||
return params; | ||
} | ||
|
||
module.exports = { | ||
createServer, | ||
runServer | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
/** | ||
* 工具 | ||
* author: liaoyanli | ||
*/ | ||
|
||
function log(msg) { | ||
console.log(msg); | ||
}; | ||
//警告打印 | ||
function warn(msg) { | ||
console.log(`warn:${msg}`); | ||
}; | ||
|
||
function error(msg) { | ||
console.log(`error:${msg}`); | ||
}; | ||
|
||
//将buffer或者字符串转换成json串 | ||
function toJson(str) { | ||
return JSON.parse(str); | ||
}; | ||
|
||
//将对象变成格式化串 | ||
function toStr(json) { | ||
return JSON.stringify(json, null, 2); | ||
} | ||
|
||
//将含有花括号的url,替换成对应的数据 | ||
function dealUrl(url) { | ||
return url.replace(/\{.*?\}/g, function(d) { | ||
let key = d.substring(1, d.length - 1); | ||
return `:${key}`; | ||
}); //此正则很重要 | ||
} | ||
|
||
//数据查找路径,针对swagger返回数据的特殊处理 | ||
function queryData(hash) { | ||
let result = ''; | ||
result = (hash.substring(2, hash.length)).split('/'); | ||
return result[1]; | ||
} | ||
module.exports = { | ||
warn, | ||
log, | ||
toJson, | ||
toStr, | ||
error, | ||
dealUrl, | ||
queryData | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
{ | ||
"name": "smock-core", | ||
"version": "0.0.2", | ||
"description": "SMock核心源码", | ||
"main": "./swagger/swagger", | ||
"repository": { | ||
"type": "git", | ||
"url": "https://github.com/jdf2e/SMock.git" | ||
}, | ||
"keywords": [ | ||
"swagger", | ||
"mock", | ||
"swagger-mock", | ||
"node", | ||
"server", | ||
"smock" | ||
], | ||
"author": "jdc-fe", | ||
"license": "MIT", | ||
"dependencies": { | ||
"body-parser": "^1.18.3", | ||
"commander": "^2.19.0", | ||
"express": "^4.16.3", | ||
"mockjs": "^1.0.1-beta3", | ||
"request": "^2.87.0" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
/** | ||
* 参数处理 | ||
* author:liaoyanli | ||
*/ | ||
//默认参数 | ||
let defaultConfig = { | ||
host: '', //接口文档ip | ||
domain: '', //接口文档域名 | ||
port: 80, //接口文档端口 | ||
path: '/v2/api-docs', | ||
method: 'GET', | ||
projectName: 'swagger', //项目名称 | ||
mockPort: 3000, //模块数据服务端口 | ||
customProtocol: 'http', //接口文档协议 | ||
jsPath: '' //指定生成的URL文件创建路径,相对当前项目根目录 | ||
}; | ||
|
||
//处理参数 | ||
function dealConfig(c) { | ||
if (c.domain) { | ||
c.headers = { | ||
host: c.domain | ||
}; | ||
} | ||
if (c.customProtocol == 'https') { | ||
c.port = 443; | ||
} | ||
//mock文件夹名 | ||
c.mockDirName = `${c.projectName?c.projectName:defaultConfig.projectName}mock`; | ||
return Object.assign(defaultConfig, c); | ||
} | ||
|
||
//抛出用户默认参数,抛出处理后参数,如:header.host不需要用户自己再配置一遍 | ||
module.exports = { | ||
defaultConfig, | ||
dealConfig | ||
} |
Oops, something went wrong.