@modern-js/plugin-express 2.4.1-beta.0 → 2.6.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 +29 -3
- package/dist/{js/node → cjs}/cli/index.js +4 -0
- package/dist/{js/node → cjs}/index.js +4 -0
- package/dist/{js/node → cjs}/plugin.js +57 -75
- package/dist/{js/node → cjs}/runtime/operators.js +24 -45
- package/dist/{js/node → cjs}/utils.js +18 -45
- package/dist/{js/treeshaking → esm}/runtime/operators.js +2 -1
- package/dist/esm-node/plugin.js +149 -0
- package/dist/esm-node/runtime/operators.js +47 -0
- package/dist/{js/modern → esm-node}/utils.js +14 -47
- package/package.json +17 -18
- package/dist/js/modern/plugin.js +0 -171
- package/dist/js/modern/runtime/operators.js +0 -68
- /package/dist/{js/node → cjs}/context.js +0 -0
- /package/dist/{js/node → cjs}/registerRoutes.js +0 -0
- /package/dist/{js/node → cjs}/runtime/hook.js +0 -0
- /package/dist/{js/node → cjs}/runtime/index.js +0 -0
- /package/dist/{js/treeshaking → esm}/cli/index.js +0 -0
- /package/dist/{js/treeshaking → esm}/context.js +0 -0
- /package/dist/{js/treeshaking → esm}/index.js +0 -0
- /package/dist/{js/treeshaking → esm}/plugin.js +0 -0
- /package/dist/{js/treeshaking → esm}/registerRoutes.js +0 -0
- /package/dist/{js/treeshaking → esm}/runtime/hook.js +0 -0
- /package/dist/{js/treeshaking → esm}/runtime/index.js +0 -0
- /package/dist/{js/treeshaking → esm}/utils.js +0 -0
- /package/dist/{js/modern → esm-node}/cli/index.js +0 -0
- /package/dist/{js/modern → esm-node}/context.js +0 -0
- /package/dist/{js/modern → esm-node}/index.js +0 -0
- /package/dist/{js/modern → esm-node}/registerRoutes.js +0 -0
- /package/dist/{js/modern → esm-node}/runtime/hook.js +0 -0
- /package/dist/{js/modern → esm-node}/runtime/index.js +0 -0
package/CHANGELOG.md
CHANGED
|
@@ -1,12 +1,38 @@
|
|
|
1
1
|
# @modern-js/plugin-express
|
|
2
2
|
|
|
3
|
-
## 2.
|
|
3
|
+
## 2.6.0
|
|
4
4
|
|
|
5
5
|
### Patch Changes
|
|
6
6
|
|
|
7
|
+
- Updated dependencies [e1f799e]
|
|
8
|
+
- Updated dependencies [7915ab3]
|
|
9
|
+
- Updated dependencies [49fa0b1]
|
|
10
|
+
- Updated dependencies [0fe658a]
|
|
11
|
+
- Updated dependencies [62930b9]
|
|
12
|
+
- @modern-js/utils@2.6.0
|
|
13
|
+
- @modern-js/types@2.6.0
|
|
14
|
+
- @modern-js/bff-core@2.6.0
|
|
15
|
+
- @modern-js/bff-runtime@2.6.0
|
|
16
|
+
|
|
17
|
+
## 2.5.0
|
|
18
|
+
|
|
19
|
+
### Patch Changes
|
|
20
|
+
|
|
21
|
+
- 89ca6cc: refactor: merge build-config into scripts/build
|
|
22
|
+
|
|
23
|
+
refactor: 把 build-config 合并进 scripts/build
|
|
24
|
+
|
|
25
|
+
- 30614fa: chore: modify package.json entry fields and build config
|
|
26
|
+
chore: 更改 package.json entry 字段以及构建配置
|
|
27
|
+
- Updated dependencies [89ca6cc]
|
|
28
|
+
- Updated dependencies [7cb8bb4]
|
|
29
|
+
- Updated dependencies [30614fa]
|
|
30
|
+
- Updated dependencies [1b0ce87]
|
|
7
31
|
- Updated dependencies [11c053b]
|
|
8
|
-
- @modern-js/
|
|
9
|
-
- @modern-js/bff-
|
|
32
|
+
- @modern-js/bff-core@2.5.0
|
|
33
|
+
- @modern-js/bff-runtime@2.5.0
|
|
34
|
+
- @modern-js/types@2.5.0
|
|
35
|
+
- @modern-js/utils@2.5.0
|
|
10
36
|
|
|
11
37
|
## 2.4.0
|
|
12
38
|
|
|
@@ -17,6 +17,10 @@ var __copyProps = (to, from, except, desc) => {
|
|
|
17
17
|
return to;
|
|
18
18
|
};
|
|
19
19
|
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
|
|
20
|
+
// If the importer is in node compatibility mode or this is not an ESM
|
|
21
|
+
// file that has been converted to a CommonJS file using a Babel-
|
|
22
|
+
// compatible transform (i.e. "__esModule" has not been set), then set
|
|
23
|
+
// "default" to the CommonJS "module.exports" for node compatibility.
|
|
20
24
|
isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
|
|
21
25
|
mod
|
|
22
26
|
));
|
|
@@ -18,6 +18,10 @@ var __copyProps = (to, from, except, desc) => {
|
|
|
18
18
|
};
|
|
19
19
|
var __reExport = (target, mod, secondTarget) => (__copyProps(target, mod, "default"), secondTarget && __copyProps(secondTarget, mod, "default"));
|
|
20
20
|
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
|
|
21
|
+
// If the importer is in node compatibility mode or this is not an ESM
|
|
22
|
+
// file that has been converted to a CommonJS file using a Babel-
|
|
23
|
+
// compatible transform (i.e. "__esModule" has not been set), then set
|
|
24
|
+
// "default" to the CommonJS "module.exports" for node compatibility.
|
|
21
25
|
isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
|
|
22
26
|
mod
|
|
23
27
|
));
|
|
@@ -17,30 +17,14 @@ var __copyProps = (to, from, except, desc) => {
|
|
|
17
17
|
return to;
|
|
18
18
|
};
|
|
19
19
|
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
|
|
20
|
+
// If the importer is in node compatibility mode or this is not an ESM
|
|
21
|
+
// file that has been converted to a CommonJS file using a Babel-
|
|
22
|
+
// compatible transform (i.e. "__esModule" has not been set), then set
|
|
23
|
+
// "default" to the CommonJS "module.exports" for node compatibility.
|
|
20
24
|
isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
|
|
21
25
|
mod
|
|
22
26
|
));
|
|
23
27
|
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
24
|
-
var __async = (__this, __arguments, generator) => {
|
|
25
|
-
return new Promise((resolve, reject) => {
|
|
26
|
-
var fulfilled = (value) => {
|
|
27
|
-
try {
|
|
28
|
-
step(generator.next(value));
|
|
29
|
-
} catch (e) {
|
|
30
|
-
reject(e);
|
|
31
|
-
}
|
|
32
|
-
};
|
|
33
|
-
var rejected = (value) => {
|
|
34
|
-
try {
|
|
35
|
-
step(generator.throw(value));
|
|
36
|
-
} catch (e) {
|
|
37
|
-
reject(e);
|
|
38
|
-
}
|
|
39
|
-
};
|
|
40
|
-
var step = (x) => x.done ? resolve(x.value) : Promise.resolve(x.value).then(fulfilled, rejected);
|
|
41
|
-
step((generator = generator.apply(__this, __arguments)).next());
|
|
42
|
-
});
|
|
43
|
-
};
|
|
44
28
|
var plugin_exports = {};
|
|
45
29
|
__export(plugin_exports, {
|
|
46
30
|
default: () => plugin_default
|
|
@@ -54,14 +38,14 @@ var import_finalhandler = __toESM(require("finalhandler"));
|
|
|
54
38
|
var import_context = require("./context");
|
|
55
39
|
var import_registerRoutes = __toESM(require("./registerRoutes"));
|
|
56
40
|
const debug = (0, import_utils.createDebugger)("express");
|
|
57
|
-
const findAppModule = (apiDir) =>
|
|
41
|
+
const findAppModule = async (apiDir) => {
|
|
58
42
|
const exts = [
|
|
59
43
|
".ts",
|
|
60
44
|
".js"
|
|
61
45
|
];
|
|
62
46
|
const paths = exts.map((ext) => path.resolve(apiDir, `app${ext}`));
|
|
63
47
|
for (const filename of paths) {
|
|
64
|
-
if (
|
|
48
|
+
if (await import_utils.fs.pathExists(filename)) {
|
|
65
49
|
delete require.cache[filename];
|
|
66
50
|
return [
|
|
67
51
|
(0, import_utils.compatRequire)(filename),
|
|
@@ -70,7 +54,7 @@ const findAppModule = (apiDir) => __async(void 0, null, function* () {
|
|
|
70
54
|
}
|
|
71
55
|
}
|
|
72
56
|
return [];
|
|
73
|
-
}
|
|
57
|
+
};
|
|
74
58
|
const initMiddlewares = (middleware, app) => {
|
|
75
59
|
middleware.forEach((middlewareItem) => {
|
|
76
60
|
const middlewareFunc = typeof middlewareItem === "string" ? (0, import_utils.compatRequire)(middlewareItem) : middlewareItem;
|
|
@@ -103,66 +87,64 @@ var plugin_default = () => ({
|
|
|
103
87
|
"@modern-js/plugin-server"
|
|
104
88
|
],
|
|
105
89
|
setup: (api) => ({
|
|
106
|
-
prepareApiServer(
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
if (!app || !app.use) {
|
|
118
|
-
app = (0, import_express.default)();
|
|
119
|
-
}
|
|
120
|
-
initApp(app);
|
|
121
|
-
if (config) {
|
|
122
|
-
const { middleware } = config;
|
|
123
|
-
initMiddlewares(middleware, app);
|
|
124
|
-
}
|
|
125
|
-
useRun(app);
|
|
126
|
-
(0, import_registerRoutes.default)(app, apiHandlerInfos);
|
|
127
|
-
if (hooks) {
|
|
128
|
-
const { afterLambdaRegisted } = hooks;
|
|
129
|
-
if (afterLambdaRegisted) {
|
|
130
|
-
afterLambdaRegisted(app);
|
|
131
|
-
}
|
|
132
|
-
}
|
|
133
|
-
} else if (mode === "function") {
|
|
90
|
+
async prepareApiServer({ pwd, config }) {
|
|
91
|
+
let app;
|
|
92
|
+
const apiDir = path.join(pwd, "./api");
|
|
93
|
+
const appContext = api.useAppContext();
|
|
94
|
+
const apiHandlerInfos = appContext.apiHandlerInfos;
|
|
95
|
+
const mode = appContext.apiMode;
|
|
96
|
+
if (mode === "framework") {
|
|
97
|
+
const appModule = await findAppModule(apiDir);
|
|
98
|
+
app = appModule[0];
|
|
99
|
+
const hooks = appModule[1];
|
|
100
|
+
if (!app || !app.use) {
|
|
134
101
|
app = (0, import_express.default)();
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
102
|
+
}
|
|
103
|
+
initApp(app);
|
|
104
|
+
if (config) {
|
|
105
|
+
const { middleware } = config;
|
|
106
|
+
initMiddlewares(middleware, app);
|
|
107
|
+
}
|
|
108
|
+
useRun(app);
|
|
109
|
+
(0, import_registerRoutes.default)(app, apiHandlerInfos);
|
|
110
|
+
if (hooks) {
|
|
111
|
+
const { afterLambdaRegisted } = hooks;
|
|
112
|
+
if (afterLambdaRegisted) {
|
|
113
|
+
afterLambdaRegisted(app);
|
|
139
114
|
}
|
|
140
|
-
useRun(app);
|
|
141
|
-
(0, import_registerRoutes.default)(app, apiHandlerInfos);
|
|
142
|
-
} else {
|
|
143
|
-
throw new Error(`mode must be function or framework`);
|
|
144
115
|
}
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
116
|
+
} else if (mode === "function") {
|
|
117
|
+
app = (0, import_express.default)();
|
|
118
|
+
initApp(app);
|
|
119
|
+
if (config) {
|
|
120
|
+
const { middleware: middleware1 } = config;
|
|
121
|
+
initMiddlewares(middleware1, app);
|
|
122
|
+
}
|
|
123
|
+
useRun(app);
|
|
124
|
+
(0, import_registerRoutes.default)(app, apiHandlerInfos);
|
|
125
|
+
} else {
|
|
126
|
+
throw new Error(`mode must be function or framework`);
|
|
127
|
+
}
|
|
128
|
+
return (req, res) => new Promise((resolve, reject) => {
|
|
129
|
+
const handler = (err) => {
|
|
130
|
+
if (err) {
|
|
131
|
+
return reject(err);
|
|
132
|
+
}
|
|
133
|
+
return (0, import_finalhandler.default)(req, res, {})(null);
|
|
134
|
+
};
|
|
135
|
+
res.on("finish", (err) => {
|
|
136
|
+
if (err) {
|
|
137
|
+
return reject(err);
|
|
138
|
+
}
|
|
139
|
+
return resolve();
|
|
159
140
|
});
|
|
141
|
+
return app(req, res, handler);
|
|
160
142
|
});
|
|
161
143
|
},
|
|
162
144
|
prepareWebServer({ config }, next) {
|
|
163
|
-
var
|
|
145
|
+
var _a;
|
|
164
146
|
const userConfig = api.useConfigContext();
|
|
165
|
-
if (!((
|
|
147
|
+
if (!((_a = userConfig == null ? void 0 : userConfig.server) == null ? void 0 : _a.enableFrameworkExt)) {
|
|
166
148
|
return next();
|
|
167
149
|
}
|
|
168
150
|
const app = (0, import_express.default)();
|
|
@@ -15,26 +15,6 @@ var __copyProps = (to, from, except, desc) => {
|
|
|
15
15
|
return to;
|
|
16
16
|
};
|
|
17
17
|
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
18
|
-
var __async = (__this, __arguments, generator) => {
|
|
19
|
-
return new Promise((resolve, reject) => {
|
|
20
|
-
var fulfilled = (value) => {
|
|
21
|
-
try {
|
|
22
|
-
step(generator.next(value));
|
|
23
|
-
} catch (e) {
|
|
24
|
-
reject(e);
|
|
25
|
-
}
|
|
26
|
-
};
|
|
27
|
-
var rejected = (value) => {
|
|
28
|
-
try {
|
|
29
|
-
step(generator.throw(value));
|
|
30
|
-
} catch (e) {
|
|
31
|
-
reject(e);
|
|
32
|
-
}
|
|
33
|
-
};
|
|
34
|
-
var step = (x) => x.done ? resolve(x.value) : Promise.resolve(x.value).then(fulfilled, rejected);
|
|
35
|
-
step((generator = generator.apply(__this, __arguments)).next());
|
|
36
|
-
});
|
|
37
|
-
};
|
|
38
18
|
var operators_exports = {};
|
|
39
19
|
__export(operators_exports, {
|
|
40
20
|
Middleware: () => Middleware,
|
|
@@ -45,33 +25,32 @@ var import_context = require("../context");
|
|
|
45
25
|
const Pipe = (func) => {
|
|
46
26
|
return {
|
|
47
27
|
name: "pipe",
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
}
|
|
28
|
+
// eslint-disable-next-line consistent-return
|
|
29
|
+
async execute(executeHelper, next) {
|
|
30
|
+
const { inputs } = executeHelper;
|
|
31
|
+
const ctx = (0, import_context.useContext)();
|
|
32
|
+
const { res } = ctx;
|
|
33
|
+
if (typeof func === "function") {
|
|
34
|
+
let isPiped = true;
|
|
35
|
+
const end = (value) => {
|
|
36
|
+
isPiped = false;
|
|
37
|
+
if (typeof value === "function") {
|
|
38
|
+
value(res);
|
|
39
|
+
return;
|
|
40
|
+
}
|
|
41
|
+
return value;
|
|
42
|
+
};
|
|
43
|
+
const output = await func(inputs, end);
|
|
44
|
+
if (!isPiped) {
|
|
45
|
+
if (output) {
|
|
46
|
+
return executeHelper.result = output;
|
|
47
|
+
} else {
|
|
48
|
+
return;
|
|
70
49
|
}
|
|
71
|
-
executeHelper.inputs = output;
|
|
72
|
-
yield next();
|
|
73
50
|
}
|
|
74
|
-
|
|
51
|
+
executeHelper.inputs = output;
|
|
52
|
+
await next();
|
|
53
|
+
}
|
|
75
54
|
}
|
|
76
55
|
};
|
|
77
56
|
};
|
|
@@ -2,22 +2,8 @@ var __create = Object.create;
|
|
|
2
2
|
var __defProp = Object.defineProperty;
|
|
3
3
|
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
4
4
|
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
5
|
-
var __getOwnPropSymbols = Object.getOwnPropertySymbols;
|
|
6
5
|
var __getProtoOf = Object.getPrototypeOf;
|
|
7
6
|
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
7
|
var __export = (target, all) => {
|
|
22
8
|
for (var name in all)
|
|
23
9
|
__defProp(target, name, { get: all[name], enumerable: true });
|
|
@@ -31,30 +17,14 @@ var __copyProps = (to, from, except, desc) => {
|
|
|
31
17
|
return to;
|
|
32
18
|
};
|
|
33
19
|
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
|
|
20
|
+
// If the importer is in node compatibility mode or this is not an ESM
|
|
21
|
+
// file that has been converted to a CommonJS file using a Babel-
|
|
22
|
+
// compatible transform (i.e. "__esModule" has not been set), then set
|
|
23
|
+
// "default" to the CommonJS "module.exports" for node compatibility.
|
|
34
24
|
isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
|
|
35
25
|
mod
|
|
36
26
|
));
|
|
37
27
|
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
38
|
-
var __async = (__this, __arguments, generator) => {
|
|
39
|
-
return new Promise((resolve, reject) => {
|
|
40
|
-
var fulfilled = (value) => {
|
|
41
|
-
try {
|
|
42
|
-
step(generator.next(value));
|
|
43
|
-
} catch (e) {
|
|
44
|
-
reject(e);
|
|
45
|
-
}
|
|
46
|
-
};
|
|
47
|
-
var rejected = (value) => {
|
|
48
|
-
try {
|
|
49
|
-
step(generator.throw(value));
|
|
50
|
-
} catch (e) {
|
|
51
|
-
reject(e);
|
|
52
|
-
}
|
|
53
|
-
};
|
|
54
|
-
var step = (x) => x.done ? resolve(x.value) : Promise.resolve(x.value).then(fulfilled, rejected);
|
|
55
|
-
step((generator = generator.apply(__this, __arguments)).next());
|
|
56
|
-
});
|
|
57
|
-
};
|
|
58
28
|
var utils_exports = {};
|
|
59
29
|
__export(utils_exports, {
|
|
60
30
|
createRouteHandler: () => createRouteHandler,
|
|
@@ -91,15 +61,15 @@ const handleResponseMeta = (res, handler) => {
|
|
|
91
61
|
}
|
|
92
62
|
};
|
|
93
63
|
const createRouteHandler = (handler) => {
|
|
94
|
-
const apiHandler = (req, res, next) =>
|
|
95
|
-
const input =
|
|
64
|
+
const apiHandler = async (req, res, next) => {
|
|
65
|
+
const input = await getInputFromRequest(req);
|
|
96
66
|
if ((0, import_bff_core.isWithMetaHandler)(handler)) {
|
|
97
67
|
try {
|
|
98
68
|
handleResponseMeta(res, handler);
|
|
99
69
|
if (res.headersSent) {
|
|
100
70
|
return;
|
|
101
71
|
}
|
|
102
|
-
const result =
|
|
72
|
+
const result = await handler(input);
|
|
103
73
|
if (result && typeof result === "object") {
|
|
104
74
|
return res.json(result);
|
|
105
75
|
}
|
|
@@ -113,7 +83,7 @@ const createRouteHandler = (handler) => {
|
|
|
113
83
|
throw error;
|
|
114
84
|
}
|
|
115
85
|
} else if ((0, import_bff_runtime.isSchemaHandler)(handler)) {
|
|
116
|
-
const result1 =
|
|
86
|
+
const result1 = await handler(input);
|
|
117
87
|
if (result1.type !== "HandleSuccess") {
|
|
118
88
|
if (result1.type === "InputValidationError") {
|
|
119
89
|
res.status(400);
|
|
@@ -128,9 +98,9 @@ const createRouteHandler = (handler) => {
|
|
|
128
98
|
} else {
|
|
129
99
|
const args = Object.values(input.params).concat(input);
|
|
130
100
|
try {
|
|
131
|
-
const body =
|
|
101
|
+
const body = await handler(...args);
|
|
132
102
|
if (res.headersSent) {
|
|
133
|
-
return
|
|
103
|
+
return await Promise.resolve();
|
|
134
104
|
}
|
|
135
105
|
if (typeof body !== "undefined") {
|
|
136
106
|
return res.json(body);
|
|
@@ -139,12 +109,12 @@ const createRouteHandler = (handler) => {
|
|
|
139
109
|
return next(e);
|
|
140
110
|
}
|
|
141
111
|
}
|
|
142
|
-
}
|
|
112
|
+
};
|
|
143
113
|
Object.defineProperties(apiHandler, Object.getOwnPropertyDescriptors(handler));
|
|
144
114
|
return apiHandler;
|
|
145
115
|
};
|
|
146
116
|
const isNormalMethod = (httpMethod) => import_bff_core.httpMethods.includes(httpMethod);
|
|
147
|
-
const getInputFromRequest = (request) =>
|
|
117
|
+
const getInputFromRequest = async (request) => {
|
|
148
118
|
const draft = {
|
|
149
119
|
params: request.params,
|
|
150
120
|
query: request.query,
|
|
@@ -158,7 +128,7 @@ const getInputFromRequest = (request) => __async(void 0, null, function* () {
|
|
|
158
128
|
} else if ((0, import_type_is.default)(request, [
|
|
159
129
|
"multipart/form-data"
|
|
160
130
|
])) {
|
|
161
|
-
draft.formData =
|
|
131
|
+
draft.formData = await resolveFormData(request);
|
|
162
132
|
} else if ((0, import_type_is.default)(request, [
|
|
163
133
|
"application/x-www-form-urlencoded"
|
|
164
134
|
])) {
|
|
@@ -167,7 +137,7 @@ const getInputFromRequest = (request) => __async(void 0, null, function* () {
|
|
|
167
137
|
draft.body = request.body;
|
|
168
138
|
}
|
|
169
139
|
return draft;
|
|
170
|
-
}
|
|
140
|
+
};
|
|
171
141
|
const resolveFormData = (request) => {
|
|
172
142
|
const form = (0, import_formidable.default)({
|
|
173
143
|
multiples: true
|
|
@@ -177,7 +147,10 @@ const resolveFormData = (request) => {
|
|
|
177
147
|
if (err) {
|
|
178
148
|
reject(err);
|
|
179
149
|
}
|
|
180
|
-
resolve(
|
|
150
|
+
resolve({
|
|
151
|
+
...fields,
|
|
152
|
+
...files
|
|
153
|
+
});
|
|
181
154
|
});
|
|
182
155
|
});
|
|
183
156
|
};
|
|
@@ -126,7 +126,8 @@ import { useContext } from "../context";
|
|
|
126
126
|
var Pipe = function(func) {
|
|
127
127
|
return {
|
|
128
128
|
name: "pipe",
|
|
129
|
-
execute:
|
|
129
|
+
execute: // eslint-disable-next-line consistent-return
|
|
130
|
+
function execute(executeHelper, next) {
|
|
130
131
|
return _asyncToGenerator(function() {
|
|
131
132
|
var inputs, ctx, res, isPiped, end, output;
|
|
132
133
|
return __generator(this, function(_state) {
|
|
@@ -0,0 +1,149 @@
|
|
|
1
|
+
import * as path from "path";
|
|
2
|
+
import express from "express";
|
|
3
|
+
import cookieParser from "cookie-parser";
|
|
4
|
+
import { fs, createDebugger, compatRequire } from "@modern-js/utils";
|
|
5
|
+
import finalhandler from "finalhandler";
|
|
6
|
+
import { run } from "./context";
|
|
7
|
+
import registerRoutes from "./registerRoutes";
|
|
8
|
+
const debug = createDebugger("express");
|
|
9
|
+
const findAppModule = async (apiDir) => {
|
|
10
|
+
const exts = [
|
|
11
|
+
".ts",
|
|
12
|
+
".js"
|
|
13
|
+
];
|
|
14
|
+
const paths = exts.map((ext) => path.resolve(apiDir, `app${ext}`));
|
|
15
|
+
for (const filename of paths) {
|
|
16
|
+
if (await fs.pathExists(filename)) {
|
|
17
|
+
delete require.cache[filename];
|
|
18
|
+
return [
|
|
19
|
+
compatRequire(filename),
|
|
20
|
+
require(filename)
|
|
21
|
+
];
|
|
22
|
+
}
|
|
23
|
+
}
|
|
24
|
+
return [];
|
|
25
|
+
};
|
|
26
|
+
const initMiddlewares = (middleware, app) => {
|
|
27
|
+
middleware.forEach((middlewareItem) => {
|
|
28
|
+
const middlewareFunc = typeof middlewareItem === "string" ? compatRequire(middlewareItem) : middlewareItem;
|
|
29
|
+
app.use(middlewareFunc);
|
|
30
|
+
});
|
|
31
|
+
};
|
|
32
|
+
const useRun = (app) => {
|
|
33
|
+
app.use((req, res, next) => {
|
|
34
|
+
run({
|
|
35
|
+
req,
|
|
36
|
+
res
|
|
37
|
+
}, next);
|
|
38
|
+
});
|
|
39
|
+
};
|
|
40
|
+
const initApp = (app) => {
|
|
41
|
+
app.use(cookieParser());
|
|
42
|
+
app.use(express.text());
|
|
43
|
+
app.use(express.urlencoded({
|
|
44
|
+
extended: true
|
|
45
|
+
}));
|
|
46
|
+
app.use(express.json());
|
|
47
|
+
return app;
|
|
48
|
+
};
|
|
49
|
+
var plugin_default = () => ({
|
|
50
|
+
name: "@modern-js/plugin-express",
|
|
51
|
+
pre: [
|
|
52
|
+
"@modern-js/plugin-bff"
|
|
53
|
+
],
|
|
54
|
+
post: [
|
|
55
|
+
"@modern-js/plugin-server"
|
|
56
|
+
],
|
|
57
|
+
setup: (api) => ({
|
|
58
|
+
async prepareApiServer({ pwd, config }) {
|
|
59
|
+
let app;
|
|
60
|
+
const apiDir = path.join(pwd, "./api");
|
|
61
|
+
const appContext = api.useAppContext();
|
|
62
|
+
const apiHandlerInfos = appContext.apiHandlerInfos;
|
|
63
|
+
const mode = appContext.apiMode;
|
|
64
|
+
if (mode === "framework") {
|
|
65
|
+
const appModule = await findAppModule(apiDir);
|
|
66
|
+
app = appModule[0];
|
|
67
|
+
const hooks = appModule[1];
|
|
68
|
+
if (!app || !app.use) {
|
|
69
|
+
app = express();
|
|
70
|
+
}
|
|
71
|
+
initApp(app);
|
|
72
|
+
if (config) {
|
|
73
|
+
const { middleware } = config;
|
|
74
|
+
initMiddlewares(middleware, app);
|
|
75
|
+
}
|
|
76
|
+
useRun(app);
|
|
77
|
+
registerRoutes(app, apiHandlerInfos);
|
|
78
|
+
if (hooks) {
|
|
79
|
+
const { afterLambdaRegisted } = hooks;
|
|
80
|
+
if (afterLambdaRegisted) {
|
|
81
|
+
afterLambdaRegisted(app);
|
|
82
|
+
}
|
|
83
|
+
}
|
|
84
|
+
} else if (mode === "function") {
|
|
85
|
+
app = express();
|
|
86
|
+
initApp(app);
|
|
87
|
+
if (config) {
|
|
88
|
+
const { middleware: middleware1 } = config;
|
|
89
|
+
initMiddlewares(middleware1, app);
|
|
90
|
+
}
|
|
91
|
+
useRun(app);
|
|
92
|
+
registerRoutes(app, apiHandlerInfos);
|
|
93
|
+
} else {
|
|
94
|
+
throw new Error(`mode must be function or framework`);
|
|
95
|
+
}
|
|
96
|
+
return (req, res) => new Promise((resolve, reject) => {
|
|
97
|
+
const handler = (err) => {
|
|
98
|
+
if (err) {
|
|
99
|
+
return reject(err);
|
|
100
|
+
}
|
|
101
|
+
return finalhandler(req, res, {})(null);
|
|
102
|
+
};
|
|
103
|
+
res.on("finish", (err) => {
|
|
104
|
+
if (err) {
|
|
105
|
+
return reject(err);
|
|
106
|
+
}
|
|
107
|
+
return resolve();
|
|
108
|
+
});
|
|
109
|
+
return app(req, res, handler);
|
|
110
|
+
});
|
|
111
|
+
},
|
|
112
|
+
prepareWebServer({ config }, next) {
|
|
113
|
+
var _a;
|
|
114
|
+
const userConfig = api.useConfigContext();
|
|
115
|
+
if (!((_a = userConfig == null ? void 0 : userConfig.server) == null ? void 0 : _a.enableFrameworkExt)) {
|
|
116
|
+
return next();
|
|
117
|
+
}
|
|
118
|
+
const app = express();
|
|
119
|
+
initApp(app);
|
|
120
|
+
if (config) {
|
|
121
|
+
const { middleware } = config;
|
|
122
|
+
debug("web middleware", middleware);
|
|
123
|
+
initMiddlewares(middleware, app);
|
|
124
|
+
}
|
|
125
|
+
return (ctx) => new Promise((resolve, reject) => {
|
|
126
|
+
const { source: { req, res } } = ctx;
|
|
127
|
+
const handler = (err) => {
|
|
128
|
+
if (err) {
|
|
129
|
+
return reject(err);
|
|
130
|
+
}
|
|
131
|
+
if (res.headersSent && res.statusCode !== 200) {
|
|
132
|
+
finalhandler(req, res, {})(null);
|
|
133
|
+
}
|
|
134
|
+
return resolve();
|
|
135
|
+
};
|
|
136
|
+
res.on("finish", (err) => {
|
|
137
|
+
if (err) {
|
|
138
|
+
return reject(err);
|
|
139
|
+
}
|
|
140
|
+
return resolve();
|
|
141
|
+
});
|
|
142
|
+
return app(req, res, handler);
|
|
143
|
+
});
|
|
144
|
+
}
|
|
145
|
+
})
|
|
146
|
+
});
|
|
147
|
+
export {
|
|
148
|
+
plugin_default as default
|
|
149
|
+
};
|
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
import { useContext } from "../context";
|
|
2
|
+
const Pipe = (func) => {
|
|
3
|
+
return {
|
|
4
|
+
name: "pipe",
|
|
5
|
+
// eslint-disable-next-line consistent-return
|
|
6
|
+
async execute(executeHelper, next) {
|
|
7
|
+
const { inputs } = executeHelper;
|
|
8
|
+
const ctx = useContext();
|
|
9
|
+
const { res } = ctx;
|
|
10
|
+
if (typeof func === "function") {
|
|
11
|
+
let isPiped = true;
|
|
12
|
+
const end = (value) => {
|
|
13
|
+
isPiped = false;
|
|
14
|
+
if (typeof value === "function") {
|
|
15
|
+
value(res);
|
|
16
|
+
return;
|
|
17
|
+
}
|
|
18
|
+
return value;
|
|
19
|
+
};
|
|
20
|
+
const output = await func(inputs, end);
|
|
21
|
+
if (!isPiped) {
|
|
22
|
+
if (output) {
|
|
23
|
+
return executeHelper.result = output;
|
|
24
|
+
} else {
|
|
25
|
+
return;
|
|
26
|
+
}
|
|
27
|
+
}
|
|
28
|
+
executeHelper.inputs = output;
|
|
29
|
+
await next();
|
|
30
|
+
}
|
|
31
|
+
}
|
|
32
|
+
};
|
|
33
|
+
};
|
|
34
|
+
const Middleware = (middleware) => {
|
|
35
|
+
return {
|
|
36
|
+
name: "middleware",
|
|
37
|
+
metadata(helper) {
|
|
38
|
+
const middlewares = helper.getMetadata("pipe") || [];
|
|
39
|
+
middlewares.push(middleware);
|
|
40
|
+
helper.setMetadata("middleware", middlewares);
|
|
41
|
+
}
|
|
42
|
+
};
|
|
43
|
+
};
|
|
44
|
+
export {
|
|
45
|
+
Middleware,
|
|
46
|
+
Pipe
|
|
47
|
+
};
|
|
@@ -1,39 +1,3 @@
|
|
|
1
|
-
var __defProp = Object.defineProperty;
|
|
2
|
-
var __getOwnPropSymbols = Object.getOwnPropertySymbols;
|
|
3
|
-
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
4
|
-
var __propIsEnum = Object.prototype.propertyIsEnumerable;
|
|
5
|
-
var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
|
|
6
|
-
var __spreadValues = (a, b) => {
|
|
7
|
-
for (var prop in b || (b = {}))
|
|
8
|
-
if (__hasOwnProp.call(b, prop))
|
|
9
|
-
__defNormalProp(a, prop, b[prop]);
|
|
10
|
-
if (__getOwnPropSymbols)
|
|
11
|
-
for (var prop of __getOwnPropSymbols(b)) {
|
|
12
|
-
if (__propIsEnum.call(b, prop))
|
|
13
|
-
__defNormalProp(a, prop, b[prop]);
|
|
14
|
-
}
|
|
15
|
-
return a;
|
|
16
|
-
};
|
|
17
|
-
var __async = (__this, __arguments, generator) => {
|
|
18
|
-
return new Promise((resolve, reject) => {
|
|
19
|
-
var fulfilled = (value) => {
|
|
20
|
-
try {
|
|
21
|
-
step(generator.next(value));
|
|
22
|
-
} catch (e) {
|
|
23
|
-
reject(e);
|
|
24
|
-
}
|
|
25
|
-
};
|
|
26
|
-
var rejected = (value) => {
|
|
27
|
-
try {
|
|
28
|
-
step(generator.throw(value));
|
|
29
|
-
} catch (e) {
|
|
30
|
-
reject(e);
|
|
31
|
-
}
|
|
32
|
-
};
|
|
33
|
-
var step = (x) => x.done ? resolve(x.value) : Promise.resolve(x.value).then(fulfilled, rejected);
|
|
34
|
-
step((generator = generator.apply(__this, __arguments)).next());
|
|
35
|
-
});
|
|
36
|
-
};
|
|
37
1
|
import "reflect-metadata";
|
|
38
2
|
import { httpMethods, isWithMetaHandler, HttpMetadata, ResponseMetaType, ValidationError } from "@modern-js/bff-core";
|
|
39
3
|
import { isSchemaHandler } from "@modern-js/bff-runtime";
|
|
@@ -64,15 +28,15 @@ const handleResponseMeta = (res, handler) => {
|
|
|
64
28
|
}
|
|
65
29
|
};
|
|
66
30
|
const createRouteHandler = (handler) => {
|
|
67
|
-
const apiHandler = (req, res, next) =>
|
|
68
|
-
const input =
|
|
31
|
+
const apiHandler = async (req, res, next) => {
|
|
32
|
+
const input = await getInputFromRequest(req);
|
|
69
33
|
if (isWithMetaHandler(handler)) {
|
|
70
34
|
try {
|
|
71
35
|
handleResponseMeta(res, handler);
|
|
72
36
|
if (res.headersSent) {
|
|
73
37
|
return;
|
|
74
38
|
}
|
|
75
|
-
const result =
|
|
39
|
+
const result = await handler(input);
|
|
76
40
|
if (result && typeof result === "object") {
|
|
77
41
|
return res.json(result);
|
|
78
42
|
}
|
|
@@ -86,7 +50,7 @@ const createRouteHandler = (handler) => {
|
|
|
86
50
|
throw error;
|
|
87
51
|
}
|
|
88
52
|
} else if (isSchemaHandler(handler)) {
|
|
89
|
-
const result1 =
|
|
53
|
+
const result1 = await handler(input);
|
|
90
54
|
if (result1.type !== "HandleSuccess") {
|
|
91
55
|
if (result1.type === "InputValidationError") {
|
|
92
56
|
res.status(400);
|
|
@@ -101,9 +65,9 @@ const createRouteHandler = (handler) => {
|
|
|
101
65
|
} else {
|
|
102
66
|
const args = Object.values(input.params).concat(input);
|
|
103
67
|
try {
|
|
104
|
-
const body =
|
|
68
|
+
const body = await handler(...args);
|
|
105
69
|
if (res.headersSent) {
|
|
106
|
-
return
|
|
70
|
+
return await Promise.resolve();
|
|
107
71
|
}
|
|
108
72
|
if (typeof body !== "undefined") {
|
|
109
73
|
return res.json(body);
|
|
@@ -112,12 +76,12 @@ const createRouteHandler = (handler) => {
|
|
|
112
76
|
return next(e);
|
|
113
77
|
}
|
|
114
78
|
}
|
|
115
|
-
}
|
|
79
|
+
};
|
|
116
80
|
Object.defineProperties(apiHandler, Object.getOwnPropertyDescriptors(handler));
|
|
117
81
|
return apiHandler;
|
|
118
82
|
};
|
|
119
83
|
const isNormalMethod = (httpMethod) => httpMethods.includes(httpMethod);
|
|
120
|
-
const getInputFromRequest = (request) =>
|
|
84
|
+
const getInputFromRequest = async (request) => {
|
|
121
85
|
const draft = {
|
|
122
86
|
params: request.params,
|
|
123
87
|
query: request.query,
|
|
@@ -131,7 +95,7 @@ const getInputFromRequest = (request) => __async(void 0, null, function* () {
|
|
|
131
95
|
} else if (typeIs(request, [
|
|
132
96
|
"multipart/form-data"
|
|
133
97
|
])) {
|
|
134
|
-
draft.formData =
|
|
98
|
+
draft.formData = await resolveFormData(request);
|
|
135
99
|
} else if (typeIs(request, [
|
|
136
100
|
"application/x-www-form-urlencoded"
|
|
137
101
|
])) {
|
|
@@ -140,7 +104,7 @@ const getInputFromRequest = (request) => __async(void 0, null, function* () {
|
|
|
140
104
|
draft.body = request.body;
|
|
141
105
|
}
|
|
142
106
|
return draft;
|
|
143
|
-
}
|
|
107
|
+
};
|
|
144
108
|
const resolveFormData = (request) => {
|
|
145
109
|
const form = formidable({
|
|
146
110
|
multiples: true
|
|
@@ -150,7 +114,10 @@ const resolveFormData = (request) => {
|
|
|
150
114
|
if (err) {
|
|
151
115
|
reject(err);
|
|
152
116
|
}
|
|
153
|
-
resolve(
|
|
117
|
+
resolve({
|
|
118
|
+
...fields,
|
|
119
|
+
...files
|
|
120
|
+
});
|
|
154
121
|
});
|
|
155
122
|
});
|
|
156
123
|
};
|
package/package.json
CHANGED
|
@@ -11,29 +11,28 @@
|
|
|
11
11
|
"modern",
|
|
12
12
|
"modern.js"
|
|
13
13
|
],
|
|
14
|
-
"version": "2.
|
|
14
|
+
"version": "2.6.0",
|
|
15
15
|
"jsnext:source": "./src/index.ts",
|
|
16
16
|
"types": "./dist/types/cli/index.d.ts",
|
|
17
|
-
"main": "./dist/
|
|
18
|
-
"module": "./dist/
|
|
19
|
-
"jsnext:modern": "./dist/js/modern/cli/index.js",
|
|
17
|
+
"main": "./dist/cjs/cli/index.js",
|
|
18
|
+
"module": "./dist/esm/cli/index.js",
|
|
20
19
|
"exports": {
|
|
21
20
|
"./types": "./types.d.ts",
|
|
22
21
|
".": {
|
|
23
22
|
"jsnext:source": "./src/cli/index.ts",
|
|
24
|
-
"default": "./dist/
|
|
23
|
+
"default": "./dist/cjs/cli/index.js"
|
|
25
24
|
},
|
|
26
25
|
"./cli": {
|
|
27
26
|
"jsnext:source": "./src/cli/index.ts",
|
|
28
|
-
"default": "./dist/
|
|
27
|
+
"default": "./dist/cjs/cli/index.js"
|
|
29
28
|
},
|
|
30
29
|
"./server": {
|
|
31
30
|
"node": {
|
|
32
31
|
"jsnext:source": "./src/index.ts",
|
|
33
|
-
"import": "./dist/
|
|
34
|
-
"require": "./dist/
|
|
32
|
+
"import": "./dist/esm-node/index.js",
|
|
33
|
+
"require": "./dist/cjs/index.js"
|
|
35
34
|
},
|
|
36
|
-
"default": "./dist/
|
|
35
|
+
"default": "./dist/esm/index.js"
|
|
37
36
|
}
|
|
38
37
|
},
|
|
39
38
|
"dependencies": {
|
|
@@ -43,10 +42,10 @@
|
|
|
43
42
|
"formidable": "^1.2.2",
|
|
44
43
|
"reflect-metadata": "^0.1.13",
|
|
45
44
|
"type-is": "^1.6.18",
|
|
46
|
-
"@modern-js/bff-core": "2.
|
|
47
|
-
"@modern-js/bff-runtime": "2.
|
|
48
|
-
"@modern-js/types": "2.
|
|
49
|
-
"@modern-js/utils": "2.
|
|
45
|
+
"@modern-js/bff-core": "2.6.0",
|
|
46
|
+
"@modern-js/bff-runtime": "2.6.0",
|
|
47
|
+
"@modern-js/types": "2.6.0",
|
|
48
|
+
"@modern-js/utils": "2.6.0"
|
|
50
49
|
},
|
|
51
50
|
"devDependencies": {
|
|
52
51
|
"@types/cookie-parser": "^1.4.2",
|
|
@@ -62,11 +61,11 @@
|
|
|
62
61
|
"supertest": "^6.1.6",
|
|
63
62
|
"typescript": "^4",
|
|
64
63
|
"zod": "^3.17.3",
|
|
65
|
-
"@modern-js/core": "2.
|
|
66
|
-
"@modern-js/app-tools": "
|
|
67
|
-
"@modern-js/server-core": "2.
|
|
68
|
-
"@scripts/jest-config": "2.
|
|
69
|
-
"@scripts/build": "2.
|
|
64
|
+
"@modern-js/core": "2.6.0",
|
|
65
|
+
"@modern-js/app-tools": "2.6.0",
|
|
66
|
+
"@modern-js/server-core": "2.6.0",
|
|
67
|
+
"@scripts/jest-config": "2.6.0",
|
|
68
|
+
"@scripts/build": "2.6.0"
|
|
70
69
|
},
|
|
71
70
|
"peerDependencies": {
|
|
72
71
|
"express": "^4.17.1"
|
package/dist/js/modern/plugin.js
DELETED
|
@@ -1,171 +0,0 @@
|
|
|
1
|
-
var __async = (__this, __arguments, generator) => {
|
|
2
|
-
return new Promise((resolve, reject) => {
|
|
3
|
-
var fulfilled = (value) => {
|
|
4
|
-
try {
|
|
5
|
-
step(generator.next(value));
|
|
6
|
-
} catch (e) {
|
|
7
|
-
reject(e);
|
|
8
|
-
}
|
|
9
|
-
};
|
|
10
|
-
var rejected = (value) => {
|
|
11
|
-
try {
|
|
12
|
-
step(generator.throw(value));
|
|
13
|
-
} catch (e) {
|
|
14
|
-
reject(e);
|
|
15
|
-
}
|
|
16
|
-
};
|
|
17
|
-
var step = (x) => x.done ? resolve(x.value) : Promise.resolve(x.value).then(fulfilled, rejected);
|
|
18
|
-
step((generator = generator.apply(__this, __arguments)).next());
|
|
19
|
-
});
|
|
20
|
-
};
|
|
21
|
-
import * as path from "path";
|
|
22
|
-
import express from "express";
|
|
23
|
-
import cookieParser from "cookie-parser";
|
|
24
|
-
import { fs, createDebugger, compatRequire } from "@modern-js/utils";
|
|
25
|
-
import finalhandler from "finalhandler";
|
|
26
|
-
import { run } from "./context";
|
|
27
|
-
import registerRoutes from "./registerRoutes";
|
|
28
|
-
const debug = createDebugger("express");
|
|
29
|
-
const findAppModule = (apiDir) => __async(void 0, null, function* () {
|
|
30
|
-
const exts = [
|
|
31
|
-
".ts",
|
|
32
|
-
".js"
|
|
33
|
-
];
|
|
34
|
-
const paths = exts.map((ext) => path.resolve(apiDir, `app${ext}`));
|
|
35
|
-
for (const filename of paths) {
|
|
36
|
-
if (yield fs.pathExists(filename)) {
|
|
37
|
-
delete require.cache[filename];
|
|
38
|
-
return [
|
|
39
|
-
compatRequire(filename),
|
|
40
|
-
require(filename)
|
|
41
|
-
];
|
|
42
|
-
}
|
|
43
|
-
}
|
|
44
|
-
return [];
|
|
45
|
-
});
|
|
46
|
-
const initMiddlewares = (middleware, app) => {
|
|
47
|
-
middleware.forEach((middlewareItem) => {
|
|
48
|
-
const middlewareFunc = typeof middlewareItem === "string" ? compatRequire(middlewareItem) : middlewareItem;
|
|
49
|
-
app.use(middlewareFunc);
|
|
50
|
-
});
|
|
51
|
-
};
|
|
52
|
-
const useRun = (app) => {
|
|
53
|
-
app.use((req, res, next) => {
|
|
54
|
-
run({
|
|
55
|
-
req,
|
|
56
|
-
res
|
|
57
|
-
}, next);
|
|
58
|
-
});
|
|
59
|
-
};
|
|
60
|
-
const initApp = (app) => {
|
|
61
|
-
app.use(cookieParser());
|
|
62
|
-
app.use(express.text());
|
|
63
|
-
app.use(express.urlencoded({
|
|
64
|
-
extended: true
|
|
65
|
-
}));
|
|
66
|
-
app.use(express.json());
|
|
67
|
-
return app;
|
|
68
|
-
};
|
|
69
|
-
var plugin_default = () => ({
|
|
70
|
-
name: "@modern-js/plugin-express",
|
|
71
|
-
pre: [
|
|
72
|
-
"@modern-js/plugin-bff"
|
|
73
|
-
],
|
|
74
|
-
post: [
|
|
75
|
-
"@modern-js/plugin-server"
|
|
76
|
-
],
|
|
77
|
-
setup: (api) => ({
|
|
78
|
-
prepareApiServer(_0) {
|
|
79
|
-
return __async(this, arguments, function* ({ pwd, config }) {
|
|
80
|
-
let app;
|
|
81
|
-
const apiDir = path.join(pwd, "./api");
|
|
82
|
-
const appContext = api.useAppContext();
|
|
83
|
-
const apiHandlerInfos = appContext.apiHandlerInfos;
|
|
84
|
-
const mode = appContext.apiMode;
|
|
85
|
-
if (mode === "framework") {
|
|
86
|
-
const appModule = yield findAppModule(apiDir);
|
|
87
|
-
app = appModule[0];
|
|
88
|
-
const hooks = appModule[1];
|
|
89
|
-
if (!app || !app.use) {
|
|
90
|
-
app = express();
|
|
91
|
-
}
|
|
92
|
-
initApp(app);
|
|
93
|
-
if (config) {
|
|
94
|
-
const { middleware } = config;
|
|
95
|
-
initMiddlewares(middleware, app);
|
|
96
|
-
}
|
|
97
|
-
useRun(app);
|
|
98
|
-
registerRoutes(app, apiHandlerInfos);
|
|
99
|
-
if (hooks) {
|
|
100
|
-
const { afterLambdaRegisted } = hooks;
|
|
101
|
-
if (afterLambdaRegisted) {
|
|
102
|
-
afterLambdaRegisted(app);
|
|
103
|
-
}
|
|
104
|
-
}
|
|
105
|
-
} else if (mode === "function") {
|
|
106
|
-
app = express();
|
|
107
|
-
initApp(app);
|
|
108
|
-
if (config) {
|
|
109
|
-
const { middleware: middleware1 } = config;
|
|
110
|
-
initMiddlewares(middleware1, app);
|
|
111
|
-
}
|
|
112
|
-
useRun(app);
|
|
113
|
-
registerRoutes(app, apiHandlerInfos);
|
|
114
|
-
} else {
|
|
115
|
-
throw new Error(`mode must be function or framework`);
|
|
116
|
-
}
|
|
117
|
-
return (req, res) => new Promise((resolve, reject) => {
|
|
118
|
-
const handler = (err) => {
|
|
119
|
-
if (err) {
|
|
120
|
-
return reject(err);
|
|
121
|
-
}
|
|
122
|
-
return finalhandler(req, res, {})(null);
|
|
123
|
-
};
|
|
124
|
-
res.on("finish", (err) => {
|
|
125
|
-
if (err) {
|
|
126
|
-
return reject(err);
|
|
127
|
-
}
|
|
128
|
-
return resolve();
|
|
129
|
-
});
|
|
130
|
-
return app(req, res, handler);
|
|
131
|
-
});
|
|
132
|
-
});
|
|
133
|
-
},
|
|
134
|
-
prepareWebServer({ config }, next) {
|
|
135
|
-
var _a2;
|
|
136
|
-
const userConfig = api.useConfigContext();
|
|
137
|
-
if (!((_a2 = userConfig == null ? void 0 : userConfig.server) == null ? void 0 : _a2.enableFrameworkExt)) {
|
|
138
|
-
return next();
|
|
139
|
-
}
|
|
140
|
-
const app = express();
|
|
141
|
-
initApp(app);
|
|
142
|
-
if (config) {
|
|
143
|
-
const { middleware } = config;
|
|
144
|
-
debug("web middleware", middleware);
|
|
145
|
-
initMiddlewares(middleware, app);
|
|
146
|
-
}
|
|
147
|
-
return (ctx) => new Promise((resolve, reject) => {
|
|
148
|
-
const { source: { req, res } } = ctx;
|
|
149
|
-
const handler = (err) => {
|
|
150
|
-
if (err) {
|
|
151
|
-
return reject(err);
|
|
152
|
-
}
|
|
153
|
-
if (res.headersSent && res.statusCode !== 200) {
|
|
154
|
-
finalhandler(req, res, {})(null);
|
|
155
|
-
}
|
|
156
|
-
return resolve();
|
|
157
|
-
};
|
|
158
|
-
res.on("finish", (err) => {
|
|
159
|
-
if (err) {
|
|
160
|
-
return reject(err);
|
|
161
|
-
}
|
|
162
|
-
return resolve();
|
|
163
|
-
});
|
|
164
|
-
return app(req, res, handler);
|
|
165
|
-
});
|
|
166
|
-
}
|
|
167
|
-
})
|
|
168
|
-
});
|
|
169
|
-
export {
|
|
170
|
-
plugin_default as default
|
|
171
|
-
};
|
|
@@ -1,68 +0,0 @@
|
|
|
1
|
-
var __async = (__this, __arguments, generator) => {
|
|
2
|
-
return new Promise((resolve, reject) => {
|
|
3
|
-
var fulfilled = (value) => {
|
|
4
|
-
try {
|
|
5
|
-
step(generator.next(value));
|
|
6
|
-
} catch (e) {
|
|
7
|
-
reject(e);
|
|
8
|
-
}
|
|
9
|
-
};
|
|
10
|
-
var rejected = (value) => {
|
|
11
|
-
try {
|
|
12
|
-
step(generator.throw(value));
|
|
13
|
-
} catch (e) {
|
|
14
|
-
reject(e);
|
|
15
|
-
}
|
|
16
|
-
};
|
|
17
|
-
var step = (x) => x.done ? resolve(x.value) : Promise.resolve(x.value).then(fulfilled, rejected);
|
|
18
|
-
step((generator = generator.apply(__this, __arguments)).next());
|
|
19
|
-
});
|
|
20
|
-
};
|
|
21
|
-
import { useContext } from "../context";
|
|
22
|
-
const Pipe = (func) => {
|
|
23
|
-
return {
|
|
24
|
-
name: "pipe",
|
|
25
|
-
execute(executeHelper, next) {
|
|
26
|
-
return __async(this, null, function* () {
|
|
27
|
-
const { inputs } = executeHelper;
|
|
28
|
-
const ctx = useContext();
|
|
29
|
-
const { res } = ctx;
|
|
30
|
-
if (typeof func === "function") {
|
|
31
|
-
let isPiped = true;
|
|
32
|
-
const end = (value) => {
|
|
33
|
-
isPiped = false;
|
|
34
|
-
if (typeof value === "function") {
|
|
35
|
-
value(res);
|
|
36
|
-
return;
|
|
37
|
-
}
|
|
38
|
-
return value;
|
|
39
|
-
};
|
|
40
|
-
const output = yield func(inputs, end);
|
|
41
|
-
if (!isPiped) {
|
|
42
|
-
if (output) {
|
|
43
|
-
return executeHelper.result = output;
|
|
44
|
-
} else {
|
|
45
|
-
return;
|
|
46
|
-
}
|
|
47
|
-
}
|
|
48
|
-
executeHelper.inputs = output;
|
|
49
|
-
yield next();
|
|
50
|
-
}
|
|
51
|
-
});
|
|
52
|
-
}
|
|
53
|
-
};
|
|
54
|
-
};
|
|
55
|
-
const Middleware = (middleware) => {
|
|
56
|
-
return {
|
|
57
|
-
name: "middleware",
|
|
58
|
-
metadata(helper) {
|
|
59
|
-
const middlewares = helper.getMetadata("pipe") || [];
|
|
60
|
-
middlewares.push(middleware);
|
|
61
|
-
helper.setMetadata("middleware", middlewares);
|
|
62
|
-
}
|
|
63
|
-
};
|
|
64
|
-
};
|
|
65
|
-
export {
|
|
66
|
-
Middleware,
|
|
67
|
-
Pipe
|
|
68
|
-
};
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|