@modern-js/plugin-express 2.0.0-beta.3 → 2.0.0-beta.6
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 +89 -0
- package/dist/js/modern/cli/index.js +28 -21
- package/dist/js/modern/context.js +4 -4
- package/dist/js/modern/index.js +4 -1
- package/dist/js/modern/plugin.js +114 -104
- package/dist/js/modern/registerRoutes.js +7 -8
- package/dist/js/modern/runtime/hook.js +4 -1
- package/dist/js/modern/runtime/index.js +8 -4
- package/dist/js/modern/runtime/operators.js +55 -41
- package/dist/js/modern/utils.js +75 -42
- package/dist/js/node/cli/index.js +54 -29
- package/dist/js/node/context.js +27 -10
- package/dist/js/node/index.js +33 -22
- package/dist/js/node/plugin.js +148 -121
- package/dist/js/node/registerRoutes.js +29 -16
- package/dist/js/node/runtime/hook.js +26 -7
- package/dist/js/node/runtime/index.js +29 -43
- package/dist/js/node/runtime/operators.js +79 -48
- package/dist/js/node/utils.js +110 -58
- package/dist/js/treeshaking/cli/index.js +58 -0
- package/dist/js/treeshaking/context.js +3 -0
- package/dist/js/treeshaking/index.js +4 -0
- package/dist/js/treeshaking/plugin.js +379 -0
- package/dist/js/treeshaking/registerRoutes.js +17 -0
- package/dist/js/treeshaking/runtime/hook.js +4 -0
- package/dist/js/treeshaking/runtime/index.js +5 -0
- package/dist/js/treeshaking/runtime/operators.js +197 -0
- package/dist/js/treeshaking/utils.js +514 -0
- package/dist/types/cli/index.d.ts +4 -1
- package/dist/types/plugin.d.ts +2 -0
- package/package.json +22 -22
- package/types.d.ts +2 -0
package/dist/js/modern/utils.js
CHANGED
|
@@ -1,11 +1,44 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
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
|
+
import "reflect-metadata";
|
|
38
|
+
import { httpMethods, isWithMetaHandler, HttpMetadata, ResponseMetaType, ValidationError } from "@modern-js/bff-core";
|
|
39
|
+
import { isSchemaHandler } from "@modern-js/bff-runtime";
|
|
40
|
+
import typeIs from "type-is";
|
|
41
|
+
import formidable from "formidable";
|
|
9
42
|
const handleResponseMeta = (res, handler) => {
|
|
10
43
|
const responseMeta = Reflect.getMetadata(HttpMetadata.Response, handler);
|
|
11
44
|
if (Array.isArray(responseMeta)) {
|
|
@@ -14,7 +47,6 @@ const handleResponseMeta = (res, handler) => {
|
|
|
14
47
|
const metaValue = meta.value;
|
|
15
48
|
switch (metaType) {
|
|
16
49
|
case ResponseMetaType.Headers:
|
|
17
|
-
// eslint-disable-next-line @typescript-eslint/ban-types
|
|
18
50
|
for (const [key, value] of Object.entries(metaValue)) {
|
|
19
51
|
res.append(key, value);
|
|
20
52
|
}
|
|
@@ -31,24 +63,22 @@ const handleResponseMeta = (res, handler) => {
|
|
|
31
63
|
}
|
|
32
64
|
}
|
|
33
65
|
};
|
|
34
|
-
|
|
35
|
-
const apiHandler =
|
|
36
|
-
const input =
|
|
66
|
+
const createRouteHandler = (handler) => {
|
|
67
|
+
const apiHandler = (req, res, next) => __async(void 0, null, function* () {
|
|
68
|
+
const input = yield getInputFromRequest(req);
|
|
37
69
|
if (isWithMetaHandler(handler)) {
|
|
38
70
|
try {
|
|
39
71
|
handleResponseMeta(res, handler);
|
|
40
72
|
if (res.headersSent) {
|
|
41
73
|
return;
|
|
42
74
|
}
|
|
43
|
-
const result =
|
|
44
|
-
if (result && typeof result ===
|
|
45
|
-
// eslint-disable-next-line consistent-return
|
|
75
|
+
const result = yield handler(input);
|
|
76
|
+
if (result && typeof result === "object") {
|
|
46
77
|
return res.json(result);
|
|
47
78
|
}
|
|
48
79
|
} catch (error) {
|
|
49
80
|
if (error instanceof ValidationError) {
|
|
50
81
|
res.status(error.status);
|
|
51
|
-
// eslint-disable-next-line consistent-return
|
|
52
82
|
return res.json({
|
|
53
83
|
message: error.message
|
|
54
84
|
});
|
|
@@ -56,63 +86,62 @@ export const createRouteHandler = handler => {
|
|
|
56
86
|
throw error;
|
|
57
87
|
}
|
|
58
88
|
} else if (isSchemaHandler(handler)) {
|
|
59
|
-
const
|
|
60
|
-
if (
|
|
61
|
-
if (
|
|
89
|
+
const result1 = yield handler(input);
|
|
90
|
+
if (result1.type !== "HandleSuccess") {
|
|
91
|
+
if (result1.type === "InputValidationError") {
|
|
62
92
|
res.status(400);
|
|
63
93
|
} else {
|
|
64
94
|
res.status(500);
|
|
65
95
|
}
|
|
66
|
-
|
|
67
|
-
return res.json(result.message);
|
|
96
|
+
return res.json(result1.message);
|
|
68
97
|
} else {
|
|
69
98
|
res.status(200);
|
|
70
|
-
|
|
71
|
-
return res.json(result.value);
|
|
99
|
+
return res.json(result1.value);
|
|
72
100
|
}
|
|
73
101
|
} else {
|
|
74
102
|
const args = Object.values(input.params).concat(input);
|
|
75
103
|
try {
|
|
76
|
-
const body =
|
|
77
|
-
|
|
78
|
-
// this should never happen
|
|
104
|
+
const body = yield handler(...args);
|
|
79
105
|
if (res.headersSent) {
|
|
80
|
-
|
|
81
|
-
return await Promise.resolve();
|
|
106
|
+
return yield Promise.resolve();
|
|
82
107
|
}
|
|
83
|
-
if (typeof body !==
|
|
84
|
-
// eslint-disable-next-line consistent-return
|
|
108
|
+
if (typeof body !== "undefined") {
|
|
85
109
|
return res.json(body);
|
|
86
110
|
}
|
|
87
111
|
} catch (e) {
|
|
88
|
-
// eslint-disable-next-line consistent-return
|
|
89
112
|
return next(e);
|
|
90
113
|
}
|
|
91
114
|
}
|
|
92
|
-
};
|
|
115
|
+
});
|
|
93
116
|
Object.defineProperties(apiHandler, Object.getOwnPropertyDescriptors(handler));
|
|
94
117
|
return apiHandler;
|
|
95
118
|
};
|
|
96
|
-
|
|
97
|
-
const getInputFromRequest =
|
|
119
|
+
const isNormalMethod = (httpMethod) => httpMethods.includes(httpMethod);
|
|
120
|
+
const getInputFromRequest = (request) => __async(void 0, null, function* () {
|
|
98
121
|
const draft = {
|
|
99
122
|
params: request.params,
|
|
100
123
|
query: request.query,
|
|
101
124
|
headers: request.headers,
|
|
102
125
|
cookies: request.headers.cookie
|
|
103
126
|
};
|
|
104
|
-
if (typeIs(request, [
|
|
127
|
+
if (typeIs(request, [
|
|
128
|
+
"application/json"
|
|
129
|
+
])) {
|
|
105
130
|
draft.data = request.body;
|
|
106
|
-
} else if (typeIs(request, [
|
|
107
|
-
|
|
108
|
-
|
|
131
|
+
} else if (typeIs(request, [
|
|
132
|
+
"multipart/form-data"
|
|
133
|
+
])) {
|
|
134
|
+
draft.formData = yield resolveFormData(request);
|
|
135
|
+
} else if (typeIs(request, [
|
|
136
|
+
"application/x-www-form-urlencoded"
|
|
137
|
+
])) {
|
|
109
138
|
draft.formUrlencoded = request.body;
|
|
110
139
|
} else {
|
|
111
140
|
draft.body = request.body;
|
|
112
141
|
}
|
|
113
142
|
return draft;
|
|
114
|
-
};
|
|
115
|
-
const resolveFormData = request => {
|
|
143
|
+
});
|
|
144
|
+
const resolveFormData = (request) => {
|
|
116
145
|
const form = formidable({
|
|
117
146
|
multiples: true
|
|
118
147
|
});
|
|
@@ -121,7 +150,11 @@ const resolveFormData = request => {
|
|
|
121
150
|
if (err) {
|
|
122
151
|
reject(err);
|
|
123
152
|
}
|
|
124
|
-
resolve(
|
|
153
|
+
resolve(__spreadValues(__spreadValues({}, fields), files));
|
|
125
154
|
});
|
|
126
155
|
});
|
|
127
|
-
};
|
|
156
|
+
};
|
|
157
|
+
export {
|
|
158
|
+
createRouteHandler,
|
|
159
|
+
isNormalMethod
|
|
160
|
+
};
|
|
@@ -1,37 +1,53 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
Object.
|
|
4
|
-
|
|
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 cli_exports = {};
|
|
25
|
+
__export(cli_exports, {
|
|
26
|
+
default: () => cli_default
|
|
5
27
|
});
|
|
6
|
-
exports
|
|
7
|
-
var path =
|
|
8
|
-
var
|
|
9
|
-
var
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
name: '@modern-js/plugin-express',
|
|
14
|
-
setup: api => {
|
|
28
|
+
module.exports = __toCommonJS(cli_exports);
|
|
29
|
+
var path = __toESM(require("path"));
|
|
30
|
+
var import_utils = require("@modern-js/utils");
|
|
31
|
+
var import_bff_core = require("@modern-js/bff-core");
|
|
32
|
+
var cli_default = () => ({
|
|
33
|
+
name: "@modern-js/plugin-express",
|
|
34
|
+
setup: (api) => {
|
|
15
35
|
let bffExportsUtils;
|
|
16
|
-
const {
|
|
17
|
-
|
|
18
|
-
} = api;
|
|
19
|
-
const runtimeModulePath = path.resolve(__dirname, '../runtime');
|
|
36
|
+
const { useAppContext } = api;
|
|
37
|
+
const runtimeModulePath = path.resolve(__dirname, "../runtime");
|
|
20
38
|
return {
|
|
21
39
|
config() {
|
|
22
40
|
const appContext = useAppContext();
|
|
23
|
-
const {
|
|
24
|
-
|
|
25
|
-
} = appContext;
|
|
26
|
-
bffExportsUtils = (0, _utils.createRuntimeExportsUtils)(appContext.internalDirectory, 'server');
|
|
41
|
+
const { appDirectory } = appContext;
|
|
42
|
+
bffExportsUtils = (0, import_utils.createRuntimeExportsUtils)(appContext.internalDirectory, "server");
|
|
27
43
|
const serverRuntimePath = bffExportsUtils.getPath();
|
|
28
|
-
const relativeRuntimePath = (0,
|
|
29
|
-
if (process.env.NODE_ENV ===
|
|
44
|
+
const relativeRuntimePath = (0, import_bff_core.getRelativeRuntimePath)(appDirectory, serverRuntimePath);
|
|
45
|
+
if (process.env.NODE_ENV === "production") {
|
|
30
46
|
return {
|
|
31
47
|
source: {
|
|
32
48
|
alias: {
|
|
33
|
-
|
|
34
|
-
|
|
49
|
+
"@modern-js/runtime/server": relativeRuntimePath,
|
|
50
|
+
"@modern-js/runtime/express": relativeRuntimePath
|
|
35
51
|
}
|
|
36
52
|
}
|
|
37
53
|
};
|
|
@@ -39,17 +55,25 @@ var _default = () => ({
|
|
|
39
55
|
return {
|
|
40
56
|
source: {
|
|
41
57
|
alias: {
|
|
42
|
-
|
|
43
|
-
|
|
58
|
+
"@modern-js/runtime/server": serverRuntimePath,
|
|
59
|
+
"@modern-js/runtime/express": serverRuntimePath
|
|
44
60
|
}
|
|
45
61
|
}
|
|
46
62
|
};
|
|
47
63
|
}
|
|
48
64
|
},
|
|
65
|
+
collectServerPlugins({ plugins }) {
|
|
66
|
+
plugins.push({
|
|
67
|
+
"@modern-js/plugin-express": "@modern-js/plugin-express/server"
|
|
68
|
+
});
|
|
69
|
+
return {
|
|
70
|
+
plugins
|
|
71
|
+
};
|
|
72
|
+
},
|
|
49
73
|
addRuntimeExports(input) {
|
|
50
74
|
const currentFile = bffExportsUtils.getPath();
|
|
51
75
|
const relativeRuntimeModulePath = path.relative(path.dirname(currentFile), runtimeModulePath);
|
|
52
|
-
const relativeFramePath = path.relative(path.dirname(currentFile), require.resolve(
|
|
76
|
+
const relativeFramePath = path.relative(path.dirname(currentFile), require.resolve("express"));
|
|
53
77
|
bffExportsUtils.addExport(`const pluginRuntime = require('${relativeRuntimeModulePath}');
|
|
54
78
|
const express = require('${relativeFramePath}')
|
|
55
79
|
module.exports = {
|
|
@@ -62,4 +86,5 @@ var _default = () => ({
|
|
|
62
86
|
};
|
|
63
87
|
}
|
|
64
88
|
});
|
|
65
|
-
|
|
89
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
90
|
+
0 && (module.exports = {});
|
package/dist/js/node/context.js
CHANGED
|
@@ -1,13 +1,30 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
Object.
|
|
4
|
-
|
|
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 context_exports = {};
|
|
19
|
+
__export(context_exports, {
|
|
20
|
+
run: () => run,
|
|
21
|
+
useContext: () => useContext
|
|
5
22
|
});
|
|
6
|
-
|
|
7
|
-
var
|
|
8
|
-
const {
|
|
23
|
+
module.exports = __toCommonJS(context_exports);
|
|
24
|
+
var import_bff_core = require("@modern-js/bff-core");
|
|
25
|
+
const { run, useContext } = (0, import_bff_core.createStorage)();
|
|
26
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
27
|
+
0 && (module.exports = {
|
|
9
28
|
run,
|
|
10
29
|
useContext
|
|
11
|
-
}
|
|
12
|
-
exports.useContext = useContext;
|
|
13
|
-
exports.run = run;
|
|
30
|
+
});
|
package/dist/js/node/index.js
CHANGED
|
@@ -1,23 +1,34 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
Object.
|
|
4
|
-
|
|
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 src_exports = {};
|
|
26
|
+
__export(src_exports, {
|
|
27
|
+
default: () => src_default
|
|
5
28
|
});
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
var
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
if (Object.prototype.hasOwnProperty.call(_exportNames, key)) return;
|
|
13
|
-
if (key in exports && exports[key] === _context[key]) return;
|
|
14
|
-
Object.defineProperty(exports, key, {
|
|
15
|
-
enumerable: true,
|
|
16
|
-
get: function () {
|
|
17
|
-
return _context[key];
|
|
18
|
-
}
|
|
19
|
-
});
|
|
20
|
-
});
|
|
21
|
-
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
|
|
22
|
-
var _default = _plugin.default;
|
|
23
|
-
exports.default = _default;
|
|
29
|
+
module.exports = __toCommonJS(src_exports);
|
|
30
|
+
var import_plugin = __toESM(require("./plugin"));
|
|
31
|
+
__reExport(src_exports, require("./context"), module.exports);
|
|
32
|
+
var src_default = import_plugin.default;
|
|
33
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
34
|
+
0 && (module.exports = {});
|