@x-9lab/xlab 1.3.1 → 1.4.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/router.js +33 -10
- package/package.json +1 -1
package/dist/router.js
CHANGED
|
@@ -16,6 +16,7 @@ function _interopRequireDefault(obj) {
|
|
|
16
16
|
default: obj
|
|
17
17
|
};
|
|
18
18
|
}
|
|
19
|
+
/**API 服务目录 */ const API_BUSINESS_DIR = "business";
|
|
19
20
|
/**生成 api */ function buildApi(api) {
|
|
20
21
|
if (!api.startsWith("/")) {
|
|
21
22
|
api = `/${api}`;
|
|
@@ -36,15 +37,18 @@ function _interopRequireDefault(obj) {
|
|
|
36
37
|
if (stats.isDirectory() && item.startsWith("$")) {
|
|
37
38
|
// 接口级别中间件
|
|
38
39
|
let middlewaresFiles = _fs.default.readdirSync(tmpPath);
|
|
40
|
+
let mws = [];
|
|
39
41
|
middlewaresFiles.forEach(function(name) {
|
|
40
42
|
let tmpMwPath = _path.default.join(tmpPath, name);
|
|
41
43
|
let fileStat = _fs.default.statSync(tmpMwPath);
|
|
42
44
|
if (!fileStat.isDirectory() && !item.startsWith(".") && !item.endsWith(".d.ts")) {
|
|
43
|
-
|
|
45
|
+
mws.push(tmpMwPath);
|
|
44
46
|
}
|
|
45
47
|
});
|
|
46
48
|
// 简单做下排序
|
|
47
|
-
|
|
49
|
+
mws.sort();
|
|
50
|
+
const partPath = dir.split(API_BUSINESS_DIR)[1] || _path.default.posix.sep;
|
|
51
|
+
middlewares[partPath] = mws;
|
|
48
52
|
}
|
|
49
53
|
if (stats.isDirectory() && !item.startsWith("@") && !item.startsWith("$")) {
|
|
50
54
|
walk(tmpPath, middlewares, callback);
|
|
@@ -60,11 +64,12 @@ function _interopRequireDefault(obj) {
|
|
|
60
64
|
var mod = require(_path.default.resolve(modPath));
|
|
61
65
|
if (mod) {
|
|
62
66
|
mod = (0, _common.getRealDefaultMod)(mod);
|
|
63
|
-
let api_path = modPath.split(
|
|
67
|
+
let api_path = modPath.split(API_BUSINESS_DIR);
|
|
64
68
|
let api;
|
|
69
|
+
let apiPart;
|
|
65
70
|
let modName;
|
|
66
71
|
// 根据路径生成 url 规则
|
|
67
|
-
api = api_path[1].replace(".js", "").split(
|
|
72
|
+
api = api_path[1].replace(".js", "").split(_path.default.posix.sep);
|
|
68
73
|
// 文件名是 index
|
|
69
74
|
const lastIsIndex = api[api.length - 1] === "index";
|
|
70
75
|
// 文件名是 index 的则会被从 api 上先强行去掉
|
|
@@ -77,9 +82,11 @@ function _interopRequireDefault(obj) {
|
|
|
77
82
|
if (!lastIsIndex) {
|
|
78
83
|
api.pop();
|
|
79
84
|
}
|
|
85
|
+
apiPart = api;
|
|
80
86
|
api = buildApi(api.join("/"));
|
|
81
87
|
mod.unshift(api);
|
|
82
88
|
} else {
|
|
89
|
+
apiPart = api;
|
|
83
90
|
// 不是数组则把文件当作正常的 api 地址
|
|
84
91
|
api = buildApi(api.join("/"));
|
|
85
92
|
}
|
|
@@ -87,6 +94,10 @@ function _interopRequireDefault(obj) {
|
|
|
87
94
|
if ((0, _utils.isObject)(mod) && ((0, _utils.isFunction)(mod.handler) || (0, _utils.isAsyncFunction)(mod.handler))) {
|
|
88
95
|
modName = mod.method || "get";
|
|
89
96
|
api = mod.api || api;
|
|
97
|
+
apiPart = api.split("/");
|
|
98
|
+
if (apiPart[0] !== "") {
|
|
99
|
+
apiPart.unshift("");
|
|
100
|
+
}
|
|
90
101
|
// 保证一定是以 api 开头的
|
|
91
102
|
if (!IS_API_REGEXP.test(api)) {
|
|
92
103
|
api = buildApi(api);
|
|
@@ -104,6 +115,18 @@ function _interopRequireDefault(obj) {
|
|
|
104
115
|
];
|
|
105
116
|
}
|
|
106
117
|
}
|
|
118
|
+
var dirMws = [];
|
|
119
|
+
var prevPart = "";
|
|
120
|
+
for(let i = 0; i < apiPart.length; i++){
|
|
121
|
+
if (!apiPart[i] || apiPart[i] === "api") {
|
|
122
|
+
continue;
|
|
123
|
+
}
|
|
124
|
+
const part = `${prevPart}${_path.default.posix.sep}${apiPart[i]}`;
|
|
125
|
+
if (middlewares[part]) {
|
|
126
|
+
dirMws = dirMws.concat(middlewares[part]);
|
|
127
|
+
}
|
|
128
|
+
prevPart = part;
|
|
129
|
+
}
|
|
107
130
|
// 直接返回函数则直接绑定
|
|
108
131
|
if ((0, _utils.isFunction)(mod) || (0, _utils.isAsyncFunction)(mod)) {
|
|
109
132
|
mod = [
|
|
@@ -117,8 +140,8 @@ function _interopRequireDefault(obj) {
|
|
|
117
140
|
modName = "get";
|
|
118
141
|
}
|
|
119
142
|
}
|
|
120
|
-
if (
|
|
121
|
-
let mws =
|
|
143
|
+
if (dirMws && dirMws.length) {
|
|
144
|
+
let mws = dirMws.map((name)=>(0, _common.getRealDefaultMod)(require(name)));
|
|
122
145
|
mod.splice.apply(mod, [
|
|
123
146
|
1,
|
|
124
147
|
0,
|
|
@@ -131,12 +154,12 @@ function _interopRequireDefault(obj) {
|
|
|
131
154
|
}
|
|
132
155
|
};
|
|
133
156
|
// 业务基本目录
|
|
134
|
-
var _p = _path.default.resolve(__dirname,
|
|
135
|
-
walk(_p,
|
|
157
|
+
var _p = _path.default.resolve(__dirname, API_BUSINESS_DIR);
|
|
158
|
+
walk(_p, {}, processBusiness);
|
|
136
159
|
// 支持自定义 api
|
|
137
|
-
_p = _path.default.resolve(process.cwd(), _defaultXConfig.default.businessDir,
|
|
160
|
+
_p = _path.default.resolve(process.cwd(), _defaultXConfig.default.businessDir, API_BUSINESS_DIR);
|
|
138
161
|
if (process.cwd() !== __dirname && (0, _common.checkFileStat)(_p)) {
|
|
139
|
-
walk(_p,
|
|
162
|
+
walk(_p, {}, processBusiness);
|
|
140
163
|
}
|
|
141
164
|
processBusiness = null;
|
|
142
165
|
console.log("");
|