@modern-js/plugin-koa 2.0.0-beta.0 → 2.0.0-beta.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 +36 -0
- package/dist/js/modern/cli/index.js +0 -3
- package/dist/js/modern/plugin.js +0 -21
- package/dist/js/modern/registerRoutes.js +0 -2
- package/dist/js/modern/utils.js +0 -20
- package/dist/js/node/cli/index.js +0 -10
- package/dist/js/node/context.js +0 -2
- package/dist/js/node/index.js +0 -5
- package/dist/js/node/plugin.js +0 -32
- package/dist/js/node/registerRoutes.js +0 -3
- package/dist/js/node/runtime.js +0 -5
- package/dist/js/node/utils.js +0 -27
- package/dist/types/cli/index.d.ts +0 -2
- package/dist/types/plugin.d.ts +0 -2
- package/package.json +11 -11
package/CHANGELOG.md
CHANGED
|
@@ -1,5 +1,41 @@
|
|
|
1
1
|
# @modern-js/plugin-koa
|
|
2
2
|
|
|
3
|
+
## 2.0.0-beta.1
|
|
4
|
+
|
|
5
|
+
### Major Changes
|
|
6
|
+
|
|
7
|
+
- dda38c9: chore: v2
|
|
8
|
+
|
|
9
|
+
### Patch Changes
|
|
10
|
+
|
|
11
|
+
- 8d24bed25b: fix: compat Hook API in /server namespace
|
|
12
|
+
fix: 在 @modern-js/runtime/server 命名空间下兼容 Hook API
|
|
13
|
+
- 3bbea92b2a: feat: support Hook、Middleware new API
|
|
14
|
+
feat: 支持 Hook、Middleware 的新 API
|
|
15
|
+
- Updated dependencies [2344eb2]
|
|
16
|
+
- Updated dependencies [a2509bfbdb]
|
|
17
|
+
- Updated dependencies [92f0ead]
|
|
18
|
+
- Updated dependencies [edd1cfb1af]
|
|
19
|
+
- Updated dependencies [cc971eabfc]
|
|
20
|
+
- Updated dependencies [5b9049f]
|
|
21
|
+
- Updated dependencies [6bda14ed71]
|
|
22
|
+
- Updated dependencies [92004d1]
|
|
23
|
+
- Updated dependencies [b8bbe036c7]
|
|
24
|
+
- Updated dependencies [40ed587]
|
|
25
|
+
- Updated dependencies [87c1ff8]
|
|
26
|
+
- Updated dependencies [d5a31df781]
|
|
27
|
+
- Updated dependencies [dda38c9]
|
|
28
|
+
- Updated dependencies [102d32e4ba]
|
|
29
|
+
- Updated dependencies [8b8e1bb571]
|
|
30
|
+
- Updated dependencies [3bbea92b2a]
|
|
31
|
+
- Updated dependencies [abf3421]
|
|
32
|
+
- Updated dependencies [543be9558e]
|
|
33
|
+
- Updated dependencies [14b712d]
|
|
34
|
+
- @modern-js/bff-core@2.0.0-beta.1
|
|
35
|
+
- @modern-js/utils@2.0.0-beta.1
|
|
36
|
+
- @modern-js/types@2.0.0-beta.1
|
|
37
|
+
- @modern-js/bff-runtime@2.0.0-beta.1
|
|
38
|
+
|
|
3
39
|
## 2.0.0-beta.0
|
|
4
40
|
|
|
5
41
|
### Major Changes
|
|
@@ -18,7 +18,6 @@ export default (() => ({
|
|
|
18
18
|
bffExportsUtils = createRuntimeExportsUtils(appContext.internalDirectory, 'server');
|
|
19
19
|
const serverRuntimePath = bffExportsUtils.getPath();
|
|
20
20
|
const relativeRuntimePath = getRelativeRuntimePath(appDirectory, serverRuntimePath);
|
|
21
|
-
|
|
22
21
|
if (process.env.NODE_ENV === 'production') {
|
|
23
22
|
return {
|
|
24
23
|
source: {
|
|
@@ -39,7 +38,6 @@ export default (() => ({
|
|
|
39
38
|
};
|
|
40
39
|
}
|
|
41
40
|
},
|
|
42
|
-
|
|
43
41
|
addRuntimeExports(input) {
|
|
44
42
|
const currentFile = bffExportsUtils.getPath();
|
|
45
43
|
const relativeRuntimeModulePath = path.relative(path.dirname(currentFile), runtimeModulePath);
|
|
@@ -53,7 +51,6 @@ export default (() => ({
|
|
|
53
51
|
`);
|
|
54
52
|
return input;
|
|
55
53
|
}
|
|
56
|
-
|
|
57
54
|
};
|
|
58
55
|
}
|
|
59
56
|
}));
|
package/dist/js/modern/plugin.js
CHANGED
|
@@ -5,11 +5,9 @@ import koaBody from 'koa-body';
|
|
|
5
5
|
import { fs, compatRequire } from '@modern-js/utils';
|
|
6
6
|
import { run } from "./context";
|
|
7
7
|
import registerRoutes from "./registerRoutes";
|
|
8
|
-
|
|
9
8
|
const findAppModule = async apiDir => {
|
|
10
9
|
const exts = ['.ts', '.js'];
|
|
11
10
|
const paths = exts.map(ext => path.join(apiDir, `app${ext}`));
|
|
12
|
-
|
|
13
11
|
for (const filename of paths) {
|
|
14
12
|
if (await fs.pathExists(filename)) {
|
|
15
13
|
// 每次获取 app.ts 的时候,避免使用缓存的 app.ts
|
|
@@ -17,17 +15,14 @@ const findAppModule = async apiDir => {
|
|
|
17
15
|
return compatRequire(filename);
|
|
18
16
|
}
|
|
19
17
|
}
|
|
20
|
-
|
|
21
18
|
return null;
|
|
22
19
|
};
|
|
23
|
-
|
|
24
20
|
const initMiddlewares = (middleware, app) => {
|
|
25
21
|
middleware.forEach(middlewareItem => {
|
|
26
22
|
const middlewareFunc = typeof middlewareItem === 'string' ? compatRequire(middlewareItem) : middlewareItem;
|
|
27
23
|
app.use(middlewareFunc);
|
|
28
24
|
});
|
|
29
25
|
};
|
|
30
|
-
|
|
31
26
|
export default (() => ({
|
|
32
27
|
name: '@modern-js/plugin-koa',
|
|
33
28
|
pre: ['@modern-js/plugin-bff'],
|
|
@@ -43,24 +38,20 @@ export default (() => ({
|
|
|
43
38
|
const appContext = api.useAppContext();
|
|
44
39
|
const apiHandlerInfos = appContext.apiHandlerInfos;
|
|
45
40
|
const mode = appContext.apiMode;
|
|
46
|
-
|
|
47
41
|
if (mode === 'framework') {
|
|
48
42
|
app = await findAppModule(apiDir);
|
|
49
|
-
|
|
50
43
|
if (!(app instanceof Koa)) {
|
|
51
44
|
app = new Koa();
|
|
52
45
|
app.use(koaBody({
|
|
53
46
|
multipart: true
|
|
54
47
|
}));
|
|
55
48
|
}
|
|
56
|
-
|
|
57
49
|
if (config) {
|
|
58
50
|
const {
|
|
59
51
|
middleware
|
|
60
52
|
} = config;
|
|
61
53
|
initMiddlewares(middleware, app);
|
|
62
54
|
}
|
|
63
|
-
|
|
64
55
|
app.use(run);
|
|
65
56
|
registerRoutes(router, apiHandlerInfos);
|
|
66
57
|
} else if (mode === 'function') {
|
|
@@ -68,59 +59,48 @@ export default (() => ({
|
|
|
68
59
|
app.use(koaBody({
|
|
69
60
|
multipart: true
|
|
70
61
|
}));
|
|
71
|
-
|
|
72
62
|
if (config) {
|
|
73
63
|
const {
|
|
74
64
|
middleware
|
|
75
65
|
} = config;
|
|
76
66
|
initMiddlewares(middleware, app);
|
|
77
67
|
}
|
|
78
|
-
|
|
79
68
|
app.use(run);
|
|
80
69
|
registerRoutes(router, apiHandlerInfos);
|
|
81
70
|
} else {
|
|
82
71
|
throw new Error(`mode must be function or framework`);
|
|
83
72
|
}
|
|
84
|
-
|
|
85
73
|
app.use(router.routes());
|
|
86
74
|
return (req, res) => {
|
|
87
75
|
return Promise.resolve(app.callback()(req, res));
|
|
88
76
|
};
|
|
89
77
|
},
|
|
90
|
-
|
|
91
78
|
prepareWebServer({
|
|
92
79
|
config
|
|
93
80
|
}, next) {
|
|
94
81
|
var _userConfig$server;
|
|
95
|
-
|
|
96
82
|
const userConfig = api.useConfigContext();
|
|
97
|
-
|
|
98
83
|
if (userConfig !== null && userConfig !== void 0 && (_userConfig$server = userConfig.server) !== null && _userConfig$server !== void 0 && _userConfig$server.disableFrameworkExt) {
|
|
99
84
|
return next();
|
|
100
85
|
}
|
|
101
|
-
|
|
102
86
|
const app = new Koa();
|
|
103
87
|
app.use(async (ctx, next) => {
|
|
104
88
|
await next();
|
|
105
|
-
|
|
106
89
|
if (!ctx.body) {
|
|
107
90
|
// restore statusCode
|
|
108
91
|
if (ctx.res.statusCode === 404 && !ctx.response._explicitStatus) {
|
|
109
92
|
ctx.res.statusCode = 200;
|
|
110
93
|
}
|
|
111
|
-
|
|
112
94
|
ctx.respond = false;
|
|
113
95
|
}
|
|
114
96
|
});
|
|
115
97
|
app.use(koaBody());
|
|
116
|
-
|
|
117
98
|
if (config) {
|
|
118
99
|
const {
|
|
119
100
|
middleware
|
|
120
101
|
} = config;
|
|
121
102
|
initMiddlewares(middleware, app);
|
|
122
103
|
}
|
|
123
|
-
|
|
124
104
|
return ctx => {
|
|
125
105
|
const {
|
|
126
106
|
source: {
|
|
@@ -136,6 +116,5 @@ export default (() => ({
|
|
|
136
116
|
return Promise.resolve(app.callback()(req, res));
|
|
137
117
|
};
|
|
138
118
|
}
|
|
139
|
-
|
|
140
119
|
})
|
|
141
120
|
}));
|
|
@@ -1,5 +1,4 @@
|
|
|
1
1
|
import { createRouteHandler } from "./utils";
|
|
2
|
-
|
|
3
2
|
const registerRoutes = (router, handlerInfos) => {
|
|
4
3
|
handlerInfos.forEach(({
|
|
5
4
|
routePath,
|
|
@@ -11,5 +10,4 @@ const registerRoutes = (router, handlerInfos) => {
|
|
|
11
10
|
router[method](routePath, routeHandler);
|
|
12
11
|
});
|
|
13
12
|
};
|
|
14
|
-
|
|
15
13
|
export default registerRoutes;
|
package/dist/js/modern/utils.js
CHANGED
|
@@ -1,15 +1,12 @@
|
|
|
1
1
|
import { httpMethods, isWithMetaHandler, ResponseMetaType, HttpMetadata } from '@modern-js/bff-core';
|
|
2
2
|
import { isSchemaHandler } from '@modern-js/bff-runtime';
|
|
3
3
|
import typeIs from 'type-is';
|
|
4
|
-
|
|
5
4
|
const handleResponseMeta = (ctx, handler) => {
|
|
6
5
|
const responseMeta = Reflect.getMetadata(HttpMetadata.Response, handler);
|
|
7
|
-
|
|
8
6
|
if (Array.isArray(responseMeta)) {
|
|
9
7
|
for (const meta of responseMeta) {
|
|
10
8
|
const metaType = meta.type;
|
|
11
9
|
const metaValue = meta.value;
|
|
12
|
-
|
|
13
10
|
switch (metaType) {
|
|
14
11
|
case ResponseMetaType.Headers:
|
|
15
12
|
// eslint-disable-next-line @typescript-eslint/ban-types
|
|
@@ -18,40 +15,31 @@ const handleResponseMeta = (ctx, handler) => {
|
|
|
18
15
|
ctx.append(key, value);
|
|
19
16
|
}
|
|
20
17
|
}
|
|
21
|
-
|
|
22
18
|
break;
|
|
23
|
-
|
|
24
19
|
case ResponseMetaType.Redirect:
|
|
25
20
|
if (typeof metaValue === 'string') {
|
|
26
21
|
ctx.redirect(metaValue);
|
|
27
22
|
}
|
|
28
|
-
|
|
29
23
|
break;
|
|
30
|
-
|
|
31
24
|
case ResponseMetaType.StatusCode:
|
|
32
25
|
if (typeof metaValue === 'number') {
|
|
33
26
|
ctx.status = metaValue;
|
|
34
27
|
}
|
|
35
|
-
|
|
36
28
|
break;
|
|
37
|
-
|
|
38
29
|
default:
|
|
39
30
|
break;
|
|
40
31
|
}
|
|
41
32
|
}
|
|
42
33
|
}
|
|
43
34
|
};
|
|
44
|
-
|
|
45
35
|
export const createRouteHandler = handler => {
|
|
46
36
|
// eslint-disable-next-line consistent-return
|
|
47
37
|
const apiHandler = async ctx => {
|
|
48
38
|
const input = await getInputFromRequest(ctx);
|
|
49
|
-
|
|
50
39
|
if (isWithMetaHandler(handler)) {
|
|
51
40
|
try {
|
|
52
41
|
handleResponseMeta(ctx, handler);
|
|
53
42
|
const body = await handler(input);
|
|
54
|
-
|
|
55
43
|
if (typeof body !== 'undefined') {
|
|
56
44
|
ctx.body = body;
|
|
57
45
|
}
|
|
@@ -62,7 +50,6 @@ export const createRouteHandler = handler => {
|
|
|
62
50
|
} else {
|
|
63
51
|
ctx.status = 500;
|
|
64
52
|
}
|
|
65
|
-
|
|
66
53
|
return ctx.body = {
|
|
67
54
|
code: error.code,
|
|
68
55
|
message: error.message
|
|
@@ -71,14 +58,12 @@ export const createRouteHandler = handler => {
|
|
|
71
58
|
}
|
|
72
59
|
} else if (isSchemaHandler(handler)) {
|
|
73
60
|
const result = await handler(input);
|
|
74
|
-
|
|
75
61
|
if (result.type !== 'HandleSuccess') {
|
|
76
62
|
if (result.type === 'InputValidationError') {
|
|
77
63
|
ctx.status = 400;
|
|
78
64
|
} else {
|
|
79
65
|
ctx.status = 500;
|
|
80
66
|
}
|
|
81
|
-
|
|
82
67
|
ctx.body = result.message;
|
|
83
68
|
} else {
|
|
84
69
|
ctx.body = result.value;
|
|
@@ -86,18 +71,15 @@ export const createRouteHandler = handler => {
|
|
|
86
71
|
} else {
|
|
87
72
|
const args = Object.values(input.params).concat(input);
|
|
88
73
|
const body = await handler(...args);
|
|
89
|
-
|
|
90
74
|
if (typeof body !== 'undefined') {
|
|
91
75
|
ctx.body = body;
|
|
92
76
|
}
|
|
93
77
|
}
|
|
94
78
|
};
|
|
95
|
-
|
|
96
79
|
Object.defineProperties(apiHandler, Object.getOwnPropertyDescriptors(handler));
|
|
97
80
|
return apiHandler;
|
|
98
81
|
};
|
|
99
82
|
export const isNormalMethod = httpMethod => httpMethods.includes(httpMethod);
|
|
100
|
-
|
|
101
83
|
const getInputFromRequest = async ctx => {
|
|
102
84
|
const draft = {
|
|
103
85
|
params: ctx.params,
|
|
@@ -105,7 +87,6 @@ const getInputFromRequest = async ctx => {
|
|
|
105
87
|
headers: ctx.headers,
|
|
106
88
|
cookies: ctx.headers.cookie
|
|
107
89
|
};
|
|
108
|
-
|
|
109
90
|
if (typeIs.is(ctx.request.type, ['application/json'])) {
|
|
110
91
|
draft.data = ctx.request.body;
|
|
111
92
|
} else if (typeIs.is(ctx.request.type, ['multipart/form-data'])) {
|
|
@@ -115,6 +96,5 @@ const getInputFromRequest = async ctx => {
|
|
|
115
96
|
} else {
|
|
116
97
|
draft.body = ctx.request.body;
|
|
117
98
|
}
|
|
118
|
-
|
|
119
99
|
return draft;
|
|
120
100
|
};
|
|
@@ -4,17 +4,11 @@ Object.defineProperty(exports, "__esModule", {
|
|
|
4
4
|
value: true
|
|
5
5
|
});
|
|
6
6
|
exports.default = void 0;
|
|
7
|
-
|
|
8
7
|
var path = _interopRequireWildcard(require("path"));
|
|
9
|
-
|
|
10
8
|
var _utils = require("@modern-js/utils");
|
|
11
|
-
|
|
12
9
|
var _bffCore = require("@modern-js/bff-core");
|
|
13
|
-
|
|
14
10
|
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); }
|
|
15
|
-
|
|
16
11
|
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; }
|
|
17
|
-
|
|
18
12
|
var _default = () => ({
|
|
19
13
|
name: '@modern-js/plugin-koa',
|
|
20
14
|
setup: api => {
|
|
@@ -32,7 +26,6 @@ var _default = () => ({
|
|
|
32
26
|
bffExportsUtils = (0, _utils.createRuntimeExportsUtils)(appContext.internalDirectory, 'server');
|
|
33
27
|
const serverRuntimePath = bffExportsUtils.getPath();
|
|
34
28
|
const relativeRuntimePath = (0, _bffCore.getRelativeRuntimePath)(appDirectory, serverRuntimePath);
|
|
35
|
-
|
|
36
29
|
if (process.env.NODE_ENV === 'production') {
|
|
37
30
|
return {
|
|
38
31
|
source: {
|
|
@@ -53,7 +46,6 @@ var _default = () => ({
|
|
|
53
46
|
};
|
|
54
47
|
}
|
|
55
48
|
},
|
|
56
|
-
|
|
57
49
|
addRuntimeExports(input) {
|
|
58
50
|
const currentFile = bffExportsUtils.getPath();
|
|
59
51
|
const relativeRuntimeModulePath = path.relative(path.dirname(currentFile), runtimeModulePath);
|
|
@@ -67,9 +59,7 @@ var _default = () => ({
|
|
|
67
59
|
`);
|
|
68
60
|
return input;
|
|
69
61
|
}
|
|
70
|
-
|
|
71
62
|
};
|
|
72
63
|
}
|
|
73
64
|
});
|
|
74
|
-
|
|
75
65
|
exports.default = _default;
|
package/dist/js/node/context.js
CHANGED
package/dist/js/node/index.js
CHANGED
|
@@ -5,11 +5,8 @@ Object.defineProperty(exports, "__esModule", {
|
|
|
5
5
|
});
|
|
6
6
|
var _exportNames = {};
|
|
7
7
|
exports.default = void 0;
|
|
8
|
-
|
|
9
8
|
var _plugin = _interopRequireDefault(require("./plugin"));
|
|
10
|
-
|
|
11
9
|
var _context = require("./context");
|
|
12
|
-
|
|
13
10
|
Object.keys(_context).forEach(function (key) {
|
|
14
11
|
if (key === "default" || key === "__esModule") return;
|
|
15
12
|
if (Object.prototype.hasOwnProperty.call(_exportNames, key)) return;
|
|
@@ -21,8 +18,6 @@ Object.keys(_context).forEach(function (key) {
|
|
|
21
18
|
}
|
|
22
19
|
});
|
|
23
20
|
});
|
|
24
|
-
|
|
25
21
|
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
|
|
26
|
-
|
|
27
22
|
var _default = _plugin.default;
|
|
28
23
|
exports.default = _default;
|
package/dist/js/node/plugin.js
CHANGED
|
@@ -4,31 +4,19 @@ Object.defineProperty(exports, "__esModule", {
|
|
|
4
4
|
value: true
|
|
5
5
|
});
|
|
6
6
|
exports.default = void 0;
|
|
7
|
-
|
|
8
7
|
var path = _interopRequireWildcard(require("path"));
|
|
9
|
-
|
|
10
8
|
var _koa = _interopRequireDefault(require("koa"));
|
|
11
|
-
|
|
12
9
|
var _koaRouter = _interopRequireDefault(require("koa-router"));
|
|
13
|
-
|
|
14
10
|
var _koaBody = _interopRequireDefault(require("koa-body"));
|
|
15
|
-
|
|
16
11
|
var _utils = require("@modern-js/utils");
|
|
17
|
-
|
|
18
12
|
var _context = require("./context");
|
|
19
|
-
|
|
20
13
|
var _registerRoutes = _interopRequireDefault(require("./registerRoutes"));
|
|
21
|
-
|
|
22
14
|
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
|
|
23
|
-
|
|
24
15
|
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); }
|
|
25
|
-
|
|
26
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; }
|
|
27
|
-
|
|
28
17
|
const findAppModule = async apiDir => {
|
|
29
18
|
const exts = ['.ts', '.js'];
|
|
30
19
|
const paths = exts.map(ext => path.join(apiDir, `app${ext}`));
|
|
31
|
-
|
|
32
20
|
for (const filename of paths) {
|
|
33
21
|
if (await _utils.fs.pathExists(filename)) {
|
|
34
22
|
// 每次获取 app.ts 的时候,避免使用缓存的 app.ts
|
|
@@ -36,17 +24,14 @@ const findAppModule = async apiDir => {
|
|
|
36
24
|
return (0, _utils.compatRequire)(filename);
|
|
37
25
|
}
|
|
38
26
|
}
|
|
39
|
-
|
|
40
27
|
return null;
|
|
41
28
|
};
|
|
42
|
-
|
|
43
29
|
const initMiddlewares = (middleware, app) => {
|
|
44
30
|
middleware.forEach(middlewareItem => {
|
|
45
31
|
const middlewareFunc = typeof middlewareItem === 'string' ? (0, _utils.compatRequire)(middlewareItem) : middlewareItem;
|
|
46
32
|
app.use(middlewareFunc);
|
|
47
33
|
});
|
|
48
34
|
};
|
|
49
|
-
|
|
50
35
|
var _default = () => ({
|
|
51
36
|
name: '@modern-js/plugin-koa',
|
|
52
37
|
pre: ['@modern-js/plugin-bff'],
|
|
@@ -62,24 +47,20 @@ var _default = () => ({
|
|
|
62
47
|
const appContext = api.useAppContext();
|
|
63
48
|
const apiHandlerInfos = appContext.apiHandlerInfos;
|
|
64
49
|
const mode = appContext.apiMode;
|
|
65
|
-
|
|
66
50
|
if (mode === 'framework') {
|
|
67
51
|
app = await findAppModule(apiDir);
|
|
68
|
-
|
|
69
52
|
if (!(app instanceof _koa.default)) {
|
|
70
53
|
app = new _koa.default();
|
|
71
54
|
app.use((0, _koaBody.default)({
|
|
72
55
|
multipart: true
|
|
73
56
|
}));
|
|
74
57
|
}
|
|
75
|
-
|
|
76
58
|
if (config) {
|
|
77
59
|
const {
|
|
78
60
|
middleware
|
|
79
61
|
} = config;
|
|
80
62
|
initMiddlewares(middleware, app);
|
|
81
63
|
}
|
|
82
|
-
|
|
83
64
|
app.use(_context.run);
|
|
84
65
|
(0, _registerRoutes.default)(router, apiHandlerInfos);
|
|
85
66
|
} else if (mode === 'function') {
|
|
@@ -87,59 +68,48 @@ var _default = () => ({
|
|
|
87
68
|
app.use((0, _koaBody.default)({
|
|
88
69
|
multipart: true
|
|
89
70
|
}));
|
|
90
|
-
|
|
91
71
|
if (config) {
|
|
92
72
|
const {
|
|
93
73
|
middleware
|
|
94
74
|
} = config;
|
|
95
75
|
initMiddlewares(middleware, app);
|
|
96
76
|
}
|
|
97
|
-
|
|
98
77
|
app.use(_context.run);
|
|
99
78
|
(0, _registerRoutes.default)(router, apiHandlerInfos);
|
|
100
79
|
} else {
|
|
101
80
|
throw new Error(`mode must be function or framework`);
|
|
102
81
|
}
|
|
103
|
-
|
|
104
82
|
app.use(router.routes());
|
|
105
83
|
return (req, res) => {
|
|
106
84
|
return Promise.resolve(app.callback()(req, res));
|
|
107
85
|
};
|
|
108
86
|
},
|
|
109
|
-
|
|
110
87
|
prepareWebServer({
|
|
111
88
|
config
|
|
112
89
|
}, next) {
|
|
113
90
|
var _userConfig$server;
|
|
114
|
-
|
|
115
91
|
const userConfig = api.useConfigContext();
|
|
116
|
-
|
|
117
92
|
if (userConfig !== null && userConfig !== void 0 && (_userConfig$server = userConfig.server) !== null && _userConfig$server !== void 0 && _userConfig$server.disableFrameworkExt) {
|
|
118
93
|
return next();
|
|
119
94
|
}
|
|
120
|
-
|
|
121
95
|
const app = new _koa.default();
|
|
122
96
|
app.use(async (ctx, next) => {
|
|
123
97
|
await next();
|
|
124
|
-
|
|
125
98
|
if (!ctx.body) {
|
|
126
99
|
// restore statusCode
|
|
127
100
|
if (ctx.res.statusCode === 404 && !ctx.response._explicitStatus) {
|
|
128
101
|
ctx.res.statusCode = 200;
|
|
129
102
|
}
|
|
130
|
-
|
|
131
103
|
ctx.respond = false;
|
|
132
104
|
}
|
|
133
105
|
});
|
|
134
106
|
app.use((0, _koaBody.default)());
|
|
135
|
-
|
|
136
107
|
if (config) {
|
|
137
108
|
const {
|
|
138
109
|
middleware
|
|
139
110
|
} = config;
|
|
140
111
|
initMiddlewares(middleware, app);
|
|
141
112
|
}
|
|
142
|
-
|
|
143
113
|
return ctx => {
|
|
144
114
|
const {
|
|
145
115
|
source: {
|
|
@@ -155,8 +125,6 @@ var _default = () => ({
|
|
|
155
125
|
return Promise.resolve(app.callback()(req, res));
|
|
156
126
|
};
|
|
157
127
|
}
|
|
158
|
-
|
|
159
128
|
})
|
|
160
129
|
});
|
|
161
|
-
|
|
162
130
|
exports.default = _default;
|
|
@@ -4,9 +4,7 @@ Object.defineProperty(exports, "__esModule", {
|
|
|
4
4
|
value: true
|
|
5
5
|
});
|
|
6
6
|
exports.default = void 0;
|
|
7
|
-
|
|
8
7
|
var _utils = require("./utils");
|
|
9
|
-
|
|
10
8
|
const registerRoutes = (router, handlerInfos) => {
|
|
11
9
|
handlerInfos.forEach(({
|
|
12
10
|
routePath,
|
|
@@ -18,6 +16,5 @@ const registerRoutes = (router, handlerInfos) => {
|
|
|
18
16
|
router[method](routePath, routeHandler);
|
|
19
17
|
});
|
|
20
18
|
};
|
|
21
|
-
|
|
22
19
|
var _default = registerRoutes;
|
|
23
20
|
exports.default = _default;
|
package/dist/js/node/runtime.js
CHANGED
|
@@ -14,11 +14,8 @@ Object.defineProperty(exports, "useContext", {
|
|
|
14
14
|
return _context.useContext;
|
|
15
15
|
}
|
|
16
16
|
});
|
|
17
|
-
|
|
18
17
|
var _context = require("./context");
|
|
19
|
-
|
|
20
18
|
var _bffCore = require("@modern-js/bff-core");
|
|
21
|
-
|
|
22
19
|
Object.keys(_bffCore).forEach(function (key) {
|
|
23
20
|
if (key === "default" || key === "__esModule") return;
|
|
24
21
|
if (Object.prototype.hasOwnProperty.call(_exportNames, key)) return;
|
|
@@ -30,7 +27,5 @@ Object.keys(_bffCore).forEach(function (key) {
|
|
|
30
27
|
}
|
|
31
28
|
});
|
|
32
29
|
});
|
|
33
|
-
|
|
34
30
|
const hook = attacher => attacher;
|
|
35
|
-
|
|
36
31
|
exports.hook = hook;
|
package/dist/js/node/utils.js
CHANGED
|
@@ -4,23 +4,16 @@ Object.defineProperty(exports, "__esModule", {
|
|
|
4
4
|
value: true
|
|
5
5
|
});
|
|
6
6
|
exports.isNormalMethod = exports.createRouteHandler = void 0;
|
|
7
|
-
|
|
8
7
|
var _bffCore = require("@modern-js/bff-core");
|
|
9
|
-
|
|
10
8
|
var _bffRuntime = require("@modern-js/bff-runtime");
|
|
11
|
-
|
|
12
9
|
var _typeIs = _interopRequireDefault(require("type-is"));
|
|
13
|
-
|
|
14
10
|
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
|
|
15
|
-
|
|
16
11
|
const handleResponseMeta = (ctx, handler) => {
|
|
17
12
|
const responseMeta = Reflect.getMetadata(_bffCore.HttpMetadata.Response, handler);
|
|
18
|
-
|
|
19
13
|
if (Array.isArray(responseMeta)) {
|
|
20
14
|
for (const meta of responseMeta) {
|
|
21
15
|
const metaType = meta.type;
|
|
22
16
|
const metaValue = meta.value;
|
|
23
|
-
|
|
24
17
|
switch (metaType) {
|
|
25
18
|
case _bffCore.ResponseMetaType.Headers:
|
|
26
19
|
// eslint-disable-next-line @typescript-eslint/ban-types
|
|
@@ -29,40 +22,31 @@ const handleResponseMeta = (ctx, handler) => {
|
|
|
29
22
|
ctx.append(key, value);
|
|
30
23
|
}
|
|
31
24
|
}
|
|
32
|
-
|
|
33
25
|
break;
|
|
34
|
-
|
|
35
26
|
case _bffCore.ResponseMetaType.Redirect:
|
|
36
27
|
if (typeof metaValue === 'string') {
|
|
37
28
|
ctx.redirect(metaValue);
|
|
38
29
|
}
|
|
39
|
-
|
|
40
30
|
break;
|
|
41
|
-
|
|
42
31
|
case _bffCore.ResponseMetaType.StatusCode:
|
|
43
32
|
if (typeof metaValue === 'number') {
|
|
44
33
|
ctx.status = metaValue;
|
|
45
34
|
}
|
|
46
|
-
|
|
47
35
|
break;
|
|
48
|
-
|
|
49
36
|
default:
|
|
50
37
|
break;
|
|
51
38
|
}
|
|
52
39
|
}
|
|
53
40
|
}
|
|
54
41
|
};
|
|
55
|
-
|
|
56
42
|
const createRouteHandler = handler => {
|
|
57
43
|
// eslint-disable-next-line consistent-return
|
|
58
44
|
const apiHandler = async ctx => {
|
|
59
45
|
const input = await getInputFromRequest(ctx);
|
|
60
|
-
|
|
61
46
|
if ((0, _bffCore.isWithMetaHandler)(handler)) {
|
|
62
47
|
try {
|
|
63
48
|
handleResponseMeta(ctx, handler);
|
|
64
49
|
const body = await handler(input);
|
|
65
|
-
|
|
66
50
|
if (typeof body !== 'undefined') {
|
|
67
51
|
ctx.body = body;
|
|
68
52
|
}
|
|
@@ -73,7 +57,6 @@ const createRouteHandler = handler => {
|
|
|
73
57
|
} else {
|
|
74
58
|
ctx.status = 500;
|
|
75
59
|
}
|
|
76
|
-
|
|
77
60
|
return ctx.body = {
|
|
78
61
|
code: error.code,
|
|
79
62
|
message: error.message
|
|
@@ -82,14 +65,12 @@ const createRouteHandler = handler => {
|
|
|
82
65
|
}
|
|
83
66
|
} else if ((0, _bffRuntime.isSchemaHandler)(handler)) {
|
|
84
67
|
const result = await handler(input);
|
|
85
|
-
|
|
86
68
|
if (result.type !== 'HandleSuccess') {
|
|
87
69
|
if (result.type === 'InputValidationError') {
|
|
88
70
|
ctx.status = 400;
|
|
89
71
|
} else {
|
|
90
72
|
ctx.status = 500;
|
|
91
73
|
}
|
|
92
|
-
|
|
93
74
|
ctx.body = result.message;
|
|
94
75
|
} else {
|
|
95
76
|
ctx.body = result.value;
|
|
@@ -97,23 +78,17 @@ const createRouteHandler = handler => {
|
|
|
97
78
|
} else {
|
|
98
79
|
const args = Object.values(input.params).concat(input);
|
|
99
80
|
const body = await handler(...args);
|
|
100
|
-
|
|
101
81
|
if (typeof body !== 'undefined') {
|
|
102
82
|
ctx.body = body;
|
|
103
83
|
}
|
|
104
84
|
}
|
|
105
85
|
};
|
|
106
|
-
|
|
107
86
|
Object.defineProperties(apiHandler, Object.getOwnPropertyDescriptors(handler));
|
|
108
87
|
return apiHandler;
|
|
109
88
|
};
|
|
110
|
-
|
|
111
89
|
exports.createRouteHandler = createRouteHandler;
|
|
112
|
-
|
|
113
90
|
const isNormalMethod = httpMethod => _bffCore.httpMethods.includes(httpMethod);
|
|
114
|
-
|
|
115
91
|
exports.isNormalMethod = isNormalMethod;
|
|
116
|
-
|
|
117
92
|
const getInputFromRequest = async ctx => {
|
|
118
93
|
const draft = {
|
|
119
94
|
params: ctx.params,
|
|
@@ -121,7 +96,6 @@ const getInputFromRequest = async ctx => {
|
|
|
121
96
|
headers: ctx.headers,
|
|
122
97
|
cookies: ctx.headers.cookie
|
|
123
98
|
};
|
|
124
|
-
|
|
125
99
|
if (_typeIs.default.is(ctx.request.type, ['application/json'])) {
|
|
126
100
|
draft.data = ctx.request.body;
|
|
127
101
|
} else if (_typeIs.default.is(ctx.request.type, ['multipart/form-data'])) {
|
|
@@ -131,6 +105,5 @@ const getInputFromRequest = async ctx => {
|
|
|
131
105
|
} else {
|
|
132
106
|
draft.body = ctx.request.body;
|
|
133
107
|
}
|
|
134
|
-
|
|
135
108
|
return draft;
|
|
136
109
|
};
|
package/dist/types/plugin.d.ts
CHANGED
package/package.json
CHANGED
|
@@ -11,7 +11,7 @@
|
|
|
11
11
|
"modern",
|
|
12
12
|
"modern.js"
|
|
13
13
|
],
|
|
14
|
-
"version": "2.0.0-beta.
|
|
14
|
+
"version": "2.0.0-beta.1",
|
|
15
15
|
"jsnext:source": "./src/index.ts",
|
|
16
16
|
"types": "./dist/types/index.d.ts",
|
|
17
17
|
"main": "./dist/js/node/index.js",
|
|
@@ -34,19 +34,15 @@
|
|
|
34
34
|
},
|
|
35
35
|
"dependencies": {
|
|
36
36
|
"@babel/runtime": "^7.18.0",
|
|
37
|
-
"@modern-js/bff-core": "2.0.0-beta.0",
|
|
38
|
-
"@modern-js/bff-runtime": "2.0.0-beta.0",
|
|
39
|
-
"@modern-js/utils": "2.0.0-beta.0",
|
|
40
|
-
"@modern-js/types": "2.0.0-beta.0",
|
|
41
37
|
"koa-body": "^4.2.0",
|
|
42
38
|
"koa-router": "^10.0.0",
|
|
43
|
-
"type-is": "^1.6.18"
|
|
39
|
+
"type-is": "^1.6.18",
|
|
40
|
+
"@modern-js/bff-core": "2.0.0-beta.1",
|
|
41
|
+
"@modern-js/bff-runtime": "2.0.0-beta.1",
|
|
42
|
+
"@modern-js/utils": "2.0.0-beta.1",
|
|
43
|
+
"@modern-js/types": "2.0.0-beta.1"
|
|
44
44
|
},
|
|
45
45
|
"devDependencies": {
|
|
46
|
-
"@modern-js/core": "2.0.0-beta.0",
|
|
47
|
-
"@modern-js/server-core": "2.0.0-beta.0",
|
|
48
|
-
"@scripts/build": "2.0.0-beta.0",
|
|
49
|
-
"@scripts/jest-config": "2.0.0-beta.0",
|
|
50
46
|
"@types/jest": "^27",
|
|
51
47
|
"@types/koa": "^2.13.4",
|
|
52
48
|
"@types/koa-router": "^7.4.4",
|
|
@@ -57,7 +53,11 @@
|
|
|
57
53
|
"koa": "^2.13.3",
|
|
58
54
|
"supertest": "^6.1.6",
|
|
59
55
|
"typescript": "^4",
|
|
60
|
-
"zod": "^3.17.3"
|
|
56
|
+
"zod": "^3.17.3",
|
|
57
|
+
"@modern-js/core": "2.0.0-beta.1",
|
|
58
|
+
"@modern-js/server-core": "2.0.0-beta.1",
|
|
59
|
+
"@scripts/build": "2.0.0-beta.1",
|
|
60
|
+
"@scripts/jest-config": "2.0.0-beta.1"
|
|
61
61
|
},
|
|
62
62
|
"modernConfig": {
|
|
63
63
|
"output": {
|