Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

egg-leaf

Package Overview
Dependencies
Maintainers
3
Versions
13
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

egg-leaf - npm Package Compare versions

Comparing version 1.5.3 to 1.6.0

46

app/middleware/leaf.js

@@ -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)

2

package.json
{
"name": "egg-leaf",
"version": "1.5.3",
"version": "1.6.0",
"description": "Mock server for egg",

@@ -5,0 +5,0 @@ "eggPlugin": {

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc