@modern-js/bff-core 1.21.6 → 1.21.7-beta.0
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/CHANGELOG.md +67 -14
- package/dist/js/modern/api.js +0 -9
- package/dist/js/modern/client/generate-client.js +2 -11
- package/dist/js/modern/client/result.js +2 -1
- package/dist/js/modern/errors/http.js +0 -7
- package/dist/js/modern/operators/http.js +0 -29
- package/dist/js/modern/router/constants.js +0 -2
- package/dist/js/modern/router/index.js +6 -66
- package/dist/js/modern/router/utils.js +1 -18
- package/dist/js/modern/types.js +0 -10
- package/dist/js/modern/utils/alias.js +6 -23
- package/dist/js/modern/utils/storage.js +0 -10
- package/dist/js/modern/utils/validate.js +4 -9
- package/dist/js/node/api.js +0 -14
- package/dist/js/node/client/generate-client.js +2 -19
- package/dist/js/node/client/index.js +0 -2
- package/dist/js/node/client/result.js +2 -5
- package/dist/js/node/errors/http.js +0 -11
- package/dist/js/node/index.js +0 -11
- package/dist/js/node/operators/http.js +0 -45
- package/dist/js/node/router/constants.js +0 -4
- package/dist/js/node/router/index.js +6 -80
- package/dist/js/node/router/utils.js +1 -28
- package/dist/js/node/types.js +0 -10
- package/dist/js/node/utils/alias.js +6 -35
- package/dist/js/node/utils/debug.js +0 -2
- package/dist/js/node/utils/index.js +0 -9
- package/dist/js/node/utils/meta.js +0 -2
- package/dist/js/node/utils/storage.js +0 -13
- package/dist/js/node/utils/validate.js +2 -17
- package/dist/types/router/constants.d.ts +0 -1
- package/dist/types/router/index.d.ts +0 -1
- package/package.json +12 -35
|
@@ -4,17 +4,11 @@ Object.defineProperty(exports, "__esModule", {
|
|
|
4
4
|
value: true
|
|
5
5
|
});
|
|
6
6
|
exports.createHttpOperator = exports.Trace = exports.SetHeaders = exports.Redirect = exports.Query = exports.Put = exports.Post = exports.Patch = exports.Params = exports.Option = exports.HttpCode = exports.Headers = exports.Head = exports.Get = exports.Delete = exports.Data = exports.Connect = void 0;
|
|
7
|
-
|
|
8
7
|
var _types = require("../types");
|
|
9
|
-
|
|
10
8
|
var _http = require("../errors/http");
|
|
11
|
-
|
|
12
9
|
function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); enumerableOnly && (symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; })), keys.push.apply(keys, symbols); } return keys; }
|
|
13
|
-
|
|
14
10
|
function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = null != arguments[i] ? arguments[i] : {}; i % 2 ? ownKeys(Object(source), !0).forEach(function (key) { _defineProperty(target, key, source[key]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)) : ownKeys(Object(source)).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } return target; }
|
|
15
|
-
|
|
16
11
|
function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
|
|
17
|
-
|
|
18
12
|
const validateInput = async (schema, input) => {
|
|
19
13
|
try {
|
|
20
14
|
return await schema.parseAsync(input);
|
|
@@ -22,20 +16,16 @@ const validateInput = async (schema, input) => {
|
|
|
22
16
|
const {
|
|
23
17
|
z: zod
|
|
24
18
|
} = require('zod');
|
|
25
|
-
|
|
26
19
|
if (error instanceof zod.ZodError) {
|
|
27
20
|
throw new _http.ValidationError(400, error.message);
|
|
28
21
|
}
|
|
29
|
-
|
|
30
22
|
throw error;
|
|
31
23
|
}
|
|
32
24
|
};
|
|
33
|
-
|
|
34
25
|
const createHttpOperator = method => {
|
|
35
26
|
return urlPath => {
|
|
36
27
|
return {
|
|
37
28
|
name: method,
|
|
38
|
-
|
|
39
29
|
metadata({
|
|
40
30
|
setMetadata
|
|
41
31
|
}) {
|
|
@@ -45,11 +35,9 @@ const createHttpOperator = method => {
|
|
|
45
35
|
method
|
|
46
36
|
});
|
|
47
37
|
}
|
|
48
|
-
|
|
49
38
|
};
|
|
50
39
|
};
|
|
51
40
|
};
|
|
52
|
-
|
|
53
41
|
exports.createHttpOperator = createHttpOperator;
|
|
54
42
|
const Get = createHttpOperator(_types.HttpMethod.Get);
|
|
55
43
|
exports.Get = Get;
|
|
@@ -69,17 +57,14 @@ const Option = createHttpOperator(_types.HttpMethod.Option);
|
|
|
69
57
|
exports.Option = Option;
|
|
70
58
|
const Head = createHttpOperator(_types.HttpMethod.Head);
|
|
71
59
|
exports.Head = Head;
|
|
72
|
-
|
|
73
60
|
const Data = schema => {
|
|
74
61
|
return {
|
|
75
62
|
name: _types.HttpMetadata.Data,
|
|
76
|
-
|
|
77
63
|
metadata({
|
|
78
64
|
setMetadata
|
|
79
65
|
}) {
|
|
80
66
|
setMetadata(_types.HttpMetadata.Data, schema);
|
|
81
67
|
},
|
|
82
|
-
|
|
83
68
|
async validate(helper, next) {
|
|
84
69
|
const {
|
|
85
70
|
inputs: {
|
|
@@ -91,22 +76,17 @@ const Data = schema => {
|
|
|
91
76
|
});
|
|
92
77
|
return next();
|
|
93
78
|
}
|
|
94
|
-
|
|
95
79
|
};
|
|
96
80
|
};
|
|
97
|
-
|
|
98
81
|
exports.Data = Data;
|
|
99
|
-
|
|
100
82
|
const Query = schema => {
|
|
101
83
|
return {
|
|
102
84
|
name: _types.HttpMetadata.Query,
|
|
103
|
-
|
|
104
85
|
metadata({
|
|
105
86
|
setMetadata
|
|
106
87
|
}) {
|
|
107
88
|
setMetadata(_types.HttpMetadata.Query, schema);
|
|
108
89
|
},
|
|
109
|
-
|
|
110
90
|
async validate(helper, next) {
|
|
111
91
|
const {
|
|
112
92
|
inputs: {
|
|
@@ -118,22 +98,17 @@ const Query = schema => {
|
|
|
118
98
|
});
|
|
119
99
|
return next();
|
|
120
100
|
}
|
|
121
|
-
|
|
122
101
|
};
|
|
123
102
|
};
|
|
124
|
-
|
|
125
103
|
exports.Query = Query;
|
|
126
|
-
|
|
127
104
|
const Params = schema => {
|
|
128
105
|
return {
|
|
129
106
|
name: _types.HttpMetadata.Params,
|
|
130
|
-
|
|
131
107
|
metadata({
|
|
132
108
|
setMetadata
|
|
133
109
|
}) {
|
|
134
110
|
setMetadata(_types.HttpMetadata.Params, schema);
|
|
135
111
|
},
|
|
136
|
-
|
|
137
112
|
async validate(helper, next) {
|
|
138
113
|
const {
|
|
139
114
|
inputs: {
|
|
@@ -145,22 +120,17 @@ const Params = schema => {
|
|
|
145
120
|
});
|
|
146
121
|
return next();
|
|
147
122
|
}
|
|
148
|
-
|
|
149
123
|
};
|
|
150
124
|
};
|
|
151
|
-
|
|
152
125
|
exports.Params = Params;
|
|
153
|
-
|
|
154
126
|
const Headers = schema => {
|
|
155
127
|
return {
|
|
156
128
|
name: _types.HttpMetadata.Headers,
|
|
157
|
-
|
|
158
129
|
metadata({
|
|
159
130
|
setMetadata
|
|
160
131
|
}) {
|
|
161
132
|
setMetadata(_types.HttpMetadata.Headers, schema);
|
|
162
133
|
},
|
|
163
|
-
|
|
164
134
|
async validate(helper, next) {
|
|
165
135
|
const {
|
|
166
136
|
inputs: {
|
|
@@ -172,12 +142,9 @@ const Headers = schema => {
|
|
|
172
142
|
});
|
|
173
143
|
return next();
|
|
174
144
|
}
|
|
175
|
-
|
|
176
145
|
};
|
|
177
146
|
};
|
|
178
|
-
|
|
179
147
|
exports.Headers = Headers;
|
|
180
|
-
|
|
181
148
|
const setResponseMeta = (helper, type, value) => {
|
|
182
149
|
const responseMetaData = helper.getMetadata(_types.HttpMetadata.Response) || [];
|
|
183
150
|
helper.setMetadata(_types.HttpMetadata.Response, [...responseMetaData, {
|
|
@@ -185,42 +152,30 @@ const setResponseMeta = (helper, type, value) => {
|
|
|
185
152
|
value
|
|
186
153
|
}]);
|
|
187
154
|
};
|
|
188
|
-
|
|
189
155
|
const HttpCode = statusCode => {
|
|
190
156
|
return {
|
|
191
157
|
name: 'HttpCode',
|
|
192
|
-
|
|
193
158
|
metadata(helper) {
|
|
194
159
|
setResponseMeta(helper, _types.ResponseMetaType.StatusCode, statusCode);
|
|
195
160
|
}
|
|
196
|
-
|
|
197
161
|
};
|
|
198
162
|
};
|
|
199
|
-
|
|
200
163
|
exports.HttpCode = HttpCode;
|
|
201
|
-
|
|
202
164
|
const SetHeaders = headers => {
|
|
203
165
|
return {
|
|
204
166
|
name: 'SetHeaders',
|
|
205
|
-
|
|
206
167
|
metadata(helper) {
|
|
207
168
|
setResponseMeta(helper, _types.ResponseMetaType.Headers, headers);
|
|
208
169
|
}
|
|
209
|
-
|
|
210
170
|
};
|
|
211
171
|
};
|
|
212
|
-
|
|
213
172
|
exports.SetHeaders = SetHeaders;
|
|
214
|
-
|
|
215
173
|
const Redirect = url => {
|
|
216
174
|
return {
|
|
217
175
|
name: 'Redirect',
|
|
218
|
-
|
|
219
176
|
metadata(helper) {
|
|
220
177
|
setResponseMeta(helper, _types.ResponseMetaType.Redirect, url);
|
|
221
178
|
}
|
|
222
|
-
|
|
223
179
|
};
|
|
224
180
|
};
|
|
225
|
-
|
|
226
181
|
exports.Redirect = Redirect;
|
|
@@ -4,19 +4,15 @@ Object.defineProperty(exports, "__esModule", {
|
|
|
4
4
|
value: true
|
|
5
5
|
});
|
|
6
6
|
exports.INDEX_SUFFIX = exports.FRAMEWORK_MODE_LAMBDA_DIR = exports.FRAMEWORK_MODE_APP_DIR = exports.AllHttpMethods = exports.API_FILE_RULES = exports.API_DIR = exports.APIMode = void 0;
|
|
7
|
-
|
|
8
7
|
var _types = require("../types");
|
|
9
|
-
|
|
10
8
|
const AllHttpMethods = Object.values(_types.HttpMethod);
|
|
11
9
|
exports.AllHttpMethods = AllHttpMethods;
|
|
12
10
|
let APIMode;
|
|
13
11
|
exports.APIMode = APIMode;
|
|
14
|
-
|
|
15
12
|
(function (APIMode) {
|
|
16
13
|
APIMode["FARMEWORK"] = "framework";
|
|
17
14
|
APIMode["FUNCTION"] = "function";
|
|
18
15
|
})(APIMode || (exports.APIMode = APIMode = {}));
|
|
19
|
-
|
|
20
16
|
const FRAMEWORK_MODE_LAMBDA_DIR = 'lambda';
|
|
21
17
|
exports.FRAMEWORK_MODE_LAMBDA_DIR = FRAMEWORK_MODE_LAMBDA_DIR;
|
|
22
18
|
const FRAMEWORK_MODE_APP_DIR = 'app';
|
|
@@ -7,19 +7,12 @@ var _exportNames = {
|
|
|
7
7
|
ApiRouter: true
|
|
8
8
|
};
|
|
9
9
|
exports.ApiRouter = void 0;
|
|
10
|
-
|
|
11
10
|
var _path = _interopRequireDefault(require("path"));
|
|
12
|
-
|
|
13
11
|
var _utils = require("@modern-js/utils");
|
|
14
|
-
|
|
15
12
|
require("reflect-metadata");
|
|
16
|
-
|
|
17
13
|
var _types = require("../types");
|
|
18
|
-
|
|
19
14
|
var _utils2 = require("../utils");
|
|
20
|
-
|
|
21
15
|
var _constants = require("./constants");
|
|
22
|
-
|
|
23
16
|
Object.keys(_constants).forEach(function (key) {
|
|
24
17
|
if (key === "default" || key === "__esModule") return;
|
|
25
18
|
if (Object.prototype.hasOwnProperty.call(_exportNames, key)) return;
|
|
@@ -31,11 +24,8 @@ Object.keys(_constants).forEach(function (key) {
|
|
|
31
24
|
}
|
|
32
25
|
});
|
|
33
26
|
});
|
|
34
|
-
|
|
35
27
|
var _utils3 = require("./utils");
|
|
36
|
-
|
|
37
28
|
var _types2 = require("./types");
|
|
38
|
-
|
|
39
29
|
Object.keys(_types2).forEach(function (key) {
|
|
40
30
|
if (key === "default" || key === "__esModule") return;
|
|
41
31
|
if (Object.prototype.hasOwnProperty.call(_exportNames, key)) return;
|
|
@@ -47,54 +37,40 @@ Object.keys(_types2).forEach(function (key) {
|
|
|
47
37
|
}
|
|
48
38
|
});
|
|
49
39
|
});
|
|
50
|
-
|
|
51
40
|
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
|
|
52
|
-
|
|
53
41
|
function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
|
|
54
|
-
|
|
55
42
|
class ApiRouter {
|
|
56
43
|
// lambdaDir is the dir which equal to the apiDir in function mode, and equal to the api/lambda dir in framework mode
|
|
44
|
+
|
|
57
45
|
constructor({
|
|
58
46
|
apiDir: _apiDir,
|
|
59
47
|
lambdaDir: _lambdaDir,
|
|
60
48
|
prefix
|
|
61
49
|
}) {
|
|
62
50
|
_defineProperty(this, "apiMode", void 0);
|
|
63
|
-
|
|
64
51
|
_defineProperty(this, "apiDir", void 0);
|
|
65
|
-
|
|
66
52
|
_defineProperty(this, "existLambdaDir", void 0);
|
|
67
|
-
|
|
68
53
|
_defineProperty(this, "lambdaDir", void 0);
|
|
69
|
-
|
|
70
54
|
_defineProperty(this, "prefix", void 0);
|
|
71
|
-
|
|
72
55
|
_defineProperty(this, "apiFiles", []);
|
|
73
|
-
|
|
74
56
|
_defineProperty(this, "getExactApiMode", apiDir => {
|
|
75
57
|
const exist = this.createExistChecker(apiDir);
|
|
76
58
|
const existLambdaDir = exist(_constants.FRAMEWORK_MODE_LAMBDA_DIR);
|
|
77
59
|
const existAppDir = exist(_constants.FRAMEWORK_MODE_APP_DIR);
|
|
78
60
|
const existAppFile = exist('app.ts') || exist('app.js');
|
|
79
|
-
|
|
80
61
|
if (existLambdaDir || existAppDir || existAppFile) {
|
|
81
62
|
return _constants.APIMode.FARMEWORK;
|
|
82
63
|
}
|
|
83
|
-
|
|
84
64
|
return _constants.APIMode.FUNCTION;
|
|
85
65
|
});
|
|
86
|
-
|
|
87
66
|
_defineProperty(this, "createExistChecker", base => target => _utils.fs.pathExistsSync(_path.default.resolve(base, target)));
|
|
88
|
-
|
|
89
67
|
_defineProperty(this, "getExactLambdaDir", apiDir => {
|
|
90
68
|
if (this.lambdaDir) {
|
|
91
69
|
return this.lambdaDir;
|
|
92
70
|
}
|
|
93
|
-
|
|
94
71
|
const lambdaDir = this.apiMode === _constants.APIMode.FARMEWORK ? _path.default.join(apiDir, _constants.FRAMEWORK_MODE_LAMBDA_DIR) : apiDir;
|
|
95
72
|
return lambdaDir;
|
|
96
73
|
});
|
|
97
|
-
|
|
98
74
|
this.validateAbsolute(_apiDir, 'apiDir');
|
|
99
75
|
this.validateAbsolute(_lambdaDir, 'lambdaDir');
|
|
100
76
|
this.prefix = this.initPrefix(prefix);
|
|
@@ -103,45 +79,34 @@ class ApiRouter {
|
|
|
103
79
|
this.lambdaDir = _lambdaDir || this.getExactLambdaDir(this.apiDir);
|
|
104
80
|
this.existLambdaDir = _utils.fs.existsSync(this.lambdaDir);
|
|
105
81
|
}
|
|
106
|
-
|
|
107
82
|
isExistLambda() {
|
|
108
83
|
return this.existLambdaDir;
|
|
109
84
|
}
|
|
110
|
-
|
|
111
85
|
getApiMode() {
|
|
112
86
|
return this.apiMode;
|
|
113
87
|
}
|
|
114
|
-
|
|
115
88
|
getLambdaDir() {
|
|
116
89
|
return this.lambdaDir;
|
|
117
90
|
}
|
|
118
|
-
|
|
119
91
|
isApiFile(filename) {
|
|
120
92
|
if (this.existLambdaDir) {
|
|
121
93
|
return false;
|
|
122
94
|
}
|
|
123
|
-
|
|
124
95
|
if (!this.apiFiles.includes(filename)) {
|
|
125
96
|
return false;
|
|
126
97
|
}
|
|
127
|
-
|
|
128
98
|
return true;
|
|
129
99
|
}
|
|
130
|
-
|
|
131
100
|
getSingleModuleHandlers(filename) {
|
|
132
101
|
const moduleInfo = this.getModuleInfo(filename);
|
|
133
|
-
|
|
134
102
|
if (moduleInfo) {
|
|
135
103
|
return this.getModuleHandlerInfos(moduleInfo);
|
|
136
104
|
}
|
|
137
|
-
|
|
138
105
|
return null;
|
|
139
106
|
}
|
|
140
|
-
|
|
141
107
|
getHandlerInfo(filename, originFuncName, handler) {
|
|
142
108
|
const httpMethod = this.getHttpMethod(originFuncName, handler);
|
|
143
109
|
const routeName = this.getRouteName(filename, handler);
|
|
144
|
-
|
|
145
110
|
if (httpMethod && routeName) {
|
|
146
111
|
return {
|
|
147
112
|
handler,
|
|
@@ -152,105 +117,80 @@ class ApiRouter {
|
|
|
152
117
|
routePath: this.getRoutePath(this.prefix, routeName)
|
|
153
118
|
};
|
|
154
119
|
}
|
|
155
|
-
|
|
156
120
|
return null;
|
|
157
|
-
}
|
|
158
|
-
|
|
121
|
+
}
|
|
159
122
|
|
|
123
|
+
// TODO: 性能提升,开发环境,判断下 lambda 目录修改时间
|
|
160
124
|
getSafeRoutePath(filename, handler) {
|
|
161
125
|
this.loadApiFiles();
|
|
162
126
|
this.validateValidApifile(filename);
|
|
163
127
|
return this.getRouteName(filename, handler);
|
|
164
128
|
}
|
|
165
|
-
|
|
166
129
|
getRouteName(filename, handler) {
|
|
167
130
|
if (handler) {
|
|
168
131
|
const trigger = Reflect.getMetadata(_types.OperatorType.Trigger, handler);
|
|
169
|
-
|
|
170
132
|
if (trigger && trigger.type === _types.TriggerType.Http) {
|
|
171
133
|
if (!trigger.path) {
|
|
172
134
|
throw new Error(`The http trigger ${trigger.name} needs to specify a path`);
|
|
173
135
|
}
|
|
174
|
-
|
|
175
136
|
return trigger.path;
|
|
176
137
|
}
|
|
177
138
|
}
|
|
178
|
-
|
|
179
139
|
const routePath = (0, _utils3.getPathFromFilename)(this.lambdaDir, filename);
|
|
180
140
|
return routePath;
|
|
181
141
|
}
|
|
182
|
-
|
|
183
142
|
getHttpMethod(originHandlerName, handler) {
|
|
184
143
|
if (handler) {
|
|
185
144
|
const trigger = Reflect.getMetadata(_types.OperatorType.Trigger, handler);
|
|
186
|
-
|
|
187
145
|
if (trigger && _types.httpMethods.includes(trigger.method)) {
|
|
188
146
|
return trigger.method;
|
|
189
147
|
}
|
|
190
148
|
}
|
|
191
|
-
|
|
192
149
|
const upperName = originHandlerName.toUpperCase();
|
|
193
|
-
|
|
194
150
|
switch (upperName) {
|
|
195
151
|
case 'GET':
|
|
196
152
|
return _types.HttpMethod.Get;
|
|
197
|
-
|
|
198
153
|
case 'POST':
|
|
199
154
|
return _types.HttpMethod.Post;
|
|
200
|
-
|
|
201
155
|
case 'PUT':
|
|
202
156
|
return _types.HttpMethod.Put;
|
|
203
|
-
|
|
204
157
|
case 'DELETE':
|
|
205
158
|
case 'DEL':
|
|
206
159
|
return _types.HttpMethod.Delete;
|
|
207
|
-
|
|
208
160
|
case 'CONNECT':
|
|
209
161
|
return _types.HttpMethod.Connect;
|
|
210
|
-
|
|
211
162
|
case 'TRACE':
|
|
212
163
|
return _types.HttpMethod.Trace;
|
|
213
|
-
|
|
214
164
|
case 'PATCH':
|
|
215
165
|
return _types.HttpMethod.Patch;
|
|
216
|
-
|
|
217
166
|
case 'OPTION':
|
|
218
167
|
return _types.HttpMethod.Option;
|
|
219
|
-
|
|
220
168
|
case 'DEFAULT':
|
|
221
169
|
{
|
|
222
170
|
return _types.HttpMethod.Get;
|
|
223
171
|
}
|
|
224
|
-
|
|
225
172
|
default:
|
|
226
173
|
_utils.logger.warn(`Only api handlers are allowd to be exported, please remove the function ${originHandlerName} from exports`);
|
|
227
|
-
|
|
228
174
|
return null;
|
|
229
175
|
}
|
|
230
176
|
}
|
|
231
|
-
|
|
232
177
|
loadApiFiles() {
|
|
233
178
|
if (!this.existLambdaDir) {
|
|
234
179
|
return [];
|
|
235
|
-
}
|
|
236
|
-
|
|
237
|
-
|
|
180
|
+
}
|
|
181
|
+
// eslint-disable-next-line no-multi-assign
|
|
238
182
|
const apiFiles = this.apiFiles = (0, _utils3.getFiles)(this.lambdaDir, _constants.API_FILE_RULES);
|
|
239
183
|
return apiFiles;
|
|
240
184
|
}
|
|
241
|
-
|
|
242
185
|
getApiFiles() {
|
|
243
186
|
if (!this.existLambdaDir) {
|
|
244
187
|
return [];
|
|
245
188
|
}
|
|
246
|
-
|
|
247
189
|
if (this.apiFiles.length > 0) {
|
|
248
190
|
return this.apiFiles;
|
|
249
191
|
}
|
|
250
|
-
|
|
251
192
|
return this.loadApiFiles();
|
|
252
193
|
}
|
|
253
|
-
|
|
254
194
|
getApiHandlers() {
|
|
255
195
|
const filenames = this.getApiFiles();
|
|
256
196
|
const moduleInfos = this.getModuleInfos(filenames);
|
|
@@ -258,30 +198,25 @@ class ApiRouter {
|
|
|
258
198
|
(0, _utils2.debug)('apiHandlers', apiHandlers.length, apiHandlers);
|
|
259
199
|
return apiHandlers;
|
|
260
200
|
}
|
|
201
|
+
|
|
261
202
|
/**
|
|
262
203
|
* 如果用户未传入或传入空串,默认为 /api
|
|
263
204
|
* 如果传入 /,则 prefix 为 /
|
|
264
205
|
*/
|
|
265
|
-
|
|
266
|
-
|
|
267
206
|
initPrefix(prefix) {
|
|
268
207
|
if (prefix === '/') {
|
|
269
208
|
return '';
|
|
270
209
|
}
|
|
271
|
-
|
|
272
210
|
return prefix || '/api';
|
|
273
211
|
}
|
|
274
|
-
|
|
275
212
|
validateAbsolute(filename, paramsName) {
|
|
276
213
|
if (typeof filename === 'string' && !_path.default.isAbsolute(filename)) {
|
|
277
214
|
throw new Error(`The ${paramsName} ${filename} is not a abolute path`);
|
|
278
215
|
}
|
|
279
216
|
}
|
|
280
|
-
|
|
281
217
|
getModuleInfos(filenames) {
|
|
282
218
|
return filenames.map(filename => this.getModuleInfo(filename)).filter(moduleInfo => Boolean(moduleInfo));
|
|
283
219
|
}
|
|
284
|
-
|
|
285
220
|
getModuleInfo(filename) {
|
|
286
221
|
try {
|
|
287
222
|
const module = (0, _utils3.requireHandlerModule)(filename);
|
|
@@ -298,12 +233,10 @@ class ApiRouter {
|
|
|
298
233
|
}
|
|
299
234
|
}
|
|
300
235
|
}
|
|
301
|
-
|
|
302
236
|
getHandlerInfos(moduleInfos) {
|
|
303
237
|
let apiHandlers = [];
|
|
304
238
|
moduleInfos.forEach(moduleInfo => {
|
|
305
239
|
const handlerInfos = this.getModuleHandlerInfos(moduleInfo);
|
|
306
|
-
|
|
307
240
|
if (handlerInfos) {
|
|
308
241
|
apiHandlers = apiHandlers.concat(handlerInfos);
|
|
309
242
|
}
|
|
@@ -311,7 +244,6 @@ class ApiRouter {
|
|
|
311
244
|
const sortedHandlers = (0, _utils3.sortRoutes)(apiHandlers);
|
|
312
245
|
return sortedHandlers;
|
|
313
246
|
}
|
|
314
|
-
|
|
315
247
|
getModuleHandlerInfos(moduleInfo) {
|
|
316
248
|
const {
|
|
317
249
|
module,
|
|
@@ -323,23 +255,17 @@ class ApiRouter {
|
|
|
323
255
|
return handlerInfo;
|
|
324
256
|
}).filter(handlerInfo => Boolean(handlerInfo));
|
|
325
257
|
}
|
|
326
|
-
|
|
327
258
|
validateValidApifile(filename) {
|
|
328
259
|
if (!this.apiFiles.includes(filename)) {
|
|
329
260
|
throw new Error(`The ${filename} is not a valid api file.`);
|
|
330
261
|
}
|
|
331
262
|
}
|
|
332
|
-
|
|
333
263
|
getRoutePath(prefix, routeName) {
|
|
334
264
|
const finalRouteName = routeName === '/' ? '' : routeName;
|
|
335
|
-
|
|
336
265
|
if (prefix === '' && finalRouteName === '') {
|
|
337
266
|
return '/';
|
|
338
267
|
}
|
|
339
|
-
|
|
340
268
|
return `${prefix}${finalRouteName}`;
|
|
341
269
|
}
|
|
342
|
-
|
|
343
270
|
}
|
|
344
|
-
|
|
345
271
|
exports.ApiRouter = ApiRouter;
|
|
@@ -4,22 +4,15 @@ Object.defineProperty(exports, "__esModule", {
|
|
|
4
4
|
value: true
|
|
5
5
|
});
|
|
6
6
|
exports.sortRoutes = exports.requireHandlerModule = exports.isHandler = exports.getPathFromFilename = exports.getFiles = void 0;
|
|
7
|
-
|
|
8
7
|
var _path = _interopRequireDefault(require("path"));
|
|
9
|
-
|
|
10
8
|
var _utils = require("@modern-js/utils");
|
|
11
|
-
|
|
12
9
|
var _constants = require("./constants");
|
|
13
|
-
|
|
14
10
|
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
|
|
15
|
-
|
|
16
11
|
const getFiles = (lambdaDir, rules) => _utils.globby.sync(rules, {
|
|
17
12
|
cwd: lambdaDir,
|
|
18
13
|
gitignore: true
|
|
19
14
|
}).map(file => _path.default.resolve(lambdaDir, file));
|
|
20
|
-
|
|
21
15
|
exports.getFiles = getFiles;
|
|
22
|
-
|
|
23
16
|
const getPathFromFilename = (baseDir, filename) => {
|
|
24
17
|
const relativeName = filename.substring(baseDir.length);
|
|
25
18
|
const relativePath = relativeName.split('.').slice(0, -1).join('.');
|
|
@@ -29,34 +22,25 @@ const getPathFromFilename = (baseDir, filename) => {
|
|
|
29
22
|
return `:${item.substring(1, item.length - 1)}`;
|
|
30
23
|
}
|
|
31
24
|
}
|
|
32
|
-
|
|
33
25
|
return item;
|
|
34
26
|
});
|
|
35
27
|
const name = nameSplit.join('/');
|
|
36
28
|
const finalName = name.endsWith(_constants.INDEX_SUFFIX) ? name.substring(0, name.length - _constants.INDEX_SUFFIX.length) : name;
|
|
37
29
|
return clearRouteName(finalName);
|
|
38
30
|
};
|
|
39
|
-
|
|
40
31
|
exports.getPathFromFilename = getPathFromFilename;
|
|
41
|
-
|
|
42
32
|
const clearRouteName = routeName => {
|
|
43
33
|
let finalRouteName = routeName.trim();
|
|
44
|
-
|
|
45
34
|
if (!finalRouteName.startsWith('/')) {
|
|
46
35
|
finalRouteName = `/${finalRouteName}`;
|
|
47
36
|
}
|
|
48
|
-
|
|
49
37
|
if (finalRouteName.length > 1 && finalRouteName.endsWith('/')) {
|
|
50
38
|
finalRouteName = finalRouteName.substring(0, finalRouteName.length - 1);
|
|
51
39
|
}
|
|
52
|
-
|
|
53
40
|
return finalRouteName;
|
|
54
41
|
};
|
|
55
|
-
|
|
56
42
|
const isHandler = input => input && typeof input === 'function';
|
|
57
|
-
|
|
58
43
|
exports.isHandler = isHandler;
|
|
59
|
-
|
|
60
44
|
const enableRegister = requireFn => {
|
|
61
45
|
// esbuild-register 做 unRegister 时,不会删除 register 添加的 require.extensions,导致第二次调用时 require.extensions['.ts'] 是 nodejs 默认 loader
|
|
62
46
|
// 所以这里根据第一次调用时,require.extensions 有没有,来判断是否需要使用 esbuild-register
|
|
@@ -68,56 +52,45 @@ const enableRegister = requireFn => {
|
|
|
68
52
|
existTsLoader = Boolean(require.extensions['.ts']);
|
|
69
53
|
firstCall = false;
|
|
70
54
|
}
|
|
71
|
-
|
|
72
55
|
if (!existTsLoader) {
|
|
73
56
|
const {
|
|
74
57
|
register
|
|
75
58
|
} = require('esbuild-register/dist/node');
|
|
76
|
-
|
|
77
59
|
const {
|
|
78
60
|
unregister
|
|
79
61
|
} = register({
|
|
80
|
-
extensions: ['.ts']
|
|
62
|
+
extensions: ['.ts', '.js']
|
|
81
63
|
});
|
|
82
64
|
const requiredModule = requireFn(modulePath);
|
|
83
65
|
unregister();
|
|
84
66
|
return requiredModule;
|
|
85
67
|
}
|
|
86
|
-
|
|
87
68
|
const requiredModule = requireFn(modulePath);
|
|
88
69
|
return requiredModule;
|
|
89
70
|
};
|
|
90
71
|
};
|
|
91
|
-
|
|
92
72
|
const isFunction = input => input && {}.toString.call(input) === '[object Function]';
|
|
93
|
-
|
|
94
73
|
const requireHandlerModule = enableRegister(modulePath => {
|
|
95
74
|
// 测试环境不走缓存,因为缓存的 h andler 文件,会被 mockAPI 函数进行 mock,升级 jest28,setupFilesAfterEnv 能做异步操作的话,可解此问题
|
|
96
75
|
const originRequire = process.env.NODE_ENV === 'test' ? jest.requireActual : require;
|
|
97
76
|
const module = originRequire(modulePath);
|
|
98
|
-
|
|
99
77
|
if (isFunction(module)) {
|
|
100
78
|
return {
|
|
101
79
|
default: module
|
|
102
80
|
};
|
|
103
81
|
}
|
|
104
|
-
|
|
105
82
|
return module;
|
|
106
83
|
});
|
|
107
84
|
exports.requireHandlerModule = requireHandlerModule;
|
|
108
|
-
|
|
109
85
|
const routeValue = routePath => {
|
|
110
86
|
if (routePath.includes(':')) {
|
|
111
87
|
return 11;
|
|
112
88
|
}
|
|
113
|
-
|
|
114
89
|
return 1;
|
|
115
90
|
};
|
|
116
|
-
|
|
117
91
|
const sortRoutes = apiHandlers => {
|
|
118
92
|
return apiHandlers.sort((handlerA, handlerB) => {
|
|
119
93
|
return routeValue(handlerA.routeName) - routeValue(handlerB.routeName);
|
|
120
94
|
});
|
|
121
95
|
};
|
|
122
|
-
|
|
123
96
|
exports.sortRoutes = sortRoutes;
|
package/dist/js/node/types.js
CHANGED
|
@@ -6,22 +6,17 @@ Object.defineProperty(exports, "__esModule", {
|
|
|
6
6
|
exports.httpMethods = exports.TriggerType = exports.ResponseMetaType = exports.OperatorType = exports.HttpMethod = exports.HttpMetadata = void 0;
|
|
7
7
|
let OperatorType;
|
|
8
8
|
exports.OperatorType = OperatorType;
|
|
9
|
-
|
|
10
9
|
(function (OperatorType) {
|
|
11
10
|
OperatorType[OperatorType["Trigger"] = 0] = "Trigger";
|
|
12
11
|
OperatorType[OperatorType["Middleware"] = 1] = "Middleware";
|
|
13
12
|
})(OperatorType || (exports.OperatorType = OperatorType = {}));
|
|
14
|
-
|
|
15
13
|
let TriggerType;
|
|
16
14
|
exports.TriggerType = TriggerType;
|
|
17
|
-
|
|
18
15
|
(function (TriggerType) {
|
|
19
16
|
TriggerType[TriggerType["Http"] = 0] = "Http";
|
|
20
17
|
})(TriggerType || (exports.TriggerType = TriggerType = {}));
|
|
21
|
-
|
|
22
18
|
let HttpMetadata;
|
|
23
19
|
exports.HttpMetadata = HttpMetadata;
|
|
24
|
-
|
|
25
20
|
(function (HttpMetadata) {
|
|
26
21
|
HttpMetadata["Method"] = "METHOD";
|
|
27
22
|
HttpMetadata["Data"] = "DATA";
|
|
@@ -30,19 +25,15 @@ exports.HttpMetadata = HttpMetadata;
|
|
|
30
25
|
HttpMetadata["Headers"] = "HEADERS";
|
|
31
26
|
HttpMetadata["Response"] = "RESPONSE";
|
|
32
27
|
})(HttpMetadata || (exports.HttpMetadata = HttpMetadata = {}));
|
|
33
|
-
|
|
34
28
|
let ResponseMetaType;
|
|
35
29
|
exports.ResponseMetaType = ResponseMetaType;
|
|
36
|
-
|
|
37
30
|
(function (ResponseMetaType) {
|
|
38
31
|
ResponseMetaType[ResponseMetaType["StatusCode"] = 0] = "StatusCode";
|
|
39
32
|
ResponseMetaType[ResponseMetaType["Redirect"] = 1] = "Redirect";
|
|
40
33
|
ResponseMetaType[ResponseMetaType["Headers"] = 2] = "Headers";
|
|
41
34
|
})(ResponseMetaType || (exports.ResponseMetaType = ResponseMetaType = {}));
|
|
42
|
-
|
|
43
35
|
let HttpMethod;
|
|
44
36
|
exports.HttpMethod = HttpMethod;
|
|
45
|
-
|
|
46
37
|
(function (HttpMethod) {
|
|
47
38
|
HttpMethod["Get"] = "GET";
|
|
48
39
|
HttpMethod["Post"] = "POST";
|
|
@@ -54,6 +45,5 @@ exports.HttpMethod = HttpMethod;
|
|
|
54
45
|
HttpMethod["Option"] = "OPTION";
|
|
55
46
|
HttpMethod["Head"] = "HEAD";
|
|
56
47
|
})(HttpMethod || (exports.HttpMethod = HttpMethod = {}));
|
|
57
|
-
|
|
58
48
|
const httpMethods = Object.values(HttpMethod);
|
|
59
49
|
exports.httpMethods = httpMethods;
|