@modern-js/plugin-express 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 +27 -0
- package/dist/cjs/cli/index.js +62 -62
- 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 +41 -55
- 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 +9 -26
- 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/dist/types/runtime/operators.d.ts +1 -1
- package/package.json +18 -11
|
@@ -1,7 +1,6 @@
|
|
|
1
1
|
import * as path from "path";
|
|
2
2
|
import { createRuntimeExportsUtils } from "@modern-js/utils";
|
|
3
|
-
|
|
4
|
-
var cli_default = () => ({
|
|
3
|
+
export default () => ({
|
|
5
4
|
name: "@modern-js/plugin-express",
|
|
6
5
|
setup: (api) => {
|
|
7
6
|
let bffExportsUtils;
|
|
@@ -10,29 +9,16 @@ var cli_default = () => ({
|
|
|
10
9
|
return {
|
|
11
10
|
config() {
|
|
12
11
|
const appContext = useAppContext();
|
|
13
|
-
const { appDirectory } = appContext;
|
|
14
12
|
bffExportsUtils = createRuntimeExportsUtils(appContext.internalDirectory, "server");
|
|
15
|
-
const
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
"@modern-js/runtime/server": relativeRuntimePath,
|
|
22
|
-
"@modern-js/runtime/express": relativeRuntimePath
|
|
23
|
-
}
|
|
24
|
-
}
|
|
25
|
-
};
|
|
26
|
-
} else {
|
|
27
|
-
return {
|
|
28
|
-
source: {
|
|
29
|
-
alias: {
|
|
30
|
-
"@modern-js/runtime/server": serverRuntimePath,
|
|
31
|
-
"@modern-js/runtime/express": serverRuntimePath
|
|
32
|
-
}
|
|
13
|
+
const runtimePath = process.env.NODE_ENV === "development" ? require.resolve("@modern-js/plugin-express/runtime") : "@modern-js/plugin-express/runtime";
|
|
14
|
+
return {
|
|
15
|
+
source: {
|
|
16
|
+
alias: {
|
|
17
|
+
"@modern-js/runtime/server": runtimePath,
|
|
18
|
+
"@modern-js/runtime/express": runtimePath
|
|
33
19
|
}
|
|
34
|
-
}
|
|
35
|
-
}
|
|
20
|
+
}
|
|
21
|
+
};
|
|
36
22
|
},
|
|
37
23
|
collectServerPlugins({ plugins }) {
|
|
38
24
|
plugins.push({
|
|
@@ -58,6 +44,3 @@ var cli_default = () => ({
|
|
|
58
44
|
};
|
|
59
45
|
}
|
|
60
46
|
});
|
|
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
|
-
};
|
|
@@ -6,6 +6,6 @@ type MaybeAsync<T> = T | Promise<T>;
|
|
|
6
6
|
type PipeFunction<T> = (value: T, end: EndFunction) => MaybeAsync<void> | MaybeAsync<T>;
|
|
7
7
|
export declare const Pipe: <T>(func: PipeFunction<T>) => Operator;
|
|
8
8
|
export type Pipe = typeof Pipe;
|
|
9
|
-
export declare const Middleware: (middleware: (req: Request, res: Response, next: NextFunction) => void) => Operator
|
|
9
|
+
export declare const Middleware: (middleware: (req: Request, res: Response, next: NextFunction) => void) => Operator<void>;
|
|
10
10
|
export type Middleware = typeof Middleware;
|
|
11
11
|
export {};
|
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.17.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",
|
|
@@ -33,6 +37,9 @@
|
|
|
33
37
|
"require": "./dist/cjs/index.js"
|
|
34
38
|
},
|
|
35
39
|
"default": "./dist/esm/index.js"
|
|
40
|
+
},
|
|
41
|
+
"./runtime": {
|
|
42
|
+
"default": "./dist/cjs/runtime/index.js"
|
|
36
43
|
}
|
|
37
44
|
},
|
|
38
45
|
"dependencies": {
|
|
@@ -42,10 +49,10 @@
|
|
|
42
49
|
"formidable": "^1.2.2",
|
|
43
50
|
"reflect-metadata": "^0.1.13",
|
|
44
51
|
"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.
|
|
52
|
+
"@modern-js/bff-core": "2.17.0",
|
|
53
|
+
"@modern-js/bff-runtime": "2.17.0",
|
|
54
|
+
"@modern-js/types": "2.17.0",
|
|
55
|
+
"@modern-js/utils": "2.17.0"
|
|
49
56
|
},
|
|
50
57
|
"devDependencies": {
|
|
51
58
|
"@types/cookie-parser": "^1.4.2",
|
|
@@ -61,11 +68,11 @@
|
|
|
61
68
|
"supertest": "^6.1.6",
|
|
62
69
|
"typescript": "^4",
|
|
63
70
|
"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.
|
|
71
|
+
"@modern-js/core": "2.17.0",
|
|
72
|
+
"@modern-js/app-tools": "2.17.0",
|
|
73
|
+
"@modern-js/server-core": "2.17.0",
|
|
74
|
+
"@scripts/build": "2.17.0",
|
|
75
|
+
"@scripts/jest-config": "2.17.0"
|
|
69
76
|
},
|
|
70
77
|
"peerDependencies": {
|
|
71
78
|
"express": "^4.17.1"
|