Comparing version 1.5.3 to 1.6.0
@@ -8,10 +8,24 @@ 'use strict'; | ||
const Mock = require('mockjs'); | ||
module.exports = () => { | ||
const RouteParser = require('route-parser'); | ||
module.exports = appConfig => { | ||
const { apiDir = '', enable } = appConfig || {}; | ||
const mockPathList = getAllMockPathList(apiDir); | ||
return function* leafMiddleware(next) { | ||
const { apiDir = '', enable } = this.app.config.leaf; | ||
if (!enable) return yield next; | ||
const apiFilePath = getApiFilePath(this.path, this.method, apiDir); | ||
let matchResult = null; | ||
mockPathList.forEach(mockPath => { | ||
const routeParser = new RouteParser(mockPath); | ||
if (routeParser.match(this.path)) { | ||
matchResult = mockPath; | ||
return null; | ||
} | ||
}); | ||
if (matchResult === null) { | ||
console.log(chalk.red(`api文件不存在, apiPath: ${this.path}`)); | ||
return yield next; | ||
} | ||
const apiFilePath = getApiFilePath(matchResult, this.method, apiDir); | ||
if (!fsExistsSync(apiFilePath)) { | ||
@@ -92,3 +106,25 @@ console.log(chalk.red(`api文件不存在, apiFilePath: ${apiFilePath}`)); | ||
/** | ||
* @description 返回所有的mock文件与URL映射的path列表 | ||
* @param {string } mockFilePath mock文件目录 | ||
* @return {Array} 所有的mock文件路径列表 | ||
*/ | ||
function getAllMockPathList(mockFilePath) { | ||
return (function getMockPath(dir) { | ||
let results = []; | ||
const fileNameOrDirName = fs.readdirSync(dir); | ||
fileNameOrDirName.forEach(function(file) { | ||
const fileDir = dir + '/' + file; | ||
const stat = fs.statSync(fileDir); | ||
if (stat && stat.isDirectory()) { | ||
results = results.concat(getMockPath(fileDir)); | ||
} else { | ||
results.push(fileDir.replace(mockFilePath, '').replace(/\..+\..+$/, '')); | ||
} | ||
}); | ||
return results; | ||
}(mockFilePath)); | ||
} | ||
/** | ||
@@ -95,0 +131,0 @@ * 检查是 api 文件是否存在 |
@@ -5,2 +5,12 @@ # Change Log | ||
<a name="1.6.0"></a> | ||
# [1.6.0](https://github.com/forsigner/egg-leaf/compare/v1.5.3...v1.6.0) (2018-04-11) | ||
### Features | ||
* 添加路径参数支持 ([0ce0c34](https://github.com/forsigner/egg-leaf/commit/0ce0c34)) | ||
<a name="1.5.3"></a> | ||
@@ -7,0 +17,0 @@ ## [1.5.3](https://github.com/forsigner/egg-leaf/compare/v1.5.2...v1.5.3) (2018-02-02) |
{ | ||
"name": "egg-leaf", | ||
"version": "1.5.3", | ||
"version": "1.6.0", | ||
"description": "Mock server for egg", | ||
@@ -5,0 +5,0 @@ "eggPlugin": { |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
New author
Supply chain riskA new npm collaborator published a version of the package for the first time. New collaborators are usually benign additions to a project, but do indicate a change to the security surface area of a package.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
9424
190
1