@modern-js/bff-core 2.15.0 → 2.17.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 +21 -0
- package/dist/cjs/api.js +22 -42
- package/dist/cjs/client/generateClient.js +63 -66
- package/dist/cjs/client/index.js +18 -17
- package/dist/cjs/client/result.js +11 -24
- package/dist/cjs/errors/http.js +26 -24
- package/dist/cjs/index.js +41 -50
- package/dist/cjs/operators/http.js +95 -101
- package/dist/cjs/router/constants.js +32 -40
- package/dist/cjs/router/index.js +114 -116
- package/dist/cjs/router/types.js +4 -15
- package/dist/cjs/router/utils.js +33 -50
- package/dist/cjs/types.js +29 -46
- package/dist/cjs/utils/alias.js +78 -58
- package/dist/cjs/utils/debug.js +8 -26
- package/dist/cjs/utils/index.js +24 -30
- package/dist/cjs/utils/meta.js +13 -28
- package/dist/cjs/utils/storage.js +51 -41
- package/dist/cjs/utils/validate.js +27 -44
- package/dist/esm/api.js +5 -5
- package/dist/esm/client/generateClient.js +6 -30
- package/dist/esm/client/result.js +2 -6
- package/dist/esm/errors/http.js +17 -6
- package/dist/esm/index.js +3 -23
- package/dist/esm/operators/http.js +22 -55
- package/dist/esm/router/constants.js +21 -20
- package/dist/esm/router/index.js +58 -61
- package/dist/esm/router/types.js +1 -0
- package/dist/esm/router/utils.js +11 -14
- package/dist/esm/types.js +16 -24
- package/dist/esm/utils/alias.js +12 -18
- package/dist/esm/utils/debug.js +1 -4
- package/dist/esm/utils/index.js +1 -4
- package/dist/esm/utils/meta.js +4 -10
- package/dist/esm/utils/storage.js +2 -6
- package/dist/esm/utils/validate.js +12 -15
- package/package.json +11 -7
package/dist/esm/errors/http.js
CHANGED
|
@@ -1,16 +1,27 @@
|
|
|
1
|
-
|
|
1
|
+
function _define_property(obj, key, value) {
|
|
2
|
+
if (key in obj) {
|
|
3
|
+
Object.defineProperty(obj, key, {
|
|
4
|
+
value,
|
|
5
|
+
enumerable: true,
|
|
6
|
+
configurable: true,
|
|
7
|
+
writable: true
|
|
8
|
+
});
|
|
9
|
+
} else {
|
|
10
|
+
obj[key] = value;
|
|
11
|
+
}
|
|
12
|
+
return obj;
|
|
13
|
+
}
|
|
14
|
+
export class HttpError extends Error {
|
|
2
15
|
constructor(status, message) {
|
|
3
16
|
super(message);
|
|
17
|
+
_define_property(this, "status", void 0);
|
|
4
18
|
this.status = status;
|
|
5
19
|
}
|
|
6
20
|
}
|
|
7
|
-
class ValidationError extends HttpError {
|
|
21
|
+
export class ValidationError extends HttpError {
|
|
8
22
|
constructor(status, message) {
|
|
9
23
|
super(status, message);
|
|
24
|
+
_define_property(this, "code", void 0);
|
|
10
25
|
this.code = "VALIDATION_ERROR";
|
|
11
26
|
}
|
|
12
27
|
}
|
|
13
|
-
export {
|
|
14
|
-
HttpError,
|
|
15
|
-
ValidationError
|
|
16
|
-
};
|
package/dist/esm/index.js
CHANGED
|
@@ -1,27 +1,7 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
1
|
+
export { Api } from "./api";
|
|
2
|
+
export { HttpError, ValidationError } from "./errors/http";
|
|
3
3
|
export * from "./router";
|
|
4
4
|
export * from "./types";
|
|
5
5
|
export * from "./client";
|
|
6
6
|
export * from "./operators/http";
|
|
7
|
-
|
|
8
|
-
getRelativeRuntimePath,
|
|
9
|
-
HANDLER_WITH_META,
|
|
10
|
-
isWithMetaHandler,
|
|
11
|
-
INPUT_PARAMS_DECIDER,
|
|
12
|
-
isInputParamsDeciderHandler,
|
|
13
|
-
createStorage,
|
|
14
|
-
registerPaths
|
|
15
|
-
} from "./utils";
|
|
16
|
-
export {
|
|
17
|
-
Api,
|
|
18
|
-
HANDLER_WITH_META,
|
|
19
|
-
HttpError,
|
|
20
|
-
INPUT_PARAMS_DECIDER,
|
|
21
|
-
ValidationError,
|
|
22
|
-
createStorage,
|
|
23
|
-
getRelativeRuntimePath,
|
|
24
|
-
isInputParamsDeciderHandler,
|
|
25
|
-
isWithMetaHandler,
|
|
26
|
-
registerPaths
|
|
27
|
-
};
|
|
7
|
+
export { getRelativeRuntimePath, HANDLER_WITH_META, isWithMetaHandler, INPUT_PARAMS_DECIDER, isInputParamsDeciderHandler, createStorage, registerPaths } from "./utils";
|
|
@@ -1,10 +1,4 @@
|
|
|
1
|
-
import {
|
|
2
|
-
HttpMetadata,
|
|
3
|
-
OperatorType,
|
|
4
|
-
HttpMethod,
|
|
5
|
-
TriggerType,
|
|
6
|
-
ResponseMetaType
|
|
7
|
-
} from "../types";
|
|
1
|
+
import { HttpMetadata, OperatorType, HttpMethod, TriggerType, ResponseMetaType } from "../types";
|
|
8
2
|
import { ValidationError } from "../errors/http";
|
|
9
3
|
const validateInput = async (schema, input) => {
|
|
10
4
|
try {
|
|
@@ -17,7 +11,7 @@ const validateInput = async (schema, input) => {
|
|
|
17
11
|
throw error;
|
|
18
12
|
}
|
|
19
13
|
};
|
|
20
|
-
const createHttpOperator = (method) => {
|
|
14
|
+
export const createHttpOperator = (method) => {
|
|
21
15
|
return (urlPath) => {
|
|
22
16
|
return {
|
|
23
17
|
name: method,
|
|
@@ -31,25 +25,23 @@ const createHttpOperator = (method) => {
|
|
|
31
25
|
};
|
|
32
26
|
};
|
|
33
27
|
};
|
|
34
|
-
const Get = createHttpOperator(HttpMethod.Get);
|
|
35
|
-
const Post = createHttpOperator(HttpMethod.Post);
|
|
36
|
-
const Put = createHttpOperator(HttpMethod.Put);
|
|
37
|
-
const Delete = createHttpOperator(HttpMethod.Delete);
|
|
38
|
-
const Connect = createHttpOperator(HttpMethod.Connect);
|
|
39
|
-
const Trace = createHttpOperator(HttpMethod.Trace);
|
|
40
|
-
const Patch = createHttpOperator(HttpMethod.Patch);
|
|
41
|
-
const Options = createHttpOperator(HttpMethod.Options);
|
|
42
|
-
const Head = createHttpOperator(HttpMethod.Head);
|
|
43
|
-
const Data = (schema) => {
|
|
28
|
+
export const Get = createHttpOperator(HttpMethod.Get);
|
|
29
|
+
export const Post = createHttpOperator(HttpMethod.Post);
|
|
30
|
+
export const Put = createHttpOperator(HttpMethod.Put);
|
|
31
|
+
export const Delete = createHttpOperator(HttpMethod.Delete);
|
|
32
|
+
export const Connect = createHttpOperator(HttpMethod.Connect);
|
|
33
|
+
export const Trace = createHttpOperator(HttpMethod.Trace);
|
|
34
|
+
export const Patch = createHttpOperator(HttpMethod.Patch);
|
|
35
|
+
export const Options = createHttpOperator(HttpMethod.Options);
|
|
36
|
+
export const Head = createHttpOperator(HttpMethod.Head);
|
|
37
|
+
export const Data = (schema) => {
|
|
44
38
|
return {
|
|
45
39
|
name: HttpMetadata.Data,
|
|
46
40
|
metadata({ setMetadata }) {
|
|
47
41
|
setMetadata(HttpMetadata.Data, schema);
|
|
48
42
|
},
|
|
49
43
|
async validate(helper, next) {
|
|
50
|
-
const {
|
|
51
|
-
inputs: { data }
|
|
52
|
-
} = helper;
|
|
44
|
+
const { inputs: { data } } = helper;
|
|
53
45
|
helper.inputs = {
|
|
54
46
|
...helper.inputs,
|
|
55
47
|
data: await validateInput(schema, data)
|
|
@@ -58,16 +50,14 @@ const Data = (schema) => {
|
|
|
58
50
|
}
|
|
59
51
|
};
|
|
60
52
|
};
|
|
61
|
-
const Query = (schema) => {
|
|
53
|
+
export const Query = (schema) => {
|
|
62
54
|
return {
|
|
63
55
|
name: HttpMetadata.Query,
|
|
64
56
|
metadata({ setMetadata }) {
|
|
65
57
|
setMetadata(HttpMetadata.Query, schema);
|
|
66
58
|
},
|
|
67
59
|
async validate(helper, next) {
|
|
68
|
-
const {
|
|
69
|
-
inputs: { query }
|
|
70
|
-
} = helper;
|
|
60
|
+
const { inputs: { query } } = helper;
|
|
71
61
|
helper.inputs = {
|
|
72
62
|
...helper.inputs,
|
|
73
63
|
query: await validateInput(schema, query)
|
|
@@ -76,16 +66,14 @@ const Query = (schema) => {
|
|
|
76
66
|
}
|
|
77
67
|
};
|
|
78
68
|
};
|
|
79
|
-
const Params = (schema) => {
|
|
69
|
+
export const Params = (schema) => {
|
|
80
70
|
return {
|
|
81
71
|
name: HttpMetadata.Params,
|
|
82
72
|
metadata({ setMetadata }) {
|
|
83
73
|
setMetadata(HttpMetadata.Params, schema);
|
|
84
74
|
},
|
|
85
75
|
async validate(helper, next) {
|
|
86
|
-
const {
|
|
87
|
-
inputs: { params }
|
|
88
|
-
} = helper;
|
|
76
|
+
const { inputs: { params } } = helper;
|
|
89
77
|
helper.inputs = {
|
|
90
78
|
...helper.inputs,
|
|
91
79
|
params: await validateInput(schema, params)
|
|
@@ -94,16 +82,14 @@ const Params = (schema) => {
|
|
|
94
82
|
}
|
|
95
83
|
};
|
|
96
84
|
};
|
|
97
|
-
const Headers = (schema) => {
|
|
85
|
+
export const Headers = (schema) => {
|
|
98
86
|
return {
|
|
99
87
|
name: HttpMetadata.Headers,
|
|
100
88
|
metadata({ setMetadata }) {
|
|
101
89
|
setMetadata(HttpMetadata.Headers, schema);
|
|
102
90
|
},
|
|
103
91
|
async validate(helper, next) {
|
|
104
|
-
const {
|
|
105
|
-
inputs: { headers }
|
|
106
|
-
} = helper;
|
|
92
|
+
const { inputs: { headers } } = helper;
|
|
107
93
|
helper.inputs = {
|
|
108
94
|
...helper.inputs,
|
|
109
95
|
headers: await validateInput(schema, headers)
|
|
@@ -122,7 +108,7 @@ const setResponseMeta = (helper, type, value) => {
|
|
|
122
108
|
}
|
|
123
109
|
]);
|
|
124
110
|
};
|
|
125
|
-
const HttpCode = (statusCode) => {
|
|
111
|
+
export const HttpCode = (statusCode) => {
|
|
126
112
|
return {
|
|
127
113
|
name: "HttpCode",
|
|
128
114
|
metadata(helper) {
|
|
@@ -130,7 +116,7 @@ const HttpCode = (statusCode) => {
|
|
|
130
116
|
}
|
|
131
117
|
};
|
|
132
118
|
};
|
|
133
|
-
const SetHeaders = (headers) => {
|
|
119
|
+
export const SetHeaders = (headers) => {
|
|
134
120
|
return {
|
|
135
121
|
name: "SetHeaders",
|
|
136
122
|
metadata(helper) {
|
|
@@ -138,7 +124,7 @@ const SetHeaders = (headers) => {
|
|
|
138
124
|
}
|
|
139
125
|
};
|
|
140
126
|
};
|
|
141
|
-
const Redirect = (url) => {
|
|
127
|
+
export const Redirect = (url) => {
|
|
142
128
|
return {
|
|
143
129
|
name: "Redirect",
|
|
144
130
|
metadata(helper) {
|
|
@@ -146,22 +132,3 @@ const Redirect = (url) => {
|
|
|
146
132
|
}
|
|
147
133
|
};
|
|
148
134
|
};
|
|
149
|
-
export {
|
|
150
|
-
Connect,
|
|
151
|
-
Data,
|
|
152
|
-
Delete,
|
|
153
|
-
Get,
|
|
154
|
-
Head,
|
|
155
|
-
Headers,
|
|
156
|
-
HttpCode,
|
|
157
|
-
Options,
|
|
158
|
-
Params,
|
|
159
|
-
Patch,
|
|
160
|
-
Post,
|
|
161
|
-
Put,
|
|
162
|
-
Query,
|
|
163
|
-
Redirect,
|
|
164
|
-
SetHeaders,
|
|
165
|
-
Trace,
|
|
166
|
-
createHttpOperator
|
|
167
|
-
};
|
|
@@ -1,15 +1,25 @@
|
|
|
1
1
|
import { HttpMethod } from "../types";
|
|
2
|
-
const AllHttpMethods = Object.values(HttpMethod);
|
|
3
|
-
var APIMode
|
|
4
|
-
|
|
5
|
-
APIMode2[
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
2
|
+
export const AllHttpMethods = Object.values(HttpMethod);
|
|
3
|
+
export var APIMode;
|
|
4
|
+
(function(APIMode2) {
|
|
5
|
+
APIMode2[
|
|
6
|
+
/**
|
|
7
|
+
* 框架模式
|
|
8
|
+
*/
|
|
9
|
+
"FARMEWORK"
|
|
10
|
+
] = "framework";
|
|
11
|
+
APIMode2[
|
|
12
|
+
/**
|
|
13
|
+
* 函数模式
|
|
14
|
+
*/
|
|
15
|
+
"FUNCTION"
|
|
16
|
+
] = "function";
|
|
17
|
+
})(APIMode || (APIMode = {}));
|
|
18
|
+
export const FRAMEWORK_MODE_LAMBDA_DIR = "lambda";
|
|
19
|
+
export const FRAMEWORK_MODE_APP_DIR = "app";
|
|
20
|
+
export const INDEX_SUFFIX = "index";
|
|
21
|
+
export const API_DIR = "api";
|
|
22
|
+
export const API_FILE_RULES = [
|
|
13
23
|
"**/*.[tj]s",
|
|
14
24
|
"!**/_*",
|
|
15
25
|
"!**/_*/**/*.[tj]s",
|
|
@@ -21,12 +31,3 @@ const API_FILE_RULES = [
|
|
|
21
31
|
"!node_modules/**",
|
|
22
32
|
"!bootstrap.js"
|
|
23
33
|
];
|
|
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
|
-
};
|
package/dist/esm/router/index.js
CHANGED
|
@@ -1,57 +1,26 @@
|
|
|
1
|
+
function _define_property(obj, key, value) {
|
|
2
|
+
if (key in obj) {
|
|
3
|
+
Object.defineProperty(obj, key, {
|
|
4
|
+
value,
|
|
5
|
+
enumerable: true,
|
|
6
|
+
configurable: true,
|
|
7
|
+
writable: true
|
|
8
|
+
});
|
|
9
|
+
} else {
|
|
10
|
+
obj[key] = value;
|
|
11
|
+
}
|
|
12
|
+
return obj;
|
|
13
|
+
}
|
|
1
14
|
import path from "path";
|
|
2
15
|
import { fs, logger } from "@modern-js/utils";
|
|
3
16
|
import "reflect-metadata";
|
|
4
17
|
import { HttpMethod, httpMethods, OperatorType, TriggerType } from "../types";
|
|
5
18
|
import { debug, INPUT_PARAMS_DECIDER } from "../utils";
|
|
6
|
-
import {
|
|
7
|
-
|
|
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";
|
|
19
|
+
import { APIMode, FRAMEWORK_MODE_LAMBDA_DIR, API_FILE_RULES, FRAMEWORK_MODE_APP_DIR } from "./constants";
|
|
20
|
+
import { getFiles, getPathFromFilename, requireHandlerModule, sortRoutes } from "./utils";
|
|
18
21
|
export * from "./types";
|
|
19
22
|
export * from "./constants";
|
|
20
|
-
class ApiRouter {
|
|
21
|
-
constructor({
|
|
22
|
-
apiDir,
|
|
23
|
-
lambdaDir,
|
|
24
|
-
prefix,
|
|
25
|
-
httpMethodDecider = "functionName"
|
|
26
|
-
}) {
|
|
27
|
-
this.apiFiles = [];
|
|
28
|
-
this.getExactApiMode = (apiDir, lambdaDir) => {
|
|
29
|
-
const exist = this.createExistChecker(apiDir);
|
|
30
|
-
const existLambdaDir = lambdaDir && fs.pathExistsSync(lambdaDir) || exist(FRAMEWORK_MODE_LAMBDA_DIR);
|
|
31
|
-
const existAppDir = exist(FRAMEWORK_MODE_APP_DIR);
|
|
32
|
-
const existAppFile = exist("app.ts") || exist("app.js");
|
|
33
|
-
if (existLambdaDir || existAppDir || existAppFile) {
|
|
34
|
-
return APIMode.FARMEWORK;
|
|
35
|
-
}
|
|
36
|
-
return APIMode.FUNCTION;
|
|
37
|
-
};
|
|
38
|
-
this.createExistChecker = (base) => (target) => fs.pathExistsSync(path.resolve(base, target));
|
|
39
|
-
this.getExactLambdaDir = (apiDir, originLambdaDir) => {
|
|
40
|
-
if (this.apiMode === APIMode.FUNCTION) {
|
|
41
|
-
return apiDir;
|
|
42
|
-
}
|
|
43
|
-
return originLambdaDir || path.join(apiDir, FRAMEWORK_MODE_LAMBDA_DIR);
|
|
44
|
-
};
|
|
45
|
-
this.validateAbsolute(apiDir, "apiDir");
|
|
46
|
-
this.validateAbsolute(lambdaDir, "lambdaDir");
|
|
47
|
-
this.prefix = this.initPrefix(prefix);
|
|
48
|
-
this.apiDir = apiDir;
|
|
49
|
-
this.httpMethodDecider = httpMethodDecider;
|
|
50
|
-
this.apiMode = this.getExactApiMode(apiDir, lambdaDir);
|
|
51
|
-
this.lambdaDir = this.getExactLambdaDir(this.apiDir, lambdaDir);
|
|
52
|
-
this.existLambdaDir = fs.existsSync(this.lambdaDir);
|
|
53
|
-
debug(`apiDir:`, this.apiDir, `lambdaDir:`, this.lambdaDir);
|
|
54
|
-
}
|
|
23
|
+
export class ApiRouter {
|
|
55
24
|
isExistLambda() {
|
|
56
25
|
return this.existLambdaDir;
|
|
57
26
|
}
|
|
@@ -100,9 +69,7 @@ class ApiRouter {
|
|
|
100
69
|
const trigger = Reflect.getMetadata(OperatorType.Trigger, handler);
|
|
101
70
|
if (trigger && trigger.type === TriggerType.Http) {
|
|
102
71
|
if (!trigger.path) {
|
|
103
|
-
throw new Error(
|
|
104
|
-
`The http trigger ${trigger.name} needs to specify a path`
|
|
105
|
-
);
|
|
72
|
+
throw new Error(`The http trigger ${trigger.name} needs to specify a path`);
|
|
106
73
|
}
|
|
107
74
|
return trigger.path;
|
|
108
75
|
}
|
|
@@ -110,9 +77,9 @@ class ApiRouter {
|
|
|
110
77
|
let routePath = getPathFromFilename(this.lambdaDir, filename);
|
|
111
78
|
if (this.httpMethodDecider === "inputParams") {
|
|
112
79
|
if (routePath.endsWith("/")) {
|
|
113
|
-
routePath += `${handler
|
|
80
|
+
routePath += `${handler === null || handler === void 0 ? void 0 : handler.name}`;
|
|
114
81
|
} else {
|
|
115
|
-
routePath += `/${handler
|
|
82
|
+
routePath += `/${handler === null || handler === void 0 ? void 0 : handler.name}`;
|
|
116
83
|
}
|
|
117
84
|
}
|
|
118
85
|
return routePath;
|
|
@@ -149,9 +116,7 @@ class ApiRouter {
|
|
|
149
116
|
}
|
|
150
117
|
default:
|
|
151
118
|
if (process.env.NODE_ENV !== "test") {
|
|
152
|
-
logger.warn(
|
|
153
|
-
`Only api handlers are allowd to be exported, please remove the function ${originHandlerName} from exports`
|
|
154
|
-
);
|
|
119
|
+
logger.warn(`Only api handlers are allowd to be exported, please remove the function ${originHandlerName} from exports`);
|
|
155
120
|
}
|
|
156
121
|
return null;
|
|
157
122
|
}
|
|
@@ -189,9 +154,9 @@ class ApiRouter {
|
|
|
189
154
|
return apiHandlers;
|
|
190
155
|
}
|
|
191
156
|
/**
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
157
|
+
* 如果用户未传入或传入空串,默认为 /api
|
|
158
|
+
* 如果传入 /,则 prefix 为 /
|
|
159
|
+
*/
|
|
195
160
|
initPrefix(prefix) {
|
|
196
161
|
if (prefix === "/") {
|
|
197
162
|
return "";
|
|
@@ -259,7 +224,39 @@ class ApiRouter {
|
|
|
259
224
|
}
|
|
260
225
|
return `${prefix}${finalRouteName}`;
|
|
261
226
|
}
|
|
227
|
+
constructor({ apiDir, lambdaDir, prefix, httpMethodDecider = "functionName" }) {
|
|
228
|
+
_define_property(this, "apiMode", void 0);
|
|
229
|
+
_define_property(this, "apiDir", void 0);
|
|
230
|
+
_define_property(this, "existLambdaDir", void 0);
|
|
231
|
+
_define_property(this, "httpMethodDecider", void 0);
|
|
232
|
+
_define_property(this, "lambdaDir", void 0);
|
|
233
|
+
_define_property(this, "prefix", void 0);
|
|
234
|
+
_define_property(this, "apiFiles", []);
|
|
235
|
+
_define_property(this, "getExactApiMode", (apiDir2, lambdaDir2) => {
|
|
236
|
+
const exist = this.createExistChecker(apiDir2);
|
|
237
|
+
const existLambdaDir = lambdaDir2 && fs.pathExistsSync(lambdaDir2) || exist(FRAMEWORK_MODE_LAMBDA_DIR);
|
|
238
|
+
const existAppDir = exist(FRAMEWORK_MODE_APP_DIR);
|
|
239
|
+
const existAppFile = exist("app.ts") || exist("app.js");
|
|
240
|
+
if (existLambdaDir || existAppDir || existAppFile) {
|
|
241
|
+
return APIMode.FARMEWORK;
|
|
242
|
+
}
|
|
243
|
+
return APIMode.FUNCTION;
|
|
244
|
+
});
|
|
245
|
+
_define_property(this, "createExistChecker", (base) => (target) => fs.pathExistsSync(path.resolve(base, target)));
|
|
246
|
+
_define_property(this, "getExactLambdaDir", (apiDir2, originLambdaDir) => {
|
|
247
|
+
if (this.apiMode === APIMode.FUNCTION) {
|
|
248
|
+
return apiDir2;
|
|
249
|
+
}
|
|
250
|
+
return originLambdaDir || path.join(apiDir2, FRAMEWORK_MODE_LAMBDA_DIR);
|
|
251
|
+
});
|
|
252
|
+
this.validateAbsolute(apiDir, "apiDir");
|
|
253
|
+
this.validateAbsolute(lambdaDir, "lambdaDir");
|
|
254
|
+
this.prefix = this.initPrefix(prefix);
|
|
255
|
+
this.apiDir = apiDir;
|
|
256
|
+
this.httpMethodDecider = httpMethodDecider;
|
|
257
|
+
this.apiMode = this.getExactApiMode(apiDir, lambdaDir);
|
|
258
|
+
this.lambdaDir = this.getExactLambdaDir(this.apiDir, lambdaDir);
|
|
259
|
+
this.existLambdaDir = fs.existsSync(this.lambdaDir);
|
|
260
|
+
debug(`apiDir:`, this.apiDir, `lambdaDir:`, this.lambdaDir);
|
|
261
|
+
}
|
|
262
262
|
}
|
|
263
|
-
export {
|
|
264
|
-
ApiRouter
|
|
265
|
-
};
|
package/dist/esm/router/types.js
CHANGED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
package/dist/esm/router/utils.js
CHANGED
|
@@ -1,11 +1,11 @@
|
|
|
1
1
|
import path from "path";
|
|
2
2
|
import { globby } from "@modern-js/utils";
|
|
3
3
|
import { INDEX_SUFFIX } from "./constants";
|
|
4
|
-
const getFiles = (lambdaDir, rules) => globby.sync(rules, {
|
|
4
|
+
export const getFiles = (lambdaDir, rules) => globby.sync(rules, {
|
|
5
5
|
cwd: lambdaDir,
|
|
6
6
|
gitignore: true
|
|
7
7
|
}).map((file) => path.resolve(lambdaDir, file));
|
|
8
|
-
const getPathFromFilename = (baseDir, filename) => {
|
|
8
|
+
export const getPathFromFilename = (baseDir, filename) => {
|
|
9
9
|
const relativeName = filename.substring(baseDir.length);
|
|
10
10
|
const relativePath = relativeName.split(".").slice(0, -1).join(".");
|
|
11
11
|
const nameSplit = relativePath.split(path.sep).map((item) => {
|
|
@@ -30,7 +30,7 @@ const clearRouteName = (routeName) => {
|
|
|
30
30
|
}
|
|
31
31
|
return finalRouteName;
|
|
32
32
|
};
|
|
33
|
-
const isHandler = (input) => input && typeof input === "function";
|
|
33
|
+
export const isHandler = (input) => input && typeof input === "function";
|
|
34
34
|
const enableRegister = (requireFn) => {
|
|
35
35
|
let existTsLoader = false;
|
|
36
36
|
let firstCall = true;
|
|
@@ -50,7 +50,9 @@ const enableRegister = (requireFn) => {
|
|
|
50
50
|
},
|
|
51
51
|
scope: true,
|
|
52
52
|
transpileOnly: true,
|
|
53
|
-
ignore: [
|
|
53
|
+
ignore: [
|
|
54
|
+
"(?:^|/)node_modules/"
|
|
55
|
+
]
|
|
54
56
|
});
|
|
55
57
|
existTsLoader = true;
|
|
56
58
|
const tsConfigPaths = require("tsconfig-paths");
|
|
@@ -69,11 +71,13 @@ const enableRegister = (requireFn) => {
|
|
|
69
71
|
};
|
|
70
72
|
};
|
|
71
73
|
const isFunction = (input) => input && {}.toString.call(input) === "[object Function]";
|
|
72
|
-
const requireHandlerModule = enableRegister((modulePath) => {
|
|
74
|
+
export const requireHandlerModule = enableRegister((modulePath) => {
|
|
73
75
|
const originRequire = process.env.NODE_ENV === "test" ? jest.requireActual : require;
|
|
74
76
|
const module = originRequire(modulePath);
|
|
75
77
|
if (isFunction(module)) {
|
|
76
|
-
return {
|
|
78
|
+
return {
|
|
79
|
+
default: module
|
|
80
|
+
};
|
|
77
81
|
}
|
|
78
82
|
return module;
|
|
79
83
|
});
|
|
@@ -83,15 +87,8 @@ const routeValue = (routePath) => {
|
|
|
83
87
|
}
|
|
84
88
|
return 1;
|
|
85
89
|
};
|
|
86
|
-
const sortRoutes = (apiHandlers) => {
|
|
90
|
+
export const sortRoutes = (apiHandlers) => {
|
|
87
91
|
return apiHandlers.sort((handlerA, handlerB) => {
|
|
88
92
|
return routeValue(handlerA.routeName) - routeValue(handlerB.routeName);
|
|
89
93
|
});
|
|
90
94
|
};
|
|
91
|
-
export {
|
|
92
|
-
getFiles,
|
|
93
|
-
getPathFromFilename,
|
|
94
|
-
isHandler,
|
|
95
|
-
requireHandlerModule,
|
|
96
|
-
sortRoutes
|
|
97
|
-
};
|
package/dist/esm/types.js
CHANGED
|
@@ -1,28 +1,29 @@
|
|
|
1
|
-
var OperatorType
|
|
1
|
+
export var OperatorType;
|
|
2
|
+
(function(OperatorType2) {
|
|
2
3
|
OperatorType2[OperatorType2["Trigger"] = 0] = "Trigger";
|
|
3
4
|
OperatorType2[OperatorType2["Middleware"] = 1] = "Middleware";
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
5
|
+
})(OperatorType || (OperatorType = {}));
|
|
6
|
+
export var TriggerType;
|
|
7
|
+
(function(TriggerType2) {
|
|
7
8
|
TriggerType2[TriggerType2["Http"] = 0] = "Http";
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
9
|
+
})(TriggerType || (TriggerType = {}));
|
|
10
|
+
export var HttpMetadata;
|
|
11
|
+
(function(HttpMetadata2) {
|
|
11
12
|
HttpMetadata2["Method"] = "METHOD";
|
|
12
13
|
HttpMetadata2["Data"] = "DATA";
|
|
13
14
|
HttpMetadata2["Query"] = "QUERY";
|
|
14
15
|
HttpMetadata2["Params"] = "PARAMS";
|
|
15
16
|
HttpMetadata2["Headers"] = "HEADERS";
|
|
16
17
|
HttpMetadata2["Response"] = "RESPONSE";
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
18
|
+
})(HttpMetadata || (HttpMetadata = {}));
|
|
19
|
+
export var ResponseMetaType;
|
|
20
|
+
(function(ResponseMetaType2) {
|
|
20
21
|
ResponseMetaType2[ResponseMetaType2["StatusCode"] = 0] = "StatusCode";
|
|
21
22
|
ResponseMetaType2[ResponseMetaType2["Redirect"] = 1] = "Redirect";
|
|
22
23
|
ResponseMetaType2[ResponseMetaType2["Headers"] = 2] = "Headers";
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
24
|
+
})(ResponseMetaType || (ResponseMetaType = {}));
|
|
25
|
+
export var HttpMethod;
|
|
26
|
+
(function(HttpMethod2) {
|
|
26
27
|
HttpMethod2["Get"] = "GET";
|
|
27
28
|
HttpMethod2["Post"] = "POST";
|
|
28
29
|
HttpMethod2["Put"] = "PUT";
|
|
@@ -32,14 +33,5 @@ var HttpMethod = /* @__PURE__ */ ((HttpMethod2) => {
|
|
|
32
33
|
HttpMethod2["Patch"] = "PATCH";
|
|
33
34
|
HttpMethod2["Options"] = "OPTIONS";
|
|
34
35
|
HttpMethod2["Head"] = "HEAD";
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
const httpMethods = Object.values(HttpMethod);
|
|
38
|
-
export {
|
|
39
|
-
HttpMetadata,
|
|
40
|
-
HttpMethod,
|
|
41
|
-
OperatorType,
|
|
42
|
-
ResponseMetaType,
|
|
43
|
-
TriggerType,
|
|
44
|
-
httpMethods
|
|
45
|
-
};
|
|
36
|
+
})(HttpMethod || (HttpMethod = {}));
|
|
37
|
+
export const httpMethods = Object.values(HttpMethod);
|
package/dist/esm/utils/alias.js
CHANGED
|
@@ -2,18 +2,12 @@ import * as path from "path";
|
|
|
2
2
|
import * as os from "os";
|
|
3
3
|
import fs from "fs";
|
|
4
4
|
import Module from "module";
|
|
5
|
-
const getRelativeRuntimePath = (appDirectory, serverRuntimePath) => {
|
|
5
|
+
export const getRelativeRuntimePath = (appDirectory, serverRuntimePath) => {
|
|
6
6
|
let relativeRuntimePath = "";
|
|
7
7
|
if (os.platform() === "win32") {
|
|
8
|
-
relativeRuntimePath = `../${path.relative(
|
|
9
|
-
appDirectory,
|
|
10
|
-
serverRuntimePath
|
|
11
|
-
)}`;
|
|
8
|
+
relativeRuntimePath = `../${path.relative(appDirectory, serverRuntimePath)}`;
|
|
12
9
|
} else {
|
|
13
|
-
relativeRuntimePath = path.join(
|
|
14
|
-
"../",
|
|
15
|
-
path.relative(appDirectory, serverRuntimePath)
|
|
16
|
-
);
|
|
10
|
+
relativeRuntimePath = path.join("../", path.relative(appDirectory, serverRuntimePath));
|
|
17
11
|
}
|
|
18
12
|
if (process.env.NODE_ENV === "development" || process.env.NODE_ENV === "test") {
|
|
19
13
|
relativeRuntimePath = `./${path.relative(appDirectory, serverRuntimePath)}`;
|
|
@@ -23,7 +17,7 @@ const getRelativeRuntimePath = (appDirectory, serverRuntimePath) => {
|
|
|
23
17
|
const sortByLongestPrefix = (arr) => {
|
|
24
18
|
return arr.concat().sort((a, b) => b.length - a.length);
|
|
25
19
|
};
|
|
26
|
-
const createMatchPath = (paths) => {
|
|
20
|
+
export const createMatchPath = (paths) => {
|
|
27
21
|
const sortedKeys = sortByLongestPrefix(Object.keys(paths));
|
|
28
22
|
const sortedPaths = {};
|
|
29
23
|
sortedKeys.forEach((key) => {
|
|
@@ -36,7 +30,9 @@ const createMatchPath = (paths) => {
|
|
|
36
30
|
if (found) {
|
|
37
31
|
let foundPaths = sortedPaths[found];
|
|
38
32
|
if (!Array.isArray(foundPaths)) {
|
|
39
|
-
foundPaths = [
|
|
33
|
+
foundPaths = [
|
|
34
|
+
foundPaths
|
|
35
|
+
];
|
|
40
36
|
}
|
|
41
37
|
foundPaths = foundPaths.filter((foundPath) => path.isAbsolute(foundPath));
|
|
42
38
|
for (const p of foundPaths) {
|
|
@@ -50,7 +46,7 @@ const createMatchPath = (paths) => {
|
|
|
50
46
|
return null;
|
|
51
47
|
};
|
|
52
48
|
};
|
|
53
|
-
const registerPaths = (paths) => {
|
|
49
|
+
export const registerPaths = (paths) => {
|
|
54
50
|
const originalResolveFilename = Module._resolveFilename;
|
|
55
51
|
const { builtinModules } = Module;
|
|
56
52
|
const matchPath = createMatchPath(paths);
|
|
@@ -59,7 +55,10 @@ const registerPaths = (paths) => {
|
|
|
59
55
|
if (!isCoreModule) {
|
|
60
56
|
const matched = matchPath(request);
|
|
61
57
|
if (matched) {
|
|
62
|
-
const modifiedArguments = [
|
|
58
|
+
const modifiedArguments = [
|
|
59
|
+
matched,
|
|
60
|
+
...[].slice.call(arguments, 1)
|
|
61
|
+
];
|
|
63
62
|
return originalResolveFilename.apply(this, modifiedArguments);
|
|
64
63
|
}
|
|
65
64
|
}
|
|
@@ -69,8 +68,3 @@ const registerPaths = (paths) => {
|
|
|
69
68
|
Module._resolveFilename = originalResolveFilename;
|
|
70
69
|
};
|
|
71
70
|
};
|
|
72
|
-
export {
|
|
73
|
-
createMatchPath,
|
|
74
|
-
getRelativeRuntimePath,
|
|
75
|
-
registerPaths
|
|
76
|
-
};
|
package/dist/esm/utils/debug.js
CHANGED
package/dist/esm/utils/index.js
CHANGED
package/dist/esm/utils/meta.js
CHANGED
|
@@ -1,14 +1,8 @@
|
|
|
1
|
-
const HANDLER_WITH_META = "HANDLER_WITH_META";
|
|
2
|
-
const INPUT_PARAMS_DECIDER = "INPUT_PARAMS_DECIDER";
|
|
3
|
-
const isWithMetaHandler = (handler) => {
|
|
1
|
+
export const HANDLER_WITH_META = "HANDLER_WITH_META";
|
|
2
|
+
export const INPUT_PARAMS_DECIDER = "INPUT_PARAMS_DECIDER";
|
|
3
|
+
export const isWithMetaHandler = (handler) => {
|
|
4
4
|
return typeof handler === "function" && handler[HANDLER_WITH_META];
|
|
5
5
|
};
|
|
6
|
-
const isInputParamsDeciderHandler = (handler) => {
|
|
6
|
+
export const isInputParamsDeciderHandler = (handler) => {
|
|
7
7
|
return typeof handler === "function" && handler[INPUT_PARAMS_DECIDER];
|
|
8
8
|
};
|
|
9
|
-
export {
|
|
10
|
-
HANDLER_WITH_META,
|
|
11
|
-
INPUT_PARAMS_DECIDER,
|
|
12
|
-
isInputParamsDeciderHandler,
|
|
13
|
-
isWithMetaHandler
|
|
14
|
-
};
|