@modern-js/plugin-koa 2.15.0 → 2.17.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +25 -0
- package/dist/cjs/cli/index.js +57 -50
- package/dist/cjs/context.js +13 -26
- package/dist/cjs/index.js +28 -37
- package/dist/cjs/plugin.js +158 -134
- package/dist/cjs/registerRoutes.js +10 -26
- package/dist/cjs/runtime.js +27 -28
- package/dist/cjs/utils.js +36 -54
- package/dist/esm/cli/index.js +47 -47
- package/dist/esm/index.js +1 -2
- package/dist/esm/plugin.js +374 -365
- package/dist/esm/registerRoutes.js +7 -8
- package/dist/esm/runtime.js +3 -4
- package/dist/esm/utils.js +391 -369
- package/dist/esm-node/cli/index.js +7 -17
- package/dist/esm-node/context.js +1 -4
- package/dist/esm-node/index.js +1 -4
- package/dist/esm-node/plugin.js +94 -92
- package/dist/esm-node/registerRoutes.js +1 -4
- package/dist/esm-node/runtime.js +2 -6
- package/dist/esm-node/utils.js +14 -23
- package/package.json +15 -11
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import * as path from "path";
|
|
2
2
|
import { createRuntimeExportsUtils } from "@modern-js/utils";
|
|
3
|
-
|
|
3
|
+
export default () => ({
|
|
4
4
|
name: "@modern-js/plugin-koa",
|
|
5
5
|
setup: (api) => {
|
|
6
6
|
let bffExportsUtils;
|
|
@@ -9,10 +9,7 @@ var cli_default = () => ({
|
|
|
9
9
|
return {
|
|
10
10
|
config() {
|
|
11
11
|
const appContext = useAppContext();
|
|
12
|
-
bffExportsUtils = createRuntimeExportsUtils(
|
|
13
|
-
appContext.internalDirectory,
|
|
14
|
-
"server"
|
|
15
|
-
);
|
|
12
|
+
bffExportsUtils = createRuntimeExportsUtils(appContext.internalDirectory, "server");
|
|
16
13
|
const runtimePath = "@modern-js/plugin-koa/runtime";
|
|
17
14
|
const alias = process.env.NODE_ENV === "production" ? runtimePath : require.resolve(runtimePath);
|
|
18
15
|
return {
|
|
@@ -33,18 +30,14 @@ var cli_default = () => ({
|
|
|
33
30
|
plugins.push({
|
|
34
31
|
"@modern-js/plugin-koa": "@modern-js/plugin-koa/server"
|
|
35
32
|
});
|
|
36
|
-
return {
|
|
33
|
+
return {
|
|
34
|
+
plugins
|
|
35
|
+
};
|
|
37
36
|
},
|
|
38
37
|
addRuntimeExports(input) {
|
|
39
38
|
const currentFile = bffExportsUtils.getPath();
|
|
40
|
-
const relativeRuntimeModulePath = path.relative(
|
|
41
|
-
|
|
42
|
-
runtimeModulePath
|
|
43
|
-
);
|
|
44
|
-
const relativeFramePath = path.relative(
|
|
45
|
-
path.dirname(currentFile),
|
|
46
|
-
require.resolve("koa")
|
|
47
|
-
);
|
|
39
|
+
const relativeRuntimeModulePath = path.relative(path.dirname(currentFile), runtimeModulePath);
|
|
40
|
+
const relativeFramePath = path.relative(path.dirname(currentFile), require.resolve("koa"));
|
|
48
41
|
bffExportsUtils.addExport(`const pluginRuntime = require('${relativeRuntimeModulePath}');
|
|
49
42
|
const Koa = require('${relativeFramePath}')
|
|
50
43
|
module.exports = {
|
|
@@ -57,6 +50,3 @@ var cli_default = () => ({
|
|
|
57
50
|
};
|
|
58
51
|
}
|
|
59
52
|
});
|
|
60
|
-
export {
|
|
61
|
-
cli_default as default
|
|
62
|
-
};
|
package/dist/esm-node/context.js
CHANGED
package/dist/esm-node/index.js
CHANGED
package/dist/esm-node/plugin.js
CHANGED
|
@@ -6,7 +6,10 @@ import { fs, compatRequire } from "@modern-js/utils";
|
|
|
6
6
|
import { run } from "./context";
|
|
7
7
|
import registerRoutes from "./registerRoutes";
|
|
8
8
|
const findAppModule = async (apiDir) => {
|
|
9
|
-
const exts = [
|
|
9
|
+
const exts = [
|
|
10
|
+
".ts",
|
|
11
|
+
".js"
|
|
12
|
+
];
|
|
10
13
|
const paths = exts.map((ext) => path.join(apiDir, `app${ext}`));
|
|
11
14
|
for (const filename of paths) {
|
|
12
15
|
if (await fs.pathExists(filename)) {
|
|
@@ -22,101 +25,100 @@ const initMiddlewares = (middleware, app) => {
|
|
|
22
25
|
app.use(middlewareFunc);
|
|
23
26
|
});
|
|
24
27
|
};
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
28
|
+
export default () => {
|
|
29
|
+
return {
|
|
30
|
+
name: "@modern-js/plugin-koa",
|
|
31
|
+
pre: [
|
|
32
|
+
"@modern-js/plugin-bff"
|
|
33
|
+
],
|
|
34
|
+
post: [
|
|
35
|
+
"@modern-js/plugin-server"
|
|
36
|
+
],
|
|
37
|
+
setup: (api) => {
|
|
38
|
+
return {
|
|
39
|
+
async prepareApiServer({ pwd, config, render }) {
|
|
40
|
+
var _userConfig_bff;
|
|
41
|
+
let app;
|
|
42
|
+
const router = new Router();
|
|
43
|
+
const apiDir = path.join(pwd, "./api");
|
|
44
|
+
const appContext = api.useAppContext();
|
|
45
|
+
const apiHandlerInfos = appContext.apiHandlerInfos;
|
|
46
|
+
const mode = appContext.apiMode;
|
|
47
|
+
const userConfig = api.useConfigContext();
|
|
48
|
+
if (mode === "framework") {
|
|
49
|
+
app = await findAppModule(apiDir);
|
|
50
|
+
if (!(app instanceof Koa)) {
|
|
51
|
+
app = new Koa();
|
|
52
|
+
app.use(koaBody({
|
|
53
|
+
multipart: true
|
|
54
|
+
}));
|
|
55
|
+
}
|
|
56
|
+
if (config) {
|
|
57
|
+
const { middleware } = config;
|
|
58
|
+
initMiddlewares(middleware, app);
|
|
59
|
+
}
|
|
60
|
+
app.use(run);
|
|
61
|
+
registerRoutes(router, apiHandlerInfos);
|
|
62
|
+
} else if (mode === "function") {
|
|
63
|
+
app = new Koa();
|
|
64
|
+
app.use(koaBody({
|
|
45
65
|
multipart: true
|
|
46
|
-
})
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
} else if (mode === "function") {
|
|
56
|
-
app = new Koa();
|
|
57
|
-
app.use(
|
|
58
|
-
koaBody({
|
|
59
|
-
multipart: true
|
|
60
|
-
})
|
|
61
|
-
);
|
|
62
|
-
if (config) {
|
|
63
|
-
const { middleware } = config;
|
|
64
|
-
initMiddlewares(middleware, app);
|
|
65
|
-
}
|
|
66
|
-
app.use(run);
|
|
67
|
-
registerRoutes(router, apiHandlerInfos);
|
|
68
|
-
} else {
|
|
69
|
-
throw new Error(`mode must be function or framework`);
|
|
70
|
-
}
|
|
71
|
-
app.use(router.routes());
|
|
72
|
-
if (((_a = userConfig.bff) == null ? void 0 : _a.enableHandleWeb) && render) {
|
|
73
|
-
app.use(async (ctx, next) => {
|
|
74
|
-
const html = await render(ctx.req, ctx.res);
|
|
75
|
-
if (html) {
|
|
76
|
-
ctx.body = html;
|
|
66
|
+
}));
|
|
67
|
+
if (config) {
|
|
68
|
+
const { middleware } = config;
|
|
69
|
+
initMiddlewares(middleware, app);
|
|
70
|
+
}
|
|
71
|
+
app.use(run);
|
|
72
|
+
registerRoutes(router, apiHandlerInfos);
|
|
73
|
+
} else {
|
|
74
|
+
throw new Error(`mode must be function or framework`);
|
|
77
75
|
}
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
const userConfig = api.useConfigContext();
|
|
88
|
-
if (!((_a = userConfig == null ? void 0 : userConfig.server) == null ? void 0 : _a.enableFrameworkExt)) {
|
|
89
|
-
return next();
|
|
90
|
-
}
|
|
91
|
-
const app = new Koa();
|
|
92
|
-
app.use(async (ctx, next2) => {
|
|
93
|
-
await next2();
|
|
94
|
-
if (!ctx.body) {
|
|
95
|
-
if (ctx.res.statusCode === 404 && !ctx.response._explicitStatus) {
|
|
96
|
-
ctx.res.statusCode = 200;
|
|
76
|
+
app.use(router.routes());
|
|
77
|
+
if (((_userConfig_bff = userConfig.bff) === null || _userConfig_bff === void 0 ? void 0 : _userConfig_bff.enableHandleWeb) && render) {
|
|
78
|
+
app.use(async (ctx, next) => {
|
|
79
|
+
const html = await render(ctx.req, ctx.res);
|
|
80
|
+
if (html) {
|
|
81
|
+
ctx.body = html;
|
|
82
|
+
}
|
|
83
|
+
await next();
|
|
84
|
+
});
|
|
97
85
|
}
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
86
|
+
return (req, res) => {
|
|
87
|
+
return Promise.resolve(app.callback()(req, res));
|
|
88
|
+
};
|
|
89
|
+
},
|
|
90
|
+
prepareWebServer({ config }, next) {
|
|
91
|
+
var _userConfig_server;
|
|
92
|
+
const userConfig = api.useConfigContext();
|
|
93
|
+
if (!(userConfig === null || userConfig === void 0 ? void 0 : (_userConfig_server = userConfig.server) === null || _userConfig_server === void 0 ? void 0 : _userConfig_server.enableFrameworkExt)) {
|
|
94
|
+
return next();
|
|
95
|
+
}
|
|
96
|
+
const app = new Koa();
|
|
97
|
+
app.use(async (ctx, next2) => {
|
|
98
|
+
await next2();
|
|
99
|
+
if (!ctx.body) {
|
|
100
|
+
if (ctx.res.statusCode === 404 && !ctx.response._explicitStatus) {
|
|
101
|
+
ctx.res.statusCode = 200;
|
|
102
|
+
}
|
|
103
|
+
ctx.respond = false;
|
|
104
|
+
}
|
|
105
|
+
});
|
|
106
|
+
app.use(koaBody());
|
|
107
|
+
if (config) {
|
|
108
|
+
const { middleware } = config;
|
|
109
|
+
initMiddlewares(middleware, app);
|
|
113
110
|
}
|
|
114
|
-
|
|
115
|
-
|
|
111
|
+
return (ctx) => {
|
|
112
|
+
const { source: { req, res } } = ctx;
|
|
113
|
+
app.on("error", (err) => {
|
|
114
|
+
if (err) {
|
|
115
|
+
throw err;
|
|
116
|
+
}
|
|
117
|
+
});
|
|
118
|
+
return Promise.resolve(app.callback()(req, res));
|
|
119
|
+
};
|
|
120
|
+
}
|
|
116
121
|
};
|
|
117
122
|
}
|
|
118
|
-
}
|
|
119
|
-
});
|
|
120
|
-
export {
|
|
121
|
-
plugin_default as default
|
|
123
|
+
};
|
|
122
124
|
};
|
package/dist/esm-node/runtime.js
CHANGED
package/dist/esm-node/utils.js
CHANGED
|
@@ -1,16 +1,8 @@
|
|
|
1
|
-
import {
|
|
2
|
-
httpMethods,
|
|
3
|
-
isWithMetaHandler,
|
|
4
|
-
ResponseMetaType,
|
|
5
|
-
HttpMetadata
|
|
6
|
-
} from "@modern-js/bff-core";
|
|
1
|
+
import { httpMethods, isWithMetaHandler, ResponseMetaType, HttpMetadata } from "@modern-js/bff-core";
|
|
7
2
|
import { isSchemaHandler } from "@modern-js/bff-runtime";
|
|
8
3
|
import typeIs from "type-is";
|
|
9
4
|
const handleResponseMeta = (ctx, handler) => {
|
|
10
|
-
const responseMeta = Reflect.getMetadata(
|
|
11
|
-
HttpMetadata.Response,
|
|
12
|
-
handler
|
|
13
|
-
);
|
|
5
|
+
const responseMeta = Reflect.getMetadata(HttpMetadata.Response, handler);
|
|
14
6
|
if (Array.isArray(responseMeta)) {
|
|
15
7
|
for (const meta of responseMeta) {
|
|
16
8
|
const metaType = meta.type;
|
|
@@ -39,7 +31,7 @@ const handleResponseMeta = (ctx, handler) => {
|
|
|
39
31
|
}
|
|
40
32
|
}
|
|
41
33
|
};
|
|
42
|
-
const createRouteHandler = (handler) => {
|
|
34
|
+
export const createRouteHandler = (handler) => {
|
|
43
35
|
const apiHandler = async (ctx) => {
|
|
44
36
|
const input = await getInputFromRequest(ctx);
|
|
45
37
|
if (isWithMetaHandler(handler)) {
|
|
@@ -82,13 +74,10 @@ const createRouteHandler = (handler) => {
|
|
|
82
74
|
}
|
|
83
75
|
}
|
|
84
76
|
};
|
|
85
|
-
Object.defineProperties(
|
|
86
|
-
apiHandler,
|
|
87
|
-
Object.getOwnPropertyDescriptors(handler)
|
|
88
|
-
);
|
|
77
|
+
Object.defineProperties(apiHandler, Object.getOwnPropertyDescriptors(handler));
|
|
89
78
|
return apiHandler;
|
|
90
79
|
};
|
|
91
|
-
const isNormalMethod = (httpMethod) => httpMethods.includes(httpMethod);
|
|
80
|
+
export const isNormalMethod = (httpMethod) => httpMethods.includes(httpMethod);
|
|
92
81
|
const getInputFromRequest = async (ctx) => {
|
|
93
82
|
const draft = {
|
|
94
83
|
params: ctx.params,
|
|
@@ -96,18 +85,20 @@ const getInputFromRequest = async (ctx) => {
|
|
|
96
85
|
headers: ctx.headers,
|
|
97
86
|
cookies: ctx.headers.cookie
|
|
98
87
|
};
|
|
99
|
-
if (typeIs.is(ctx.request.type, [
|
|
88
|
+
if (typeIs.is(ctx.request.type, [
|
|
89
|
+
"application/json"
|
|
90
|
+
])) {
|
|
100
91
|
draft.data = ctx.request.body;
|
|
101
|
-
} else if (typeIs.is(ctx.request.type, [
|
|
92
|
+
} else if (typeIs.is(ctx.request.type, [
|
|
93
|
+
"multipart/form-data"
|
|
94
|
+
])) {
|
|
102
95
|
draft.formData = ctx.request.files;
|
|
103
|
-
} else if (typeIs.is(ctx.request.type, [
|
|
96
|
+
} else if (typeIs.is(ctx.request.type, [
|
|
97
|
+
"application/x-www-form-urlencoded"
|
|
98
|
+
])) {
|
|
104
99
|
draft.formUrlencoded = ctx.request.body;
|
|
105
100
|
} else {
|
|
106
101
|
draft.body = ctx.request.body;
|
|
107
102
|
}
|
|
108
103
|
return draft;
|
|
109
104
|
};
|
|
110
|
-
export {
|
|
111
|
-
createRouteHandler,
|
|
112
|
-
isNormalMethod
|
|
113
|
-
};
|
package/package.json
CHANGED
|
@@ -3,7 +3,11 @@
|
|
|
3
3
|
"description": "A Progressive React Framework for modern web development.",
|
|
4
4
|
"homepage": "https://modernjs.dev",
|
|
5
5
|
"bugs": "https://github.com/web-infra-dev/modern.js/issues",
|
|
6
|
-
"repository":
|
|
6
|
+
"repository": {
|
|
7
|
+
"type": "git",
|
|
8
|
+
"url": "https://github.com/web-infra-dev/modern.js",
|
|
9
|
+
"directory": "packages/server/plugin-koa"
|
|
10
|
+
},
|
|
7
11
|
"license": "MIT",
|
|
8
12
|
"keywords": [
|
|
9
13
|
"react",
|
|
@@ -11,7 +15,7 @@
|
|
|
11
15
|
"modern",
|
|
12
16
|
"modern.js"
|
|
13
17
|
],
|
|
14
|
-
"version": "2.
|
|
18
|
+
"version": "2.17.0",
|
|
15
19
|
"jsnext:source": "./src/index.ts",
|
|
16
20
|
"types": "./dist/types/cli/index.d.ts",
|
|
17
21
|
"main": "./dist/cjs/cli/index.js",
|
|
@@ -63,10 +67,10 @@
|
|
|
63
67
|
"koa-body": "^4.2.0",
|
|
64
68
|
"koa-router": "^10.0.0",
|
|
65
69
|
"type-is": "^1.6.18",
|
|
66
|
-
"@modern-js/bff-core": "2.
|
|
67
|
-
"@modern-js/bff-runtime": "2.
|
|
68
|
-
"@modern-js/utils": "2.
|
|
69
|
-
"@modern-js/types": "2.
|
|
70
|
+
"@modern-js/bff-core": "2.17.0",
|
|
71
|
+
"@modern-js/bff-runtime": "2.17.0",
|
|
72
|
+
"@modern-js/utils": "2.17.0",
|
|
73
|
+
"@modern-js/types": "2.17.0"
|
|
70
74
|
},
|
|
71
75
|
"devDependencies": {
|
|
72
76
|
"@types/jest": "^29",
|
|
@@ -80,11 +84,11 @@
|
|
|
80
84
|
"supertest": "^6.1.6",
|
|
81
85
|
"typescript": "^4",
|
|
82
86
|
"zod": "^3.17.3",
|
|
83
|
-
"@modern-js/core": "2.
|
|
84
|
-
"@modern-js/server-core": "2.
|
|
85
|
-
"@modern-js/app-tools": "2.
|
|
86
|
-
"@scripts/build": "2.
|
|
87
|
-
"@scripts/jest-config": "2.
|
|
87
|
+
"@modern-js/core": "2.17.0",
|
|
88
|
+
"@modern-js/server-core": "2.17.0",
|
|
89
|
+
"@modern-js/app-tools": "2.17.0",
|
|
90
|
+
"@scripts/build": "2.17.0",
|
|
91
|
+
"@scripts/jest-config": "2.17.0"
|
|
88
92
|
},
|
|
89
93
|
"peerDependencies": {
|
|
90
94
|
"koa": "^2.13.3"
|