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