@modern-js/plugin-express 1.4.2 → 1.5.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 +33 -0
- package/dist/js/modern/index.js +0 -1
- package/dist/js/modern/plugin.js +90 -91
- package/dist/js/node/index.js +0 -1
- package/dist/js/node/plugin.js +89 -91
- package/dist/types/plugin.d.ts +2 -1
- package/jest.config.js +0 -1
- package/package.json +14 -13
- package/tests/fixtures/function-mode/api/nest/user.ts +0 -1
- package/tests/fixtures/function-mode/api/upload.ts +4 -0
- package/tests/fixtures/lambda-mode/api/lambda/upload.ts +4 -0
- package/tests/functionMode.test.ts +15 -0
- package/tests/lambdaMode.test.ts +27 -10
package/CHANGELOG.md
CHANGED
|
@@ -1,5 +1,38 @@
|
|
|
1
1
|
# @modern-js/plugin-express
|
|
2
2
|
|
|
3
|
+
## 1.5.0
|
|
4
|
+
|
|
5
|
+
### Minor Changes
|
|
6
|
+
|
|
7
|
+
- d2d1d6b2: feat: support server config
|
|
8
|
+
|
|
9
|
+
### Patch Changes
|
|
10
|
+
|
|
11
|
+
- Updated dependencies [77ff9754]
|
|
12
|
+
- Updated dependencies [d2d1d6b2]
|
|
13
|
+
- Updated dependencies [07a4887e]
|
|
14
|
+
- Updated dependencies [ea2ae711]
|
|
15
|
+
- Updated dependencies [17d0cc46]
|
|
16
|
+
- Updated dependencies [d2d1d6b2]
|
|
17
|
+
- @modern-js/utils@1.4.0
|
|
18
|
+
- @modern-js/types@1.4.0
|
|
19
|
+
|
|
20
|
+
## 1.4.4
|
|
21
|
+
|
|
22
|
+
### Patch Changes
|
|
23
|
+
|
|
24
|
+
- bebb39b6: chore: improve devDependencies and peerDependencies
|
|
25
|
+
- Updated dependencies [132f7b53]
|
|
26
|
+
- @modern-js/utils@1.3.7
|
|
27
|
+
|
|
28
|
+
## 1.4.3
|
|
29
|
+
|
|
30
|
+
### Patch Changes
|
|
31
|
+
|
|
32
|
+
- c1b8fa0a: feat: convert to new server plugin
|
|
33
|
+
- Updated dependencies [c2046f37]
|
|
34
|
+
- @modern-js/utils@1.3.6
|
|
35
|
+
|
|
3
36
|
## 1.4.2
|
|
4
37
|
|
|
5
38
|
### Patch Changes
|
package/dist/js/modern/index.js
CHANGED
package/dist/js/modern/plugin.js
CHANGED
|
@@ -1,7 +1,6 @@
|
|
|
1
1
|
import * as path from 'path';
|
|
2
2
|
import express from 'express';
|
|
3
3
|
import cookieParser from 'cookie-parser';
|
|
4
|
-
import { createPlugin } from '@modern-js/server-core';
|
|
5
4
|
import { requireModule } from '@modern-js/bff-utils';
|
|
6
5
|
import { fs, createDebugger } from '@modern-js/utils';
|
|
7
6
|
import finalhandler from 'finalhandler';
|
|
@@ -48,115 +47,115 @@ const initApp = app => {
|
|
|
48
47
|
return app;
|
|
49
48
|
};
|
|
50
49
|
|
|
51
|
-
export default
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
50
|
+
export default (() => ({
|
|
51
|
+
name: '@modern-js/plugin-express',
|
|
52
|
+
pre: ['@modern-js/plugin-bff'],
|
|
53
|
+
setup: () => ({
|
|
54
|
+
async prepareApiServer({
|
|
55
|
+
pwd,
|
|
56
|
+
mode,
|
|
57
|
+
config
|
|
58
|
+
}) {
|
|
59
|
+
let app;
|
|
60
|
+
const apiDir = path.join(pwd, './api');
|
|
61
|
+
|
|
62
|
+
if (mode === 'framework') {
|
|
63
|
+
app = await findAppModule(apiDir);
|
|
64
|
+
|
|
65
|
+
if (!app || !app.use) {
|
|
66
|
+
// console.warn('There is not api/app.ts.');
|
|
67
|
+
app = express();
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
initApp(app);
|
|
71
|
+
|
|
72
|
+
if (config) {
|
|
73
|
+
const {
|
|
74
|
+
middleware
|
|
75
|
+
} = config;
|
|
76
|
+
initMiddlewares(middleware, app);
|
|
77
|
+
} // eslint-disable-next-line react-hooks/rules-of-hooks
|
|
78
|
+
|
|
79
|
+
|
|
80
|
+
useRun(app);
|
|
81
|
+
registerRoutes(app);
|
|
82
|
+
} else if (mode === 'function') {
|
|
66
83
|
app = express();
|
|
84
|
+
initApp(app);
|
|
85
|
+
|
|
86
|
+
if (config) {
|
|
87
|
+
const {
|
|
88
|
+
middleware
|
|
89
|
+
} = config;
|
|
90
|
+
initMiddlewares(middleware, app);
|
|
91
|
+
} // eslint-disable-next-line react-hooks/rules-of-hooks
|
|
92
|
+
|
|
93
|
+
|
|
94
|
+
useRun(app);
|
|
95
|
+
registerRoutes(app);
|
|
96
|
+
} else {
|
|
97
|
+
throw new Error(`mode must be function or framework`);
|
|
67
98
|
}
|
|
68
99
|
|
|
69
|
-
|
|
100
|
+
return (req, res) => new Promise((resolve, reject) => {
|
|
101
|
+
const handler = err => {
|
|
102
|
+
if (err) {
|
|
103
|
+
return reject(err);
|
|
104
|
+
} // finalhanlder will trigger 'finish' event
|
|
70
105
|
|
|
71
|
-
if (config) {
|
|
72
|
-
const {
|
|
73
|
-
middleware
|
|
74
|
-
} = config;
|
|
75
|
-
initMiddlewares(middleware, app);
|
|
76
|
-
} // eslint-disable-next-line react-hooks/rules-of-hooks
|
|
77
106
|
|
|
107
|
+
return finalhandler(req, res, {})(null); // return resolve();
|
|
108
|
+
};
|
|
109
|
+
|
|
110
|
+
res.on('finish', err => {
|
|
111
|
+
if (err) {
|
|
112
|
+
return reject(err);
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
return resolve();
|
|
116
|
+
});
|
|
117
|
+
return app(req, res, handler);
|
|
118
|
+
});
|
|
119
|
+
},
|
|
78
120
|
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
}
|
|
82
|
-
app = express();
|
|
121
|
+
prepareWebServer({
|
|
122
|
+
config
|
|
123
|
+
}) {
|
|
124
|
+
const app = express();
|
|
83
125
|
initApp(app);
|
|
84
126
|
|
|
85
127
|
if (config) {
|
|
86
128
|
const {
|
|
87
129
|
middleware
|
|
88
130
|
} = config;
|
|
131
|
+
debug('web middleware', middleware);
|
|
89
132
|
initMiddlewares(middleware, app);
|
|
90
|
-
}
|
|
133
|
+
}
|
|
91
134
|
|
|
135
|
+
return (req, res) => new Promise((resolve, reject) => {
|
|
136
|
+
const handler = err => {
|
|
137
|
+
if (err) {
|
|
138
|
+
return reject(err);
|
|
139
|
+
}
|
|
92
140
|
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
throw new Error(`mode must be function or framework`);
|
|
97
|
-
}
|
|
141
|
+
if (res.headersSent && res.statusCode !== 200) {
|
|
142
|
+
finalhandler(req, res, {})(null);
|
|
143
|
+
}
|
|
98
144
|
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
if (err) {
|
|
102
|
-
return reject(err);
|
|
103
|
-
} // finalhanlder will trigger 'finish' event
|
|
145
|
+
return resolve();
|
|
146
|
+
}; // when user call res.send
|
|
104
147
|
|
|
105
148
|
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
if (err) {
|
|
111
|
-
return reject(err);
|
|
112
|
-
}
|
|
149
|
+
res.on('finish', err => {
|
|
150
|
+
if (err) {
|
|
151
|
+
return reject(err);
|
|
152
|
+
}
|
|
113
153
|
|
|
114
|
-
|
|
154
|
+
return resolve();
|
|
155
|
+
});
|
|
156
|
+
return app(req, res, handler);
|
|
115
157
|
});
|
|
116
|
-
return app(req, res, handler);
|
|
117
|
-
});
|
|
118
|
-
},
|
|
119
|
-
|
|
120
|
-
prepareWebServer({
|
|
121
|
-
config
|
|
122
|
-
}) {
|
|
123
|
-
const app = express();
|
|
124
|
-
initApp(app);
|
|
125
|
-
|
|
126
|
-
if (config) {
|
|
127
|
-
const {
|
|
128
|
-
middleware
|
|
129
|
-
} = config;
|
|
130
|
-
debug('web middleware', middleware);
|
|
131
|
-
initMiddlewares(middleware, app);
|
|
132
158
|
}
|
|
133
159
|
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
if (err) {
|
|
137
|
-
return reject(err);
|
|
138
|
-
}
|
|
139
|
-
|
|
140
|
-
if (res.headersSent && res.statusCode !== 200) {
|
|
141
|
-
finalhandler(req, res, {})(null);
|
|
142
|
-
}
|
|
143
|
-
|
|
144
|
-
return resolve();
|
|
145
|
-
}; // when user call res.send
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
res.on('finish', err => {
|
|
149
|
-
if (err) {
|
|
150
|
-
return reject(err);
|
|
151
|
-
}
|
|
152
|
-
|
|
153
|
-
return resolve();
|
|
154
|
-
});
|
|
155
|
-
return app(req, res, handler);
|
|
156
|
-
});
|
|
157
|
-
}
|
|
158
|
-
|
|
159
|
-
}), {
|
|
160
|
-
name: '@modern-js/plugin-express',
|
|
161
|
-
pre: ['@modern-js/plugin-bff']
|
|
162
|
-
});
|
|
160
|
+
})
|
|
161
|
+
}));
|
package/dist/js/node/index.js
CHANGED
|
@@ -24,6 +24,5 @@ Object.keys(_context).forEach(function (key) {
|
|
|
24
24
|
|
|
25
25
|
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
|
|
26
26
|
|
|
27
|
-
// eslint-disable-next-line filenames/match-exported
|
|
28
27
|
var _default = _plugin.default;
|
|
29
28
|
exports.default = _default;
|
package/dist/js/node/plugin.js
CHANGED
|
@@ -11,8 +11,6 @@ var _express = _interopRequireDefault(require("express"));
|
|
|
11
11
|
|
|
12
12
|
var _cookieParser = _interopRequireDefault(require("cookie-parser"));
|
|
13
13
|
|
|
14
|
-
var _serverCore = require("@modern-js/server-core");
|
|
15
|
-
|
|
16
14
|
var _bffUtils = require("@modern-js/bff-utils");
|
|
17
15
|
|
|
18
16
|
var _utils = require("@modern-js/utils");
|
|
@@ -70,117 +68,117 @@ const initApp = app => {
|
|
|
70
68
|
return app;
|
|
71
69
|
};
|
|
72
70
|
|
|
73
|
-
var _default = (
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
71
|
+
var _default = () => ({
|
|
72
|
+
name: '@modern-js/plugin-express',
|
|
73
|
+
pre: ['@modern-js/plugin-bff'],
|
|
74
|
+
setup: () => ({
|
|
75
|
+
async prepareApiServer({
|
|
76
|
+
pwd,
|
|
77
|
+
mode,
|
|
78
|
+
config
|
|
79
|
+
}) {
|
|
80
|
+
let app;
|
|
81
|
+
const apiDir = path.join(pwd, './api');
|
|
82
|
+
|
|
83
|
+
if (mode === 'framework') {
|
|
84
|
+
app = await findAppModule(apiDir);
|
|
85
|
+
|
|
86
|
+
if (!app || !app.use) {
|
|
87
|
+
// console.warn('There is not api/app.ts.');
|
|
88
|
+
app = (0, _express.default)();
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
initApp(app);
|
|
92
|
+
|
|
93
|
+
if (config) {
|
|
94
|
+
const {
|
|
95
|
+
middleware
|
|
96
|
+
} = config;
|
|
97
|
+
initMiddlewares(middleware, app);
|
|
98
|
+
} // eslint-disable-next-line react-hooks/rules-of-hooks
|
|
99
|
+
|
|
100
|
+
|
|
101
|
+
useRun(app);
|
|
102
|
+
(0, _registerRoutes.default)(app);
|
|
103
|
+
} else if (mode === 'function') {
|
|
88
104
|
app = (0, _express.default)();
|
|
105
|
+
initApp(app);
|
|
106
|
+
|
|
107
|
+
if (config) {
|
|
108
|
+
const {
|
|
109
|
+
middleware
|
|
110
|
+
} = config;
|
|
111
|
+
initMiddlewares(middleware, app);
|
|
112
|
+
} // eslint-disable-next-line react-hooks/rules-of-hooks
|
|
113
|
+
|
|
114
|
+
|
|
115
|
+
useRun(app);
|
|
116
|
+
(0, _registerRoutes.default)(app);
|
|
117
|
+
} else {
|
|
118
|
+
throw new Error(`mode must be function or framework`);
|
|
89
119
|
}
|
|
90
120
|
|
|
91
|
-
|
|
121
|
+
return (req, res) => new Promise((resolve, reject) => {
|
|
122
|
+
const handler = err => {
|
|
123
|
+
if (err) {
|
|
124
|
+
return reject(err);
|
|
125
|
+
} // finalhanlder will trigger 'finish' event
|
|
92
126
|
|
|
93
|
-
if (config) {
|
|
94
|
-
const {
|
|
95
|
-
middleware
|
|
96
|
-
} = config;
|
|
97
|
-
initMiddlewares(middleware, app);
|
|
98
|
-
} // eslint-disable-next-line react-hooks/rules-of-hooks
|
|
99
127
|
|
|
128
|
+
return (0, _finalhandler.default)(req, res, {})(null); // return resolve();
|
|
129
|
+
};
|
|
100
130
|
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
131
|
+
res.on('finish', err => {
|
|
132
|
+
if (err) {
|
|
133
|
+
return reject(err);
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
return resolve();
|
|
137
|
+
});
|
|
138
|
+
return app(req, res, handler);
|
|
139
|
+
});
|
|
140
|
+
},
|
|
141
|
+
|
|
142
|
+
prepareWebServer({
|
|
143
|
+
config
|
|
144
|
+
}) {
|
|
145
|
+
const app = (0, _express.default)();
|
|
105
146
|
initApp(app);
|
|
106
147
|
|
|
107
148
|
if (config) {
|
|
108
149
|
const {
|
|
109
150
|
middleware
|
|
110
151
|
} = config;
|
|
152
|
+
debug('web middleware', middleware);
|
|
111
153
|
initMiddlewares(middleware, app);
|
|
112
|
-
}
|
|
154
|
+
}
|
|
113
155
|
|
|
156
|
+
return (req, res) => new Promise((resolve, reject) => {
|
|
157
|
+
const handler = err => {
|
|
158
|
+
if (err) {
|
|
159
|
+
return reject(err);
|
|
160
|
+
}
|
|
114
161
|
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
throw new Error(`mode must be function or framework`);
|
|
119
|
-
}
|
|
162
|
+
if (res.headersSent && res.statusCode !== 200) {
|
|
163
|
+
(0, _finalhandler.default)(req, res, {})(null);
|
|
164
|
+
}
|
|
120
165
|
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
if (err) {
|
|
124
|
-
return reject(err);
|
|
125
|
-
} // finalhanlder will trigger 'finish' event
|
|
166
|
+
return resolve();
|
|
167
|
+
}; // when user call res.send
|
|
126
168
|
|
|
127
169
|
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
if (err) {
|
|
133
|
-
return reject(err);
|
|
134
|
-
}
|
|
170
|
+
res.on('finish', err => {
|
|
171
|
+
if (err) {
|
|
172
|
+
return reject(err);
|
|
173
|
+
}
|
|
135
174
|
|
|
136
|
-
|
|
175
|
+
return resolve();
|
|
176
|
+
});
|
|
177
|
+
return app(req, res, handler);
|
|
137
178
|
});
|
|
138
|
-
return app(req, res, handler);
|
|
139
|
-
});
|
|
140
|
-
},
|
|
141
|
-
|
|
142
|
-
prepareWebServer({
|
|
143
|
-
config
|
|
144
|
-
}) {
|
|
145
|
-
const app = (0, _express.default)();
|
|
146
|
-
initApp(app);
|
|
147
|
-
|
|
148
|
-
if (config) {
|
|
149
|
-
const {
|
|
150
|
-
middleware
|
|
151
|
-
} = config;
|
|
152
|
-
debug('web middleware', middleware);
|
|
153
|
-
initMiddlewares(middleware, app);
|
|
154
179
|
}
|
|
155
180
|
|
|
156
|
-
|
|
157
|
-
const handler = err => {
|
|
158
|
-
if (err) {
|
|
159
|
-
return reject(err);
|
|
160
|
-
}
|
|
161
|
-
|
|
162
|
-
if (res.headersSent && res.statusCode !== 200) {
|
|
163
|
-
(0, _finalhandler.default)(req, res, {})(null);
|
|
164
|
-
}
|
|
165
|
-
|
|
166
|
-
return resolve();
|
|
167
|
-
}; // when user call res.send
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
res.on('finish', err => {
|
|
171
|
-
if (err) {
|
|
172
|
-
return reject(err);
|
|
173
|
-
}
|
|
174
|
-
|
|
175
|
-
return resolve();
|
|
176
|
-
});
|
|
177
|
-
return app(req, res, handler);
|
|
178
|
-
});
|
|
179
|
-
}
|
|
180
|
-
|
|
181
|
-
}), {
|
|
182
|
-
name: '@modern-js/plugin-express',
|
|
183
|
-
pre: ['@modern-js/plugin-bff']
|
|
181
|
+
})
|
|
184
182
|
});
|
|
185
183
|
|
|
186
184
|
exports.default = _default;
|
package/dist/types/plugin.d.ts
CHANGED
package/jest.config.js
CHANGED
package/package.json
CHANGED
|
@@ -11,7 +11,7 @@
|
|
|
11
11
|
"modern",
|
|
12
12
|
"modern.js"
|
|
13
13
|
],
|
|
14
|
-
"version": "1.
|
|
14
|
+
"version": "1.5.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,18 +35,21 @@
|
|
|
35
35
|
"dependencies": {
|
|
36
36
|
"@babel/runtime": "^7.15.3",
|
|
37
37
|
"@modern-js/adapter-helpers": "^1.2.1",
|
|
38
|
-
"@modern-js/
|
|
38
|
+
"@modern-js/bff-runtime": "^1.2.1",
|
|
39
|
+
"@modern-js/bff-utils": "^1.2.2",
|
|
40
|
+
"@modern-js/types": "^1.4.0",
|
|
41
|
+
"@modern-js/utils": "^1.4.0",
|
|
39
42
|
"cookie-parser": "^1.4.5",
|
|
40
43
|
"finalhandler": "^1.1.2",
|
|
41
44
|
"formidable": "^1.2.2",
|
|
42
|
-
"type-is": "^1.6.18"
|
|
43
|
-
"@modern-js/bff-utils": "^1.2.2",
|
|
44
|
-
"@modern-js/server-core": "^1.2.3",
|
|
45
|
-
"@modern-js/bff-runtime": "^1.2.1"
|
|
45
|
+
"type-is": "^1.6.18"
|
|
46
46
|
},
|
|
47
47
|
"devDependencies": {
|
|
48
|
-
"
|
|
49
|
-
"@modern-js/server-
|
|
48
|
+
"@modern-js/core": "^1.7.0",
|
|
49
|
+
"@modern-js/server-core": "^1.3.0",
|
|
50
|
+
"@modern-js/server-utils": "^1.2.2",
|
|
51
|
+
"@scripts/build": "0.0.0",
|
|
52
|
+
"@scripts/jest-config": "0.0.0",
|
|
50
53
|
"@types/cookie-parser": "^1.4.2",
|
|
51
54
|
"@types/express": "^4.17.13",
|
|
52
55
|
"@types/finalhandler": "^1.1.1",
|
|
@@ -55,12 +58,10 @@
|
|
|
55
58
|
"@types/node": "^14",
|
|
56
59
|
"@types/supertest": "^2.0.11",
|
|
57
60
|
"@types/type-is": "^1.6.3",
|
|
58
|
-
"
|
|
59
|
-
"typescript": "^4",
|
|
60
|
-
"@modern-js/core": "^1.5.0",
|
|
61
|
-
"@scripts/build": "0.0.0",
|
|
61
|
+
"express": "^4.17.1",
|
|
62
62
|
"jest": "^27",
|
|
63
|
-
"
|
|
63
|
+
"supertest": "^6.1.6",
|
|
64
|
+
"typescript": "^4"
|
|
64
65
|
},
|
|
65
66
|
"modernConfig": {
|
|
66
67
|
"output": {
|
|
@@ -90,4 +90,19 @@ describe('function-mode', () => {
|
|
|
90
90
|
expect(res.status).toBe(200);
|
|
91
91
|
expect(res.body.protocol).toBe('Farrow-API');
|
|
92
92
|
});
|
|
93
|
+
|
|
94
|
+
test('should support upload file', done => {
|
|
95
|
+
request(apiHandler)
|
|
96
|
+
.post('/upload')
|
|
97
|
+
.field('my_field', 'value')
|
|
98
|
+
.attach('file', __filename)
|
|
99
|
+
.end(async (err, res) => {
|
|
100
|
+
if (err) {
|
|
101
|
+
throw err;
|
|
102
|
+
}
|
|
103
|
+
expect(res.statusCode).toBe(200);
|
|
104
|
+
expect(res.body.message).toBe('success');
|
|
105
|
+
done();
|
|
106
|
+
});
|
|
107
|
+
});
|
|
93
108
|
});
|
package/tests/lambdaMode.test.ts
CHANGED
|
@@ -13,6 +13,7 @@ const API_DIR = './api';
|
|
|
13
13
|
describe('lambda-mode', () => {
|
|
14
14
|
const id = '666';
|
|
15
15
|
const name = 'modern';
|
|
16
|
+
const prefix = '/api';
|
|
16
17
|
const foo = { id, name };
|
|
17
18
|
let apiHandler: any;
|
|
18
19
|
|
|
@@ -21,40 +22,41 @@ describe('lambda-mode', () => {
|
|
|
21
22
|
.clone()
|
|
22
23
|
.usePlugin(APIPlugin, plugin)
|
|
23
24
|
.init();
|
|
25
|
+
|
|
24
26
|
apiHandler = await runner.prepareApiServer({
|
|
25
27
|
pwd,
|
|
26
28
|
mode: 'framework',
|
|
27
|
-
prefix
|
|
29
|
+
prefix,
|
|
28
30
|
});
|
|
29
31
|
});
|
|
30
32
|
|
|
31
33
|
test('should works', async () => {
|
|
32
|
-
const res = await request(apiHandler).get(
|
|
34
|
+
const res = await request(apiHandler).get(`${prefix}/hello`);
|
|
33
35
|
expect(res.status).toBe(200);
|
|
34
36
|
expect(res.body).toEqual({ message: 'hello' });
|
|
35
37
|
});
|
|
36
38
|
|
|
37
39
|
test('should works with query', async () => {
|
|
38
|
-
const res = await request(apiHandler).get(
|
|
40
|
+
const res = await request(apiHandler).get(`${prefix}/nest/user?id=${id}`);
|
|
39
41
|
expect(res.status).toBe(200);
|
|
40
42
|
expect(res.body.query.id).toBe(id);
|
|
41
43
|
});
|
|
42
44
|
|
|
43
45
|
test('should works with body', async () => {
|
|
44
|
-
const res = await request(apiHandler).post(
|
|
46
|
+
const res = await request(apiHandler).post(`${prefix}/nest/user`).send(foo);
|
|
45
47
|
expect(res.status).toBe(200);
|
|
46
48
|
expect(res.body.data).toEqual(foo);
|
|
47
49
|
});
|
|
48
50
|
|
|
49
51
|
test('should works with dynamic route ', async () => {
|
|
50
|
-
const res = await request(apiHandler).post(
|
|
52
|
+
const res = await request(apiHandler).post(`${prefix}/nest/${id}`);
|
|
51
53
|
expect(res.status).toBe(200);
|
|
52
54
|
expect(res.body).toEqual({ id });
|
|
53
55
|
});
|
|
54
56
|
|
|
55
57
|
test('should works with context', async () => {
|
|
56
58
|
const res = await request(apiHandler)
|
|
57
|
-
.post(
|
|
59
|
+
.post(`${prefix}/nest/user?id=${id}`)
|
|
58
60
|
.send(foo);
|
|
59
61
|
expect(res.status).toBe(200);
|
|
60
62
|
expect(res.body.data).toEqual(foo);
|
|
@@ -63,7 +65,7 @@ describe('lambda-mode', () => {
|
|
|
63
65
|
|
|
64
66
|
test('should support cookies', async () => {
|
|
65
67
|
const res = await request(apiHandler)
|
|
66
|
-
.post(
|
|
68
|
+
.post(`${prefix}/nest/user?id=${id}`)
|
|
67
69
|
.set('Cookie', [`id=${id};name=${name}`]);
|
|
68
70
|
expect(res.status).toBe(200);
|
|
69
71
|
expect(res.body.cookies.id).toBe(id);
|
|
@@ -71,19 +73,19 @@ describe('lambda-mode', () => {
|
|
|
71
73
|
});
|
|
72
74
|
|
|
73
75
|
test('should works with schema', async () => {
|
|
74
|
-
const res = await request(apiHandler).patch(
|
|
76
|
+
const res = await request(apiHandler).patch(`${prefix}/nest/user`).send({
|
|
75
77
|
id: 777,
|
|
76
78
|
name: 'xxx',
|
|
77
79
|
});
|
|
78
80
|
expect(res.status).toBe(200);
|
|
79
81
|
|
|
80
|
-
const res2 = await request(apiHandler).patch(
|
|
82
|
+
const res2 = await request(apiHandler).patch(`${prefix}/nest/user`).send({
|
|
81
83
|
id: 'aaa',
|
|
82
84
|
name: 'xxx',
|
|
83
85
|
});
|
|
84
86
|
expect(res2.status).toBe(400);
|
|
85
87
|
|
|
86
|
-
const res3 = await request(apiHandler).patch(
|
|
88
|
+
const res3 = await request(apiHandler).patch(`${prefix}/nest/user`).send({
|
|
87
89
|
id: '777',
|
|
88
90
|
name: 'xxx',
|
|
89
91
|
});
|
|
@@ -97,6 +99,21 @@ describe('lambda-mode', () => {
|
|
|
97
99
|
expect(res.status).toBe(200);
|
|
98
100
|
expect(res.body.protocol).toBe('Farrow-API');
|
|
99
101
|
});
|
|
102
|
+
|
|
103
|
+
test('should support upload file', done => {
|
|
104
|
+
request(apiHandler)
|
|
105
|
+
.post(`${prefix}/upload`)
|
|
106
|
+
.field('my_field', 'value')
|
|
107
|
+
.attach('file', __filename)
|
|
108
|
+
.end(async (err, res) => {
|
|
109
|
+
if (err) {
|
|
110
|
+
throw err;
|
|
111
|
+
}
|
|
112
|
+
expect(res.statusCode).toBe(200);
|
|
113
|
+
expect(res.body.message).toBe('success');
|
|
114
|
+
done();
|
|
115
|
+
});
|
|
116
|
+
});
|
|
100
117
|
});
|
|
101
118
|
|
|
102
119
|
describe('add middwares', () => {
|