@modern-js/plugin-koa 1.4.3 → 1.4.6
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 +35 -0
- package/dist/js/modern/cli/index.js +3 -8
- package/dist/js/modern/index.js +0 -1
- package/dist/js/modern/plugin.js +85 -82
- package/dist/js/modern/registerRoutes.js +3 -32
- package/dist/js/node/cli/index.js +4 -8
- package/dist/js/node/index.js +0 -1
- package/dist/js/node/plugin.js +84 -82
- package/dist/js/node/registerRoutes.js +3 -33
- package/dist/types/plugin.d.ts +2 -1
- package/jest.config.js +0 -1
- package/package.json +13 -15
- package/tests/.eslintrc.js +0 -6
- package/tests/common.ts +0 -9
- package/tests/fixtures/assets/index.html +0 -12
- package/tests/fixtures/function-mode/api/_app.ts +0 -0
- package/tests/fixtures/function-mode/api/hello.ts +0 -1
- package/tests/fixtures/function-mode/api/nest/user.ts +0 -33
- package/tests/fixtures/lambda-mode/api/app.ts +0 -1
- package/tests/fixtures/lambda-mode/api/lambda/hello.ts +0 -1
- package/tests/fixtures/lambda-mode/api/lambda/nest/[id].ts +0 -1
- package/tests/fixtures/lambda-mode/api/lambda/nest/user.ts +0 -33
- package/tests/functionMode.test.ts +0 -53
- package/tests/helpers.ts +0 -14
- package/tests/lambdaMode.test.ts +0 -301
- package/tests/tsconfig.json +0 -12
- package/tests/webServer.test.ts +0 -81
package/CHANGELOG.md
CHANGED
|
@@ -1,5 +1,40 @@
|
|
|
1
1
|
# @modern-js/plugin-koa
|
|
2
2
|
|
|
3
|
+
## 1.4.6
|
|
4
|
+
|
|
5
|
+
### Patch Changes
|
|
6
|
+
|
|
7
|
+
- 04ae5262: chore: bump @modern-js/utils to v1.4.1 in dependencies
|
|
8
|
+
- 60f7d8bf: feat: add tests dir to npmignore
|
|
9
|
+
- befd9e5b: fix: compatible with babel-plugin-resolver's handling of relative paths on windows
|
|
10
|
+
- Updated dependencies [b8599d09]
|
|
11
|
+
- Updated dependencies [6cffe99d]
|
|
12
|
+
- Updated dependencies [04ae5262]
|
|
13
|
+
- Updated dependencies [60f7d8bf]
|
|
14
|
+
- Updated dependencies [befd9e5b]
|
|
15
|
+
- Updated dependencies [3bf4f8b0]
|
|
16
|
+
- @modern-js/utils@1.5.0
|
|
17
|
+
- @modern-js/adapter-helpers@1.2.3
|
|
18
|
+
- @modern-js/bff-runtime@1.2.2
|
|
19
|
+
- @modern-js/bff-utils@1.2.3
|
|
20
|
+
|
|
21
|
+
## 1.4.5
|
|
22
|
+
|
|
23
|
+
### Patch Changes
|
|
24
|
+
|
|
25
|
+
- bebb39b6: chore: improve devDependencies and peerDependencies
|
|
26
|
+
- Updated dependencies [132f7b53]
|
|
27
|
+
- @modern-js/utils@1.3.7
|
|
28
|
+
|
|
29
|
+
## 1.4.4
|
|
30
|
+
|
|
31
|
+
### Patch Changes
|
|
32
|
+
|
|
33
|
+
- 952432ac: fix: upload files
|
|
34
|
+
- c1b8fa0a: feat: convert to new server plugin
|
|
35
|
+
- Updated dependencies [c2046f37]
|
|
36
|
+
- @modern-js/utils@1.3.6
|
|
37
|
+
|
|
3
38
|
## 1.4.3
|
|
4
39
|
|
|
5
40
|
### Patch Changes
|
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import * as path from 'path';
|
|
2
2
|
import { createRuntimeExportsUtils } from '@modern-js/utils';
|
|
3
|
+
import { getRelativeRuntimePath } from '@modern-js/adapter-helpers';
|
|
3
4
|
export default (() => ({
|
|
4
5
|
name: '@modern-js/plugin-koa',
|
|
5
6
|
setup: api => {
|
|
@@ -16,14 +17,8 @@ export default (() => ({
|
|
|
16
17
|
appDirectory
|
|
17
18
|
} = appContext;
|
|
18
19
|
bffExportsUtils = createRuntimeExportsUtils(appContext.internalDirectory, 'server');
|
|
19
|
-
const serverRuntimePath = bffExportsUtils.getPath();
|
|
20
|
-
|
|
21
|
-
let relativeRuntimePath = path.join('../', path.relative(appDirectory, serverRuntimePath));
|
|
22
|
-
|
|
23
|
-
if (process.env.NODE_ENV === 'development' || process.env.NODE_ENV === 'test') {
|
|
24
|
-
relativeRuntimePath = `./${path.relative(appDirectory, serverRuntimePath)}`;
|
|
25
|
-
}
|
|
26
|
-
|
|
20
|
+
const serverRuntimePath = bffExportsUtils.getPath();
|
|
21
|
+
const relativeRuntimePath = getRelativeRuntimePath(appDirectory, serverRuntimePath);
|
|
27
22
|
return {
|
|
28
23
|
source: {
|
|
29
24
|
alias: {
|
package/dist/js/modern/index.js
CHANGED
package/dist/js/modern/plugin.js
CHANGED
|
@@ -2,7 +2,6 @@ import * as path from 'path';
|
|
|
2
2
|
import Koa from 'koa';
|
|
3
3
|
import Router from 'koa-router';
|
|
4
4
|
import koaBody from 'koa-body';
|
|
5
|
-
import { createPlugin } from '@modern-js/server-core';
|
|
6
5
|
import { requireModule } from '@modern-js/bff-utils';
|
|
7
6
|
import { fs } from '@modern-js/utils';
|
|
8
7
|
import { run } from "./context";
|
|
@@ -28,37 +27,85 @@ const initMiddlewares = (middleware, app) => {
|
|
|
28
27
|
});
|
|
29
28
|
};
|
|
30
29
|
|
|
31
|
-
export default
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
30
|
+
export default (() => ({
|
|
31
|
+
name: '@modern-js/plugin-koa',
|
|
32
|
+
pre: ['@modern-js/plugin-bff'],
|
|
33
|
+
setup: () => ({
|
|
34
|
+
async prepareApiServer({
|
|
35
|
+
pwd,
|
|
36
|
+
mode,
|
|
37
|
+
config,
|
|
38
|
+
prefix
|
|
39
|
+
}) {
|
|
40
|
+
let app;
|
|
41
|
+
const router = new Router();
|
|
42
|
+
const apiDir = path.join(pwd, './api');
|
|
43
|
+
|
|
44
|
+
if (mode === 'framework') {
|
|
45
|
+
app = await findAppModule(apiDir);
|
|
46
|
+
|
|
47
|
+
if (!(app instanceof Koa)) {
|
|
48
|
+
app = new Koa();
|
|
49
|
+
app.use(koaBody({
|
|
50
|
+
multipart: true
|
|
51
|
+
}));
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
if (config) {
|
|
55
|
+
const {
|
|
56
|
+
middleware
|
|
57
|
+
} = config;
|
|
58
|
+
initMiddlewares(middleware, app);
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
app.use(run);
|
|
62
|
+
registerRoutes(router, prefix);
|
|
63
|
+
} else if (mode === 'function') {
|
|
47
64
|
app = new Koa();
|
|
48
|
-
app.use(koaBody(
|
|
49
|
-
|
|
65
|
+
app.use(koaBody({
|
|
66
|
+
multipart: true
|
|
67
|
+
}));
|
|
68
|
+
|
|
69
|
+
if (config) {
|
|
70
|
+
const {
|
|
71
|
+
middleware
|
|
72
|
+
} = config;
|
|
73
|
+
initMiddlewares(middleware, app);
|
|
74
|
+
}
|
|
50
75
|
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
initMiddlewares(middleware, app);
|
|
76
|
+
app.use(run);
|
|
77
|
+
registerRoutes(router, prefix);
|
|
78
|
+
} else {
|
|
79
|
+
throw new Error(`mode must be function or framework`);
|
|
56
80
|
}
|
|
57
81
|
|
|
58
|
-
app.use(
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
82
|
+
app.use(router.routes());
|
|
83
|
+
return (req, res) => {
|
|
84
|
+
app.on('error', err => {
|
|
85
|
+
if (err) {
|
|
86
|
+
throw err;
|
|
87
|
+
}
|
|
88
|
+
});
|
|
89
|
+
return Promise.resolve(app.callback()(req, res));
|
|
90
|
+
};
|
|
91
|
+
},
|
|
92
|
+
|
|
93
|
+
prepareWebServer({
|
|
94
|
+
config
|
|
95
|
+
}) {
|
|
96
|
+
const app = new Koa();
|
|
97
|
+
app.use(async (ctx, next) => {
|
|
98
|
+
await next();
|
|
99
|
+
|
|
100
|
+
if (!ctx.body) {
|
|
101
|
+
// restore statusCode
|
|
102
|
+
if (ctx.res.statusCode === 404 && !ctx.response._explicitStatus) {
|
|
103
|
+
ctx.res.statusCode = 200;
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
ctx.respond = false;
|
|
107
|
+
}
|
|
108
|
+
});
|
|
62
109
|
app.use(koaBody());
|
|
63
110
|
|
|
64
111
|
if (config) {
|
|
@@ -68,59 +115,15 @@ export default createPlugin(() => ({
|
|
|
68
115
|
initMiddlewares(middleware, app);
|
|
69
116
|
}
|
|
70
117
|
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
118
|
+
return (req, res) => {
|
|
119
|
+
app.on('error', err => {
|
|
120
|
+
if (err) {
|
|
121
|
+
throw err;
|
|
122
|
+
}
|
|
123
|
+
});
|
|
124
|
+
return Promise.resolve(app.callback()(req, res));
|
|
125
|
+
};
|
|
75
126
|
}
|
|
76
127
|
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
app.on('error', err => {
|
|
80
|
-
if (err) {
|
|
81
|
-
throw err;
|
|
82
|
-
}
|
|
83
|
-
});
|
|
84
|
-
return Promise.resolve(app.callback()(req, res));
|
|
85
|
-
};
|
|
86
|
-
},
|
|
87
|
-
|
|
88
|
-
prepareWebServer({
|
|
89
|
-
config
|
|
90
|
-
}) {
|
|
91
|
-
const app = new Koa();
|
|
92
|
-
app.use(async (ctx, next) => {
|
|
93
|
-
await next();
|
|
94
|
-
|
|
95
|
-
if (!ctx.body) {
|
|
96
|
-
// restore statusCode
|
|
97
|
-
if (ctx.res.statusCode === 404 && !ctx.response._explicitStatus) {
|
|
98
|
-
ctx.res.statusCode = 200;
|
|
99
|
-
}
|
|
100
|
-
|
|
101
|
-
ctx.respond = false;
|
|
102
|
-
}
|
|
103
|
-
});
|
|
104
|
-
app.use(koaBody());
|
|
105
|
-
|
|
106
|
-
if (config) {
|
|
107
|
-
const {
|
|
108
|
-
middleware
|
|
109
|
-
} = config;
|
|
110
|
-
initMiddlewares(middleware, app);
|
|
111
|
-
}
|
|
112
|
-
|
|
113
|
-
return (req, res) => {
|
|
114
|
-
app.on('error', err => {
|
|
115
|
-
if (err) {
|
|
116
|
-
throw err;
|
|
117
|
-
}
|
|
118
|
-
});
|
|
119
|
-
return Promise.resolve(app.callback()(req, res));
|
|
120
|
-
};
|
|
121
|
-
}
|
|
122
|
-
|
|
123
|
-
}), {
|
|
124
|
-
name: '@modern-js/plugin-koa',
|
|
125
|
-
pre: ['@modern-js/plugin-bff']
|
|
126
|
-
});
|
|
128
|
+
})
|
|
129
|
+
}));
|
|
@@ -1,13 +1,6 @@
|
|
|
1
|
-
function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); enumerableOnly && (symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; })), keys.push.apply(keys, symbols); } return keys; }
|
|
2
|
-
|
|
3
|
-
function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = null != arguments[i] ? arguments[i] : {}; i % 2 ? ownKeys(Object(source), !0).forEach(function (key) { _defineProperty(target, key, source[key]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)) : ownKeys(Object(source)).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } return target; }
|
|
4
|
-
|
|
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
|
-
|
|
7
1
|
import { HttpMethod, useAPIHandlerInfos } from '@modern-js/bff-utils';
|
|
8
2
|
import { isSchemaHandler } from '@modern-js/bff-runtime';
|
|
9
3
|
import typeIs from 'type-is';
|
|
10
|
-
import formidable from 'formidable';
|
|
11
4
|
import { sortDynamicRoutes } from '@modern-js/adapter-helpers';
|
|
12
5
|
import { createDebugger } from '@modern-js/utils';
|
|
13
6
|
const debug = createDebugger('koa');
|
|
@@ -35,25 +28,18 @@ const registerRoutes = (router, prefix) => {
|
|
|
35
28
|
|
|
36
29
|
if (result.type !== 'HandleSuccess') {
|
|
37
30
|
if (result.type === 'InputValidationError') {
|
|
38
|
-
// eslint-disable-next-line require-atomic-updates
|
|
39
31
|
ctx.status = 400;
|
|
40
32
|
} else {
|
|
41
|
-
// eslint-disable-next-line require-atomic-updates
|
|
42
33
|
ctx.status = 500;
|
|
43
|
-
}
|
|
44
|
-
|
|
34
|
+
}
|
|
45
35
|
|
|
46
36
|
ctx.body = result.message;
|
|
47
37
|
} else {
|
|
48
|
-
// eslint-disable-next-line require-atomic-updates
|
|
49
38
|
ctx.body = result.value;
|
|
50
39
|
}
|
|
51
40
|
} else {
|
|
52
|
-
const args = Object.values(input.params).concat(input); // eslint-disable-next-line
|
|
53
|
-
|
|
54
|
-
ctx.type = 'json'; // eslint-disable-next-line @typescript-eslint/ban-ts-comment
|
|
41
|
+
const args = Object.values(input.params).concat(input); // eslint-disable-next-line @typescript-eslint/ban-ts-comment
|
|
55
42
|
// @ts-expect-error
|
|
56
|
-
// eslint-disable-next-line require-atomic-updates
|
|
57
43
|
|
|
58
44
|
ctx.body = await handler(...args);
|
|
59
45
|
}
|
|
@@ -85,7 +71,7 @@ const getInputFromRequest = async ctx => {
|
|
|
85
71
|
if (typeIs.is(ctx.request.type, ['application/json'])) {
|
|
86
72
|
draft.data = ctx.request.body;
|
|
87
73
|
} else if (typeIs.is(ctx.request.type, ['multipart/form-data'])) {
|
|
88
|
-
draft.formData =
|
|
74
|
+
draft.formData = ctx.request.files;
|
|
89
75
|
} else if (typeIs.is(ctx.request.type, ['application/x-www-form-urlencoded'])) {
|
|
90
76
|
draft.formUrlencoded = ctx.request.body;
|
|
91
77
|
} else {
|
|
@@ -93,19 +79,4 @@ const getInputFromRequest = async ctx => {
|
|
|
93
79
|
}
|
|
94
80
|
|
|
95
81
|
return draft;
|
|
96
|
-
};
|
|
97
|
-
|
|
98
|
-
const resvoleFormData = ctx => {
|
|
99
|
-
const form = formidable({
|
|
100
|
-
multiples: true
|
|
101
|
-
});
|
|
102
|
-
return new Promise((resolve, reject) => {
|
|
103
|
-
form.parse(ctx.req, (err, fields, files) => {
|
|
104
|
-
if (err) {
|
|
105
|
-
reject(err);
|
|
106
|
-
}
|
|
107
|
-
|
|
108
|
-
resolve(_objectSpread(_objectSpread({}, fields), files));
|
|
109
|
-
});
|
|
110
|
-
});
|
|
111
82
|
};
|
|
@@ -9,6 +9,8 @@ var path = _interopRequireWildcard(require("path"));
|
|
|
9
9
|
|
|
10
10
|
var _utils = require("@modern-js/utils");
|
|
11
11
|
|
|
12
|
+
var _adapterHelpers = require("@modern-js/adapter-helpers");
|
|
13
|
+
|
|
12
14
|
function _getRequireWildcardCache(nodeInterop) { if (typeof WeakMap !== "function") return null; var cacheBabelInterop = new WeakMap(); var cacheNodeInterop = new WeakMap(); return (_getRequireWildcardCache = function (nodeInterop) { return nodeInterop ? cacheNodeInterop : cacheBabelInterop; })(nodeInterop); }
|
|
13
15
|
|
|
14
16
|
function _interopRequireWildcard(obj, nodeInterop) { if (!nodeInterop && obj && obj.__esModule) { return obj; } if (obj === null || typeof obj !== "object" && typeof obj !== "function") { return { default: obj }; } var cache = _getRequireWildcardCache(nodeInterop); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (key !== "default" && Object.prototype.hasOwnProperty.call(obj, key)) { var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null; if (desc && (desc.get || desc.set)) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } newObj.default = obj; if (cache) { cache.set(obj, newObj); } return newObj; }
|
|
@@ -29,14 +31,8 @@ var _default = () => ({
|
|
|
29
31
|
appDirectory
|
|
30
32
|
} = appContext;
|
|
31
33
|
bffExportsUtils = (0, _utils.createRuntimeExportsUtils)(appContext.internalDirectory, 'server');
|
|
32
|
-
const serverRuntimePath = bffExportsUtils.getPath();
|
|
33
|
-
|
|
34
|
-
let relativeRuntimePath = path.join('../', path.relative(appDirectory, serverRuntimePath));
|
|
35
|
-
|
|
36
|
-
if (process.env.NODE_ENV === 'development' || process.env.NODE_ENV === 'test') {
|
|
37
|
-
relativeRuntimePath = `./${path.relative(appDirectory, serverRuntimePath)}`;
|
|
38
|
-
}
|
|
39
|
-
|
|
34
|
+
const serverRuntimePath = bffExportsUtils.getPath();
|
|
35
|
+
const relativeRuntimePath = (0, _adapterHelpers.getRelativeRuntimePath)(appDirectory, serverRuntimePath);
|
|
40
36
|
return {
|
|
41
37
|
source: {
|
|
42
38
|
alias: {
|
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
|
@@ -13,8 +13,6 @@ var _koaRouter = _interopRequireDefault(require("koa-router"));
|
|
|
13
13
|
|
|
14
14
|
var _koaBody = _interopRequireDefault(require("koa-body"));
|
|
15
15
|
|
|
16
|
-
var _serverCore = require("@modern-js/server-core");
|
|
17
|
-
|
|
18
16
|
var _bffUtils = require("@modern-js/bff-utils");
|
|
19
17
|
|
|
20
18
|
var _utils = require("@modern-js/utils");
|
|
@@ -49,37 +47,85 @@ const initMiddlewares = (middleware, app) => {
|
|
|
49
47
|
});
|
|
50
48
|
};
|
|
51
49
|
|
|
52
|
-
var _default = (
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
50
|
+
var _default = () => ({
|
|
51
|
+
name: '@modern-js/plugin-koa',
|
|
52
|
+
pre: ['@modern-js/plugin-bff'],
|
|
53
|
+
setup: () => ({
|
|
54
|
+
async prepareApiServer({
|
|
55
|
+
pwd,
|
|
56
|
+
mode,
|
|
57
|
+
config,
|
|
58
|
+
prefix
|
|
59
|
+
}) {
|
|
60
|
+
let app;
|
|
61
|
+
const router = new _koaRouter.default();
|
|
62
|
+
const apiDir = path.join(pwd, './api');
|
|
63
|
+
|
|
64
|
+
if (mode === 'framework') {
|
|
65
|
+
app = await findAppModule(apiDir);
|
|
66
|
+
|
|
67
|
+
if (!(app instanceof _koa.default)) {
|
|
68
|
+
app = new _koa.default();
|
|
69
|
+
app.use((0, _koaBody.default)({
|
|
70
|
+
multipart: true
|
|
71
|
+
}));
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
if (config) {
|
|
75
|
+
const {
|
|
76
|
+
middleware
|
|
77
|
+
} = config;
|
|
78
|
+
initMiddlewares(middleware, app);
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
app.use(_context.run);
|
|
82
|
+
(0, _registerRoutes.default)(router, prefix);
|
|
83
|
+
} else if (mode === 'function') {
|
|
68
84
|
app = new _koa.default();
|
|
69
|
-
app.use((0, _koaBody.default)(
|
|
70
|
-
|
|
85
|
+
app.use((0, _koaBody.default)({
|
|
86
|
+
multipart: true
|
|
87
|
+
}));
|
|
88
|
+
|
|
89
|
+
if (config) {
|
|
90
|
+
const {
|
|
91
|
+
middleware
|
|
92
|
+
} = config;
|
|
93
|
+
initMiddlewares(middleware, app);
|
|
94
|
+
}
|
|
71
95
|
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
initMiddlewares(middleware, app);
|
|
96
|
+
app.use(_context.run);
|
|
97
|
+
(0, _registerRoutes.default)(router, prefix);
|
|
98
|
+
} else {
|
|
99
|
+
throw new Error(`mode must be function or framework`);
|
|
77
100
|
}
|
|
78
101
|
|
|
79
|
-
app.use(
|
|
80
|
-
(
|
|
81
|
-
|
|
82
|
-
|
|
102
|
+
app.use(router.routes());
|
|
103
|
+
return (req, res) => {
|
|
104
|
+
app.on('error', err => {
|
|
105
|
+
if (err) {
|
|
106
|
+
throw err;
|
|
107
|
+
}
|
|
108
|
+
});
|
|
109
|
+
return Promise.resolve(app.callback()(req, res));
|
|
110
|
+
};
|
|
111
|
+
},
|
|
112
|
+
|
|
113
|
+
prepareWebServer({
|
|
114
|
+
config
|
|
115
|
+
}) {
|
|
116
|
+
const app = new _koa.default();
|
|
117
|
+
app.use(async (ctx, next) => {
|
|
118
|
+
await next();
|
|
119
|
+
|
|
120
|
+
if (!ctx.body) {
|
|
121
|
+
// restore statusCode
|
|
122
|
+
if (ctx.res.statusCode === 404 && !ctx.response._explicitStatus) {
|
|
123
|
+
ctx.res.statusCode = 200;
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
ctx.respond = false;
|
|
127
|
+
}
|
|
128
|
+
});
|
|
83
129
|
app.use((0, _koaBody.default)());
|
|
84
130
|
|
|
85
131
|
if (config) {
|
|
@@ -89,61 +135,17 @@ var _default = (0, _serverCore.createPlugin)(() => ({
|
|
|
89
135
|
initMiddlewares(middleware, app);
|
|
90
136
|
}
|
|
91
137
|
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
138
|
+
return (req, res) => {
|
|
139
|
+
app.on('error', err => {
|
|
140
|
+
if (err) {
|
|
141
|
+
throw err;
|
|
142
|
+
}
|
|
143
|
+
});
|
|
144
|
+
return Promise.resolve(app.callback()(req, res));
|
|
145
|
+
};
|
|
96
146
|
}
|
|
97
147
|
|
|
98
|
-
|
|
99
|
-
return (req, res) => {
|
|
100
|
-
app.on('error', err => {
|
|
101
|
-
if (err) {
|
|
102
|
-
throw err;
|
|
103
|
-
}
|
|
104
|
-
});
|
|
105
|
-
return Promise.resolve(app.callback()(req, res));
|
|
106
|
-
};
|
|
107
|
-
},
|
|
108
|
-
|
|
109
|
-
prepareWebServer({
|
|
110
|
-
config
|
|
111
|
-
}) {
|
|
112
|
-
const app = new _koa.default();
|
|
113
|
-
app.use(async (ctx, next) => {
|
|
114
|
-
await next();
|
|
115
|
-
|
|
116
|
-
if (!ctx.body) {
|
|
117
|
-
// restore statusCode
|
|
118
|
-
if (ctx.res.statusCode === 404 && !ctx.response._explicitStatus) {
|
|
119
|
-
ctx.res.statusCode = 200;
|
|
120
|
-
}
|
|
121
|
-
|
|
122
|
-
ctx.respond = false;
|
|
123
|
-
}
|
|
124
|
-
});
|
|
125
|
-
app.use((0, _koaBody.default)());
|
|
126
|
-
|
|
127
|
-
if (config) {
|
|
128
|
-
const {
|
|
129
|
-
middleware
|
|
130
|
-
} = config;
|
|
131
|
-
initMiddlewares(middleware, app);
|
|
132
|
-
}
|
|
133
|
-
|
|
134
|
-
return (req, res) => {
|
|
135
|
-
app.on('error', err => {
|
|
136
|
-
if (err) {
|
|
137
|
-
throw err;
|
|
138
|
-
}
|
|
139
|
-
});
|
|
140
|
-
return Promise.resolve(app.callback()(req, res));
|
|
141
|
-
};
|
|
142
|
-
}
|
|
143
|
-
|
|
144
|
-
}), {
|
|
145
|
-
name: '@modern-js/plugin-koa',
|
|
146
|
-
pre: ['@modern-js/plugin-bff']
|
|
148
|
+
})
|
|
147
149
|
});
|
|
148
150
|
|
|
149
151
|
exports.default = _default;
|
|
@@ -11,20 +11,12 @@ var _bffRuntime = require("@modern-js/bff-runtime");
|
|
|
11
11
|
|
|
12
12
|
var _typeIs = _interopRequireDefault(require("type-is"));
|
|
13
13
|
|
|
14
|
-
var _formidable = _interopRequireDefault(require("formidable"));
|
|
15
|
-
|
|
16
14
|
var _adapterHelpers = require("@modern-js/adapter-helpers");
|
|
17
15
|
|
|
18
16
|
var _utils = require("@modern-js/utils");
|
|
19
17
|
|
|
20
18
|
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
|
|
21
19
|
|
|
22
|
-
function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); enumerableOnly && (symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; })), keys.push.apply(keys, symbols); } return keys; }
|
|
23
|
-
|
|
24
|
-
function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = null != arguments[i] ? arguments[i] : {}; i % 2 ? ownKeys(Object(source), !0).forEach(function (key) { _defineProperty(target, key, source[key]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)) : ownKeys(Object(source)).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } return target; }
|
|
25
|
-
|
|
26
|
-
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; }
|
|
27
|
-
|
|
28
20
|
const debug = (0, _utils.createDebugger)('koa');
|
|
29
21
|
|
|
30
22
|
const registerRoutes = (router, prefix) => {
|
|
@@ -50,25 +42,18 @@ const registerRoutes = (router, prefix) => {
|
|
|
50
42
|
|
|
51
43
|
if (result.type !== 'HandleSuccess') {
|
|
52
44
|
if (result.type === 'InputValidationError') {
|
|
53
|
-
// eslint-disable-next-line require-atomic-updates
|
|
54
45
|
ctx.status = 400;
|
|
55
46
|
} else {
|
|
56
|
-
// eslint-disable-next-line require-atomic-updates
|
|
57
47
|
ctx.status = 500;
|
|
58
|
-
}
|
|
59
|
-
|
|
48
|
+
}
|
|
60
49
|
|
|
61
50
|
ctx.body = result.message;
|
|
62
51
|
} else {
|
|
63
|
-
// eslint-disable-next-line require-atomic-updates
|
|
64
52
|
ctx.body = result.value;
|
|
65
53
|
}
|
|
66
54
|
} else {
|
|
67
|
-
const args = Object.values(input.params).concat(input); // eslint-disable-next-line
|
|
68
|
-
|
|
69
|
-
ctx.type = 'json'; // eslint-disable-next-line @typescript-eslint/ban-ts-comment
|
|
55
|
+
const args = Object.values(input.params).concat(input); // eslint-disable-next-line @typescript-eslint/ban-ts-comment
|
|
70
56
|
// @ts-expect-error
|
|
71
|
-
// eslint-disable-next-line require-atomic-updates
|
|
72
57
|
|
|
73
58
|
ctx.body = await handler(...args);
|
|
74
59
|
}
|
|
@@ -101,7 +86,7 @@ const getInputFromRequest = async ctx => {
|
|
|
101
86
|
if (_typeIs.default.is(ctx.request.type, ['application/json'])) {
|
|
102
87
|
draft.data = ctx.request.body;
|
|
103
88
|
} else if (_typeIs.default.is(ctx.request.type, ['multipart/form-data'])) {
|
|
104
|
-
draft.formData =
|
|
89
|
+
draft.formData = ctx.request.files;
|
|
105
90
|
} else if (_typeIs.default.is(ctx.request.type, ['application/x-www-form-urlencoded'])) {
|
|
106
91
|
draft.formUrlencoded = ctx.request.body;
|
|
107
92
|
} else {
|
|
@@ -109,19 +94,4 @@ const getInputFromRequest = async ctx => {
|
|
|
109
94
|
}
|
|
110
95
|
|
|
111
96
|
return draft;
|
|
112
|
-
};
|
|
113
|
-
|
|
114
|
-
const resvoleFormData = ctx => {
|
|
115
|
-
const form = (0, _formidable.default)({
|
|
116
|
-
multiples: true
|
|
117
|
-
});
|
|
118
|
-
return new Promise((resolve, reject) => {
|
|
119
|
-
form.parse(ctx.req, (err, fields, files) => {
|
|
120
|
-
if (err) {
|
|
121
|
-
reject(err);
|
|
122
|
-
}
|
|
123
|
-
|
|
124
|
-
resolve(_objectSpread(_objectSpread({}, fields), files));
|
|
125
|
-
});
|
|
126
|
-
});
|
|
127
97
|
};
|
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.4.
|
|
14
|
+
"version": "1.4.6",
|
|
15
15
|
"jsnext:source": "./src/index.ts",
|
|
16
16
|
"types": "./dist/types/index.d.ts",
|
|
17
17
|
"main": "./dist/js/node/index.js",
|
|
@@ -34,31 +34,29 @@
|
|
|
34
34
|
},
|
|
35
35
|
"dependencies": {
|
|
36
36
|
"@babel/runtime": "^7.15.3",
|
|
37
|
-
"@modern-js/adapter-helpers": "^1.2.
|
|
38
|
-
"@modern-js/
|
|
39
|
-
"
|
|
37
|
+
"@modern-js/adapter-helpers": "^1.2.3",
|
|
38
|
+
"@modern-js/bff-runtime": "^1.2.2",
|
|
39
|
+
"@modern-js/bff-utils": "^1.2.3",
|
|
40
|
+
"@modern-js/utils": "^1.5.0",
|
|
40
41
|
"koa-body": "^4.2.0",
|
|
41
42
|
"koa-router": "^10.0.0",
|
|
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"
|
|
43
|
+
"type-is": "^1.6.18"
|
|
46
44
|
},
|
|
47
45
|
"devDependencies": {
|
|
48
|
-
"
|
|
49
|
-
"@
|
|
46
|
+
"@scripts/build": "0.0.0",
|
|
47
|
+
"@scripts/jest-config": "0.0.0",
|
|
50
48
|
"@types/jest": "^27.0.1",
|
|
51
49
|
"@types/koa": "^2.13.4",
|
|
52
50
|
"@types/koa-router": "^7.4.4",
|
|
53
51
|
"@types/node": "^14",
|
|
54
52
|
"@types/supertest": "^2.0.11",
|
|
55
53
|
"@types/type-is": "^1.6.3",
|
|
56
|
-
"supertest": "^6.1.6",
|
|
57
|
-
"typescript": "^4",
|
|
58
|
-
"@scripts/build": "0.0.0",
|
|
59
54
|
"jest": "^27",
|
|
60
|
-
"@
|
|
61
|
-
"@modern-js/core": "
|
|
55
|
+
"@modern-js/server-core": "1.3.1",
|
|
56
|
+
"@modern-js/core": "1.8.0",
|
|
57
|
+
"supertest": "^6.1.6",
|
|
58
|
+
"koa": "^2.13.3",
|
|
59
|
+
"typescript": "^4"
|
|
62
60
|
},
|
|
63
61
|
"modernConfig": {
|
|
64
62
|
"output": {
|
package/tests/.eslintrc.js
DELETED
package/tests/common.ts
DELETED
|
@@ -1,9 +0,0 @@
|
|
|
1
|
-
// globby needs setImmediate
|
|
2
|
-
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
|
|
3
|
-
// @ts-expect-error
|
|
4
|
-
global.setImmediate = setTimeout;
|
|
5
|
-
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
|
|
6
|
-
// @ts-expect-error
|
|
7
|
-
global.clearImmediate = clearTimeout;
|
|
8
|
-
|
|
9
|
-
export const INTROSPECTION_ROUTE_PATH = '/__introspection__';
|
|
@@ -1,12 +0,0 @@
|
|
|
1
|
-
<!DOCTYPE html>
|
|
2
|
-
<html lang="en">
|
|
3
|
-
<head>
|
|
4
|
-
<meta charset="UTF-8">
|
|
5
|
-
<meta http-equiv="X-UA-Compatible" content="IE=edge">
|
|
6
|
-
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
|
7
|
-
<title>Document</title>
|
|
8
|
-
</head>
|
|
9
|
-
<body>
|
|
10
|
-
hello koa plugin
|
|
11
|
-
</body>
|
|
12
|
-
</html>
|
|
File without changes
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export const get = () => ({ message: 'hello koa' });
|
|
@@ -1,33 +0,0 @@
|
|
|
1
|
-
import { match } from '@modern-js/bff-runtime';
|
|
2
|
-
import { useContext } from '../../../../../src/context';
|
|
3
|
-
|
|
4
|
-
export const get = ({ query }: { query: Record<string, unknown> }) => ({
|
|
5
|
-
query,
|
|
6
|
-
});
|
|
7
|
-
|
|
8
|
-
export const post = ({ data }: { data: Record<string, unknown> }) => {
|
|
9
|
-
// eslint-disable-next-line react-hooks/rules-of-hooks
|
|
10
|
-
const ctx = useContext();
|
|
11
|
-
const { cookies } = ctx;
|
|
12
|
-
return {
|
|
13
|
-
query: ctx.query,
|
|
14
|
-
data,
|
|
15
|
-
cookies: {
|
|
16
|
-
id: cookies.get('id'),
|
|
17
|
-
name: cookies.get('name'),
|
|
18
|
-
},
|
|
19
|
-
};
|
|
20
|
-
};
|
|
21
|
-
|
|
22
|
-
export const patch = match(
|
|
23
|
-
{
|
|
24
|
-
request: {
|
|
25
|
-
data: {
|
|
26
|
-
id: Number,
|
|
27
|
-
name: String,
|
|
28
|
-
},
|
|
29
|
-
},
|
|
30
|
-
response: { id: Number },
|
|
31
|
-
},
|
|
32
|
-
({ data: { id } }) => ({ id }),
|
|
33
|
-
);
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
// used by the test, empty file
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export const get = () => ({ message: 'hello koa' });
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export const post = (id: string) => ({ id });
|
|
@@ -1,33 +0,0 @@
|
|
|
1
|
-
import { match } from '@modern-js/bff-runtime';
|
|
2
|
-
import { useContext } from '../../../../../../src/context';
|
|
3
|
-
|
|
4
|
-
export const get = ({ query }: { query: Record<string, unknown> }) => ({
|
|
5
|
-
query,
|
|
6
|
-
});
|
|
7
|
-
|
|
8
|
-
export const post = ({ data }: { data: Record<string, unknown> }) => {
|
|
9
|
-
// eslint-disable-next-line react-hooks/rules-of-hooks
|
|
10
|
-
const ctx = useContext();
|
|
11
|
-
const { cookies } = ctx;
|
|
12
|
-
return {
|
|
13
|
-
query: ctx.query,
|
|
14
|
-
data,
|
|
15
|
-
cookies: {
|
|
16
|
-
id: cookies.get('id'),
|
|
17
|
-
name: cookies.get('name'),
|
|
18
|
-
},
|
|
19
|
-
};
|
|
20
|
-
};
|
|
21
|
-
|
|
22
|
-
export const patch = match(
|
|
23
|
-
{
|
|
24
|
-
request: {
|
|
25
|
-
data: {
|
|
26
|
-
id: Number,
|
|
27
|
-
name: String,
|
|
28
|
-
},
|
|
29
|
-
},
|
|
30
|
-
response: { id: Number },
|
|
31
|
-
},
|
|
32
|
-
({ data: { id } }) => ({ id }),
|
|
33
|
-
);
|
|
@@ -1,53 +0,0 @@
|
|
|
1
|
-
import * as path from 'path';
|
|
2
|
-
import request from 'supertest';
|
|
3
|
-
import { serverManager } from '@modern-js/server-core';
|
|
4
|
-
import plugin from '../src/plugin';
|
|
5
|
-
import { APIPlugin } from './helpers';
|
|
6
|
-
import './common';
|
|
7
|
-
|
|
8
|
-
const pwd = path.join(__dirname, './fixtures/function-mode');
|
|
9
|
-
|
|
10
|
-
describe('function-mode', () => {
|
|
11
|
-
const id = '666';
|
|
12
|
-
const name = 'foo';
|
|
13
|
-
const foo = { id, name };
|
|
14
|
-
let apiHandler: any;
|
|
15
|
-
|
|
16
|
-
beforeAll(async () => {
|
|
17
|
-
const runner = await serverManager
|
|
18
|
-
.clone()
|
|
19
|
-
.usePlugin(APIPlugin, plugin)
|
|
20
|
-
.init();
|
|
21
|
-
apiHandler = await runner.prepareApiServer({
|
|
22
|
-
pwd,
|
|
23
|
-
mode: 'function',
|
|
24
|
-
});
|
|
25
|
-
});
|
|
26
|
-
|
|
27
|
-
test('should works with body', async () => {
|
|
28
|
-
const res = await request(apiHandler).post('/nest/user').send(foo);
|
|
29
|
-
expect(res.status).toBe(200);
|
|
30
|
-
expect(res.body.data).toEqual(foo);
|
|
31
|
-
});
|
|
32
|
-
|
|
33
|
-
test('should works with schema', async () => {
|
|
34
|
-
const res = await request(apiHandler).patch('/nest/user').send({
|
|
35
|
-
id: 777,
|
|
36
|
-
name: 'xxx',
|
|
37
|
-
});
|
|
38
|
-
expect(res.status).toBe(200);
|
|
39
|
-
expect(res.body.id).toBe(777);
|
|
40
|
-
|
|
41
|
-
const res2 = await request(apiHandler).patch('/nest/user').send({
|
|
42
|
-
id: 'aaa',
|
|
43
|
-
name: 'xxx',
|
|
44
|
-
});
|
|
45
|
-
expect(res2.status).toBe(400);
|
|
46
|
-
|
|
47
|
-
const res3 = await request(apiHandler).patch('/nest/user').send({
|
|
48
|
-
id: '777',
|
|
49
|
-
name: 'xxx',
|
|
50
|
-
});
|
|
51
|
-
expect(res3.status).toBe(500);
|
|
52
|
-
});
|
|
53
|
-
});
|
package/tests/helpers.ts
DELETED
|
@@ -1,14 +0,0 @@
|
|
|
1
|
-
import path from 'path';
|
|
2
|
-
import { createPlugin } from '@modern-js/server-core';
|
|
3
|
-
import { injectAPIHandlerInfos, API_DIR } from '@modern-js/bff-utils';
|
|
4
|
-
|
|
5
|
-
export const APIPlugin = createPlugin(() => ({
|
|
6
|
-
prepareApiServer(props, next) {
|
|
7
|
-
const { pwd, prefix } = props;
|
|
8
|
-
const apiDir = path.resolve(pwd, API_DIR);
|
|
9
|
-
|
|
10
|
-
injectAPIHandlerInfos(apiDir, prefix);
|
|
11
|
-
|
|
12
|
-
return next(props);
|
|
13
|
-
},
|
|
14
|
-
}));
|
package/tests/lambdaMode.test.ts
DELETED
|
@@ -1,301 +0,0 @@
|
|
|
1
|
-
import * as path from 'path';
|
|
2
|
-
import request from 'supertest';
|
|
3
|
-
import Koa from 'koa';
|
|
4
|
-
import { serverManager } from '@modern-js/server-core';
|
|
5
|
-
import Router from 'koa-router';
|
|
6
|
-
import koaBody from 'koa-body';
|
|
7
|
-
import plugin from '../src/plugin';
|
|
8
|
-
import { APIPlugin } from './helpers';
|
|
9
|
-
import { INTROSPECTION_ROUTE_PATH } from './common';
|
|
10
|
-
|
|
11
|
-
const pwd = path.join(__dirname, './fixtures/lambda-mode');
|
|
12
|
-
const API_DIR = './api';
|
|
13
|
-
const MockKoa = Koa;
|
|
14
|
-
const MockRouter = Router;
|
|
15
|
-
const MockKoaBody = koaBody;
|
|
16
|
-
|
|
17
|
-
describe('lambda-mode', () => {
|
|
18
|
-
const id = '666';
|
|
19
|
-
const name = 'foo';
|
|
20
|
-
const foo = { id, name };
|
|
21
|
-
let apiHandler: any;
|
|
22
|
-
|
|
23
|
-
beforeAll(async () => {
|
|
24
|
-
const runner = await serverManager
|
|
25
|
-
.clone()
|
|
26
|
-
.usePlugin(APIPlugin, plugin)
|
|
27
|
-
.init();
|
|
28
|
-
apiHandler = await runner.prepareApiServer({
|
|
29
|
-
pwd,
|
|
30
|
-
mode: 'framework',
|
|
31
|
-
prefix: '/api',
|
|
32
|
-
});
|
|
33
|
-
});
|
|
34
|
-
|
|
35
|
-
test('should works', async () => {
|
|
36
|
-
const res = await request(apiHandler).get('/api/hello');
|
|
37
|
-
expect(res.status).toBe(200);
|
|
38
|
-
});
|
|
39
|
-
|
|
40
|
-
test('should works with query', async () => {
|
|
41
|
-
const res = await request(apiHandler).get(`/api/nest/user?id=${id}`);
|
|
42
|
-
expect(res.status).toBe(200);
|
|
43
|
-
expect(res.body.query.id).toBe(id);
|
|
44
|
-
});
|
|
45
|
-
|
|
46
|
-
test('should works with body', async () => {
|
|
47
|
-
const res = await request(apiHandler).post('/api/nest/user').send(foo);
|
|
48
|
-
expect(res.status).toBe(200);
|
|
49
|
-
expect(res.body.data).toEqual(foo);
|
|
50
|
-
});
|
|
51
|
-
|
|
52
|
-
test('should works with dynamic route ', async () => {
|
|
53
|
-
const res = await request(apiHandler).post(`/api/nest/${id}`);
|
|
54
|
-
expect(res.status).toBe(200);
|
|
55
|
-
expect(res.body).toEqual({ id });
|
|
56
|
-
});
|
|
57
|
-
|
|
58
|
-
test('should works with context', async () => {
|
|
59
|
-
const res = await request(apiHandler)
|
|
60
|
-
.post(`/api/nest/user?id=${id}`)
|
|
61
|
-
.send(foo);
|
|
62
|
-
expect(res.status).toBe(200);
|
|
63
|
-
expect(res.body.data).toEqual(foo);
|
|
64
|
-
expect(res.body.query.id).toBe(id);
|
|
65
|
-
});
|
|
66
|
-
|
|
67
|
-
test('should support cookies', async () => {
|
|
68
|
-
const res = await request(apiHandler)
|
|
69
|
-
.post(`/api/nest/user?id=${id}`)
|
|
70
|
-
.set('Cookie', [`id=${id};name=${name}`]);
|
|
71
|
-
expect(res.status).toBe(200);
|
|
72
|
-
expect(res.body.cookies.id).toBe(id);
|
|
73
|
-
expect(res.body.cookies.name).toBe(name);
|
|
74
|
-
});
|
|
75
|
-
|
|
76
|
-
test('should works with schema', async () => {
|
|
77
|
-
const res = await request(apiHandler).patch('/api/nest/user').send({
|
|
78
|
-
id: 777,
|
|
79
|
-
name: 'xxx',
|
|
80
|
-
});
|
|
81
|
-
expect(res.status).toBe(200);
|
|
82
|
-
|
|
83
|
-
const res2 = await request(apiHandler).patch('/api/nest/user').send({
|
|
84
|
-
id: 'aaa',
|
|
85
|
-
name: 'xxx',
|
|
86
|
-
});
|
|
87
|
-
expect(res2.status).toBe(400);
|
|
88
|
-
|
|
89
|
-
const res3 = await request(apiHandler).patch('/api/nest/user').send({
|
|
90
|
-
id: '777',
|
|
91
|
-
name: 'xxx',
|
|
92
|
-
});
|
|
93
|
-
expect(res3.status).toBe(500);
|
|
94
|
-
});
|
|
95
|
-
|
|
96
|
-
test('introspection', async () => {
|
|
97
|
-
const res = await request(apiHandler).get(
|
|
98
|
-
`/api${INTROSPECTION_ROUTE_PATH}`,
|
|
99
|
-
);
|
|
100
|
-
expect(res.status).toBe(200);
|
|
101
|
-
expect(res.body.protocol).toBe('Farrow-API');
|
|
102
|
-
});
|
|
103
|
-
});
|
|
104
|
-
|
|
105
|
-
describe('add middlewares', () => {
|
|
106
|
-
let runner: any;
|
|
107
|
-
|
|
108
|
-
beforeAll(async () => {
|
|
109
|
-
runner = await serverManager.clone().usePlugin(APIPlugin, plugin).init();
|
|
110
|
-
});
|
|
111
|
-
|
|
112
|
-
test('should works', async () => {
|
|
113
|
-
const foo = 'foo';
|
|
114
|
-
const fakeMiddleware = jest.fn(async (ctx: any, next: any) => {
|
|
115
|
-
await next();
|
|
116
|
-
ctx.body = foo;
|
|
117
|
-
});
|
|
118
|
-
const fakeMiddleware2 = jest.fn(async (ctx: any, next: any) => {
|
|
119
|
-
await next();
|
|
120
|
-
});
|
|
121
|
-
|
|
122
|
-
// eslint-disable-next-line @typescript-eslint/naming-convention
|
|
123
|
-
const [mock_fakeMiddleware, mock_fakeMiddleware2] = [
|
|
124
|
-
fakeMiddleware,
|
|
125
|
-
fakeMiddleware2,
|
|
126
|
-
];
|
|
127
|
-
const apiHandler = await runner.prepareApiServer({
|
|
128
|
-
pwd,
|
|
129
|
-
mode: 'framework',
|
|
130
|
-
config: {
|
|
131
|
-
middleware: [
|
|
132
|
-
mock_fakeMiddleware,
|
|
133
|
-
mock_fakeMiddleware2,
|
|
134
|
-
mock_fakeMiddleware2,
|
|
135
|
-
],
|
|
136
|
-
},
|
|
137
|
-
});
|
|
138
|
-
|
|
139
|
-
const res = await request(apiHandler).get('/user');
|
|
140
|
-
expect(fakeMiddleware.mock.calls.length).toBe(1);
|
|
141
|
-
expect(fakeMiddleware2.mock.calls.length).toBe(2);
|
|
142
|
-
expect(res.status).toBe(200);
|
|
143
|
-
expect(res.text).toEqual(foo);
|
|
144
|
-
});
|
|
145
|
-
});
|
|
146
|
-
|
|
147
|
-
describe('support use koaBody in app.ts', () => {
|
|
148
|
-
const id = '666';
|
|
149
|
-
const name = 'modernjs';
|
|
150
|
-
const foo = { id, name };
|
|
151
|
-
|
|
152
|
-
let runner: any;
|
|
153
|
-
|
|
154
|
-
beforeAll(async () => {
|
|
155
|
-
runner = await serverManager.clone().usePlugin(APIPlugin, plugin).init();
|
|
156
|
-
});
|
|
157
|
-
|
|
158
|
-
test('support use koaBody', async () => {
|
|
159
|
-
const mock_foo = foo;
|
|
160
|
-
jest.mock(
|
|
161
|
-
path.join(pwd, API_DIR, 'app.ts'),
|
|
162
|
-
() => {
|
|
163
|
-
const app = new MockKoa();
|
|
164
|
-
// test app takes effect
|
|
165
|
-
app.use(async (ctx, next) => {
|
|
166
|
-
ctx.request.query = mock_foo;
|
|
167
|
-
await next();
|
|
168
|
-
});
|
|
169
|
-
|
|
170
|
-
app.use(MockKoaBody());
|
|
171
|
-
|
|
172
|
-
return {
|
|
173
|
-
__esModule: true,
|
|
174
|
-
default: app,
|
|
175
|
-
};
|
|
176
|
-
},
|
|
177
|
-
{ virtual: true },
|
|
178
|
-
);
|
|
179
|
-
|
|
180
|
-
const apiHandler = await runner.prepareApiServer({
|
|
181
|
-
pwd,
|
|
182
|
-
mode: 'framework',
|
|
183
|
-
});
|
|
184
|
-
const res = await request(apiHandler).post('/nest/user').send(foo);
|
|
185
|
-
expect(res.status).toBe(200);
|
|
186
|
-
expect(res.body.query).toEqual(foo);
|
|
187
|
-
expect(res.body.data).toEqual(foo);
|
|
188
|
-
});
|
|
189
|
-
});
|
|
190
|
-
|
|
191
|
-
describe('support app.ts in lambda mode', () => {
|
|
192
|
-
const name = 'modernjs';
|
|
193
|
-
|
|
194
|
-
let runner: any;
|
|
195
|
-
|
|
196
|
-
beforeAll(async () => {
|
|
197
|
-
runner = await serverManager.clone().usePlugin(APIPlugin, plugin).init();
|
|
198
|
-
});
|
|
199
|
-
|
|
200
|
-
beforeEach(() => {
|
|
201
|
-
jest.resetModules();
|
|
202
|
-
});
|
|
203
|
-
|
|
204
|
-
test('support es module', async () => {
|
|
205
|
-
jest.mock(
|
|
206
|
-
path.join(pwd, API_DIR, 'app.ts'),
|
|
207
|
-
() => {
|
|
208
|
-
const app = new MockKoa();
|
|
209
|
-
return {
|
|
210
|
-
__esModule: true,
|
|
211
|
-
default: app,
|
|
212
|
-
};
|
|
213
|
-
},
|
|
214
|
-
{ virtual: true },
|
|
215
|
-
);
|
|
216
|
-
|
|
217
|
-
const apiHandler = await runner.prepareApiServer({
|
|
218
|
-
pwd,
|
|
219
|
-
mode: 'framework',
|
|
220
|
-
});
|
|
221
|
-
|
|
222
|
-
const res = await request(apiHandler).get(`/nest/user?name=${name}`);
|
|
223
|
-
expect(res.status).toBe(200);
|
|
224
|
-
expect(res.body.query.name).toBe(name);
|
|
225
|
-
});
|
|
226
|
-
|
|
227
|
-
test('support commonjs module', async () => {
|
|
228
|
-
jest.mock(
|
|
229
|
-
path.join(pwd, API_DIR, 'app.ts'),
|
|
230
|
-
() => {
|
|
231
|
-
const app = new MockKoa();
|
|
232
|
-
return app;
|
|
233
|
-
},
|
|
234
|
-
{ virtual: true },
|
|
235
|
-
);
|
|
236
|
-
|
|
237
|
-
const apiHandler = await runner.prepareApiServer({
|
|
238
|
-
pwd,
|
|
239
|
-
mode: 'framework',
|
|
240
|
-
});
|
|
241
|
-
|
|
242
|
-
const res = await request(apiHandler).get(`/nest/user?name=${name}`);
|
|
243
|
-
expect(res.status).toBe(200);
|
|
244
|
-
expect(res.body.query.name).toBe(name);
|
|
245
|
-
});
|
|
246
|
-
|
|
247
|
-
test('support use middleware', async () => {
|
|
248
|
-
const mock_name = name;
|
|
249
|
-
jest.mock(
|
|
250
|
-
path.join(pwd, API_DIR, 'app.ts'),
|
|
251
|
-
() => {
|
|
252
|
-
const app = new MockKoa();
|
|
253
|
-
app.use(async (ctx, next) => {
|
|
254
|
-
ctx.query.name = mock_name;
|
|
255
|
-
await next();
|
|
256
|
-
});
|
|
257
|
-
return app;
|
|
258
|
-
},
|
|
259
|
-
{ virtual: true },
|
|
260
|
-
);
|
|
261
|
-
|
|
262
|
-
const apiHandler = await runner.prepareApiServer({
|
|
263
|
-
pwd,
|
|
264
|
-
mode: 'framework',
|
|
265
|
-
});
|
|
266
|
-
|
|
267
|
-
const res = await request(apiHandler).get(`/nest/user`);
|
|
268
|
-
expect(res.status).toBe(200);
|
|
269
|
-
expect(res.body.query.name).toBe(name);
|
|
270
|
-
});
|
|
271
|
-
|
|
272
|
-
test('support use router', async () => {
|
|
273
|
-
jest.mock(
|
|
274
|
-
path.join(pwd, API_DIR, 'app.ts'),
|
|
275
|
-
() => {
|
|
276
|
-
const app = new MockKoa();
|
|
277
|
-
const router = new MockRouter();
|
|
278
|
-
router.get('/hello', ctx => {
|
|
279
|
-
ctx.body = `foo`;
|
|
280
|
-
});
|
|
281
|
-
|
|
282
|
-
app.use(router.routes());
|
|
283
|
-
return app;
|
|
284
|
-
},
|
|
285
|
-
{ virtual: true },
|
|
286
|
-
);
|
|
287
|
-
|
|
288
|
-
const apiHandler = await runner.prepareApiServer({
|
|
289
|
-
pwd,
|
|
290
|
-
mode: 'framework',
|
|
291
|
-
});
|
|
292
|
-
|
|
293
|
-
const res1 = await request(apiHandler).get(`/hello`);
|
|
294
|
-
expect(res1.status).toBe(200);
|
|
295
|
-
expect(res1.text).toBe('foo');
|
|
296
|
-
|
|
297
|
-
const res2 = await request(apiHandler).get(`/nest/user?name=${name}`);
|
|
298
|
-
expect(res2.status).toBe(200);
|
|
299
|
-
expect(res2.body.query.name).toBe(name);
|
|
300
|
-
});
|
|
301
|
-
});
|
package/tests/tsconfig.json
DELETED
package/tests/webServer.test.ts
DELETED
|
@@ -1,81 +0,0 @@
|
|
|
1
|
-
import * as path from 'path';
|
|
2
|
-
import fs from 'fs';
|
|
3
|
-
import request from 'supertest';
|
|
4
|
-
import { serverManager } from '@modern-js/server-core';
|
|
5
|
-
import plugin from '../src/plugin';
|
|
6
|
-
import { APIPlugin } from './helpers';
|
|
7
|
-
import './common';
|
|
8
|
-
|
|
9
|
-
const pwd = path.join(__dirname, './fixtures/function-mode');
|
|
10
|
-
|
|
11
|
-
describe('webServer', () => {
|
|
12
|
-
const id = '666';
|
|
13
|
-
const name = 'foo';
|
|
14
|
-
const foo = { id, name };
|
|
15
|
-
let webHandler: any;
|
|
16
|
-
let runner: any;
|
|
17
|
-
|
|
18
|
-
beforeAll(async () => {
|
|
19
|
-
runner = await serverManager.clone().usePlugin(APIPlugin, plugin).init();
|
|
20
|
-
});
|
|
21
|
-
|
|
22
|
-
test('support json', async () => {
|
|
23
|
-
const middleware = (ctx: any) => {
|
|
24
|
-
// ctx.req.url = `${ctx.req.url}?id=${id}`;
|
|
25
|
-
ctx.body = foo;
|
|
26
|
-
};
|
|
27
|
-
|
|
28
|
-
webHandler = await runner.prepareWebServer({
|
|
29
|
-
pwd,
|
|
30
|
-
config: { middleware: [middleware] },
|
|
31
|
-
});
|
|
32
|
-
|
|
33
|
-
const res = await request(webHandler).get('/');
|
|
34
|
-
expect(res.status).toBe(200);
|
|
35
|
-
expect(res.get('content-type')).toBe('application/json; charset=utf-8');
|
|
36
|
-
expect(res.body).toEqual(foo);
|
|
37
|
-
});
|
|
38
|
-
|
|
39
|
-
test('support stream', async () => {
|
|
40
|
-
const middleware = (ctx: any) => {
|
|
41
|
-
ctx.response.set('content-type', 'text/html');
|
|
42
|
-
ctx.body = fs.createReadStream(
|
|
43
|
-
path.resolve(__dirname, './fixtures/assets/index.html'),
|
|
44
|
-
);
|
|
45
|
-
};
|
|
46
|
-
|
|
47
|
-
webHandler = await runner.prepareWebServer({
|
|
48
|
-
pwd,
|
|
49
|
-
config: { middleware: [middleware] },
|
|
50
|
-
});
|
|
51
|
-
|
|
52
|
-
const res = await request(webHandler).get('/');
|
|
53
|
-
expect(res.status).toBe(200);
|
|
54
|
-
expect(res.get('content-type')).toBe('text/html');
|
|
55
|
-
expect(res.text).toContain(`hello koa plugin`);
|
|
56
|
-
});
|
|
57
|
-
|
|
58
|
-
test('should support Multiple middleware', async () => {
|
|
59
|
-
const middleware1 = async (ctx: any, next: any) => {
|
|
60
|
-
await next();
|
|
61
|
-
};
|
|
62
|
-
|
|
63
|
-
const middleware2 = async (ctx: any, next: any) => {
|
|
64
|
-
ctx.response.set('content-type', 'text/html');
|
|
65
|
-
ctx.body = fs.createReadStream(
|
|
66
|
-
path.resolve(__dirname, './fixtures/assets/index.html'),
|
|
67
|
-
);
|
|
68
|
-
await next();
|
|
69
|
-
};
|
|
70
|
-
|
|
71
|
-
webHandler = await runner.prepareWebServer({
|
|
72
|
-
pwd,
|
|
73
|
-
config: { middleware: [middleware1, middleware2] },
|
|
74
|
-
});
|
|
75
|
-
|
|
76
|
-
const res = await request(webHandler).get('/');
|
|
77
|
-
expect(res.status).toBe(200);
|
|
78
|
-
expect(res.get('content-type')).toBe('text/html');
|
|
79
|
-
expect(res.text).toContain(`hello koa plugin`);
|
|
80
|
-
});
|
|
81
|
-
});
|