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