@modern-js/plugin-koa 1.21.5 → 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 +66 -24
- package/dist/js/modern/cli/index.js +4 -5
- package/dist/js/modern/plugin.js +19 -25
- package/dist/js/modern/registerRoutes.js +0 -2
- package/dist/js/modern/utils.js +0 -20
- package/dist/js/node/cli/index.js +4 -12
- package/dist/js/node/context.js +0 -2
- package/dist/js/node/index.js +0 -5
- package/dist/js/node/plugin.js +19 -36
- 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 +12 -33
- package/types.d.ts +28 -0
package/CHANGELOG.md
CHANGED
|
@@ -1,29 +1,71 @@
|
|
|
1
1
|
# @modern-js/plugin-koa
|
|
2
2
|
|
|
3
|
-
##
|
|
4
|
-
|
|
5
|
-
###
|
|
6
|
-
|
|
7
|
-
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
-
|
|
17
|
-
-
|
|
18
|
-
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
-
|
|
25
|
-
-
|
|
26
|
-
-
|
|
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
|
+
|
|
39
|
+
## 2.0.0-beta.0
|
|
40
|
+
|
|
41
|
+
### Major Changes
|
|
42
|
+
|
|
43
|
+
- dda38c9: chore: v2
|
|
44
|
+
|
|
45
|
+
### Patch Changes
|
|
46
|
+
|
|
47
|
+
- 8d24bed25: fix: compat Hook API in /server namespace
|
|
48
|
+
fix: 在 @modern-js/runtime/server 命名空间下兼容 Hook API
|
|
49
|
+
- 3bbea92b2: feat: support Hook、Middleware new API
|
|
50
|
+
feat: 支持 Hook、Middleware 的新 API
|
|
51
|
+
- Updated dependencies [a2509bf]
|
|
52
|
+
- Updated dependencies [edd1cfb1a]
|
|
53
|
+
- Updated dependencies [cc971eabf]
|
|
54
|
+
- Updated dependencies [5b9049f]
|
|
55
|
+
- Updated dependencies [6bda14ed7]
|
|
56
|
+
- Updated dependencies [b8bbe036c]
|
|
57
|
+
- Updated dependencies [d5a31df78]
|
|
58
|
+
- Updated dependencies [dda38c9]
|
|
59
|
+
- Updated dependencies [102d32e4b]
|
|
60
|
+
- Updated dependencies [8b8e1bb57]
|
|
61
|
+
- Updated dependencies [3bbea92b2]
|
|
62
|
+
- Updated dependencies [abf3421]
|
|
63
|
+
- Updated dependencies [543be95]
|
|
64
|
+
- Updated dependencies [14b712d]
|
|
65
|
+
- @modern-js/bff-core@2.0.0-beta.0
|
|
66
|
+
- @modern-js/utils@2.0.0-beta.0
|
|
67
|
+
- @modern-js/types@2.0.0-beta.0
|
|
68
|
+
- @modern-js/bff-runtime@2.0.0-beta.0
|
|
27
69
|
|
|
28
70
|
## 1.21.2
|
|
29
71
|
|
|
@@ -18,12 +18,12 @@ 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: {
|
|
25
24
|
alias: {
|
|
26
|
-
'@modern-js/runtime/server': relativeRuntimePath
|
|
25
|
+
'@modern-js/runtime/server': relativeRuntimePath,
|
|
26
|
+
'@modern-js/runtime/koa': relativeRuntimePath
|
|
27
27
|
}
|
|
28
28
|
}
|
|
29
29
|
};
|
|
@@ -31,13 +31,13 @@ export default (() => ({
|
|
|
31
31
|
return {
|
|
32
32
|
source: {
|
|
33
33
|
alias: {
|
|
34
|
-
'@modern-js/runtime/server': serverRuntimePath
|
|
34
|
+
'@modern-js/runtime/server': serverRuntimePath,
|
|
35
|
+
'@modern-js/runtime/koa': serverRuntimePath
|
|
35
36
|
}
|
|
36
37
|
}
|
|
37
38
|
};
|
|
38
39
|
}
|
|
39
40
|
},
|
|
40
|
-
|
|
41
41
|
addRuntimeExports(input) {
|
|
42
42
|
const currentFile = bffExportsUtils.getPath();
|
|
43
43
|
const relativeRuntimeModulePath = path.relative(path.dirname(currentFile), runtimeModulePath);
|
|
@@ -51,7 +51,6 @@ export default (() => ({
|
|
|
51
51
|
`);
|
|
52
52
|
return input;
|
|
53
53
|
}
|
|
54
|
-
|
|
55
54
|
};
|
|
56
55
|
}
|
|
57
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,20 +15,18 @@ 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'],
|
|
29
|
+
post: ['@modern-js/plugin-server'],
|
|
34
30
|
setup: api => ({
|
|
35
31
|
async prepareApiServer({
|
|
36
32
|
pwd,
|
|
@@ -42,24 +38,20 @@ export default (() => ({
|
|
|
42
38
|
const appContext = api.useAppContext();
|
|
43
39
|
const apiHandlerInfos = appContext.apiHandlerInfos;
|
|
44
40
|
const mode = appContext.apiMode;
|
|
45
|
-
|
|
46
41
|
if (mode === 'framework') {
|
|
47
42
|
app = await findAppModule(apiDir);
|
|
48
|
-
|
|
49
43
|
if (!(app instanceof Koa)) {
|
|
50
44
|
app = new Koa();
|
|
51
45
|
app.use(koaBody({
|
|
52
46
|
multipart: true
|
|
53
47
|
}));
|
|
54
48
|
}
|
|
55
|
-
|
|
56
49
|
if (config) {
|
|
57
50
|
const {
|
|
58
51
|
middleware
|
|
59
52
|
} = config;
|
|
60
53
|
initMiddlewares(middleware, app);
|
|
61
54
|
}
|
|
62
|
-
|
|
63
55
|
app.use(run);
|
|
64
56
|
registerRoutes(router, apiHandlerInfos);
|
|
65
57
|
} else if (mode === 'function') {
|
|
@@ -67,60 +59,62 @@ export default (() => ({
|
|
|
67
59
|
app.use(koaBody({
|
|
68
60
|
multipart: true
|
|
69
61
|
}));
|
|
70
|
-
|
|
71
62
|
if (config) {
|
|
72
63
|
const {
|
|
73
64
|
middleware
|
|
74
65
|
} = config;
|
|
75
66
|
initMiddlewares(middleware, app);
|
|
76
67
|
}
|
|
77
|
-
|
|
78
68
|
app.use(run);
|
|
79
69
|
registerRoutes(router, apiHandlerInfos);
|
|
80
70
|
} else {
|
|
81
71
|
throw new Error(`mode must be function or framework`);
|
|
82
72
|
}
|
|
83
|
-
|
|
84
73
|
app.use(router.routes());
|
|
85
74
|
return (req, res) => {
|
|
86
75
|
return Promise.resolve(app.callback()(req, res));
|
|
87
76
|
};
|
|
88
77
|
},
|
|
89
|
-
|
|
90
78
|
prepareWebServer({
|
|
91
79
|
config
|
|
92
|
-
}) {
|
|
80
|
+
}, next) {
|
|
81
|
+
var _userConfig$server;
|
|
82
|
+
const userConfig = api.useConfigContext();
|
|
83
|
+
if (userConfig !== null && userConfig !== void 0 && (_userConfig$server = userConfig.server) !== null && _userConfig$server !== void 0 && _userConfig$server.disableFrameworkExt) {
|
|
84
|
+
return next();
|
|
85
|
+
}
|
|
93
86
|
const app = new Koa();
|
|
94
87
|
app.use(async (ctx, next) => {
|
|
95
88
|
await next();
|
|
96
|
-
|
|
97
89
|
if (!ctx.body) {
|
|
98
90
|
// restore statusCode
|
|
99
91
|
if (ctx.res.statusCode === 404 && !ctx.response._explicitStatus) {
|
|
100
92
|
ctx.res.statusCode = 200;
|
|
101
93
|
}
|
|
102
|
-
|
|
103
94
|
ctx.respond = false;
|
|
104
95
|
}
|
|
105
96
|
});
|
|
106
97
|
app.use(koaBody());
|
|
107
|
-
|
|
108
98
|
if (config) {
|
|
109
99
|
const {
|
|
110
100
|
middleware
|
|
111
101
|
} = config;
|
|
112
102
|
initMiddlewares(middleware, app);
|
|
113
103
|
}
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
104
|
+
return ctx => {
|
|
105
|
+
const {
|
|
106
|
+
source: {
|
|
107
|
+
req,
|
|
108
|
+
res
|
|
109
|
+
}
|
|
110
|
+
} = ctx;
|
|
111
|
+
app.on('error', err => {
|
|
112
|
+
if (err) {
|
|
113
|
+
throw err;
|
|
114
|
+
}
|
|
115
|
+
});
|
|
121
116
|
return Promise.resolve(app.callback()(req, res));
|
|
122
117
|
};
|
|
123
118
|
}
|
|
124
|
-
|
|
125
119
|
})
|
|
126
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,12 +26,12 @@ 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: {
|
|
39
32
|
alias: {
|
|
40
|
-
'@modern-js/runtime/server': relativeRuntimePath
|
|
33
|
+
'@modern-js/runtime/server': relativeRuntimePath,
|
|
34
|
+
'@modern-js/runtime/koa': relativeRuntimePath
|
|
41
35
|
}
|
|
42
36
|
}
|
|
43
37
|
};
|
|
@@ -45,13 +39,13 @@ var _default = () => ({
|
|
|
45
39
|
return {
|
|
46
40
|
source: {
|
|
47
41
|
alias: {
|
|
48
|
-
'@modern-js/runtime/server': serverRuntimePath
|
|
42
|
+
'@modern-js/runtime/server': serverRuntimePath,
|
|
43
|
+
'@modern-js/runtime/koa': serverRuntimePath
|
|
49
44
|
}
|
|
50
45
|
}
|
|
51
46
|
};
|
|
52
47
|
}
|
|
53
48
|
},
|
|
54
|
-
|
|
55
49
|
addRuntimeExports(input) {
|
|
56
50
|
const currentFile = bffExportsUtils.getPath();
|
|
57
51
|
const relativeRuntimeModulePath = path.relative(path.dirname(currentFile), runtimeModulePath);
|
|
@@ -65,9 +59,7 @@ var _default = () => ({
|
|
|
65
59
|
`);
|
|
66
60
|
return input;
|
|
67
61
|
}
|
|
68
|
-
|
|
69
62
|
};
|
|
70
63
|
}
|
|
71
64
|
});
|
|
72
|
-
|
|
73
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,20 +24,18 @@ 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'],
|
|
38
|
+
post: ['@modern-js/plugin-server'],
|
|
53
39
|
setup: api => ({
|
|
54
40
|
async prepareApiServer({
|
|
55
41
|
pwd,
|
|
@@ -61,24 +47,20 @@ var _default = () => ({
|
|
|
61
47
|
const appContext = api.useAppContext();
|
|
62
48
|
const apiHandlerInfos = appContext.apiHandlerInfos;
|
|
63
49
|
const mode = appContext.apiMode;
|
|
64
|
-
|
|
65
50
|
if (mode === 'framework') {
|
|
66
51
|
app = await findAppModule(apiDir);
|
|
67
|
-
|
|
68
52
|
if (!(app instanceof _koa.default)) {
|
|
69
53
|
app = new _koa.default();
|
|
70
54
|
app.use((0, _koaBody.default)({
|
|
71
55
|
multipart: true
|
|
72
56
|
}));
|
|
73
57
|
}
|
|
74
|
-
|
|
75
58
|
if (config) {
|
|
76
59
|
const {
|
|
77
60
|
middleware
|
|
78
61
|
} = config;
|
|
79
62
|
initMiddlewares(middleware, app);
|
|
80
63
|
}
|
|
81
|
-
|
|
82
64
|
app.use(_context.run);
|
|
83
65
|
(0, _registerRoutes.default)(router, apiHandlerInfos);
|
|
84
66
|
} else if (mode === 'function') {
|
|
@@ -86,62 +68,63 @@ var _default = () => ({
|
|
|
86
68
|
app.use((0, _koaBody.default)({
|
|
87
69
|
multipart: true
|
|
88
70
|
}));
|
|
89
|
-
|
|
90
71
|
if (config) {
|
|
91
72
|
const {
|
|
92
73
|
middleware
|
|
93
74
|
} = config;
|
|
94
75
|
initMiddlewares(middleware, app);
|
|
95
76
|
}
|
|
96
|
-
|
|
97
77
|
app.use(_context.run);
|
|
98
78
|
(0, _registerRoutes.default)(router, apiHandlerInfos);
|
|
99
79
|
} else {
|
|
100
80
|
throw new Error(`mode must be function or framework`);
|
|
101
81
|
}
|
|
102
|
-
|
|
103
82
|
app.use(router.routes());
|
|
104
83
|
return (req, res) => {
|
|
105
84
|
return Promise.resolve(app.callback()(req, res));
|
|
106
85
|
};
|
|
107
86
|
},
|
|
108
|
-
|
|
109
87
|
prepareWebServer({
|
|
110
88
|
config
|
|
111
|
-
}) {
|
|
89
|
+
}, next) {
|
|
90
|
+
var _userConfig$server;
|
|
91
|
+
const userConfig = api.useConfigContext();
|
|
92
|
+
if (userConfig !== null && userConfig !== void 0 && (_userConfig$server = userConfig.server) !== null && _userConfig$server !== void 0 && _userConfig$server.disableFrameworkExt) {
|
|
93
|
+
return next();
|
|
94
|
+
}
|
|
112
95
|
const app = new _koa.default();
|
|
113
96
|
app.use(async (ctx, next) => {
|
|
114
97
|
await next();
|
|
115
|
-
|
|
116
98
|
if (!ctx.body) {
|
|
117
99
|
// restore statusCode
|
|
118
100
|
if (ctx.res.statusCode === 404 && !ctx.response._explicitStatus) {
|
|
119
101
|
ctx.res.statusCode = 200;
|
|
120
102
|
}
|
|
121
|
-
|
|
122
103
|
ctx.respond = false;
|
|
123
104
|
}
|
|
124
105
|
});
|
|
125
106
|
app.use((0, _koaBody.default)());
|
|
126
|
-
|
|
127
107
|
if (config) {
|
|
128
108
|
const {
|
|
129
109
|
middleware
|
|
130
110
|
} = config;
|
|
131
111
|
initMiddlewares(middleware, app);
|
|
132
112
|
}
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
113
|
+
return ctx => {
|
|
114
|
+
const {
|
|
115
|
+
source: {
|
|
116
|
+
req,
|
|
117
|
+
res
|
|
118
|
+
}
|
|
119
|
+
} = ctx;
|
|
120
|
+
app.on('error', err => {
|
|
121
|
+
if (err) {
|
|
122
|
+
throw err;
|
|
123
|
+
}
|
|
124
|
+
});
|
|
140
125
|
return Promise.resolve(app.callback()(req, res));
|
|
141
126
|
};
|
|
142
127
|
}
|
|
143
|
-
|
|
144
128
|
})
|
|
145
129
|
});
|
|
146
|
-
|
|
147
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": "
|
|
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",
|
|
@@ -37,9 +37,10 @@
|
|
|
37
37
|
"koa-body": "^4.2.0",
|
|
38
38
|
"koa-router": "^10.0.0",
|
|
39
39
|
"type-is": "^1.6.18",
|
|
40
|
-
"@modern-js/bff-core": "
|
|
41
|
-
"@modern-js/bff-runtime": "
|
|
42
|
-
"@modern-js/utils": "
|
|
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"
|
|
43
44
|
},
|
|
44
45
|
"devDependencies": {
|
|
45
46
|
"@types/jest": "^27",
|
|
@@ -53,10 +54,10 @@
|
|
|
53
54
|
"supertest": "^6.1.6",
|
|
54
55
|
"typescript": "^4",
|
|
55
56
|
"zod": "^3.17.3",
|
|
56
|
-
"@modern-js/core": "
|
|
57
|
-
"@modern-js/server-core": "
|
|
58
|
-
"@scripts/build": "
|
|
59
|
-
"@scripts/jest-config": "
|
|
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"
|
|
60
61
|
},
|
|
61
62
|
"modernConfig": {
|
|
62
63
|
"output": {
|
|
@@ -70,32 +71,10 @@
|
|
|
70
71
|
"registry": "https://registry.npmjs.org/",
|
|
71
72
|
"access": "public"
|
|
72
73
|
},
|
|
73
|
-
"wireit": {
|
|
74
|
-
"build": {
|
|
75
|
-
"command": "modern build",
|
|
76
|
-
"files": [
|
|
77
|
-
"src/**/*",
|
|
78
|
-
"tsconfig.json",
|
|
79
|
-
"package.json"
|
|
80
|
-
],
|
|
81
|
-
"output": [
|
|
82
|
-
"dist/**/*"
|
|
83
|
-
]
|
|
84
|
-
},
|
|
85
|
-
"test": {
|
|
86
|
-
"command": "jest --passWithNoTests",
|
|
87
|
-
"files": [
|
|
88
|
-
"src/**/*",
|
|
89
|
-
"tsconfig.json",
|
|
90
|
-
"package.json",
|
|
91
|
-
"tests/**/*"
|
|
92
|
-
],
|
|
93
|
-
"output": []
|
|
94
|
-
}
|
|
95
|
-
},
|
|
96
74
|
"scripts": {
|
|
97
75
|
"new": "modern new",
|
|
98
|
-
"
|
|
99
|
-
"
|
|
76
|
+
"dev": "modern build --watch",
|
|
77
|
+
"build": "modern build",
|
|
78
|
+
"test": "jest --passWithNoTests"
|
|
100
79
|
}
|
|
101
80
|
}
|
package/types.d.ts
CHANGED
|
@@ -1,14 +1,42 @@
|
|
|
1
1
|
/// <reference path="./dist/types/index.d.ts" />
|
|
2
|
+
declare module '@modern-js/runtime/koa' {
|
|
3
|
+
|
|
4
|
+
import { Context, Middleware } from 'koa';
|
|
5
|
+
|
|
6
|
+
export type { Middleware as RequestHandler };
|
|
7
|
+
|
|
8
|
+
export function useContext(): Context;
|
|
9
|
+
|
|
10
|
+
export * from '@modern-js/bff-core';
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
// Todo: remove on next version
|
|
2
14
|
declare module '@modern-js/runtime/server' {
|
|
3
15
|
|
|
4
16
|
import { Context, Middleware } from 'koa';
|
|
17
|
+
import type { AfterMatchContext, AfterRenderContext, NextFunction } from '@modern-js/types';
|
|
18
|
+
|
|
19
|
+
export type AfterRenderHook = (
|
|
20
|
+
context: AfterRenderContext,
|
|
21
|
+
next: NextFunction,
|
|
22
|
+
) => void;
|
|
23
|
+
|
|
24
|
+
export type AfterMatchHook = (
|
|
25
|
+
context: AfterMatchContext,
|
|
26
|
+
next: NextFunction,
|
|
27
|
+
) => void;
|
|
28
|
+
|
|
5
29
|
|
|
6
30
|
type KoaOptions = {
|
|
7
31
|
addMiddleware: (...input: Middleware[]) => void;
|
|
32
|
+
afterRender: (hook: AfterRenderHook) => void;
|
|
33
|
+
afterMatch: (hook: AfterMatchHook) => void;
|
|
8
34
|
};
|
|
9
35
|
|
|
10
36
|
type KoaAttacher = (options: KoaOptions) => void;
|
|
11
37
|
|
|
38
|
+
export type { Middleware as RequestHandler };
|
|
39
|
+
|
|
12
40
|
export function useContext(): Context;
|
|
13
41
|
|
|
14
42
|
export function hook(attacher: KoaAttacher): KoaAttacher;
|