@modern-js/plugin-express 1.6.1-beta.0 → 1.7.1
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 +42 -0
- package/dist/js/modern/plugin.js +1 -0
- package/dist/js/modern/registerRoutes.js +7 -1
- package/dist/js/modern/runtime/index.js +3 -0
- package/dist/js/modern/runtime/operators.js +61 -0
- package/dist/js/modern/utils.js +25 -12
- package/dist/js/node/plugin.js +1 -0
- package/dist/js/node/registerRoutes.js +7 -1
- package/dist/js/node/{runtime.js → runtime/index.js} +16 -2
- package/dist/js/node/runtime/operators.js +75 -0
- package/dist/js/node/utils.js +24 -11
- package/dist/types/runtime/index.d.ts +3 -0
- package/dist/types/runtime/operators.d.ts +11 -0
- package/package.json +8 -10
- package/types.d.ts +3 -0
- package/dist/js/modern/runtime.js +0 -2
- package/dist/types/runtime.d.ts +0 -2
package/CHANGELOG.md
CHANGED
|
@@ -1,5 +1,47 @@
|
|
|
1
1
|
# @modern-js/plugin-express
|
|
2
2
|
|
|
3
|
+
## 1.7.1
|
|
4
|
+
|
|
5
|
+
### Patch Changes
|
|
6
|
+
|
|
7
|
+
- 19f2b87: fix: optimize the implentation of pipe
|
|
8
|
+
fix: 优化 Pipe 操作符的实现
|
|
9
|
+
- Updated dependencies [19f2b87]
|
|
10
|
+
- Updated dependencies [44e3bb1]
|
|
11
|
+
- @modern-js/bff-core@1.2.1
|
|
12
|
+
- @modern-js/types@1.6.1
|
|
13
|
+
- @modern-js/utils@1.8.0
|
|
14
|
+
|
|
15
|
+
## 1.7.0
|
|
16
|
+
|
|
17
|
+
### Minor Changes
|
|
18
|
+
|
|
19
|
+
- aadd066: feat: support Pipe and Middleware operators
|
|
20
|
+
- 83660b6: chore(server): delete unused `@modern-js/server-utils` dependence
|
|
21
|
+
|
|
22
|
+
chore(server): 删除未使用的 `@modern-js/server-utils` 依赖
|
|
23
|
+
|
|
24
|
+
### Patch Changes
|
|
25
|
+
|
|
26
|
+
- f745969: fix: allow BFF function to return void
|
|
27
|
+
fix: 允许 BFF 函数返回 void
|
|
28
|
+
- Updated dependencies [aadd066]
|
|
29
|
+
- Updated dependencies [4fc801f]
|
|
30
|
+
- Updated dependencies [83660b6]
|
|
31
|
+
- Updated dependencies [c8614b8]
|
|
32
|
+
- @modern-js/bff-core@1.2.0
|
|
33
|
+
- @modern-js/utils@1.8.0
|
|
34
|
+
- @modern-js/bff-runtime@1.4.0
|
|
35
|
+
|
|
36
|
+
## 1.6.1
|
|
37
|
+
|
|
38
|
+
### Patch Changes
|
|
39
|
+
|
|
40
|
+
- 74f7fd7: fix: fix bff hot reload not works when has app.ts
|
|
41
|
+
fix: 修复有 app.ts 的时候, bff 热更新不生效的问题
|
|
42
|
+
- Updated dependencies [74f7fd7]
|
|
43
|
+
- @modern-js/bff-core@1.1.3
|
|
44
|
+
|
|
3
45
|
## 1.6.0
|
|
4
46
|
|
|
5
47
|
### Minor Changes
|
package/dist/js/modern/plugin.js
CHANGED
|
@@ -9,7 +9,13 @@ const registerRoutes = (app, handlerInfos) => {
|
|
|
9
9
|
}) => {
|
|
10
10
|
const routeHandler = createRouteHandler(handler);
|
|
11
11
|
const method = httpMethod.toLowerCase();
|
|
12
|
-
|
|
12
|
+
const routeMiddlwares = Reflect.getMetadata('middleware', handler) || [];
|
|
13
|
+
|
|
14
|
+
if (routeMiddlwares.length > 0) {
|
|
15
|
+
app[method](routePath, routeMiddlwares, routeHandler);
|
|
16
|
+
} else {
|
|
17
|
+
app[method](routePath, routeHandler);
|
|
18
|
+
}
|
|
13
19
|
});
|
|
14
20
|
};
|
|
15
21
|
|
|
@@ -0,0 +1,61 @@
|
|
|
1
|
+
import { useContext } from "../context";
|
|
2
|
+
export const Pipe = func => {
|
|
3
|
+
return {
|
|
4
|
+
name: 'pipe',
|
|
5
|
+
|
|
6
|
+
// eslint-disable-next-line consistent-return
|
|
7
|
+
async execute(executeHelper, next) {
|
|
8
|
+
const {
|
|
9
|
+
inputs
|
|
10
|
+
} = executeHelper;
|
|
11
|
+
const ctx = useContext();
|
|
12
|
+
const {
|
|
13
|
+
res
|
|
14
|
+
} = ctx;
|
|
15
|
+
|
|
16
|
+
if (typeof func === 'function') {
|
|
17
|
+
let isPiped = true;
|
|
18
|
+
|
|
19
|
+
const end = value => {
|
|
20
|
+
isPiped = false;
|
|
21
|
+
|
|
22
|
+
if (typeof value === 'function') {
|
|
23
|
+
value(res);
|
|
24
|
+
return;
|
|
25
|
+
} // eslint-disable-next-line consistent-return
|
|
26
|
+
|
|
27
|
+
|
|
28
|
+
return value;
|
|
29
|
+
};
|
|
30
|
+
|
|
31
|
+
const output = await func(inputs, end);
|
|
32
|
+
|
|
33
|
+
if (!isPiped) {
|
|
34
|
+
if (output) {
|
|
35
|
+
return executeHelper.result = output;
|
|
36
|
+
} else {
|
|
37
|
+
// eslint-disable-next-line consistent-return
|
|
38
|
+
return;
|
|
39
|
+
}
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
executeHelper.inputs = output;
|
|
43
|
+
await next();
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
};
|
|
48
|
+
}; // eslint-disable-next-line @typescript-eslint/no-redeclare
|
|
49
|
+
|
|
50
|
+
export const Middleware = middleware => {
|
|
51
|
+
return {
|
|
52
|
+
name: 'middleware',
|
|
53
|
+
|
|
54
|
+
metadata(helper) {
|
|
55
|
+
const middlewares = helper.getMetadata('pipe') || [];
|
|
56
|
+
middlewares.push(middleware);
|
|
57
|
+
helper.setMetadata('middleware', middlewares);
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
};
|
|
61
|
+
}; // eslint-disable-next-line @typescript-eslint/no-redeclare
|
package/dist/js/modern/utils.js
CHANGED
|
@@ -5,7 +5,7 @@ function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { va
|
|
|
5
5
|
function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
|
|
6
6
|
|
|
7
7
|
import 'reflect-metadata';
|
|
8
|
-
import { httpMethods, isWithMetaHandler, HttpMetadata, ResponseMetaType } from '@modern-js/bff-core';
|
|
8
|
+
import { httpMethods, isWithMetaHandler, HttpMetadata, ResponseMetaType, ValidationError } from '@modern-js/bff-core';
|
|
9
9
|
import { isSchemaHandler } from '@modern-js/bff-runtime';
|
|
10
10
|
import typeIs from 'type-is';
|
|
11
11
|
import formidable from 'formidable';
|
|
@@ -49,21 +49,27 @@ export const createRouteHandler = handler => {
|
|
|
49
49
|
if (isWithMetaHandler(handler)) {
|
|
50
50
|
try {
|
|
51
51
|
handleResponseMeta(res, handler);
|
|
52
|
+
|
|
53
|
+
if (res.headersSent) {
|
|
54
|
+
return;
|
|
55
|
+
}
|
|
56
|
+
|
|
52
57
|
const result = await handler(input);
|
|
53
|
-
|
|
58
|
+
|
|
59
|
+
if (result && typeof result === 'object') {
|
|
60
|
+
// eslint-disable-next-line consistent-return
|
|
61
|
+
return res.json(result);
|
|
62
|
+
}
|
|
54
63
|
} catch (error) {
|
|
55
|
-
if (error instanceof
|
|
56
|
-
|
|
57
|
-
res.status(error.status);
|
|
58
|
-
} else {
|
|
59
|
-
res.status(500);
|
|
60
|
-
}
|
|
64
|
+
if (error instanceof ValidationError) {
|
|
65
|
+
res.status(error.status); // eslint-disable-next-line consistent-return
|
|
61
66
|
|
|
62
67
|
return res.json({
|
|
63
|
-
code: error.code,
|
|
64
68
|
message: error.message
|
|
65
69
|
});
|
|
66
70
|
}
|
|
71
|
+
|
|
72
|
+
throw error;
|
|
67
73
|
}
|
|
68
74
|
} else if (isSchemaHandler(handler)) {
|
|
69
75
|
const result = await handler(input);
|
|
@@ -73,11 +79,13 @@ export const createRouteHandler = handler => {
|
|
|
73
79
|
res.status(400);
|
|
74
80
|
} else {
|
|
75
81
|
res.status(500);
|
|
76
|
-
}
|
|
82
|
+
} // eslint-disable-next-line consistent-return
|
|
83
|
+
|
|
77
84
|
|
|
78
85
|
return res.json(result.message);
|
|
79
86
|
} else {
|
|
80
|
-
res.status(200);
|
|
87
|
+
res.status(200); // eslint-disable-next-line consistent-return
|
|
88
|
+
|
|
81
89
|
return res.json(result.value);
|
|
82
90
|
}
|
|
83
91
|
} else {
|
|
@@ -87,11 +95,16 @@ export const createRouteHandler = handler => {
|
|
|
87
95
|
const body = await handler(...args); // this should never happen
|
|
88
96
|
|
|
89
97
|
if (res.headersSent) {
|
|
98
|
+
// eslint-disable-next-line consistent-return
|
|
90
99
|
return await Promise.resolve();
|
|
91
100
|
}
|
|
92
101
|
|
|
93
|
-
|
|
102
|
+
if (typeof body !== 'undefined') {
|
|
103
|
+
// eslint-disable-next-line consistent-return
|
|
104
|
+
return res.json(body);
|
|
105
|
+
}
|
|
94
106
|
} catch (e) {
|
|
107
|
+
// eslint-disable-next-line consistent-return
|
|
95
108
|
return next(e);
|
|
96
109
|
}
|
|
97
110
|
}
|
package/dist/js/node/plugin.js
CHANGED
|
@@ -17,7 +17,13 @@ const registerRoutes = (app, handlerInfos) => {
|
|
|
17
17
|
}) => {
|
|
18
18
|
const routeHandler = (0, _utils.createRouteHandler)(handler);
|
|
19
19
|
const method = httpMethod.toLowerCase();
|
|
20
|
-
|
|
20
|
+
const routeMiddlwares = Reflect.getMetadata('middleware', handler) || [];
|
|
21
|
+
|
|
22
|
+
if (routeMiddlwares.length > 0) {
|
|
23
|
+
app[method](routePath, routeMiddlwares, routeHandler);
|
|
24
|
+
} else {
|
|
25
|
+
app[method](routePath, routeHandler);
|
|
26
|
+
}
|
|
21
27
|
});
|
|
22
28
|
};
|
|
23
29
|
|
|
@@ -13,8 +13,6 @@ Object.defineProperty(exports, "useContext", {
|
|
|
13
13
|
}
|
|
14
14
|
});
|
|
15
15
|
|
|
16
|
-
var _context = require("./context");
|
|
17
|
-
|
|
18
16
|
var _bffCore = require("@modern-js/bff-core");
|
|
19
17
|
|
|
20
18
|
Object.keys(_bffCore).forEach(function (key) {
|
|
@@ -27,4 +25,20 @@ Object.keys(_bffCore).forEach(function (key) {
|
|
|
27
25
|
return _bffCore[key];
|
|
28
26
|
}
|
|
29
27
|
});
|
|
28
|
+
});
|
|
29
|
+
|
|
30
|
+
var _context = require("../context");
|
|
31
|
+
|
|
32
|
+
var _operators = require("./operators");
|
|
33
|
+
|
|
34
|
+
Object.keys(_operators).forEach(function (key) {
|
|
35
|
+
if (key === "default" || key === "__esModule") return;
|
|
36
|
+
if (Object.prototype.hasOwnProperty.call(_exportNames, key)) return;
|
|
37
|
+
if (key in exports && exports[key] === _operators[key]) return;
|
|
38
|
+
Object.defineProperty(exports, key, {
|
|
39
|
+
enumerable: true,
|
|
40
|
+
get: function () {
|
|
41
|
+
return _operators[key];
|
|
42
|
+
}
|
|
43
|
+
});
|
|
30
44
|
});
|
|
@@ -0,0 +1,75 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, "__esModule", {
|
|
4
|
+
value: true
|
|
5
|
+
});
|
|
6
|
+
exports.Pipe = exports.Middleware = void 0;
|
|
7
|
+
|
|
8
|
+
var _context = require("../context");
|
|
9
|
+
|
|
10
|
+
const Pipe = func => {
|
|
11
|
+
return {
|
|
12
|
+
name: 'pipe',
|
|
13
|
+
|
|
14
|
+
// eslint-disable-next-line consistent-return
|
|
15
|
+
async execute(executeHelper, next) {
|
|
16
|
+
const {
|
|
17
|
+
inputs
|
|
18
|
+
} = executeHelper;
|
|
19
|
+
const ctx = (0, _context.useContext)();
|
|
20
|
+
const {
|
|
21
|
+
res
|
|
22
|
+
} = ctx;
|
|
23
|
+
|
|
24
|
+
if (typeof func === 'function') {
|
|
25
|
+
let isPiped = true;
|
|
26
|
+
|
|
27
|
+
const end = value => {
|
|
28
|
+
isPiped = false;
|
|
29
|
+
|
|
30
|
+
if (typeof value === 'function') {
|
|
31
|
+
value(res);
|
|
32
|
+
return;
|
|
33
|
+
} // eslint-disable-next-line consistent-return
|
|
34
|
+
|
|
35
|
+
|
|
36
|
+
return value;
|
|
37
|
+
};
|
|
38
|
+
|
|
39
|
+
const output = await func(inputs, end);
|
|
40
|
+
|
|
41
|
+
if (!isPiped) {
|
|
42
|
+
if (output) {
|
|
43
|
+
return executeHelper.result = output;
|
|
44
|
+
} else {
|
|
45
|
+
// eslint-disable-next-line consistent-return
|
|
46
|
+
return;
|
|
47
|
+
}
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
executeHelper.inputs = output;
|
|
51
|
+
await next();
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
};
|
|
56
|
+
}; // eslint-disable-next-line @typescript-eslint/no-redeclare
|
|
57
|
+
|
|
58
|
+
|
|
59
|
+
exports.Pipe = Pipe;
|
|
60
|
+
|
|
61
|
+
const Middleware = middleware => {
|
|
62
|
+
return {
|
|
63
|
+
name: 'middleware',
|
|
64
|
+
|
|
65
|
+
metadata(helper) {
|
|
66
|
+
const middlewares = helper.getMetadata('pipe') || [];
|
|
67
|
+
middlewares.push(middleware);
|
|
68
|
+
helper.setMetadata('middleware', middlewares);
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
};
|
|
72
|
+
}; // eslint-disable-next-line @typescript-eslint/no-redeclare
|
|
73
|
+
|
|
74
|
+
|
|
75
|
+
exports.Middleware = Middleware;
|
package/dist/js/node/utils.js
CHANGED
|
@@ -62,21 +62,27 @@ const createRouteHandler = handler => {
|
|
|
62
62
|
if ((0, _bffCore.isWithMetaHandler)(handler)) {
|
|
63
63
|
try {
|
|
64
64
|
handleResponseMeta(res, handler);
|
|
65
|
+
|
|
66
|
+
if (res.headersSent) {
|
|
67
|
+
return;
|
|
68
|
+
}
|
|
69
|
+
|
|
65
70
|
const result = await handler(input);
|
|
66
|
-
|
|
71
|
+
|
|
72
|
+
if (result && typeof result === 'object') {
|
|
73
|
+
// eslint-disable-next-line consistent-return
|
|
74
|
+
return res.json(result);
|
|
75
|
+
}
|
|
67
76
|
} catch (error) {
|
|
68
|
-
if (error instanceof
|
|
69
|
-
|
|
70
|
-
res.status(error.status);
|
|
71
|
-
} else {
|
|
72
|
-
res.status(500);
|
|
73
|
-
}
|
|
77
|
+
if (error instanceof _bffCore.ValidationError) {
|
|
78
|
+
res.status(error.status); // eslint-disable-next-line consistent-return
|
|
74
79
|
|
|
75
80
|
return res.json({
|
|
76
|
-
code: error.code,
|
|
77
81
|
message: error.message
|
|
78
82
|
});
|
|
79
83
|
}
|
|
84
|
+
|
|
85
|
+
throw error;
|
|
80
86
|
}
|
|
81
87
|
} else if ((0, _bffRuntime.isSchemaHandler)(handler)) {
|
|
82
88
|
const result = await handler(input);
|
|
@@ -86,11 +92,13 @@ const createRouteHandler = handler => {
|
|
|
86
92
|
res.status(400);
|
|
87
93
|
} else {
|
|
88
94
|
res.status(500);
|
|
89
|
-
}
|
|
95
|
+
} // eslint-disable-next-line consistent-return
|
|
96
|
+
|
|
90
97
|
|
|
91
98
|
return res.json(result.message);
|
|
92
99
|
} else {
|
|
93
|
-
res.status(200);
|
|
100
|
+
res.status(200); // eslint-disable-next-line consistent-return
|
|
101
|
+
|
|
94
102
|
return res.json(result.value);
|
|
95
103
|
}
|
|
96
104
|
} else {
|
|
@@ -100,11 +108,16 @@ const createRouteHandler = handler => {
|
|
|
100
108
|
const body = await handler(...args); // this should never happen
|
|
101
109
|
|
|
102
110
|
if (res.headersSent) {
|
|
111
|
+
// eslint-disable-next-line consistent-return
|
|
103
112
|
return await Promise.resolve();
|
|
104
113
|
}
|
|
105
114
|
|
|
106
|
-
|
|
115
|
+
if (typeof body !== 'undefined') {
|
|
116
|
+
// eslint-disable-next-line consistent-return
|
|
117
|
+
return res.json(body);
|
|
118
|
+
}
|
|
107
119
|
} catch (e) {
|
|
120
|
+
// eslint-disable-next-line consistent-return
|
|
108
121
|
return next(e);
|
|
109
122
|
}
|
|
110
123
|
}
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import { Operator } from '@modern-js/bff-core';
|
|
2
|
+
import { NextFunction } from '@modern-js/types';
|
|
3
|
+
import type { Request, Response } from 'express';
|
|
4
|
+
export declare type EndFunction = ((func: (res: Response) => void) => void) & ((data: unknown) => void);
|
|
5
|
+
declare type MaybeAsync<T> = T | Promise<T>;
|
|
6
|
+
declare type PipeFunction<T> = (value: T, end: EndFunction) => MaybeAsync<void> | MaybeAsync<T>;
|
|
7
|
+
export declare const Pipe: <T>(func: PipeFunction<T>) => Operator<void>;
|
|
8
|
+
export declare type Pipe = typeof Pipe;
|
|
9
|
+
export declare const Middleware: (middleware: (req: Request, res: Response, next: NextFunction) => void) => Operator<void>;
|
|
10
|
+
export declare type Middleware = typeof Middleware;
|
|
11
|
+
export {};
|
package/package.json
CHANGED
|
@@ -11,7 +11,7 @@
|
|
|
11
11
|
"modern",
|
|
12
12
|
"modern.js"
|
|
13
13
|
],
|
|
14
|
-
"version": "1.
|
|
14
|
+
"version": "1.7.1",
|
|
15
15
|
"jsnext:source": "./src/index.ts",
|
|
16
16
|
"types": "./dist/types/index.d.ts",
|
|
17
17
|
"main": "./dist/js/node/index.js",
|
|
@@ -35,10 +35,10 @@
|
|
|
35
35
|
"dependencies": {
|
|
36
36
|
"@babel/runtime": "^7.18.0",
|
|
37
37
|
"@modern-js/adapter-helpers": "^1.3.0",
|
|
38
|
-
"@modern-js/bff-core": "^1.1
|
|
39
|
-
"@modern-js/bff-runtime": "^1.
|
|
40
|
-
"@modern-js/types": "^1.
|
|
41
|
-
"@modern-js/utils": "^1.
|
|
38
|
+
"@modern-js/bff-core": "^1.2.1",
|
|
39
|
+
"@modern-js/bff-runtime": "^1.4.0",
|
|
40
|
+
"@modern-js/types": "^1.6.1",
|
|
41
|
+
"@modern-js/utils": "^1.8.0",
|
|
42
42
|
"cookie-parser": "^1.4.5",
|
|
43
43
|
"finalhandler": "^1.1.2",
|
|
44
44
|
"formidable": "^1.2.2",
|
|
@@ -46,9 +46,8 @@
|
|
|
46
46
|
"type-is": "^1.6.18"
|
|
47
47
|
},
|
|
48
48
|
"devDependencies": {
|
|
49
|
-
"@modern-js/core": "1.
|
|
50
|
-
"@modern-js/server-core": "1.4.
|
|
51
|
-
"@modern-js/server-utils": "1.2.11",
|
|
49
|
+
"@modern-js/core": "1.13.2",
|
|
50
|
+
"@modern-js/server-core": "1.4.1",
|
|
52
51
|
"@scripts/build": "0.0.0",
|
|
53
52
|
"@scripts/jest-config": "0.0.0",
|
|
54
53
|
"@types/cookie-parser": "^1.4.2",
|
|
@@ -75,8 +74,7 @@
|
|
|
75
74
|
},
|
|
76
75
|
"publishConfig": {
|
|
77
76
|
"registry": "https://registry.npmjs.org/",
|
|
78
|
-
"access": "public"
|
|
79
|
-
"types": "./dist/types/index.d.ts"
|
|
77
|
+
"access": "public"
|
|
80
78
|
},
|
|
81
79
|
"wireit": {
|
|
82
80
|
"build": {
|
package/types.d.ts
CHANGED
|
@@ -11,6 +11,9 @@ declare module '@modern-js/runtime/server' {
|
|
|
11
11
|
|
|
12
12
|
type Context = { req: Request; res: Response };
|
|
13
13
|
|
|
14
|
+
export const Pipe: import('./src/runtime').Pipe;
|
|
15
|
+
export const Middleware: import('./src/runtime').Middleware;
|
|
16
|
+
|
|
14
17
|
export function useContext(): Context;
|
|
15
18
|
|
|
16
19
|
export function hook(attacher: ExpressAttacher): ExpressAttacher;
|
package/dist/types/runtime.d.ts
DELETED