@modern-js/plugin-express 2.14.0 → 2.16.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 +25 -0
- package/dist/cjs/cli/index.js +56 -42
- package/dist/cjs/context.js +13 -26
- package/dist/cjs/index.js +28 -37
- package/dist/cjs/plugin.js +178 -155
- package/dist/cjs/registerRoutes.js +11 -27
- package/dist/cjs/runtime/hook.js +7 -25
- package/dist/cjs/runtime/index.js +30 -31
- package/dist/cjs/runtime/operators.js +15 -28
- package/dist/cjs/utils.js +36 -54
- package/dist/esm/cli/index.js +51 -51
- package/dist/esm/index.js +1 -2
- package/dist/esm/plugin.js +385 -376
- package/dist/esm/registerRoutes.js +12 -13
- package/dist/esm/runtime/hook.js +2 -3
- package/dist/esm/runtime/index.js +2 -3
- package/dist/esm/runtime/operators.js +196 -187
- package/dist/esm/utils.js +541 -515
- package/dist/esm-node/cli/index.js +1 -4
- package/dist/esm-node/context.js +1 -4
- package/dist/esm-node/index.js +1 -4
- package/dist/esm-node/plugin.js +108 -107
- package/dist/esm-node/registerRoutes.js +1 -4
- package/dist/esm-node/runtime/hook.js +1 -4
- package/dist/esm-node/runtime/index.js +2 -6
- package/dist/esm-node/runtime/operators.js +2 -6
- package/dist/esm-node/utils.js +4 -8
- package/package.json +15 -11
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import * as path from "path";
|
|
2
2
|
import { createRuntimeExportsUtils } from "@modern-js/utils";
|
|
3
3
|
import { getRelativeRuntimePath } from "@modern-js/bff-core";
|
|
4
|
-
|
|
4
|
+
export default () => ({
|
|
5
5
|
name: "@modern-js/plugin-express",
|
|
6
6
|
setup: (api) => {
|
|
7
7
|
let bffExportsUtils;
|
|
@@ -58,6 +58,3 @@ var cli_default = () => ({
|
|
|
58
58
|
};
|
|
59
59
|
}
|
|
60
60
|
});
|
|
61
|
-
export {
|
|
62
|
-
cli_default as default
|
|
63
|
-
};
|
package/dist/esm-node/context.js
CHANGED
package/dist/esm-node/index.js
CHANGED
package/dist/esm-node/plugin.js
CHANGED
|
@@ -46,116 +46,117 @@ const initApp = (app) => {
|
|
|
46
46
|
app.use(express.json());
|
|
47
47
|
return app;
|
|
48
48
|
};
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
app.use(async (req, res, next) => {
|
|
101
|
-
const html = await render(req, res);
|
|
102
|
-
if (html) {
|
|
103
|
-
res.end(html);
|
|
104
|
-
}
|
|
105
|
-
next();
|
|
106
|
-
});
|
|
107
|
-
}
|
|
108
|
-
return (req, res) => new Promise((resolve, reject) => {
|
|
109
|
-
const handler = (err) => {
|
|
110
|
-
if (err) {
|
|
111
|
-
return reject(err);
|
|
49
|
+
export default () => {
|
|
50
|
+
return {
|
|
51
|
+
name: "@modern-js/plugin-express",
|
|
52
|
+
pre: [
|
|
53
|
+
"@modern-js/plugin-bff"
|
|
54
|
+
],
|
|
55
|
+
post: [
|
|
56
|
+
"@modern-js/plugin-server"
|
|
57
|
+
],
|
|
58
|
+
setup: (api) => {
|
|
59
|
+
return {
|
|
60
|
+
async prepareApiServer({ pwd, config, render }) {
|
|
61
|
+
var _userConfig_bff;
|
|
62
|
+
let app;
|
|
63
|
+
const appContext = api.useAppContext();
|
|
64
|
+
const apiHandlerInfos = appContext.apiHandlerInfos;
|
|
65
|
+
const apiDirectory = appContext.apiDirectory;
|
|
66
|
+
const apiDir = apiDirectory || path.join(pwd, "./api");
|
|
67
|
+
const mode = appContext.apiMode;
|
|
68
|
+
const userConfig = api.useConfigContext();
|
|
69
|
+
if (mode === "framework") {
|
|
70
|
+
const appModule = await findAppModule(apiDir);
|
|
71
|
+
app = appModule[0];
|
|
72
|
+
const hooks = appModule[1];
|
|
73
|
+
if (!app || !app.use) {
|
|
74
|
+
app = express();
|
|
75
|
+
}
|
|
76
|
+
initApp(app);
|
|
77
|
+
if (config) {
|
|
78
|
+
const { middleware } = config;
|
|
79
|
+
initMiddlewares(middleware, app);
|
|
80
|
+
}
|
|
81
|
+
useRun(app);
|
|
82
|
+
registerRoutes(app, apiHandlerInfos);
|
|
83
|
+
if (hooks) {
|
|
84
|
+
const { afterLambdaRegisted } = hooks;
|
|
85
|
+
if (afterLambdaRegisted) {
|
|
86
|
+
afterLambdaRegisted(app);
|
|
87
|
+
}
|
|
88
|
+
}
|
|
89
|
+
} else if (mode === "function") {
|
|
90
|
+
app = express();
|
|
91
|
+
initApp(app);
|
|
92
|
+
if (config) {
|
|
93
|
+
const { middleware } = config;
|
|
94
|
+
initMiddlewares(middleware, app);
|
|
95
|
+
}
|
|
96
|
+
useRun(app);
|
|
97
|
+
registerRoutes(app, apiHandlerInfos);
|
|
98
|
+
} else {
|
|
99
|
+
throw new Error(`mode must be function or framework`);
|
|
112
100
|
}
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
101
|
+
if (((_userConfig_bff = userConfig.bff) === null || _userConfig_bff === void 0 ? void 0 : _userConfig_bff.enableHandleWeb) && render) {
|
|
102
|
+
app.use(async (req, res, next) => {
|
|
103
|
+
const html = await render(req, res);
|
|
104
|
+
if (html) {
|
|
105
|
+
res.end(html);
|
|
106
|
+
}
|
|
107
|
+
next();
|
|
108
|
+
});
|
|
118
109
|
}
|
|
119
|
-
return
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
if (err) {
|
|
141
|
-
return reject(err);
|
|
110
|
+
return (req, res) => new Promise((resolve, reject) => {
|
|
111
|
+
const handler = (err) => {
|
|
112
|
+
if (err) {
|
|
113
|
+
return reject(err);
|
|
114
|
+
}
|
|
115
|
+
return finalhandler(req, res, {})(null);
|
|
116
|
+
};
|
|
117
|
+
res.on("finish", (err) => {
|
|
118
|
+
if (err) {
|
|
119
|
+
return reject(err);
|
|
120
|
+
}
|
|
121
|
+
return resolve();
|
|
122
|
+
});
|
|
123
|
+
return app(req, res, handler);
|
|
124
|
+
});
|
|
125
|
+
},
|
|
126
|
+
prepareWebServer({ config }, next) {
|
|
127
|
+
var _userConfig_server;
|
|
128
|
+
const userConfig = api.useConfigContext();
|
|
129
|
+
if (!(userConfig === null || userConfig === void 0 ? void 0 : (_userConfig_server = userConfig.server) === null || _userConfig_server === void 0 ? void 0 : _userConfig_server.enableFrameworkExt)) {
|
|
130
|
+
return next();
|
|
142
131
|
}
|
|
143
|
-
|
|
144
|
-
|
|
132
|
+
const app = express();
|
|
133
|
+
initApp(app);
|
|
134
|
+
if (config) {
|
|
135
|
+
const { middleware } = config;
|
|
136
|
+
debug("web middleware", middleware);
|
|
137
|
+
initMiddlewares(middleware, app);
|
|
145
138
|
}
|
|
146
|
-
return
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
139
|
+
return (ctx) => new Promise((resolve, reject) => {
|
|
140
|
+
const { source: { req, res } } = ctx;
|
|
141
|
+
const handler = (err) => {
|
|
142
|
+
if (err) {
|
|
143
|
+
return reject(err);
|
|
144
|
+
}
|
|
145
|
+
if (res.headersSent && res.statusCode !== 200) {
|
|
146
|
+
finalhandler(req, res, {})(null);
|
|
147
|
+
}
|
|
148
|
+
return resolve();
|
|
149
|
+
};
|
|
150
|
+
res.on("finish", (err) => {
|
|
151
|
+
if (err) {
|
|
152
|
+
return reject(err);
|
|
153
|
+
}
|
|
154
|
+
return resolve();
|
|
155
|
+
});
|
|
156
|
+
return app(req, res, handler);
|
|
157
|
+
});
|
|
158
|
+
}
|
|
159
|
+
};
|
|
156
160
|
}
|
|
157
|
-
}
|
|
158
|
-
});
|
|
159
|
-
export {
|
|
160
|
-
plugin_default as default
|
|
161
|
+
};
|
|
161
162
|
};
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { useContext } from "../context";
|
|
2
|
-
const Pipe = (func) => {
|
|
2
|
+
export const Pipe = (func) => {
|
|
3
3
|
return {
|
|
4
4
|
name: "pipe",
|
|
5
5
|
// eslint-disable-next-line consistent-return
|
|
@@ -31,7 +31,7 @@ const Pipe = (func) => {
|
|
|
31
31
|
}
|
|
32
32
|
};
|
|
33
33
|
};
|
|
34
|
-
const Middleware = (middleware) => {
|
|
34
|
+
export const Middleware = (middleware) => {
|
|
35
35
|
return {
|
|
36
36
|
name: "middleware",
|
|
37
37
|
metadata(helper) {
|
|
@@ -41,7 +41,3 @@ const Middleware = (middleware) => {
|
|
|
41
41
|
}
|
|
42
42
|
};
|
|
43
43
|
};
|
|
44
|
-
export {
|
|
45
|
-
Middleware,
|
|
46
|
-
Pipe
|
|
47
|
-
};
|
package/dist/esm-node/utils.js
CHANGED
|
@@ -27,9 +27,8 @@ const handleResponseMeta = (res, handler) => {
|
|
|
27
27
|
}
|
|
28
28
|
}
|
|
29
29
|
};
|
|
30
|
-
const createRouteHandler = (handler) => {
|
|
30
|
+
export const createRouteHandler = (handler) => {
|
|
31
31
|
const apiHandler = async (req, res, next) => {
|
|
32
|
-
var _a;
|
|
33
32
|
const input = await getInputFromRequest(req);
|
|
34
33
|
if (isWithMetaHandler(handler)) {
|
|
35
34
|
try {
|
|
@@ -65,7 +64,8 @@ const createRouteHandler = (handler) => {
|
|
|
65
64
|
}
|
|
66
65
|
} else if (isInputParamsDeciderHandler(handler)) {
|
|
67
66
|
try {
|
|
68
|
-
|
|
67
|
+
var _input_data;
|
|
68
|
+
const args = (input === null || input === void 0 ? void 0 : (_input_data = input.data) === null || _input_data === void 0 ? void 0 : _input_data.args) || [];
|
|
69
69
|
const body = await handler(...args);
|
|
70
70
|
if (typeof body !== "undefined") {
|
|
71
71
|
if (typeof body === "object") {
|
|
@@ -94,7 +94,7 @@ const createRouteHandler = (handler) => {
|
|
|
94
94
|
Object.defineProperties(apiHandler, Object.getOwnPropertyDescriptors(handler));
|
|
95
95
|
return apiHandler;
|
|
96
96
|
};
|
|
97
|
-
const isNormalMethod = (httpMethod) => httpMethods.includes(httpMethod);
|
|
97
|
+
export const isNormalMethod = (httpMethod) => httpMethods.includes(httpMethod);
|
|
98
98
|
const getInputFromRequest = async (request) => {
|
|
99
99
|
const draft = {
|
|
100
100
|
params: request.params,
|
|
@@ -135,7 +135,3 @@ const resolveFormData = (request) => {
|
|
|
135
135
|
});
|
|
136
136
|
});
|
|
137
137
|
};
|
|
138
|
-
export {
|
|
139
|
-
createRouteHandler,
|
|
140
|
-
isNormalMethod
|
|
141
|
-
};
|
package/package.json
CHANGED
|
@@ -3,7 +3,11 @@
|
|
|
3
3
|
"description": "A Progressive React Framework for modern web development.",
|
|
4
4
|
"homepage": "https://modernjs.dev",
|
|
5
5
|
"bugs": "https://github.com/web-infra-dev/modern.js/issues",
|
|
6
|
-
"repository":
|
|
6
|
+
"repository": {
|
|
7
|
+
"type": "git",
|
|
8
|
+
"url": "https://github.com/web-infra-dev/modern.js",
|
|
9
|
+
"directory": "packages/server/plugin-express"
|
|
10
|
+
},
|
|
7
11
|
"license": "MIT",
|
|
8
12
|
"keywords": [
|
|
9
13
|
"react",
|
|
@@ -11,7 +15,7 @@
|
|
|
11
15
|
"modern",
|
|
12
16
|
"modern.js"
|
|
13
17
|
],
|
|
14
|
-
"version": "2.
|
|
18
|
+
"version": "2.16.0",
|
|
15
19
|
"jsnext:source": "./src/index.ts",
|
|
16
20
|
"types": "./dist/types/cli/index.d.ts",
|
|
17
21
|
"main": "./dist/cjs/cli/index.js",
|
|
@@ -42,10 +46,10 @@
|
|
|
42
46
|
"formidable": "^1.2.2",
|
|
43
47
|
"reflect-metadata": "^0.1.13",
|
|
44
48
|
"type-is": "^1.6.18",
|
|
45
|
-
"@modern-js/bff-core": "2.
|
|
46
|
-
"@modern-js/bff-runtime": "2.
|
|
47
|
-
"@modern-js/types": "2.
|
|
48
|
-
"@modern-js/utils": "2.
|
|
49
|
+
"@modern-js/bff-core": "2.16.0",
|
|
50
|
+
"@modern-js/bff-runtime": "2.16.0",
|
|
51
|
+
"@modern-js/types": "2.16.0",
|
|
52
|
+
"@modern-js/utils": "2.16.0"
|
|
49
53
|
},
|
|
50
54
|
"devDependencies": {
|
|
51
55
|
"@types/cookie-parser": "^1.4.2",
|
|
@@ -61,11 +65,11 @@
|
|
|
61
65
|
"supertest": "^6.1.6",
|
|
62
66
|
"typescript": "^4",
|
|
63
67
|
"zod": "^3.17.3",
|
|
64
|
-
"@modern-js/core": "2.
|
|
65
|
-
"@modern-js/app-tools": "2.
|
|
66
|
-
"@modern-js/server-core": "2.
|
|
67
|
-
"@scripts/build": "2.
|
|
68
|
-
"@scripts/jest-config": "2.
|
|
68
|
+
"@modern-js/core": "2.16.0",
|
|
69
|
+
"@modern-js/app-tools": "2.16.0",
|
|
70
|
+
"@modern-js/server-core": "2.16.0",
|
|
71
|
+
"@scripts/build": "2.16.0",
|
|
72
|
+
"@scripts/jest-config": "2.16.0"
|
|
69
73
|
},
|
|
70
74
|
"peerDependencies": {
|
|
71
75
|
"express": "^4.17.1"
|