@modern-js/plugin-express 1.6.1 → 1.7.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 +21 -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 +25 -0
- package/dist/js/modern/utils.js +52 -3
- 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 +38 -0
- package/dist/js/node/utils.js +51 -2
- package/dist/types/runtime/index.d.ts +3 -0
- package/dist/types/runtime/operators.d.ts +10 -0
- package/package.json +5 -6
- 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,26 @@
|
|
|
1
1
|
# @modern-js/plugin-express
|
|
2
2
|
|
|
3
|
+
## 1.7.0
|
|
4
|
+
|
|
5
|
+
### Minor Changes
|
|
6
|
+
|
|
7
|
+
- aadd066: feat: support Pipe and Middleware operators
|
|
8
|
+
- 83660b6: chore(server): delete unused `@modern-js/server-utils` dependence
|
|
9
|
+
|
|
10
|
+
chore(server): 删除未使用的 `@modern-js/server-utils` 依赖
|
|
11
|
+
|
|
12
|
+
### Patch Changes
|
|
13
|
+
|
|
14
|
+
- f745969: fix: allow BFF function to return void
|
|
15
|
+
fix: 允许 BFF 函数返回 void
|
|
16
|
+
- Updated dependencies [aadd066]
|
|
17
|
+
- Updated dependencies [4fc801f]
|
|
18
|
+
- Updated dependencies [83660b6]
|
|
19
|
+
- Updated dependencies [c8614b8]
|
|
20
|
+
- @modern-js/bff-core@1.2.0
|
|
21
|
+
- @modern-js/utils@1.8.0
|
|
22
|
+
- @modern-js/bff-runtime@1.4.0
|
|
23
|
+
|
|
3
24
|
## 1.6.1
|
|
4
25
|
|
|
5
26
|
### Patch Changes
|
|
@@ -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,25 @@
|
|
|
1
|
+
export const Pipe = func => {
|
|
2
|
+
return {
|
|
3
|
+
name: 'pipe',
|
|
4
|
+
|
|
5
|
+
metadata(helper) {
|
|
6
|
+
const pipeFuncs = helper.getMetadata('pipe') || [];
|
|
7
|
+
pipeFuncs.push(func);
|
|
8
|
+
helper.setMetadata('pipe', pipeFuncs);
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
};
|
|
12
|
+
}; // eslint-disable-next-line @typescript-eslint/no-redeclare
|
|
13
|
+
|
|
14
|
+
export const Middleware = middleware => {
|
|
15
|
+
return {
|
|
16
|
+
name: 'middleware',
|
|
17
|
+
|
|
18
|
+
metadata(helper) {
|
|
19
|
+
const middlewares = helper.getMetadata('pipe') || [];
|
|
20
|
+
middlewares.push(middleware);
|
|
21
|
+
helper.setMetadata('middleware', middlewares);
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
};
|
|
25
|
+
}; // 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';
|
|
@@ -44,11 +44,49 @@ const handleResponseMeta = (res, handler) => {
|
|
|
44
44
|
|
|
45
45
|
export const createRouteHandler = handler => {
|
|
46
46
|
const apiHandler = async (req, res, next) => {
|
|
47
|
-
|
|
47
|
+
let input = await getInputFromRequest(req);
|
|
48
48
|
|
|
49
49
|
if (isWithMetaHandler(handler)) {
|
|
50
|
+
const pipeFuncs = Reflect.getMetadata('pipe', handler);
|
|
51
|
+
let isPiped = true;
|
|
52
|
+
|
|
53
|
+
const end = value => {
|
|
54
|
+
isPiped = false;
|
|
55
|
+
|
|
56
|
+
if (typeof value === 'function') {
|
|
57
|
+
value(res);
|
|
58
|
+
return;
|
|
59
|
+
} // eslint-disable-next-line consistent-return
|
|
60
|
+
|
|
61
|
+
|
|
62
|
+
return value;
|
|
63
|
+
};
|
|
64
|
+
|
|
65
|
+
if (Array.isArray(pipeFuncs)) {
|
|
66
|
+
for (const pipeFunc of pipeFuncs) {
|
|
67
|
+
const output = await pipeFunc(input, end);
|
|
68
|
+
|
|
69
|
+
if (!isPiped) {
|
|
70
|
+
if (output) {
|
|
71
|
+
return res.send(output);
|
|
72
|
+
} else {
|
|
73
|
+
// eslint-disable-next-line consistent-return
|
|
74
|
+
return;
|
|
75
|
+
}
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
input = output;
|
|
79
|
+
}
|
|
80
|
+
}
|
|
81
|
+
|
|
50
82
|
try {
|
|
51
83
|
handleResponseMeta(res, handler);
|
|
84
|
+
|
|
85
|
+
if (res.headersSent) {
|
|
86
|
+
// eslint-disable-next-line consistent-return
|
|
87
|
+
return;
|
|
88
|
+
}
|
|
89
|
+
|
|
52
90
|
const result = await handler(input);
|
|
53
91
|
return res.json(result);
|
|
54
92
|
} catch (error) {
|
|
@@ -64,6 +102,15 @@ export const createRouteHandler = handler => {
|
|
|
64
102
|
message: error.message
|
|
65
103
|
});
|
|
66
104
|
}
|
|
105
|
+
|
|
106
|
+
if (error instanceof ValidationError) {
|
|
107
|
+
res.status(error.status);
|
|
108
|
+
return res.json({
|
|
109
|
+
message: error.message
|
|
110
|
+
});
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
throw error;
|
|
67
114
|
}
|
|
68
115
|
} else if (isSchemaHandler(handler)) {
|
|
69
116
|
const result = await handler(input);
|
|
@@ -90,7 +137,9 @@ export const createRouteHandler = handler => {
|
|
|
90
137
|
return await Promise.resolve();
|
|
91
138
|
}
|
|
92
139
|
|
|
93
|
-
|
|
140
|
+
if (typeof body !== 'undefined') {
|
|
141
|
+
return res.json(body);
|
|
142
|
+
}
|
|
94
143
|
} catch (e) {
|
|
95
144
|
return next(e);
|
|
96
145
|
}
|
|
@@ -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,38 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, "__esModule", {
|
|
4
|
+
value: true
|
|
5
|
+
});
|
|
6
|
+
exports.Pipe = exports.Middleware = void 0;
|
|
7
|
+
|
|
8
|
+
const Pipe = func => {
|
|
9
|
+
return {
|
|
10
|
+
name: 'pipe',
|
|
11
|
+
|
|
12
|
+
metadata(helper) {
|
|
13
|
+
const pipeFuncs = helper.getMetadata('pipe') || [];
|
|
14
|
+
pipeFuncs.push(func);
|
|
15
|
+
helper.setMetadata('pipe', pipeFuncs);
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
};
|
|
19
|
+
}; // eslint-disable-next-line @typescript-eslint/no-redeclare
|
|
20
|
+
|
|
21
|
+
|
|
22
|
+
exports.Pipe = Pipe;
|
|
23
|
+
|
|
24
|
+
const Middleware = middleware => {
|
|
25
|
+
return {
|
|
26
|
+
name: 'middleware',
|
|
27
|
+
|
|
28
|
+
metadata(helper) {
|
|
29
|
+
const middlewares = helper.getMetadata('pipe') || [];
|
|
30
|
+
middlewares.push(middleware);
|
|
31
|
+
helper.setMetadata('middleware', middlewares);
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
};
|
|
35
|
+
}; // eslint-disable-next-line @typescript-eslint/no-redeclare
|
|
36
|
+
|
|
37
|
+
|
|
38
|
+
exports.Middleware = Middleware;
|
package/dist/js/node/utils.js
CHANGED
|
@@ -57,11 +57,49 @@ const handleResponseMeta = (res, handler) => {
|
|
|
57
57
|
|
|
58
58
|
const createRouteHandler = handler => {
|
|
59
59
|
const apiHandler = async (req, res, next) => {
|
|
60
|
-
|
|
60
|
+
let input = await getInputFromRequest(req);
|
|
61
61
|
|
|
62
62
|
if ((0, _bffCore.isWithMetaHandler)(handler)) {
|
|
63
|
+
const pipeFuncs = Reflect.getMetadata('pipe', handler);
|
|
64
|
+
let isPiped = true;
|
|
65
|
+
|
|
66
|
+
const end = value => {
|
|
67
|
+
isPiped = false;
|
|
68
|
+
|
|
69
|
+
if (typeof value === 'function') {
|
|
70
|
+
value(res);
|
|
71
|
+
return;
|
|
72
|
+
} // eslint-disable-next-line consistent-return
|
|
73
|
+
|
|
74
|
+
|
|
75
|
+
return value;
|
|
76
|
+
};
|
|
77
|
+
|
|
78
|
+
if (Array.isArray(pipeFuncs)) {
|
|
79
|
+
for (const pipeFunc of pipeFuncs) {
|
|
80
|
+
const output = await pipeFunc(input, end);
|
|
81
|
+
|
|
82
|
+
if (!isPiped) {
|
|
83
|
+
if (output) {
|
|
84
|
+
return res.send(output);
|
|
85
|
+
} else {
|
|
86
|
+
// eslint-disable-next-line consistent-return
|
|
87
|
+
return;
|
|
88
|
+
}
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
input = output;
|
|
92
|
+
}
|
|
93
|
+
}
|
|
94
|
+
|
|
63
95
|
try {
|
|
64
96
|
handleResponseMeta(res, handler);
|
|
97
|
+
|
|
98
|
+
if (res.headersSent) {
|
|
99
|
+
// eslint-disable-next-line consistent-return
|
|
100
|
+
return;
|
|
101
|
+
}
|
|
102
|
+
|
|
65
103
|
const result = await handler(input);
|
|
66
104
|
return res.json(result);
|
|
67
105
|
} catch (error) {
|
|
@@ -77,6 +115,15 @@ const createRouteHandler = handler => {
|
|
|
77
115
|
message: error.message
|
|
78
116
|
});
|
|
79
117
|
}
|
|
118
|
+
|
|
119
|
+
if (error instanceof _bffCore.ValidationError) {
|
|
120
|
+
res.status(error.status);
|
|
121
|
+
return res.json({
|
|
122
|
+
message: error.message
|
|
123
|
+
});
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
throw error;
|
|
80
127
|
}
|
|
81
128
|
} else if ((0, _bffRuntime.isSchemaHandler)(handler)) {
|
|
82
129
|
const result = await handler(input);
|
|
@@ -103,7 +150,9 @@ const createRouteHandler = handler => {
|
|
|
103
150
|
return await Promise.resolve();
|
|
104
151
|
}
|
|
105
152
|
|
|
106
|
-
|
|
153
|
+
if (typeof body !== 'undefined') {
|
|
154
|
+
return res.json(body);
|
|
155
|
+
}
|
|
107
156
|
} catch (e) {
|
|
108
157
|
return next(e);
|
|
109
158
|
}
|
|
@@ -0,0 +1,10 @@
|
|
|
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 PipeFunction<T> = (value: T, end: EndFunction) => void;
|
|
6
|
+
export declare const Pipe: <T>(func: PipeFunction<T>) => Operator<void>;
|
|
7
|
+
export declare type Pipe = typeof Pipe;
|
|
8
|
+
export declare const Middleware: (middleware: (req: Request, res: Response, next: NextFunction) => void) => Operator<void>;
|
|
9
|
+
export declare type Middleware = typeof Middleware;
|
|
10
|
+
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.0",
|
|
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.
|
|
39
|
-
"@modern-js/bff-runtime": "^1.
|
|
38
|
+
"@modern-js/bff-core": "^1.2.0",
|
|
39
|
+
"@modern-js/bff-runtime": "^1.4.0",
|
|
40
40
|
"@modern-js/types": "^1.6.0",
|
|
41
|
-
"@modern-js/utils": "^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.13.
|
|
49
|
+
"@modern-js/core": "1.13.1",
|
|
50
50
|
"@modern-js/server-core": "1.4.1",
|
|
51
|
-
"@modern-js/server-utils": "1.2.11",
|
|
52
51
|
"@scripts/build": "0.0.0",
|
|
53
52
|
"@scripts/jest-config": "0.0.0",
|
|
54
53
|
"@types/cookie-parser": "^1.4.2",
|
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