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