@modern-js/plugin-express 1.7.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
CHANGED
|
@@ -1,5 +1,17 @@
|
|
|
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
|
+
|
|
3
15
|
## 1.7.0
|
|
4
16
|
|
|
5
17
|
### Minor Changes
|
|
@@ -1,11 +1,47 @@
|
|
|
1
|
+
import { useContext } from "../context";
|
|
1
2
|
export const Pipe = func => {
|
|
2
3
|
return {
|
|
3
4
|
name: 'pipe',
|
|
4
5
|
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
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
|
+
}
|
|
9
45
|
}
|
|
10
46
|
|
|
11
47
|
};
|
package/dist/js/modern/utils.js
CHANGED
|
@@ -44,67 +44,26 @@ const handleResponseMeta = (res, handler) => {
|
|
|
44
44
|
|
|
45
45
|
export const createRouteHandler = handler => {
|
|
46
46
|
const apiHandler = async (req, res, next) => {
|
|
47
|
-
|
|
47
|
+
const 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
|
-
|
|
82
50
|
try {
|
|
83
51
|
handleResponseMeta(res, handler);
|
|
84
52
|
|
|
85
53
|
if (res.headersSent) {
|
|
86
|
-
// eslint-disable-next-line consistent-return
|
|
87
54
|
return;
|
|
88
55
|
}
|
|
89
56
|
|
|
90
57
|
const result = await handler(input);
|
|
91
|
-
return res.json(result);
|
|
92
|
-
} catch (error) {
|
|
93
|
-
if (error instanceof Error) {
|
|
94
|
-
if (error.status) {
|
|
95
|
-
res.status(error.status);
|
|
96
|
-
} else {
|
|
97
|
-
res.status(500);
|
|
98
|
-
}
|
|
99
58
|
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
});
|
|
59
|
+
if (result && typeof result === 'object') {
|
|
60
|
+
// eslint-disable-next-line consistent-return
|
|
61
|
+
return res.json(result);
|
|
104
62
|
}
|
|
105
|
-
|
|
63
|
+
} catch (error) {
|
|
106
64
|
if (error instanceof ValidationError) {
|
|
107
|
-
res.status(error.status);
|
|
65
|
+
res.status(error.status); // eslint-disable-next-line consistent-return
|
|
66
|
+
|
|
108
67
|
return res.json({
|
|
109
68
|
message: error.message
|
|
110
69
|
});
|
|
@@ -120,11 +79,13 @@ export const createRouteHandler = handler => {
|
|
|
120
79
|
res.status(400);
|
|
121
80
|
} else {
|
|
122
81
|
res.status(500);
|
|
123
|
-
}
|
|
82
|
+
} // eslint-disable-next-line consistent-return
|
|
83
|
+
|
|
124
84
|
|
|
125
85
|
return res.json(result.message);
|
|
126
86
|
} else {
|
|
127
|
-
res.status(200);
|
|
87
|
+
res.status(200); // eslint-disable-next-line consistent-return
|
|
88
|
+
|
|
128
89
|
return res.json(result.value);
|
|
129
90
|
}
|
|
130
91
|
} else {
|
|
@@ -134,13 +95,16 @@ export const createRouteHandler = handler => {
|
|
|
134
95
|
const body = await handler(...args); // this should never happen
|
|
135
96
|
|
|
136
97
|
if (res.headersSent) {
|
|
98
|
+
// eslint-disable-next-line consistent-return
|
|
137
99
|
return await Promise.resolve();
|
|
138
100
|
}
|
|
139
101
|
|
|
140
102
|
if (typeof body !== 'undefined') {
|
|
103
|
+
// eslint-disable-next-line consistent-return
|
|
141
104
|
return res.json(body);
|
|
142
105
|
}
|
|
143
106
|
} catch (e) {
|
|
107
|
+
// eslint-disable-next-line consistent-return
|
|
144
108
|
return next(e);
|
|
145
109
|
}
|
|
146
110
|
}
|
|
@@ -5,14 +5,51 @@ Object.defineProperty(exports, "__esModule", {
|
|
|
5
5
|
});
|
|
6
6
|
exports.Pipe = exports.Middleware = void 0;
|
|
7
7
|
|
|
8
|
+
var _context = require("../context");
|
|
9
|
+
|
|
8
10
|
const Pipe = func => {
|
|
9
11
|
return {
|
|
10
12
|
name: 'pipe',
|
|
11
13
|
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
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
|
+
}
|
|
16
53
|
}
|
|
17
54
|
|
|
18
55
|
};
|
package/dist/js/node/utils.js
CHANGED
|
@@ -57,67 +57,26 @@ const handleResponseMeta = (res, handler) => {
|
|
|
57
57
|
|
|
58
58
|
const createRouteHandler = handler => {
|
|
59
59
|
const apiHandler = async (req, res, next) => {
|
|
60
|
-
|
|
60
|
+
const 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
|
-
|
|
95
63
|
try {
|
|
96
64
|
handleResponseMeta(res, handler);
|
|
97
65
|
|
|
98
66
|
if (res.headersSent) {
|
|
99
|
-
// eslint-disable-next-line consistent-return
|
|
100
67
|
return;
|
|
101
68
|
}
|
|
102
69
|
|
|
103
70
|
const result = await handler(input);
|
|
104
|
-
return res.json(result);
|
|
105
|
-
} catch (error) {
|
|
106
|
-
if (error instanceof Error) {
|
|
107
|
-
if (error.status) {
|
|
108
|
-
res.status(error.status);
|
|
109
|
-
} else {
|
|
110
|
-
res.status(500);
|
|
111
|
-
}
|
|
112
71
|
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
});
|
|
72
|
+
if (result && typeof result === 'object') {
|
|
73
|
+
// eslint-disable-next-line consistent-return
|
|
74
|
+
return res.json(result);
|
|
117
75
|
}
|
|
118
|
-
|
|
76
|
+
} catch (error) {
|
|
119
77
|
if (error instanceof _bffCore.ValidationError) {
|
|
120
|
-
res.status(error.status);
|
|
78
|
+
res.status(error.status); // eslint-disable-next-line consistent-return
|
|
79
|
+
|
|
121
80
|
return res.json({
|
|
122
81
|
message: error.message
|
|
123
82
|
});
|
|
@@ -133,11 +92,13 @@ const createRouteHandler = handler => {
|
|
|
133
92
|
res.status(400);
|
|
134
93
|
} else {
|
|
135
94
|
res.status(500);
|
|
136
|
-
}
|
|
95
|
+
} // eslint-disable-next-line consistent-return
|
|
96
|
+
|
|
137
97
|
|
|
138
98
|
return res.json(result.message);
|
|
139
99
|
} else {
|
|
140
|
-
res.status(200);
|
|
100
|
+
res.status(200); // eslint-disable-next-line consistent-return
|
|
101
|
+
|
|
141
102
|
return res.json(result.value);
|
|
142
103
|
}
|
|
143
104
|
} else {
|
|
@@ -147,13 +108,16 @@ const createRouteHandler = handler => {
|
|
|
147
108
|
const body = await handler(...args); // this should never happen
|
|
148
109
|
|
|
149
110
|
if (res.headersSent) {
|
|
111
|
+
// eslint-disable-next-line consistent-return
|
|
150
112
|
return await Promise.resolve();
|
|
151
113
|
}
|
|
152
114
|
|
|
153
115
|
if (typeof body !== 'undefined') {
|
|
116
|
+
// eslint-disable-next-line consistent-return
|
|
154
117
|
return res.json(body);
|
|
155
118
|
}
|
|
156
119
|
} catch (e) {
|
|
120
|
+
// eslint-disable-next-line consistent-return
|
|
157
121
|
return next(e);
|
|
158
122
|
}
|
|
159
123
|
}
|
|
@@ -2,7 +2,8 @@ import { Operator } from '@modern-js/bff-core';
|
|
|
2
2
|
import { NextFunction } from '@modern-js/types';
|
|
3
3
|
import type { Request, Response } from 'express';
|
|
4
4
|
export declare type EndFunction = ((func: (res: Response) => void) => void) & ((data: unknown) => void);
|
|
5
|
-
declare type
|
|
5
|
+
declare type MaybeAsync<T> = T | Promise<T>;
|
|
6
|
+
declare type PipeFunction<T> = (value: T, end: EndFunction) => MaybeAsync<void> | MaybeAsync<T>;
|
|
6
7
|
export declare const Pipe: <T>(func: PipeFunction<T>) => Operator<void>;
|
|
7
8
|
export declare type Pipe = typeof Pipe;
|
|
8
9
|
export declare const Middleware: (middleware: (req: Request, res: Response, next: NextFunction) => void) => Operator<void>;
|
package/package.json
CHANGED
|
@@ -11,7 +11,7 @@
|
|
|
11
11
|
"modern",
|
|
12
12
|
"modern.js"
|
|
13
13
|
],
|
|
14
|
-
"version": "1.7.
|
|
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,9 +35,9 @@
|
|
|
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.2.
|
|
38
|
+
"@modern-js/bff-core": "^1.2.1",
|
|
39
39
|
"@modern-js/bff-runtime": "^1.4.0",
|
|
40
|
-
"@modern-js/types": "^1.6.
|
|
40
|
+
"@modern-js/types": "^1.6.1",
|
|
41
41
|
"@modern-js/utils": "^1.8.0",
|
|
42
42
|
"cookie-parser": "^1.4.5",
|
|
43
43
|
"finalhandler": "^1.1.2",
|
|
@@ -46,7 +46,7 @@
|
|
|
46
46
|
"type-is": "^1.6.18"
|
|
47
47
|
},
|
|
48
48
|
"devDependencies": {
|
|
49
|
-
"@modern-js/core": "1.13.
|
|
49
|
+
"@modern-js/core": "1.13.2",
|
|
50
50
|
"@modern-js/server-core": "1.4.1",
|
|
51
51
|
"@scripts/build": "0.0.0",
|
|
52
52
|
"@scripts/jest-config": "0.0.0",
|