@modern-js/plugin-koa 2.4.1-beta.0 → 2.6.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 +31 -3
- package/dist/{js/node → cjs}/cli/index.js +4 -0
- package/dist/{js/node → cjs}/index.js +4 -0
- package/dist/{js/node → cjs}/plugin.js +59 -66
- package/dist/{js/node → cjs}/utils.js +12 -28
- package/dist/{js/treeshaking → esm}/plugin.js +36 -2
- package/dist/esm-node/plugin.js +122 -0
- package/dist/{js/modern → esm-node}/utils.js +8 -28
- package/package.json +17 -18
- package/dist/js/modern/plugin.js +0 -133
- /package/dist/{js/node → cjs}/context.js +0 -0
- /package/dist/{js/node → cjs}/registerRoutes.js +0 -0
- /package/dist/{js/node → cjs}/runtime.js +0 -0
- /package/dist/{js/treeshaking → esm}/cli/index.js +0 -0
- /package/dist/{js/treeshaking → esm}/context.js +0 -0
- /package/dist/{js/treeshaking → esm}/index.js +0 -0
- /package/dist/{js/treeshaking → esm}/registerRoutes.js +0 -0
- /package/dist/{js/treeshaking → esm}/runtime.js +0 -0
- /package/dist/{js/treeshaking → esm}/utils.js +0 -0
- /package/dist/{js/modern → esm-node}/cli/index.js +0 -0
- /package/dist/{js/modern → esm-node}/context.js +0 -0
- /package/dist/{js/modern → esm-node}/index.js +0 -0
- /package/dist/{js/modern → esm-node}/registerRoutes.js +0 -0
- /package/dist/{js/modern → esm-node}/runtime.js +0 -0
package/CHANGELOG.md
CHANGED
|
@@ -1,12 +1,40 @@
|
|
|
1
1
|
# @modern-js/plugin-koa
|
|
2
2
|
|
|
3
|
-
## 2.
|
|
3
|
+
## 2.6.0
|
|
4
4
|
|
|
5
5
|
### Patch Changes
|
|
6
6
|
|
|
7
|
+
- Updated dependencies [e1f799e]
|
|
8
|
+
- Updated dependencies [7915ab3]
|
|
9
|
+
- Updated dependencies [49fa0b1]
|
|
10
|
+
- Updated dependencies [0fe658a]
|
|
11
|
+
- Updated dependencies [62930b9]
|
|
12
|
+
- @modern-js/utils@2.6.0
|
|
13
|
+
- @modern-js/types@2.6.0
|
|
14
|
+
- @modern-js/bff-core@2.6.0
|
|
15
|
+
- @modern-js/bff-runtime@2.6.0
|
|
16
|
+
|
|
17
|
+
## 2.5.0
|
|
18
|
+
|
|
19
|
+
### Patch Changes
|
|
20
|
+
|
|
21
|
+
- 89ca6cc: refactor: merge build-config into scripts/build
|
|
22
|
+
|
|
23
|
+
refactor: 把 build-config 合并进 scripts/build
|
|
24
|
+
|
|
25
|
+
- 6fca567: feat: support bff handle complete server, include page render
|
|
26
|
+
feat: 支持 bff 处理整个服务,包括页面渲染
|
|
27
|
+
- 30614fa: chore: modify package.json entry fields and build config
|
|
28
|
+
chore: 更改 package.json entry 字段以及构建配置
|
|
29
|
+
- Updated dependencies [89ca6cc]
|
|
30
|
+
- Updated dependencies [7cb8bb4]
|
|
31
|
+
- Updated dependencies [30614fa]
|
|
32
|
+
- Updated dependencies [1b0ce87]
|
|
7
33
|
- Updated dependencies [11c053b]
|
|
8
|
-
- @modern-js/
|
|
9
|
-
- @modern-js/bff-
|
|
34
|
+
- @modern-js/bff-core@2.5.0
|
|
35
|
+
- @modern-js/bff-runtime@2.5.0
|
|
36
|
+
- @modern-js/types@2.5.0
|
|
37
|
+
- @modern-js/utils@2.5.0
|
|
10
38
|
|
|
11
39
|
## 2.4.0
|
|
12
40
|
|
|
@@ -17,6 +17,10 @@ var __copyProps = (to, from, except, desc) => {
|
|
|
17
17
|
return to;
|
|
18
18
|
};
|
|
19
19
|
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
|
|
20
|
+
// If the importer is in node compatibility mode or this is not an ESM
|
|
21
|
+
// file that has been converted to a CommonJS file using a Babel-
|
|
22
|
+
// compatible transform (i.e. "__esModule" has not been set), then set
|
|
23
|
+
// "default" to the CommonJS "module.exports" for node compatibility.
|
|
20
24
|
isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
|
|
21
25
|
mod
|
|
22
26
|
));
|
|
@@ -18,6 +18,10 @@ var __copyProps = (to, from, except, desc) => {
|
|
|
18
18
|
};
|
|
19
19
|
var __reExport = (target, mod, secondTarget) => (__copyProps(target, mod, "default"), secondTarget && __copyProps(secondTarget, mod, "default"));
|
|
20
20
|
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
|
|
21
|
+
// If the importer is in node compatibility mode or this is not an ESM
|
|
22
|
+
// file that has been converted to a CommonJS file using a Babel-
|
|
23
|
+
// compatible transform (i.e. "__esModule" has not been set), then set
|
|
24
|
+
// "default" to the CommonJS "module.exports" for node compatibility.
|
|
21
25
|
isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
|
|
22
26
|
mod
|
|
23
27
|
));
|
|
@@ -17,30 +17,14 @@ var __copyProps = (to, from, except, desc) => {
|
|
|
17
17
|
return to;
|
|
18
18
|
};
|
|
19
19
|
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
|
|
20
|
+
// If the importer is in node compatibility mode or this is not an ESM
|
|
21
|
+
// file that has been converted to a CommonJS file using a Babel-
|
|
22
|
+
// compatible transform (i.e. "__esModule" has not been set), then set
|
|
23
|
+
// "default" to the CommonJS "module.exports" for node compatibility.
|
|
20
24
|
isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
|
|
21
25
|
mod
|
|
22
26
|
));
|
|
23
27
|
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
24
|
-
var __async = (__this, __arguments, generator) => {
|
|
25
|
-
return new Promise((resolve, reject) => {
|
|
26
|
-
var fulfilled = (value) => {
|
|
27
|
-
try {
|
|
28
|
-
step(generator.next(value));
|
|
29
|
-
} catch (e) {
|
|
30
|
-
reject(e);
|
|
31
|
-
}
|
|
32
|
-
};
|
|
33
|
-
var rejected = (value) => {
|
|
34
|
-
try {
|
|
35
|
-
step(generator.throw(value));
|
|
36
|
-
} catch (e) {
|
|
37
|
-
reject(e);
|
|
38
|
-
}
|
|
39
|
-
};
|
|
40
|
-
var step = (x) => x.done ? resolve(x.value) : Promise.resolve(x.value).then(fulfilled, rejected);
|
|
41
|
-
step((generator = generator.apply(__this, __arguments)).next());
|
|
42
|
-
});
|
|
43
|
-
};
|
|
44
28
|
var plugin_exports = {};
|
|
45
29
|
__export(plugin_exports, {
|
|
46
30
|
default: () => plugin_default
|
|
@@ -53,17 +37,17 @@ var import_koa_body = __toESM(require("koa-body"));
|
|
|
53
37
|
var import_utils = require("@modern-js/utils");
|
|
54
38
|
var import_context = require("./context");
|
|
55
39
|
var import_registerRoutes = __toESM(require("./registerRoutes"));
|
|
56
|
-
const findAppModule = (apiDir) =>
|
|
40
|
+
const findAppModule = async (apiDir) => {
|
|
57
41
|
const exts = [".ts", ".js"];
|
|
58
42
|
const paths = exts.map((ext) => path.join(apiDir, `app${ext}`));
|
|
59
43
|
for (const filename of paths) {
|
|
60
|
-
if (
|
|
44
|
+
if (await import_utils.fs.pathExists(filename)) {
|
|
61
45
|
delete require.cache[filename];
|
|
62
46
|
return (0, import_utils.compatRequire)(filename);
|
|
63
47
|
}
|
|
64
48
|
}
|
|
65
49
|
return null;
|
|
66
|
-
}
|
|
50
|
+
};
|
|
67
51
|
const initMiddlewares = (middleware, app) => {
|
|
68
52
|
middleware.forEach((middlewareItem) => {
|
|
69
53
|
const middlewareFunc = typeof middlewareItem === "string" ? (0, import_utils.compatRequire)(middlewareItem) : middlewareItem;
|
|
@@ -75,68 +59,77 @@ var plugin_default = () => ({
|
|
|
75
59
|
pre: ["@modern-js/plugin-bff"],
|
|
76
60
|
post: ["@modern-js/plugin-server"],
|
|
77
61
|
setup: (api) => ({
|
|
78
|
-
prepareApiServer(
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
app.use(
|
|
91
|
-
(0, import_koa_body.default)({
|
|
92
|
-
multipart: true
|
|
93
|
-
})
|
|
94
|
-
);
|
|
95
|
-
}
|
|
96
|
-
if (config) {
|
|
97
|
-
const { middleware } = config;
|
|
98
|
-
initMiddlewares(middleware, app);
|
|
99
|
-
}
|
|
100
|
-
app.use(import_context.run);
|
|
101
|
-
(0, import_registerRoutes.default)(router, apiHandlerInfos);
|
|
102
|
-
} else if (mode === "function") {
|
|
62
|
+
async prepareApiServer({ pwd, config, render }) {
|
|
63
|
+
var _a;
|
|
64
|
+
let app;
|
|
65
|
+
const router = new import_koa_router.default();
|
|
66
|
+
const apiDir = path.join(pwd, "./api");
|
|
67
|
+
const appContext = api.useAppContext();
|
|
68
|
+
const apiHandlerInfos = appContext.apiHandlerInfos;
|
|
69
|
+
const mode = appContext.apiMode;
|
|
70
|
+
const userConfig = api.useConfigContext();
|
|
71
|
+
if (mode === "framework") {
|
|
72
|
+
app = await findAppModule(apiDir);
|
|
73
|
+
if (!(app instanceof import_koa.default)) {
|
|
103
74
|
app = new import_koa.default();
|
|
104
75
|
app.use(
|
|
105
76
|
(0, import_koa_body.default)({
|
|
106
77
|
multipart: true
|
|
107
78
|
})
|
|
108
79
|
);
|
|
109
|
-
if (config) {
|
|
110
|
-
const { middleware } = config;
|
|
111
|
-
initMiddlewares(middleware, app);
|
|
112
|
-
}
|
|
113
|
-
app.use(import_context.run);
|
|
114
|
-
(0, import_registerRoutes.default)(router, apiHandlerInfos);
|
|
115
|
-
} else {
|
|
116
|
-
throw new Error(`mode must be function or framework`);
|
|
117
80
|
}
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
}
|
|
122
|
-
|
|
81
|
+
if (config) {
|
|
82
|
+
const { middleware } = config;
|
|
83
|
+
initMiddlewares(middleware, app);
|
|
84
|
+
}
|
|
85
|
+
app.use(import_context.run);
|
|
86
|
+
(0, import_registerRoutes.default)(router, apiHandlerInfos);
|
|
87
|
+
} else if (mode === "function") {
|
|
88
|
+
app = new import_koa.default();
|
|
89
|
+
app.use(
|
|
90
|
+
(0, import_koa_body.default)({
|
|
91
|
+
multipart: true
|
|
92
|
+
})
|
|
93
|
+
);
|
|
94
|
+
if (config) {
|
|
95
|
+
const { middleware } = config;
|
|
96
|
+
initMiddlewares(middleware, app);
|
|
97
|
+
}
|
|
98
|
+
app.use(import_context.run);
|
|
99
|
+
(0, import_registerRoutes.default)(router, apiHandlerInfos);
|
|
100
|
+
} else {
|
|
101
|
+
throw new Error(`mode must be function or framework`);
|
|
102
|
+
}
|
|
103
|
+
app.use(router.routes());
|
|
104
|
+
if (((_a = userConfig.bff) == null ? void 0 : _a.enableHandleWeb) && render) {
|
|
105
|
+
app.use(async (ctx, next) => {
|
|
106
|
+
const html = await render(ctx.req, ctx.res);
|
|
107
|
+
if (html) {
|
|
108
|
+
ctx.body = html;
|
|
109
|
+
}
|
|
110
|
+
await next();
|
|
111
|
+
});
|
|
112
|
+
}
|
|
113
|
+
return (req, res) => {
|
|
114
|
+
return Promise.resolve(app.callback()(req, res));
|
|
115
|
+
};
|
|
123
116
|
},
|
|
124
117
|
prepareWebServer({ config }, next) {
|
|
125
|
-
var
|
|
118
|
+
var _a;
|
|
126
119
|
const userConfig = api.useConfigContext();
|
|
127
|
-
if (!((
|
|
120
|
+
if (!((_a = userConfig == null ? void 0 : userConfig.server) == null ? void 0 : _a.enableFrameworkExt)) {
|
|
128
121
|
return next();
|
|
129
122
|
}
|
|
130
123
|
const app = new import_koa.default();
|
|
131
|
-
app.use((ctx, next2) =>
|
|
132
|
-
|
|
124
|
+
app.use(async (ctx, next2) => {
|
|
125
|
+
await next2();
|
|
133
126
|
if (!ctx.body) {
|
|
134
127
|
if (ctx.res.statusCode === 404 && !ctx.response._explicitStatus) {
|
|
135
128
|
ctx.res.statusCode = 200;
|
|
136
129
|
}
|
|
137
130
|
ctx.respond = false;
|
|
138
131
|
}
|
|
139
|
-
})
|
|
132
|
+
});
|
|
140
133
|
app.use((0, import_koa_body.default)());
|
|
141
134
|
if (config) {
|
|
142
135
|
const { middleware } = config;
|
|
@@ -17,30 +17,14 @@ var __copyProps = (to, from, except, desc) => {
|
|
|
17
17
|
return to;
|
|
18
18
|
};
|
|
19
19
|
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
|
|
20
|
+
// If the importer is in node compatibility mode or this is not an ESM
|
|
21
|
+
// file that has been converted to a CommonJS file using a Babel-
|
|
22
|
+
// compatible transform (i.e. "__esModule" has not been set), then set
|
|
23
|
+
// "default" to the CommonJS "module.exports" for node compatibility.
|
|
20
24
|
isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
|
|
21
25
|
mod
|
|
22
26
|
));
|
|
23
27
|
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
24
|
-
var __async = (__this, __arguments, generator) => {
|
|
25
|
-
return new Promise((resolve, reject) => {
|
|
26
|
-
var fulfilled = (value) => {
|
|
27
|
-
try {
|
|
28
|
-
step(generator.next(value));
|
|
29
|
-
} catch (e) {
|
|
30
|
-
reject(e);
|
|
31
|
-
}
|
|
32
|
-
};
|
|
33
|
-
var rejected = (value) => {
|
|
34
|
-
try {
|
|
35
|
-
step(generator.throw(value));
|
|
36
|
-
} catch (e) {
|
|
37
|
-
reject(e);
|
|
38
|
-
}
|
|
39
|
-
};
|
|
40
|
-
var step = (x) => x.done ? resolve(x.value) : Promise.resolve(x.value).then(fulfilled, rejected);
|
|
41
|
-
step((generator = generator.apply(__this, __arguments)).next());
|
|
42
|
-
});
|
|
43
|
-
};
|
|
44
28
|
var utils_exports = {};
|
|
45
29
|
__export(utils_exports, {
|
|
46
30
|
createRouteHandler: () => createRouteHandler,
|
|
@@ -84,12 +68,12 @@ const handleResponseMeta = (ctx, handler) => {
|
|
|
84
68
|
}
|
|
85
69
|
};
|
|
86
70
|
const createRouteHandler = (handler) => {
|
|
87
|
-
const apiHandler = (ctx) =>
|
|
88
|
-
const input =
|
|
71
|
+
const apiHandler = async (ctx) => {
|
|
72
|
+
const input = await getInputFromRequest(ctx);
|
|
89
73
|
if ((0, import_bff_core.isWithMetaHandler)(handler)) {
|
|
90
74
|
try {
|
|
91
75
|
handleResponseMeta(ctx, handler);
|
|
92
|
-
const body =
|
|
76
|
+
const body = await handler(input);
|
|
93
77
|
if (typeof body !== "undefined") {
|
|
94
78
|
ctx.body = body;
|
|
95
79
|
}
|
|
@@ -107,7 +91,7 @@ const createRouteHandler = (handler) => {
|
|
|
107
91
|
}
|
|
108
92
|
}
|
|
109
93
|
} else if ((0, import_bff_runtime.isSchemaHandler)(handler)) {
|
|
110
|
-
const result =
|
|
94
|
+
const result = await handler(input);
|
|
111
95
|
if (result.type !== "HandleSuccess") {
|
|
112
96
|
if (result.type === "InputValidationError") {
|
|
113
97
|
ctx.status = 400;
|
|
@@ -120,12 +104,12 @@ const createRouteHandler = (handler) => {
|
|
|
120
104
|
}
|
|
121
105
|
} else {
|
|
122
106
|
const args = Object.values(input.params).concat(input);
|
|
123
|
-
const body =
|
|
107
|
+
const body = await handler(...args);
|
|
124
108
|
if (typeof body !== "undefined") {
|
|
125
109
|
ctx.body = body;
|
|
126
110
|
}
|
|
127
111
|
}
|
|
128
|
-
}
|
|
112
|
+
};
|
|
129
113
|
Object.defineProperties(
|
|
130
114
|
apiHandler,
|
|
131
115
|
Object.getOwnPropertyDescriptors(handler)
|
|
@@ -133,7 +117,7 @@ const createRouteHandler = (handler) => {
|
|
|
133
117
|
return apiHandler;
|
|
134
118
|
};
|
|
135
119
|
const isNormalMethod = (httpMethod) => import_bff_core.httpMethods.includes(httpMethod);
|
|
136
|
-
const getInputFromRequest = (ctx) =>
|
|
120
|
+
const getInputFromRequest = async (ctx) => {
|
|
137
121
|
const draft = {
|
|
138
122
|
params: ctx.params,
|
|
139
123
|
query: ctx.query,
|
|
@@ -150,7 +134,7 @@ const getInputFromRequest = (ctx) => __async(void 0, null, function* () {
|
|
|
150
134
|
draft.body = ctx.request.body;
|
|
151
135
|
}
|
|
152
136
|
return draft;
|
|
153
|
-
}
|
|
137
|
+
};
|
|
154
138
|
// Annotate the CommonJS export names for ESM import in node:
|
|
155
139
|
0 && (module.exports = {
|
|
156
140
|
createRouteHandler,
|
|
@@ -241,9 +241,9 @@ var plugin_default = function() {
|
|
|
241
241
|
setup: function(api) {
|
|
242
242
|
return {
|
|
243
243
|
prepareApiServer: function prepareApiServer(param) {
|
|
244
|
-
var pwd = param.pwd, config = param.config;
|
|
244
|
+
var pwd = param.pwd, config = param.config, render = param.render;
|
|
245
245
|
return _asyncToGenerator(function() {
|
|
246
|
-
var app, router, apiDir, appContext, apiHandlerInfos, mode, middleware, middleware1;
|
|
246
|
+
var _userConfig_bff, app, router, apiDir, appContext, apiHandlerInfos, mode, userConfig, middleware, middleware1;
|
|
247
247
|
return __generator(this, function(_state) {
|
|
248
248
|
switch(_state.label){
|
|
249
249
|
case 0:
|
|
@@ -252,6 +252,7 @@ var plugin_default = function() {
|
|
|
252
252
|
appContext = api.useAppContext();
|
|
253
253
|
apiHandlerInfos = appContext.apiHandlerInfos;
|
|
254
254
|
mode = appContext.apiMode;
|
|
255
|
+
userConfig = api.useConfigContext();
|
|
255
256
|
if (!(mode === "framework")) return [
|
|
256
257
|
3,
|
|
257
258
|
2
|
|
@@ -296,6 +297,39 @@ var plugin_default = function() {
|
|
|
296
297
|
_state.label = 3;
|
|
297
298
|
case 3:
|
|
298
299
|
app.use(router.routes());
|
|
300
|
+
if (((_userConfig_bff = userConfig.bff) === null || _userConfig_bff === void 0 ? void 0 : _userConfig_bff.enableHandleWeb) && render) {
|
|
301
|
+
app.use(function() {
|
|
302
|
+
var _ref = _asyncToGenerator(function(ctx, next) {
|
|
303
|
+
var html;
|
|
304
|
+
return __generator(this, function(_state) {
|
|
305
|
+
switch(_state.label){
|
|
306
|
+
case 0:
|
|
307
|
+
return [
|
|
308
|
+
4,
|
|
309
|
+
render(ctx.req, ctx.res)
|
|
310
|
+
];
|
|
311
|
+
case 1:
|
|
312
|
+
html = _state.sent();
|
|
313
|
+
if (html) {
|
|
314
|
+
ctx.body = html;
|
|
315
|
+
}
|
|
316
|
+
return [
|
|
317
|
+
4,
|
|
318
|
+
next()
|
|
319
|
+
];
|
|
320
|
+
case 2:
|
|
321
|
+
_state.sent();
|
|
322
|
+
return [
|
|
323
|
+
2
|
|
324
|
+
];
|
|
325
|
+
}
|
|
326
|
+
});
|
|
327
|
+
});
|
|
328
|
+
return function(ctx, next) {
|
|
329
|
+
return _ref.apply(this, arguments);
|
|
330
|
+
};
|
|
331
|
+
}());
|
|
332
|
+
}
|
|
299
333
|
return [
|
|
300
334
|
2,
|
|
301
335
|
function(req, res) {
|
|
@@ -0,0 +1,122 @@
|
|
|
1
|
+
import * as path from "path";
|
|
2
|
+
import Koa from "koa";
|
|
3
|
+
import Router from "koa-router";
|
|
4
|
+
import koaBody from "koa-body";
|
|
5
|
+
import { fs, compatRequire } from "@modern-js/utils";
|
|
6
|
+
import { run } from "./context";
|
|
7
|
+
import registerRoutes from "./registerRoutes";
|
|
8
|
+
const findAppModule = async (apiDir) => {
|
|
9
|
+
const exts = [".ts", ".js"];
|
|
10
|
+
const paths = exts.map((ext) => path.join(apiDir, `app${ext}`));
|
|
11
|
+
for (const filename of paths) {
|
|
12
|
+
if (await fs.pathExists(filename)) {
|
|
13
|
+
delete require.cache[filename];
|
|
14
|
+
return compatRequire(filename);
|
|
15
|
+
}
|
|
16
|
+
}
|
|
17
|
+
return null;
|
|
18
|
+
};
|
|
19
|
+
const initMiddlewares = (middleware, app) => {
|
|
20
|
+
middleware.forEach((middlewareItem) => {
|
|
21
|
+
const middlewareFunc = typeof middlewareItem === "string" ? compatRequire(middlewareItem) : middlewareItem;
|
|
22
|
+
app.use(middlewareFunc);
|
|
23
|
+
});
|
|
24
|
+
};
|
|
25
|
+
var plugin_default = () => ({
|
|
26
|
+
name: "@modern-js/plugin-koa",
|
|
27
|
+
pre: ["@modern-js/plugin-bff"],
|
|
28
|
+
post: ["@modern-js/plugin-server"],
|
|
29
|
+
setup: (api) => ({
|
|
30
|
+
async prepareApiServer({ pwd, config, render }) {
|
|
31
|
+
var _a;
|
|
32
|
+
let app;
|
|
33
|
+
const router = new Router();
|
|
34
|
+
const apiDir = path.join(pwd, "./api");
|
|
35
|
+
const appContext = api.useAppContext();
|
|
36
|
+
const apiHandlerInfos = appContext.apiHandlerInfos;
|
|
37
|
+
const mode = appContext.apiMode;
|
|
38
|
+
const userConfig = api.useConfigContext();
|
|
39
|
+
if (mode === "framework") {
|
|
40
|
+
app = await findAppModule(apiDir);
|
|
41
|
+
if (!(app instanceof Koa)) {
|
|
42
|
+
app = new Koa();
|
|
43
|
+
app.use(
|
|
44
|
+
koaBody({
|
|
45
|
+
multipart: true
|
|
46
|
+
})
|
|
47
|
+
);
|
|
48
|
+
}
|
|
49
|
+
if (config) {
|
|
50
|
+
const { middleware } = config;
|
|
51
|
+
initMiddlewares(middleware, app);
|
|
52
|
+
}
|
|
53
|
+
app.use(run);
|
|
54
|
+
registerRoutes(router, apiHandlerInfos);
|
|
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;
|
|
77
|
+
}
|
|
78
|
+
await next();
|
|
79
|
+
});
|
|
80
|
+
}
|
|
81
|
+
return (req, res) => {
|
|
82
|
+
return Promise.resolve(app.callback()(req, res));
|
|
83
|
+
};
|
|
84
|
+
},
|
|
85
|
+
prepareWebServer({ config }, next) {
|
|
86
|
+
var _a;
|
|
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;
|
|
97
|
+
}
|
|
98
|
+
ctx.respond = false;
|
|
99
|
+
}
|
|
100
|
+
});
|
|
101
|
+
app.use(koaBody());
|
|
102
|
+
if (config) {
|
|
103
|
+
const { middleware } = config;
|
|
104
|
+
initMiddlewares(middleware, app);
|
|
105
|
+
}
|
|
106
|
+
return (ctx) => {
|
|
107
|
+
const {
|
|
108
|
+
source: { req, res }
|
|
109
|
+
} = ctx;
|
|
110
|
+
app.on("error", (err) => {
|
|
111
|
+
if (err) {
|
|
112
|
+
throw err;
|
|
113
|
+
}
|
|
114
|
+
});
|
|
115
|
+
return Promise.resolve(app.callback()(req, res));
|
|
116
|
+
};
|
|
117
|
+
}
|
|
118
|
+
})
|
|
119
|
+
});
|
|
120
|
+
export {
|
|
121
|
+
plugin_default as default
|
|
122
|
+
};
|
|
@@ -1,23 +1,3 @@
|
|
|
1
|
-
var __async = (__this, __arguments, generator) => {
|
|
2
|
-
return new Promise((resolve, reject) => {
|
|
3
|
-
var fulfilled = (value) => {
|
|
4
|
-
try {
|
|
5
|
-
step(generator.next(value));
|
|
6
|
-
} catch (e) {
|
|
7
|
-
reject(e);
|
|
8
|
-
}
|
|
9
|
-
};
|
|
10
|
-
var rejected = (value) => {
|
|
11
|
-
try {
|
|
12
|
-
step(generator.throw(value));
|
|
13
|
-
} catch (e) {
|
|
14
|
-
reject(e);
|
|
15
|
-
}
|
|
16
|
-
};
|
|
17
|
-
var step = (x) => x.done ? resolve(x.value) : Promise.resolve(x.value).then(fulfilled, rejected);
|
|
18
|
-
step((generator = generator.apply(__this, __arguments)).next());
|
|
19
|
-
});
|
|
20
|
-
};
|
|
21
1
|
import {
|
|
22
2
|
httpMethods,
|
|
23
3
|
isWithMetaHandler,
|
|
@@ -60,12 +40,12 @@ const handleResponseMeta = (ctx, handler) => {
|
|
|
60
40
|
}
|
|
61
41
|
};
|
|
62
42
|
const createRouteHandler = (handler) => {
|
|
63
|
-
const apiHandler = (ctx) =>
|
|
64
|
-
const input =
|
|
43
|
+
const apiHandler = async (ctx) => {
|
|
44
|
+
const input = await getInputFromRequest(ctx);
|
|
65
45
|
if (isWithMetaHandler(handler)) {
|
|
66
46
|
try {
|
|
67
47
|
handleResponseMeta(ctx, handler);
|
|
68
|
-
const body =
|
|
48
|
+
const body = await handler(input);
|
|
69
49
|
if (typeof body !== "undefined") {
|
|
70
50
|
ctx.body = body;
|
|
71
51
|
}
|
|
@@ -83,7 +63,7 @@ const createRouteHandler = (handler) => {
|
|
|
83
63
|
}
|
|
84
64
|
}
|
|
85
65
|
} else if (isSchemaHandler(handler)) {
|
|
86
|
-
const result =
|
|
66
|
+
const result = await handler(input);
|
|
87
67
|
if (result.type !== "HandleSuccess") {
|
|
88
68
|
if (result.type === "InputValidationError") {
|
|
89
69
|
ctx.status = 400;
|
|
@@ -96,12 +76,12 @@ const createRouteHandler = (handler) => {
|
|
|
96
76
|
}
|
|
97
77
|
} else {
|
|
98
78
|
const args = Object.values(input.params).concat(input);
|
|
99
|
-
const body =
|
|
79
|
+
const body = await handler(...args);
|
|
100
80
|
if (typeof body !== "undefined") {
|
|
101
81
|
ctx.body = body;
|
|
102
82
|
}
|
|
103
83
|
}
|
|
104
|
-
}
|
|
84
|
+
};
|
|
105
85
|
Object.defineProperties(
|
|
106
86
|
apiHandler,
|
|
107
87
|
Object.getOwnPropertyDescriptors(handler)
|
|
@@ -109,7 +89,7 @@ const createRouteHandler = (handler) => {
|
|
|
109
89
|
return apiHandler;
|
|
110
90
|
};
|
|
111
91
|
const isNormalMethod = (httpMethod) => httpMethods.includes(httpMethod);
|
|
112
|
-
const getInputFromRequest = (ctx) =>
|
|
92
|
+
const getInputFromRequest = async (ctx) => {
|
|
113
93
|
const draft = {
|
|
114
94
|
params: ctx.params,
|
|
115
95
|
query: ctx.query,
|
|
@@ -126,7 +106,7 @@ const getInputFromRequest = (ctx) => __async(void 0, null, function* () {
|
|
|
126
106
|
draft.body = ctx.request.body;
|
|
127
107
|
}
|
|
128
108
|
return draft;
|
|
129
|
-
}
|
|
109
|
+
};
|
|
130
110
|
export {
|
|
131
111
|
createRouteHandler,
|
|
132
112
|
isNormalMethod
|
package/package.json
CHANGED
|
@@ -11,29 +11,28 @@
|
|
|
11
11
|
"modern",
|
|
12
12
|
"modern.js"
|
|
13
13
|
],
|
|
14
|
-
"version": "2.
|
|
14
|
+
"version": "2.6.0",
|
|
15
15
|
"jsnext:source": "./src/index.ts",
|
|
16
16
|
"types": "./dist/types/cli/index.d.ts",
|
|
17
|
-
"main": "./dist/
|
|
18
|
-
"module": "./dist/
|
|
19
|
-
"jsnext:modern": "./dist/js/modern/cli/index.js",
|
|
17
|
+
"main": "./dist/cjs/cli/index.js",
|
|
18
|
+
"module": "./dist/esm/cli/index.js",
|
|
20
19
|
"exports": {
|
|
21
20
|
"./types": "./types.d.ts",
|
|
22
21
|
".": {
|
|
23
22
|
"jsnext:source": "./src/cli/index.ts",
|
|
24
|
-
"default": "./dist/
|
|
23
|
+
"default": "./dist/cjs/cli/index.js"
|
|
25
24
|
},
|
|
26
25
|
"./cli": {
|
|
27
26
|
"jsnext:source": "./src/cli/index.ts",
|
|
28
|
-
"default": "./dist/
|
|
27
|
+
"default": "./dist/cjs/cli/index.js"
|
|
29
28
|
},
|
|
30
29
|
"./server": {
|
|
31
30
|
"node": {
|
|
32
31
|
"jsnext:source": "./src/index.ts",
|
|
33
|
-
"import": "./dist/
|
|
34
|
-
"require": "./dist/
|
|
32
|
+
"import": "./dist/esm-node/index.js",
|
|
33
|
+
"require": "./dist/cjs/index.js"
|
|
35
34
|
},
|
|
36
|
-
"default": "./dist/
|
|
35
|
+
"default": "./dist/esm/index.js"
|
|
37
36
|
}
|
|
38
37
|
},
|
|
39
38
|
"typesVersions": {
|
|
@@ -54,10 +53,10 @@
|
|
|
54
53
|
"koa-body": "^4.2.0",
|
|
55
54
|
"koa-router": "^10.0.0",
|
|
56
55
|
"type-is": "^1.6.18",
|
|
57
|
-
"@modern-js/bff-core": "2.
|
|
58
|
-
"@modern-js/
|
|
59
|
-
"@modern-js/
|
|
60
|
-
"@modern-js/types": "2.
|
|
56
|
+
"@modern-js/bff-core": "2.6.0",
|
|
57
|
+
"@modern-js/bff-runtime": "2.6.0",
|
|
58
|
+
"@modern-js/utils": "2.6.0",
|
|
59
|
+
"@modern-js/types": "2.6.0"
|
|
61
60
|
},
|
|
62
61
|
"devDependencies": {
|
|
63
62
|
"@types/jest": "^27",
|
|
@@ -71,11 +70,11 @@
|
|
|
71
70
|
"supertest": "^6.1.6",
|
|
72
71
|
"typescript": "^4",
|
|
73
72
|
"zod": "^3.17.3",
|
|
74
|
-
"@modern-js/core": "2.
|
|
75
|
-
"@modern-js/server-core": "2.
|
|
76
|
-
"@modern-js/app-tools": "
|
|
77
|
-
"@scripts/jest-config": "2.
|
|
78
|
-
"@scripts/build": "2.
|
|
73
|
+
"@modern-js/core": "2.6.0",
|
|
74
|
+
"@modern-js/server-core": "2.6.0",
|
|
75
|
+
"@modern-js/app-tools": "2.6.0",
|
|
76
|
+
"@scripts/jest-config": "2.6.0",
|
|
77
|
+
"@scripts/build": "2.6.0"
|
|
79
78
|
},
|
|
80
79
|
"peerDependencies": {
|
|
81
80
|
"koa": "^2.13.3"
|
package/dist/js/modern/plugin.js
DELETED
|
@@ -1,133 +0,0 @@
|
|
|
1
|
-
var __async = (__this, __arguments, generator) => {
|
|
2
|
-
return new Promise((resolve, reject) => {
|
|
3
|
-
var fulfilled = (value) => {
|
|
4
|
-
try {
|
|
5
|
-
step(generator.next(value));
|
|
6
|
-
} catch (e) {
|
|
7
|
-
reject(e);
|
|
8
|
-
}
|
|
9
|
-
};
|
|
10
|
-
var rejected = (value) => {
|
|
11
|
-
try {
|
|
12
|
-
step(generator.throw(value));
|
|
13
|
-
} catch (e) {
|
|
14
|
-
reject(e);
|
|
15
|
-
}
|
|
16
|
-
};
|
|
17
|
-
var step = (x) => x.done ? resolve(x.value) : Promise.resolve(x.value).then(fulfilled, rejected);
|
|
18
|
-
step((generator = generator.apply(__this, __arguments)).next());
|
|
19
|
-
});
|
|
20
|
-
};
|
|
21
|
-
import * as path from "path";
|
|
22
|
-
import Koa from "koa";
|
|
23
|
-
import Router from "koa-router";
|
|
24
|
-
import koaBody from "koa-body";
|
|
25
|
-
import { fs, compatRequire } from "@modern-js/utils";
|
|
26
|
-
import { run } from "./context";
|
|
27
|
-
import registerRoutes from "./registerRoutes";
|
|
28
|
-
const findAppModule = (apiDir) => __async(void 0, null, function* () {
|
|
29
|
-
const exts = [".ts", ".js"];
|
|
30
|
-
const paths = exts.map((ext) => path.join(apiDir, `app${ext}`));
|
|
31
|
-
for (const filename of paths) {
|
|
32
|
-
if (yield fs.pathExists(filename)) {
|
|
33
|
-
delete require.cache[filename];
|
|
34
|
-
return compatRequire(filename);
|
|
35
|
-
}
|
|
36
|
-
}
|
|
37
|
-
return null;
|
|
38
|
-
});
|
|
39
|
-
const initMiddlewares = (middleware, app) => {
|
|
40
|
-
middleware.forEach((middlewareItem) => {
|
|
41
|
-
const middlewareFunc = typeof middlewareItem === "string" ? compatRequire(middlewareItem) : middlewareItem;
|
|
42
|
-
app.use(middlewareFunc);
|
|
43
|
-
});
|
|
44
|
-
};
|
|
45
|
-
var plugin_default = () => ({
|
|
46
|
-
name: "@modern-js/plugin-koa",
|
|
47
|
-
pre: ["@modern-js/plugin-bff"],
|
|
48
|
-
post: ["@modern-js/plugin-server"],
|
|
49
|
-
setup: (api) => ({
|
|
50
|
-
prepareApiServer(_0) {
|
|
51
|
-
return __async(this, arguments, function* ({ pwd, config }) {
|
|
52
|
-
let app;
|
|
53
|
-
const router = new Router();
|
|
54
|
-
const apiDir = path.join(pwd, "./api");
|
|
55
|
-
const appContext = api.useAppContext();
|
|
56
|
-
const apiHandlerInfos = appContext.apiHandlerInfos;
|
|
57
|
-
const mode = appContext.apiMode;
|
|
58
|
-
if (mode === "framework") {
|
|
59
|
-
app = yield findAppModule(apiDir);
|
|
60
|
-
if (!(app instanceof Koa)) {
|
|
61
|
-
app = new Koa();
|
|
62
|
-
app.use(
|
|
63
|
-
koaBody({
|
|
64
|
-
multipart: true
|
|
65
|
-
})
|
|
66
|
-
);
|
|
67
|
-
}
|
|
68
|
-
if (config) {
|
|
69
|
-
const { middleware } = config;
|
|
70
|
-
initMiddlewares(middleware, app);
|
|
71
|
-
}
|
|
72
|
-
app.use(run);
|
|
73
|
-
registerRoutes(router, apiHandlerInfos);
|
|
74
|
-
} else if (mode === "function") {
|
|
75
|
-
app = new Koa();
|
|
76
|
-
app.use(
|
|
77
|
-
koaBody({
|
|
78
|
-
multipart: true
|
|
79
|
-
})
|
|
80
|
-
);
|
|
81
|
-
if (config) {
|
|
82
|
-
const { middleware } = config;
|
|
83
|
-
initMiddlewares(middleware, app);
|
|
84
|
-
}
|
|
85
|
-
app.use(run);
|
|
86
|
-
registerRoutes(router, apiHandlerInfos);
|
|
87
|
-
} else {
|
|
88
|
-
throw new Error(`mode must be function or framework`);
|
|
89
|
-
}
|
|
90
|
-
app.use(router.routes());
|
|
91
|
-
return (req, res) => {
|
|
92
|
-
return Promise.resolve(app.callback()(req, res));
|
|
93
|
-
};
|
|
94
|
-
});
|
|
95
|
-
},
|
|
96
|
-
prepareWebServer({ config }, next) {
|
|
97
|
-
var _a2;
|
|
98
|
-
const userConfig = api.useConfigContext();
|
|
99
|
-
if (!((_a2 = userConfig == null ? void 0 : userConfig.server) == null ? void 0 : _a2.enableFrameworkExt)) {
|
|
100
|
-
return next();
|
|
101
|
-
}
|
|
102
|
-
const app = new Koa();
|
|
103
|
-
app.use((ctx, next2) => __async(this, null, function* () {
|
|
104
|
-
yield next2();
|
|
105
|
-
if (!ctx.body) {
|
|
106
|
-
if (ctx.res.statusCode === 404 && !ctx.response._explicitStatus) {
|
|
107
|
-
ctx.res.statusCode = 200;
|
|
108
|
-
}
|
|
109
|
-
ctx.respond = false;
|
|
110
|
-
}
|
|
111
|
-
}));
|
|
112
|
-
app.use(koaBody());
|
|
113
|
-
if (config) {
|
|
114
|
-
const { middleware } = config;
|
|
115
|
-
initMiddlewares(middleware, app);
|
|
116
|
-
}
|
|
117
|
-
return (ctx) => {
|
|
118
|
-
const {
|
|
119
|
-
source: { req, res }
|
|
120
|
-
} = ctx;
|
|
121
|
-
app.on("error", (err) => {
|
|
122
|
-
if (err) {
|
|
123
|
-
throw err;
|
|
124
|
-
}
|
|
125
|
-
});
|
|
126
|
-
return Promise.resolve(app.callback()(req, res));
|
|
127
|
-
};
|
|
128
|
-
}
|
|
129
|
-
})
|
|
130
|
-
});
|
|
131
|
-
export {
|
|
132
|
-
plugin_default as default
|
|
133
|
-
};
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|