@modern-js/bff-core 2.5.0 → 2.5.1-alpha.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/dist/cjs/client/generate-client.js +5 -3
- package/dist/cjs/index.js +4 -0
- package/dist/cjs/operators/http.js +7 -1
- package/dist/cjs/router/index.js +51 -27
- package/dist/cjs/utils/meta.js +8 -0
- package/dist/esm/client/generate-client.js +5 -3
- package/dist/esm/index.js +4 -0
- package/dist/esm/operators/http.js +1 -1
- package/dist/esm/router/index.js +52 -28
- package/dist/esm/utils/meta.js +6 -0
- package/dist/js/modern/api.js +69 -0
- package/dist/js/modern/client/generate-client.js +79 -0
- package/dist/js/modern/client/index.js +1 -0
- package/dist/js/modern/client/result.js +22 -0
- package/dist/js/modern/errors/http.js +16 -0
- package/dist/js/modern/index.js +23 -0
- package/dist/js/modern/operators/http.js +210 -0
- package/dist/js/modern/router/constants.js +32 -0
- package/dist/js/modern/router/index.js +236 -0
- package/dist/js/modern/router/types.js +0 -0
- package/dist/js/modern/router/utils.js +83 -0
- package/dist/js/modern/types.js +45 -0
- package/dist/js/modern/utils/alias.js +76 -0
- package/dist/js/modern/utils/debug.js +5 -0
- package/dist/js/modern/utils/index.js +8 -0
- package/dist/js/modern/utils/meta.js +8 -0
- package/dist/js/modern/utils/storage.js +42 -0
- package/dist/js/modern/utils/validate.js +43 -0
- package/dist/js/node/api.js +98 -0
- package/dist/js/node/client/generate-client.js +109 -0
- package/dist/js/node/client/index.js +17 -0
- package/dist/js/node/client/result.js +46 -0
- package/dist/js/node/errors/http.js +40 -0
- package/dist/js/node/index.js +48 -0
- package/dist/js/node/operators/http.js +241 -0
- package/dist/js/node/router/constants.js +61 -0
- package/dist/js/node/router/index.js +256 -0
- package/dist/js/node/router/types.js +15 -0
- package/dist/js/node/router/utils.js +116 -0
- package/dist/js/node/types.js +73 -0
- package/dist/js/node/utils/alias.js +107 -0
- package/dist/js/node/utils/debug.js +28 -0
- package/dist/js/node/utils/index.js +32 -0
- package/dist/js/node/utils/meta.js +32 -0
- package/dist/js/node/utils/storage.js +71 -0
- package/dist/js/node/utils/validate.js +74 -0
- package/dist/types/client/generate-client.d.ts +4 -1
- package/dist/types/index.d.ts +1 -1
- package/dist/types/router/index.d.ts +5 -1
- package/dist/types/utils/meta.d.ts +3 -1
- package/package.json +8 -7
|
@@ -0,0 +1,210 @@
|
|
|
1
|
+
var __defProp = Object.defineProperty;
|
|
2
|
+
var __defProps = Object.defineProperties;
|
|
3
|
+
var __getOwnPropDescs = Object.getOwnPropertyDescriptors;
|
|
4
|
+
var __getOwnPropSymbols = Object.getOwnPropertySymbols;
|
|
5
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
6
|
+
var __propIsEnum = Object.prototype.propertyIsEnumerable;
|
|
7
|
+
var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
|
|
8
|
+
var __spreadValues = (a, b) => {
|
|
9
|
+
for (var prop in b || (b = {}))
|
|
10
|
+
if (__hasOwnProp.call(b, prop))
|
|
11
|
+
__defNormalProp(a, prop, b[prop]);
|
|
12
|
+
if (__getOwnPropSymbols)
|
|
13
|
+
for (var prop of __getOwnPropSymbols(b)) {
|
|
14
|
+
if (__propIsEnum.call(b, prop))
|
|
15
|
+
__defNormalProp(a, prop, b[prop]);
|
|
16
|
+
}
|
|
17
|
+
return a;
|
|
18
|
+
};
|
|
19
|
+
var __spreadProps = (a, b) => __defProps(a, __getOwnPropDescs(b));
|
|
20
|
+
var __async = (__this, __arguments, generator) => {
|
|
21
|
+
return new Promise((resolve, reject) => {
|
|
22
|
+
var fulfilled = (value) => {
|
|
23
|
+
try {
|
|
24
|
+
step(generator.next(value));
|
|
25
|
+
} catch (e) {
|
|
26
|
+
reject(e);
|
|
27
|
+
}
|
|
28
|
+
};
|
|
29
|
+
var rejected = (value) => {
|
|
30
|
+
try {
|
|
31
|
+
step(generator.throw(value));
|
|
32
|
+
} catch (e) {
|
|
33
|
+
reject(e);
|
|
34
|
+
}
|
|
35
|
+
};
|
|
36
|
+
var step = (x) => x.done ? resolve(x.value) : Promise.resolve(x.value).then(fulfilled, rejected);
|
|
37
|
+
step((generator = generator.apply(__this, __arguments)).next());
|
|
38
|
+
});
|
|
39
|
+
};
|
|
40
|
+
import {
|
|
41
|
+
HttpMetadata,
|
|
42
|
+
OperatorType,
|
|
43
|
+
HttpMethod,
|
|
44
|
+
TriggerType,
|
|
45
|
+
ResponseMetaType
|
|
46
|
+
} from "../types";
|
|
47
|
+
import { ValidationError } from "../errors/http";
|
|
48
|
+
const validateInput = (schema, input) => __async(void 0, null, function* () {
|
|
49
|
+
try {
|
|
50
|
+
return yield schema.parseAsync(input);
|
|
51
|
+
} catch (error) {
|
|
52
|
+
const { z: zod } = require("zod");
|
|
53
|
+
if (error instanceof zod.ZodError) {
|
|
54
|
+
throw new ValidationError(400, error.message);
|
|
55
|
+
}
|
|
56
|
+
throw error;
|
|
57
|
+
}
|
|
58
|
+
});
|
|
59
|
+
const createHttpOperator = (method) => {
|
|
60
|
+
return (urlPath) => {
|
|
61
|
+
return {
|
|
62
|
+
name: method,
|
|
63
|
+
metadata({ setMetadata }) {
|
|
64
|
+
setMetadata(OperatorType.Trigger, {
|
|
65
|
+
type: TriggerType.Http,
|
|
66
|
+
path: urlPath,
|
|
67
|
+
method
|
|
68
|
+
});
|
|
69
|
+
}
|
|
70
|
+
};
|
|
71
|
+
};
|
|
72
|
+
};
|
|
73
|
+
const Get = createHttpOperator(HttpMethod.Get);
|
|
74
|
+
const Post = createHttpOperator(HttpMethod.Post);
|
|
75
|
+
const Put = createHttpOperator(HttpMethod.Put);
|
|
76
|
+
const Delete = createHttpOperator(HttpMethod.Delete);
|
|
77
|
+
const Connect = createHttpOperator(HttpMethod.Connect);
|
|
78
|
+
const Trace = createHttpOperator(HttpMethod.Trace);
|
|
79
|
+
const Patch = createHttpOperator(HttpMethod.Patch);
|
|
80
|
+
const Option = createHttpOperator(HttpMethod.Option);
|
|
81
|
+
const Head = createHttpOperator(HttpMethod.Head);
|
|
82
|
+
const Data = (schema) => {
|
|
83
|
+
return {
|
|
84
|
+
name: HttpMetadata.Data,
|
|
85
|
+
metadata({ setMetadata }) {
|
|
86
|
+
setMetadata(HttpMetadata.Data, schema);
|
|
87
|
+
},
|
|
88
|
+
validate(helper, next) {
|
|
89
|
+
return __async(this, null, function* () {
|
|
90
|
+
const {
|
|
91
|
+
inputs: { data }
|
|
92
|
+
} = helper;
|
|
93
|
+
helper.inputs = __spreadProps(__spreadValues({}, helper.inputs), {
|
|
94
|
+
data: yield validateInput(schema, data)
|
|
95
|
+
});
|
|
96
|
+
return next();
|
|
97
|
+
});
|
|
98
|
+
}
|
|
99
|
+
};
|
|
100
|
+
};
|
|
101
|
+
const Query = (schema) => {
|
|
102
|
+
return {
|
|
103
|
+
name: HttpMetadata.Query,
|
|
104
|
+
metadata({ setMetadata }) {
|
|
105
|
+
setMetadata(HttpMetadata.Query, schema);
|
|
106
|
+
},
|
|
107
|
+
validate(helper, next) {
|
|
108
|
+
return __async(this, null, function* () {
|
|
109
|
+
const {
|
|
110
|
+
inputs: { query }
|
|
111
|
+
} = helper;
|
|
112
|
+
helper.inputs = __spreadProps(__spreadValues({}, helper.inputs), {
|
|
113
|
+
query: yield validateInput(schema, query)
|
|
114
|
+
});
|
|
115
|
+
return next();
|
|
116
|
+
});
|
|
117
|
+
}
|
|
118
|
+
};
|
|
119
|
+
};
|
|
120
|
+
const Params = (schema) => {
|
|
121
|
+
return {
|
|
122
|
+
name: HttpMetadata.Params,
|
|
123
|
+
metadata({ setMetadata }) {
|
|
124
|
+
setMetadata(HttpMetadata.Params, schema);
|
|
125
|
+
},
|
|
126
|
+
validate(helper, next) {
|
|
127
|
+
return __async(this, null, function* () {
|
|
128
|
+
const {
|
|
129
|
+
inputs: { params }
|
|
130
|
+
} = helper;
|
|
131
|
+
helper.inputs = __spreadProps(__spreadValues({}, helper.inputs), {
|
|
132
|
+
params: yield validateInput(schema, params)
|
|
133
|
+
});
|
|
134
|
+
return next();
|
|
135
|
+
});
|
|
136
|
+
}
|
|
137
|
+
};
|
|
138
|
+
};
|
|
139
|
+
const Headers = (schema) => {
|
|
140
|
+
return {
|
|
141
|
+
name: HttpMetadata.Headers,
|
|
142
|
+
metadata({ setMetadata }) {
|
|
143
|
+
setMetadata(HttpMetadata.Headers, schema);
|
|
144
|
+
},
|
|
145
|
+
validate(helper, next) {
|
|
146
|
+
return __async(this, null, function* () {
|
|
147
|
+
const {
|
|
148
|
+
inputs: { headers }
|
|
149
|
+
} = helper;
|
|
150
|
+
helper.inputs = __spreadProps(__spreadValues({}, helper.inputs), {
|
|
151
|
+
headers: yield validateInput(schema, headers)
|
|
152
|
+
});
|
|
153
|
+
return next();
|
|
154
|
+
});
|
|
155
|
+
}
|
|
156
|
+
};
|
|
157
|
+
};
|
|
158
|
+
const setResponseMeta = (helper, type, value) => {
|
|
159
|
+
const responseMetaData = helper.getMetadata(HttpMetadata.Response) || [];
|
|
160
|
+
helper.setMetadata(HttpMetadata.Response, [
|
|
161
|
+
...responseMetaData,
|
|
162
|
+
{
|
|
163
|
+
type,
|
|
164
|
+
value
|
|
165
|
+
}
|
|
166
|
+
]);
|
|
167
|
+
};
|
|
168
|
+
const HttpCode = (statusCode) => {
|
|
169
|
+
return {
|
|
170
|
+
name: "HttpCode",
|
|
171
|
+
metadata(helper) {
|
|
172
|
+
setResponseMeta(helper, ResponseMetaType.StatusCode, statusCode);
|
|
173
|
+
}
|
|
174
|
+
};
|
|
175
|
+
};
|
|
176
|
+
const SetHeaders = (headers) => {
|
|
177
|
+
return {
|
|
178
|
+
name: "SetHeaders",
|
|
179
|
+
metadata(helper) {
|
|
180
|
+
setResponseMeta(helper, ResponseMetaType.Headers, headers);
|
|
181
|
+
}
|
|
182
|
+
};
|
|
183
|
+
};
|
|
184
|
+
const Redirect = (url) => {
|
|
185
|
+
return {
|
|
186
|
+
name: "Redirect",
|
|
187
|
+
metadata(helper) {
|
|
188
|
+
setResponseMeta(helper, ResponseMetaType.Redirect, url);
|
|
189
|
+
}
|
|
190
|
+
};
|
|
191
|
+
};
|
|
192
|
+
export {
|
|
193
|
+
Connect,
|
|
194
|
+
Data,
|
|
195
|
+
Delete,
|
|
196
|
+
Get,
|
|
197
|
+
Head,
|
|
198
|
+
Headers,
|
|
199
|
+
HttpCode,
|
|
200
|
+
Option,
|
|
201
|
+
Params,
|
|
202
|
+
Patch,
|
|
203
|
+
Post,
|
|
204
|
+
Put,
|
|
205
|
+
Query,
|
|
206
|
+
Redirect,
|
|
207
|
+
SetHeaders,
|
|
208
|
+
Trace,
|
|
209
|
+
createHttpOperator
|
|
210
|
+
};
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
import { HttpMethod } from "../types";
|
|
2
|
+
const AllHttpMethods = Object.values(HttpMethod);
|
|
3
|
+
var APIMode = /* @__PURE__ */ ((APIMode2) => {
|
|
4
|
+
APIMode2["FARMEWORK"] = "framework";
|
|
5
|
+
APIMode2["FUNCTION"] = "function";
|
|
6
|
+
return APIMode2;
|
|
7
|
+
})(APIMode || {});
|
|
8
|
+
const FRAMEWORK_MODE_LAMBDA_DIR = "lambda";
|
|
9
|
+
const FRAMEWORK_MODE_APP_DIR = "app";
|
|
10
|
+
const INDEX_SUFFIX = "index";
|
|
11
|
+
const API_DIR = "api";
|
|
12
|
+
const API_FILE_RULES = [
|
|
13
|
+
"**/*.[tj]s",
|
|
14
|
+
"!**/_*",
|
|
15
|
+
"!**/_*/**/*.[tj]s",
|
|
16
|
+
"!**/*.test.js",
|
|
17
|
+
"!**/*.test.ts",
|
|
18
|
+
"!**/*.d.ts",
|
|
19
|
+
"!__test__/*.ts",
|
|
20
|
+
"!__tests__/*.ts",
|
|
21
|
+
"!node_modules/**",
|
|
22
|
+
"!bootstrap.js"
|
|
23
|
+
];
|
|
24
|
+
export {
|
|
25
|
+
APIMode,
|
|
26
|
+
API_DIR,
|
|
27
|
+
API_FILE_RULES,
|
|
28
|
+
AllHttpMethods,
|
|
29
|
+
FRAMEWORK_MODE_APP_DIR,
|
|
30
|
+
FRAMEWORK_MODE_LAMBDA_DIR,
|
|
31
|
+
INDEX_SUFFIX
|
|
32
|
+
};
|
|
@@ -0,0 +1,236 @@
|
|
|
1
|
+
import path from "path";
|
|
2
|
+
import { fs, logger } from "@modern-js/utils";
|
|
3
|
+
import "reflect-metadata";
|
|
4
|
+
import { HttpMethod, httpMethods, OperatorType, TriggerType } from "../types";
|
|
5
|
+
import { debug } from "../utils";
|
|
6
|
+
import {
|
|
7
|
+
APIMode,
|
|
8
|
+
FRAMEWORK_MODE_LAMBDA_DIR,
|
|
9
|
+
API_FILE_RULES,
|
|
10
|
+
FRAMEWORK_MODE_APP_DIR
|
|
11
|
+
} from "./constants";
|
|
12
|
+
import {
|
|
13
|
+
getFiles,
|
|
14
|
+
getPathFromFilename,
|
|
15
|
+
requireHandlerModule,
|
|
16
|
+
sortRoutes
|
|
17
|
+
} from "./utils";
|
|
18
|
+
export * from "./types";
|
|
19
|
+
export * from "./constants";
|
|
20
|
+
class ApiRouter {
|
|
21
|
+
constructor({
|
|
22
|
+
apiDir,
|
|
23
|
+
lambdaDir,
|
|
24
|
+
prefix
|
|
25
|
+
}) {
|
|
26
|
+
this.apiFiles = [];
|
|
27
|
+
this.getExactApiMode = (apiDir) => {
|
|
28
|
+
const exist = this.createExistChecker(apiDir);
|
|
29
|
+
const existLambdaDir = exist(FRAMEWORK_MODE_LAMBDA_DIR);
|
|
30
|
+
const existAppDir = exist(FRAMEWORK_MODE_APP_DIR);
|
|
31
|
+
const existAppFile = exist("app.ts") || exist("app.js");
|
|
32
|
+
if (existLambdaDir || existAppDir || existAppFile) {
|
|
33
|
+
return APIMode.FARMEWORK;
|
|
34
|
+
}
|
|
35
|
+
return APIMode.FUNCTION;
|
|
36
|
+
};
|
|
37
|
+
this.createExistChecker = (base) => (target) => fs.pathExistsSync(path.resolve(base, target));
|
|
38
|
+
this.getExactLambdaDir = (apiDir) => {
|
|
39
|
+
if (this.lambdaDir) {
|
|
40
|
+
return this.lambdaDir;
|
|
41
|
+
}
|
|
42
|
+
const lambdaDir = this.apiMode === APIMode.FARMEWORK ? path.join(apiDir, FRAMEWORK_MODE_LAMBDA_DIR) : apiDir;
|
|
43
|
+
return lambdaDir;
|
|
44
|
+
};
|
|
45
|
+
this.validateAbsolute(apiDir, "apiDir");
|
|
46
|
+
this.validateAbsolute(lambdaDir, "lambdaDir");
|
|
47
|
+
this.prefix = this.initPrefix(prefix);
|
|
48
|
+
this.apiDir = apiDir;
|
|
49
|
+
this.apiMode = this.getExactApiMode(apiDir);
|
|
50
|
+
this.lambdaDir = lambdaDir || this.getExactLambdaDir(this.apiDir);
|
|
51
|
+
this.existLambdaDir = fs.existsSync(this.lambdaDir);
|
|
52
|
+
}
|
|
53
|
+
isExistLambda() {
|
|
54
|
+
return this.existLambdaDir;
|
|
55
|
+
}
|
|
56
|
+
getApiMode() {
|
|
57
|
+
return this.apiMode;
|
|
58
|
+
}
|
|
59
|
+
getLambdaDir() {
|
|
60
|
+
return this.lambdaDir;
|
|
61
|
+
}
|
|
62
|
+
isApiFile(filename) {
|
|
63
|
+
if (this.existLambdaDir) {
|
|
64
|
+
return false;
|
|
65
|
+
}
|
|
66
|
+
if (!this.apiFiles.includes(filename)) {
|
|
67
|
+
return false;
|
|
68
|
+
}
|
|
69
|
+
return true;
|
|
70
|
+
}
|
|
71
|
+
getSingleModuleHandlers(filename) {
|
|
72
|
+
const moduleInfo = this.getModuleInfo(filename);
|
|
73
|
+
if (moduleInfo) {
|
|
74
|
+
return this.getModuleHandlerInfos(moduleInfo);
|
|
75
|
+
}
|
|
76
|
+
return null;
|
|
77
|
+
}
|
|
78
|
+
getHandlerInfo(filename, originFuncName, handler) {
|
|
79
|
+
const httpMethod = this.getHttpMethod(originFuncName, handler);
|
|
80
|
+
const routeName = this.getRouteName(filename, handler);
|
|
81
|
+
if (httpMethod && routeName) {
|
|
82
|
+
return {
|
|
83
|
+
handler,
|
|
84
|
+
name: originFuncName,
|
|
85
|
+
httpMethod,
|
|
86
|
+
routeName,
|
|
87
|
+
filename,
|
|
88
|
+
routePath: this.getRoutePath(this.prefix, routeName)
|
|
89
|
+
};
|
|
90
|
+
}
|
|
91
|
+
return null;
|
|
92
|
+
}
|
|
93
|
+
getSafeRoutePath(filename, handler) {
|
|
94
|
+
this.loadApiFiles();
|
|
95
|
+
this.validateValidApifile(filename);
|
|
96
|
+
return this.getRouteName(filename, handler);
|
|
97
|
+
}
|
|
98
|
+
getRouteName(filename, handler) {
|
|
99
|
+
if (handler) {
|
|
100
|
+
const trigger = Reflect.getMetadata(OperatorType.Trigger, handler);
|
|
101
|
+
if (trigger && trigger.type === TriggerType.Http) {
|
|
102
|
+
if (!trigger.path) {
|
|
103
|
+
throw new Error(
|
|
104
|
+
`The http trigger ${trigger.name} needs to specify a path`
|
|
105
|
+
);
|
|
106
|
+
}
|
|
107
|
+
return trigger.path;
|
|
108
|
+
}
|
|
109
|
+
}
|
|
110
|
+
const routePath = getPathFromFilename(this.lambdaDir, filename);
|
|
111
|
+
return routePath;
|
|
112
|
+
}
|
|
113
|
+
getHttpMethod(originHandlerName, handler) {
|
|
114
|
+
if (handler) {
|
|
115
|
+
const trigger = Reflect.getMetadata(OperatorType.Trigger, handler);
|
|
116
|
+
if (trigger && httpMethods.includes(trigger.method)) {
|
|
117
|
+
return trigger.method;
|
|
118
|
+
}
|
|
119
|
+
}
|
|
120
|
+
const upperName = originHandlerName.toUpperCase();
|
|
121
|
+
switch (upperName) {
|
|
122
|
+
case "GET":
|
|
123
|
+
return HttpMethod.Get;
|
|
124
|
+
case "POST":
|
|
125
|
+
return HttpMethod.Post;
|
|
126
|
+
case "PUT":
|
|
127
|
+
return HttpMethod.Put;
|
|
128
|
+
case "DELETE":
|
|
129
|
+
case "DEL":
|
|
130
|
+
return HttpMethod.Delete;
|
|
131
|
+
case "CONNECT":
|
|
132
|
+
return HttpMethod.Connect;
|
|
133
|
+
case "TRACE":
|
|
134
|
+
return HttpMethod.Trace;
|
|
135
|
+
case "PATCH":
|
|
136
|
+
return HttpMethod.Patch;
|
|
137
|
+
case "OPTION":
|
|
138
|
+
return HttpMethod.Option;
|
|
139
|
+
case "DEFAULT": {
|
|
140
|
+
return HttpMethod.Get;
|
|
141
|
+
}
|
|
142
|
+
default:
|
|
143
|
+
logger.warn(
|
|
144
|
+
`Only api handlers are allowd to be exported, please remove the function ${originHandlerName} from exports`
|
|
145
|
+
);
|
|
146
|
+
return null;
|
|
147
|
+
}
|
|
148
|
+
}
|
|
149
|
+
loadApiFiles() {
|
|
150
|
+
if (!this.existLambdaDir) {
|
|
151
|
+
return [];
|
|
152
|
+
}
|
|
153
|
+
const apiFiles = this.apiFiles = getFiles(this.lambdaDir, API_FILE_RULES);
|
|
154
|
+
return apiFiles;
|
|
155
|
+
}
|
|
156
|
+
getApiFiles() {
|
|
157
|
+
if (!this.existLambdaDir) {
|
|
158
|
+
return [];
|
|
159
|
+
}
|
|
160
|
+
if (this.apiFiles.length > 0) {
|
|
161
|
+
return this.apiFiles;
|
|
162
|
+
}
|
|
163
|
+
return this.loadApiFiles();
|
|
164
|
+
}
|
|
165
|
+
getApiHandlers() {
|
|
166
|
+
const filenames = this.getApiFiles();
|
|
167
|
+
const moduleInfos = this.getModuleInfos(filenames);
|
|
168
|
+
const apiHandlers = this.getHandlerInfos(moduleInfos);
|
|
169
|
+
debug("apiHandlers", apiHandlers.length, apiHandlers);
|
|
170
|
+
return apiHandlers;
|
|
171
|
+
}
|
|
172
|
+
initPrefix(prefix) {
|
|
173
|
+
if (prefix === "/") {
|
|
174
|
+
return "";
|
|
175
|
+
}
|
|
176
|
+
return prefix || "/api";
|
|
177
|
+
}
|
|
178
|
+
validateAbsolute(filename, paramsName) {
|
|
179
|
+
if (typeof filename === "string" && !path.isAbsolute(filename)) {
|
|
180
|
+
throw new Error(`The ${paramsName} ${filename} is not a abolute path`);
|
|
181
|
+
}
|
|
182
|
+
}
|
|
183
|
+
getModuleInfos(filenames) {
|
|
184
|
+
return filenames.map((filename) => this.getModuleInfo(filename)).filter((moduleInfo) => Boolean(moduleInfo));
|
|
185
|
+
}
|
|
186
|
+
getModuleInfo(filename) {
|
|
187
|
+
try {
|
|
188
|
+
const module = requireHandlerModule(filename);
|
|
189
|
+
return {
|
|
190
|
+
filename,
|
|
191
|
+
module
|
|
192
|
+
};
|
|
193
|
+
} catch (err) {
|
|
194
|
+
if (process.env.NODE_ENV === "production") {
|
|
195
|
+
throw err;
|
|
196
|
+
} else {
|
|
197
|
+
console.error(err);
|
|
198
|
+
return null;
|
|
199
|
+
}
|
|
200
|
+
}
|
|
201
|
+
}
|
|
202
|
+
getHandlerInfos(moduleInfos) {
|
|
203
|
+
let apiHandlers = [];
|
|
204
|
+
moduleInfos.forEach((moduleInfo) => {
|
|
205
|
+
const handlerInfos = this.getModuleHandlerInfos(moduleInfo);
|
|
206
|
+
if (handlerInfos) {
|
|
207
|
+
apiHandlers = apiHandlers.concat(handlerInfos);
|
|
208
|
+
}
|
|
209
|
+
});
|
|
210
|
+
const sortedHandlers = sortRoutes(apiHandlers);
|
|
211
|
+
return sortedHandlers;
|
|
212
|
+
}
|
|
213
|
+
getModuleHandlerInfos(moduleInfo) {
|
|
214
|
+
const { module, filename } = moduleInfo;
|
|
215
|
+
return Object.entries(module).filter(([, handler]) => typeof handler === "function").map(([key]) => {
|
|
216
|
+
const handler = module[key];
|
|
217
|
+
const handlerInfo = this.getHandlerInfo(filename, key, handler);
|
|
218
|
+
return handlerInfo;
|
|
219
|
+
}).filter((handlerInfo) => Boolean(handlerInfo));
|
|
220
|
+
}
|
|
221
|
+
validateValidApifile(filename) {
|
|
222
|
+
if (!this.apiFiles.includes(filename)) {
|
|
223
|
+
throw new Error(`The ${filename} is not a valid api file.`);
|
|
224
|
+
}
|
|
225
|
+
}
|
|
226
|
+
getRoutePath(prefix, routeName) {
|
|
227
|
+
const finalRouteName = routeName === "/" ? "" : routeName;
|
|
228
|
+
if (prefix === "" && finalRouteName === "") {
|
|
229
|
+
return "/";
|
|
230
|
+
}
|
|
231
|
+
return `${prefix}${finalRouteName}`;
|
|
232
|
+
}
|
|
233
|
+
}
|
|
234
|
+
export {
|
|
235
|
+
ApiRouter
|
|
236
|
+
};
|
|
File without changes
|
|
@@ -0,0 +1,83 @@
|
|
|
1
|
+
import path from "path";
|
|
2
|
+
import { globby } from "@modern-js/utils";
|
|
3
|
+
import { INDEX_SUFFIX } from "./constants";
|
|
4
|
+
const getFiles = (lambdaDir, rules) => globby.sync(rules, {
|
|
5
|
+
cwd: lambdaDir,
|
|
6
|
+
gitignore: true
|
|
7
|
+
}).map((file) => path.resolve(lambdaDir, file));
|
|
8
|
+
const getPathFromFilename = (baseDir, filename) => {
|
|
9
|
+
const relativeName = filename.substring(baseDir.length);
|
|
10
|
+
const relativePath = relativeName.split(".").slice(0, -1).join(".");
|
|
11
|
+
const nameSplit = relativePath.split(path.sep).map((item) => {
|
|
12
|
+
if (item.length > 2) {
|
|
13
|
+
if (item.startsWith("[") && item.endsWith("]")) {
|
|
14
|
+
return `:${item.substring(1, item.length - 1)}`;
|
|
15
|
+
}
|
|
16
|
+
}
|
|
17
|
+
return item;
|
|
18
|
+
});
|
|
19
|
+
const name = nameSplit.join("/");
|
|
20
|
+
const finalName = name.endsWith(INDEX_SUFFIX) ? name.substring(0, name.length - INDEX_SUFFIX.length) : name;
|
|
21
|
+
return clearRouteName(finalName);
|
|
22
|
+
};
|
|
23
|
+
const clearRouteName = (routeName) => {
|
|
24
|
+
let finalRouteName = routeName.trim();
|
|
25
|
+
if (!finalRouteName.startsWith("/")) {
|
|
26
|
+
finalRouteName = `/${finalRouteName}`;
|
|
27
|
+
}
|
|
28
|
+
if (finalRouteName.length > 1 && finalRouteName.endsWith("/")) {
|
|
29
|
+
finalRouteName = finalRouteName.substring(0, finalRouteName.length - 1);
|
|
30
|
+
}
|
|
31
|
+
return finalRouteName;
|
|
32
|
+
};
|
|
33
|
+
const isHandler = (input) => input && typeof input === "function";
|
|
34
|
+
const enableRegister = (requireFn) => {
|
|
35
|
+
let existTsLoader = false;
|
|
36
|
+
let firstCall = true;
|
|
37
|
+
return (modulePath) => {
|
|
38
|
+
if (firstCall) {
|
|
39
|
+
existTsLoader = Boolean(require.extensions[".ts"]);
|
|
40
|
+
firstCall = false;
|
|
41
|
+
}
|
|
42
|
+
if (!existTsLoader) {
|
|
43
|
+
const {
|
|
44
|
+
register
|
|
45
|
+
} = require("esbuild-register/dist/node");
|
|
46
|
+
const { unregister } = register({
|
|
47
|
+
extensions: [".ts"]
|
|
48
|
+
});
|
|
49
|
+
const requiredModule2 = requireFn(modulePath);
|
|
50
|
+
unregister();
|
|
51
|
+
return requiredModule2;
|
|
52
|
+
}
|
|
53
|
+
const requiredModule = requireFn(modulePath);
|
|
54
|
+
return requiredModule;
|
|
55
|
+
};
|
|
56
|
+
};
|
|
57
|
+
const isFunction = (input) => input && {}.toString.call(input) === "[object Function]";
|
|
58
|
+
const requireHandlerModule = enableRegister((modulePath) => {
|
|
59
|
+
const originRequire = process.env.NODE_ENV === "test" ? jest.requireActual : require;
|
|
60
|
+
const module = originRequire(modulePath);
|
|
61
|
+
if (isFunction(module)) {
|
|
62
|
+
return { default: module };
|
|
63
|
+
}
|
|
64
|
+
return module;
|
|
65
|
+
});
|
|
66
|
+
const routeValue = (routePath) => {
|
|
67
|
+
if (routePath.includes(":")) {
|
|
68
|
+
return 11;
|
|
69
|
+
}
|
|
70
|
+
return 1;
|
|
71
|
+
};
|
|
72
|
+
const sortRoutes = (apiHandlers) => {
|
|
73
|
+
return apiHandlers.sort((handlerA, handlerB) => {
|
|
74
|
+
return routeValue(handlerA.routeName) - routeValue(handlerB.routeName);
|
|
75
|
+
});
|
|
76
|
+
};
|
|
77
|
+
export {
|
|
78
|
+
getFiles,
|
|
79
|
+
getPathFromFilename,
|
|
80
|
+
isHandler,
|
|
81
|
+
requireHandlerModule,
|
|
82
|
+
sortRoutes
|
|
83
|
+
};
|
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
var OperatorType = /* @__PURE__ */ ((OperatorType2) => {
|
|
2
|
+
OperatorType2[OperatorType2["Trigger"] = 0] = "Trigger";
|
|
3
|
+
OperatorType2[OperatorType2["Middleware"] = 1] = "Middleware";
|
|
4
|
+
return OperatorType2;
|
|
5
|
+
})(OperatorType || {});
|
|
6
|
+
var TriggerType = /* @__PURE__ */ ((TriggerType2) => {
|
|
7
|
+
TriggerType2[TriggerType2["Http"] = 0] = "Http";
|
|
8
|
+
return TriggerType2;
|
|
9
|
+
})(TriggerType || {});
|
|
10
|
+
var HttpMetadata = /* @__PURE__ */ ((HttpMetadata2) => {
|
|
11
|
+
HttpMetadata2["Method"] = "METHOD";
|
|
12
|
+
HttpMetadata2["Data"] = "DATA";
|
|
13
|
+
HttpMetadata2["Query"] = "QUERY";
|
|
14
|
+
HttpMetadata2["Params"] = "PARAMS";
|
|
15
|
+
HttpMetadata2["Headers"] = "HEADERS";
|
|
16
|
+
HttpMetadata2["Response"] = "RESPONSE";
|
|
17
|
+
return HttpMetadata2;
|
|
18
|
+
})(HttpMetadata || {});
|
|
19
|
+
var ResponseMetaType = /* @__PURE__ */ ((ResponseMetaType2) => {
|
|
20
|
+
ResponseMetaType2[ResponseMetaType2["StatusCode"] = 0] = "StatusCode";
|
|
21
|
+
ResponseMetaType2[ResponseMetaType2["Redirect"] = 1] = "Redirect";
|
|
22
|
+
ResponseMetaType2[ResponseMetaType2["Headers"] = 2] = "Headers";
|
|
23
|
+
return ResponseMetaType2;
|
|
24
|
+
})(ResponseMetaType || {});
|
|
25
|
+
var HttpMethod = /* @__PURE__ */ ((HttpMethod2) => {
|
|
26
|
+
HttpMethod2["Get"] = "GET";
|
|
27
|
+
HttpMethod2["Post"] = "POST";
|
|
28
|
+
HttpMethod2["Put"] = "PUT";
|
|
29
|
+
HttpMethod2["Delete"] = "DELETE";
|
|
30
|
+
HttpMethod2["Connect"] = "CONNECT";
|
|
31
|
+
HttpMethod2["Trace"] = "TRACE";
|
|
32
|
+
HttpMethod2["Patch"] = "PATCH";
|
|
33
|
+
HttpMethod2["Option"] = "OPTION";
|
|
34
|
+
HttpMethod2["Head"] = "HEAD";
|
|
35
|
+
return HttpMethod2;
|
|
36
|
+
})(HttpMethod || {});
|
|
37
|
+
const httpMethods = Object.values(HttpMethod);
|
|
38
|
+
export {
|
|
39
|
+
HttpMetadata,
|
|
40
|
+
HttpMethod,
|
|
41
|
+
OperatorType,
|
|
42
|
+
ResponseMetaType,
|
|
43
|
+
TriggerType,
|
|
44
|
+
httpMethods
|
|
45
|
+
};
|
|
@@ -0,0 +1,76 @@
|
|
|
1
|
+
import * as path from "path";
|
|
2
|
+
import * as os from "os";
|
|
3
|
+
import fs from "fs";
|
|
4
|
+
import Module from "module";
|
|
5
|
+
const getRelativeRuntimePath = (appDirectory, serverRuntimePath) => {
|
|
6
|
+
let relativeRuntimePath = "";
|
|
7
|
+
if (os.platform() === "win32") {
|
|
8
|
+
relativeRuntimePath = `../${path.relative(
|
|
9
|
+
appDirectory,
|
|
10
|
+
serverRuntimePath
|
|
11
|
+
)}`;
|
|
12
|
+
} else {
|
|
13
|
+
relativeRuntimePath = path.join(
|
|
14
|
+
"../",
|
|
15
|
+
path.relative(appDirectory, serverRuntimePath)
|
|
16
|
+
);
|
|
17
|
+
}
|
|
18
|
+
if (process.env.NODE_ENV === "development" || process.env.NODE_ENV === "test") {
|
|
19
|
+
relativeRuntimePath = `./${path.relative(appDirectory, serverRuntimePath)}`;
|
|
20
|
+
}
|
|
21
|
+
return relativeRuntimePath;
|
|
22
|
+
};
|
|
23
|
+
const sortByLongestPrefix = (arr) => {
|
|
24
|
+
return arr.concat().sort((a, b) => b.length - a.length);
|
|
25
|
+
};
|
|
26
|
+
const createMatchPath = (paths) => {
|
|
27
|
+
const sortedKeys = sortByLongestPrefix(Object.keys(paths));
|
|
28
|
+
const sortedPaths = {};
|
|
29
|
+
sortedKeys.forEach((key) => {
|
|
30
|
+
sortedPaths[key] = paths[key];
|
|
31
|
+
});
|
|
32
|
+
return (request) => {
|
|
33
|
+
const found = Object.keys(sortedPaths).find((key) => {
|
|
34
|
+
return request.startsWith(key);
|
|
35
|
+
});
|
|
36
|
+
if (found) {
|
|
37
|
+
let foundPaths = sortedPaths[found];
|
|
38
|
+
if (!Array.isArray(foundPaths)) {
|
|
39
|
+
foundPaths = [foundPaths];
|
|
40
|
+
}
|
|
41
|
+
foundPaths = foundPaths.filter((foundPath) => path.isAbsolute(foundPath));
|
|
42
|
+
for (const p of foundPaths) {
|
|
43
|
+
const foundPath = request.replace(found, p);
|
|
44
|
+
if (fs.existsSync(foundPath)) {
|
|
45
|
+
return foundPath;
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
return request.replace(found, foundPaths[0]);
|
|
49
|
+
}
|
|
50
|
+
return null;
|
|
51
|
+
};
|
|
52
|
+
};
|
|
53
|
+
const registerPaths = (paths) => {
|
|
54
|
+
const originalResolveFilename = Module._resolveFilename;
|
|
55
|
+
const { builtinModules } = Module;
|
|
56
|
+
const matchPath = createMatchPath(paths);
|
|
57
|
+
Module._resolveFilename = function(request, _parent) {
|
|
58
|
+
const isCoreModule = builtinModules.includes(request);
|
|
59
|
+
if (!isCoreModule) {
|
|
60
|
+
const matched = matchPath(request);
|
|
61
|
+
if (matched) {
|
|
62
|
+
const modifiedArguments = [matched, ...[].slice.call(arguments, 1)];
|
|
63
|
+
return originalResolveFilename.apply(this, modifiedArguments);
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
return originalResolveFilename.apply(this, arguments);
|
|
67
|
+
};
|
|
68
|
+
return () => {
|
|
69
|
+
Module._resolveFilename = originalResolveFilename;
|
|
70
|
+
};
|
|
71
|
+
};
|
|
72
|
+
export {
|
|
73
|
+
createMatchPath,
|
|
74
|
+
getRelativeRuntimePath,
|
|
75
|
+
registerPaths
|
|
76
|
+
};
|