@modern-js/plugin-koa 2.4.0 → 2.5.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 +22 -0
- package/dist/{js/node → cjs}/cli/index.js +0 -0
- package/dist/{js/node → cjs}/context.js +0 -0
- package/dist/{js/node → cjs}/index.js +0 -0
- package/dist/{js/node → cjs}/plugin.js +55 -66
- package/dist/{js/node → cjs}/registerRoutes.js +0 -0
- package/dist/{js/node → cjs}/runtime.js +0 -0
- package/dist/{js/node → cjs}/utils.js +8 -28
- package/dist/{js/treeshaking → esm}/cli/index.js +0 -0
- package/dist/esm/context.js +3 -0
- package/dist/{js/treeshaking → esm}/index.js +0 -0
- package/dist/{js/treeshaking → esm}/plugin.js +39 -5
- 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 +1 -1
- 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/esm-node/plugin.js +122 -0
- package/dist/{js/modern → esm-node}/registerRoutes.js +0 -0
- package/dist/{js/modern → esm-node}/runtime.js +0 -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/treeshaking/context.js +0 -3
package/CHANGELOG.md
CHANGED
|
@@ -1,5 +1,27 @@
|
|
|
1
1
|
# @modern-js/plugin-koa
|
|
2
2
|
|
|
3
|
+
## 2.5.0
|
|
4
|
+
|
|
5
|
+
### Patch Changes
|
|
6
|
+
|
|
7
|
+
- 89ca6cc: refactor: merge build-config into scripts/build
|
|
8
|
+
|
|
9
|
+
refactor: 把 build-config 合并进 scripts/build
|
|
10
|
+
|
|
11
|
+
- 6fca567: feat: support bff handle complete server, include page render
|
|
12
|
+
feat: 支持 bff 处理整个服务,包括页面渲染
|
|
13
|
+
- 30614fa: chore: modify package.json entry fields and build config
|
|
14
|
+
chore: 更改 package.json entry 字段以及构建配置
|
|
15
|
+
- Updated dependencies [89ca6cc]
|
|
16
|
+
- Updated dependencies [7cb8bb4]
|
|
17
|
+
- Updated dependencies [30614fa]
|
|
18
|
+
- Updated dependencies [1b0ce87]
|
|
19
|
+
- Updated dependencies [11c053b]
|
|
20
|
+
- @modern-js/bff-core@2.5.0
|
|
21
|
+
- @modern-js/bff-runtime@2.5.0
|
|
22
|
+
- @modern-js/types@2.5.0
|
|
23
|
+
- @modern-js/utils@2.5.0
|
|
24
|
+
|
|
3
25
|
## 2.4.0
|
|
4
26
|
|
|
5
27
|
### Patch Changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
@@ -21,26 +21,6 @@ var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__ge
|
|
|
21
21
|
mod
|
|
22
22
|
));
|
|
23
23
|
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
24
|
var plugin_exports = {};
|
|
45
25
|
__export(plugin_exports, {
|
|
46
26
|
default: () => plugin_default
|
|
@@ -53,17 +33,17 @@ var import_koa_body = __toESM(require("koa-body"));
|
|
|
53
33
|
var import_utils = require("@modern-js/utils");
|
|
54
34
|
var import_context = require("./context");
|
|
55
35
|
var import_registerRoutes = __toESM(require("./registerRoutes"));
|
|
56
|
-
const findAppModule = (apiDir) =>
|
|
36
|
+
const findAppModule = async (apiDir) => {
|
|
57
37
|
const exts = [".ts", ".js"];
|
|
58
38
|
const paths = exts.map((ext) => path.join(apiDir, `app${ext}`));
|
|
59
39
|
for (const filename of paths) {
|
|
60
|
-
if (
|
|
40
|
+
if (await import_utils.fs.pathExists(filename)) {
|
|
61
41
|
delete require.cache[filename];
|
|
62
42
|
return (0, import_utils.compatRequire)(filename);
|
|
63
43
|
}
|
|
64
44
|
}
|
|
65
45
|
return null;
|
|
66
|
-
}
|
|
46
|
+
};
|
|
67
47
|
const initMiddlewares = (middleware, app) => {
|
|
68
48
|
middleware.forEach((middlewareItem) => {
|
|
69
49
|
const middlewareFunc = typeof middlewareItem === "string" ? (0, import_utils.compatRequire)(middlewareItem) : middlewareItem;
|
|
@@ -75,68 +55,77 @@ var plugin_default = () => ({
|
|
|
75
55
|
pre: ["@modern-js/plugin-bff"],
|
|
76
56
|
post: ["@modern-js/plugin-server"],
|
|
77
57
|
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") {
|
|
58
|
+
async prepareApiServer({ pwd, config, render }) {
|
|
59
|
+
var _a;
|
|
60
|
+
let app;
|
|
61
|
+
const router = new import_koa_router.default();
|
|
62
|
+
const apiDir = path.join(pwd, "./api");
|
|
63
|
+
const appContext = api.useAppContext();
|
|
64
|
+
const apiHandlerInfos = appContext.apiHandlerInfos;
|
|
65
|
+
const mode = appContext.apiMode;
|
|
66
|
+
const userConfig = api.useConfigContext();
|
|
67
|
+
if (mode === "framework") {
|
|
68
|
+
app = await findAppModule(apiDir);
|
|
69
|
+
if (!(app instanceof import_koa.default)) {
|
|
103
70
|
app = new import_koa.default();
|
|
104
71
|
app.use(
|
|
105
72
|
(0, import_koa_body.default)({
|
|
106
73
|
multipart: true
|
|
107
74
|
})
|
|
108
75
|
);
|
|
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
76
|
}
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
}
|
|
122
|
-
|
|
77
|
+
if (config) {
|
|
78
|
+
const { middleware } = config;
|
|
79
|
+
initMiddlewares(middleware, app);
|
|
80
|
+
}
|
|
81
|
+
app.use(import_context.run);
|
|
82
|
+
(0, import_registerRoutes.default)(router, apiHandlerInfos);
|
|
83
|
+
} else if (mode === "function") {
|
|
84
|
+
app = new import_koa.default();
|
|
85
|
+
app.use(
|
|
86
|
+
(0, import_koa_body.default)({
|
|
87
|
+
multipart: true
|
|
88
|
+
})
|
|
89
|
+
);
|
|
90
|
+
if (config) {
|
|
91
|
+
const { middleware } = config;
|
|
92
|
+
initMiddlewares(middleware, app);
|
|
93
|
+
}
|
|
94
|
+
app.use(import_context.run);
|
|
95
|
+
(0, import_registerRoutes.default)(router, apiHandlerInfos);
|
|
96
|
+
} else {
|
|
97
|
+
throw new Error(`mode must be function or framework`);
|
|
98
|
+
}
|
|
99
|
+
app.use(router.routes());
|
|
100
|
+
if (((_a = userConfig.bff) == null ? void 0 : _a.enableHandleWeb) && render) {
|
|
101
|
+
app.use(async (ctx, next) => {
|
|
102
|
+
const html = await render(ctx.req, ctx.res);
|
|
103
|
+
if (html) {
|
|
104
|
+
ctx.body = html;
|
|
105
|
+
}
|
|
106
|
+
await next();
|
|
107
|
+
});
|
|
108
|
+
}
|
|
109
|
+
return (req, res) => {
|
|
110
|
+
return Promise.resolve(app.callback()(req, res));
|
|
111
|
+
};
|
|
123
112
|
},
|
|
124
113
|
prepareWebServer({ config }, next) {
|
|
125
|
-
var
|
|
114
|
+
var _a;
|
|
126
115
|
const userConfig = api.useConfigContext();
|
|
127
|
-
if (!((
|
|
116
|
+
if (!((_a = userConfig == null ? void 0 : userConfig.server) == null ? void 0 : _a.enableFrameworkExt)) {
|
|
128
117
|
return next();
|
|
129
118
|
}
|
|
130
119
|
const app = new import_koa.default();
|
|
131
|
-
app.use((ctx, next2) =>
|
|
132
|
-
|
|
120
|
+
app.use(async (ctx, next2) => {
|
|
121
|
+
await next2();
|
|
133
122
|
if (!ctx.body) {
|
|
134
123
|
if (ctx.res.statusCode === 404 && !ctx.response._explicitStatus) {
|
|
135
124
|
ctx.res.statusCode = 200;
|
|
136
125
|
}
|
|
137
126
|
ctx.respond = false;
|
|
138
127
|
}
|
|
139
|
-
})
|
|
128
|
+
});
|
|
140
129
|
app.use((0, import_koa_body.default)());
|
|
141
130
|
if (config) {
|
|
142
131
|
const { middleware } = config;
|
|
File without changes
|
|
File without changes
|
|
@@ -21,26 +21,6 @@ var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__ge
|
|
|
21
21
|
mod
|
|
22
22
|
));
|
|
23
23
|
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
24
|
var utils_exports = {};
|
|
45
25
|
__export(utils_exports, {
|
|
46
26
|
createRouteHandler: () => createRouteHandler,
|
|
@@ -84,12 +64,12 @@ const handleResponseMeta = (ctx, handler) => {
|
|
|
84
64
|
}
|
|
85
65
|
};
|
|
86
66
|
const createRouteHandler = (handler) => {
|
|
87
|
-
const apiHandler = (ctx) =>
|
|
88
|
-
const input =
|
|
67
|
+
const apiHandler = async (ctx) => {
|
|
68
|
+
const input = await getInputFromRequest(ctx);
|
|
89
69
|
if ((0, import_bff_core.isWithMetaHandler)(handler)) {
|
|
90
70
|
try {
|
|
91
71
|
handleResponseMeta(ctx, handler);
|
|
92
|
-
const body =
|
|
72
|
+
const body = await handler(input);
|
|
93
73
|
if (typeof body !== "undefined") {
|
|
94
74
|
ctx.body = body;
|
|
95
75
|
}
|
|
@@ -107,7 +87,7 @@ const createRouteHandler = (handler) => {
|
|
|
107
87
|
}
|
|
108
88
|
}
|
|
109
89
|
} else if ((0, import_bff_runtime.isSchemaHandler)(handler)) {
|
|
110
|
-
const result =
|
|
90
|
+
const result = await handler(input);
|
|
111
91
|
if (result.type !== "HandleSuccess") {
|
|
112
92
|
if (result.type === "InputValidationError") {
|
|
113
93
|
ctx.status = 400;
|
|
@@ -120,12 +100,12 @@ const createRouteHandler = (handler) => {
|
|
|
120
100
|
}
|
|
121
101
|
} else {
|
|
122
102
|
const args = Object.values(input.params).concat(input);
|
|
123
|
-
const body =
|
|
103
|
+
const body = await handler(...args);
|
|
124
104
|
if (typeof body !== "undefined") {
|
|
125
105
|
ctx.body = body;
|
|
126
106
|
}
|
|
127
107
|
}
|
|
128
|
-
}
|
|
108
|
+
};
|
|
129
109
|
Object.defineProperties(
|
|
130
110
|
apiHandler,
|
|
131
111
|
Object.getOwnPropertyDescriptors(handler)
|
|
@@ -133,7 +113,7 @@ const createRouteHandler = (handler) => {
|
|
|
133
113
|
return apiHandler;
|
|
134
114
|
};
|
|
135
115
|
const isNormalMethod = (httpMethod) => import_bff_core.httpMethods.includes(httpMethod);
|
|
136
|
-
const getInputFromRequest = (ctx) =>
|
|
116
|
+
const getInputFromRequest = async (ctx) => {
|
|
137
117
|
const draft = {
|
|
138
118
|
params: ctx.params,
|
|
139
119
|
query: ctx.query,
|
|
@@ -150,7 +130,7 @@ const getInputFromRequest = (ctx) => __async(void 0, null, function* () {
|
|
|
150
130
|
draft.body = ctx.request.body;
|
|
151
131
|
}
|
|
152
132
|
return draft;
|
|
153
|
-
}
|
|
133
|
+
};
|
|
154
134
|
// Annotate the CommonJS export names for ESM import in node:
|
|
155
135
|
0 && (module.exports = {
|
|
156
136
|
createRouteHandler,
|
|
File without changes
|
|
File without changes
|
|
@@ -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) {
|
|
@@ -308,9 +342,9 @@ var plugin_default = function() {
|
|
|
308
342
|
},
|
|
309
343
|
prepareWebServer: function prepareWebServer(param, next) {
|
|
310
344
|
var config = param.config;
|
|
311
|
-
var
|
|
345
|
+
var _userConfig_server;
|
|
312
346
|
var userConfig = api.useConfigContext();
|
|
313
|
-
if (!(userConfig === null || userConfig === void 0 ? void 0 : (
|
|
347
|
+
if (!(userConfig === null || userConfig === void 0 ? void 0 : (_userConfig_server = userConfig.server) === null || _userConfig_server === void 0 ? void 0 : _userConfig_server.enableFrameworkExt)) {
|
|
314
348
|
return next();
|
|
315
349
|
}
|
|
316
350
|
var app = new Koa();
|
|
@@ -347,7 +381,7 @@ var plugin_default = function() {
|
|
|
347
381
|
initMiddlewares(middleware, app);
|
|
348
382
|
}
|
|
349
383
|
return function(ctx) {
|
|
350
|
-
var
|
|
384
|
+
var _ctx_source = ctx.source, req = _ctx_source.req, res = _ctx_source.res;
|
|
351
385
|
app.on("error", function(err) {
|
|
352
386
|
if (err) {
|
|
353
387
|
throw err;
|
|
File without changes
|
|
File without changes
|
|
@@ -204,7 +204,7 @@ var handleResponseMeta = function(ctx, handler) {
|
|
|
204
204
|
var _iteratorNormalCompletion1 = true, _didIteratorError1 = false, _iteratorError1 = undefined;
|
|
205
205
|
try {
|
|
206
206
|
for(var _iterator1 = Object.entries(metaValue)[Symbol.iterator](), _step1; !(_iteratorNormalCompletion1 = (_step1 = _iterator1.next()).done); _iteratorNormalCompletion1 = true){
|
|
207
|
-
var
|
|
207
|
+
var _step_value = _slicedToArray(_step1.value, 2), key = _step_value[0], value = _step_value[1];
|
|
208
208
|
if (typeof value === "string") {
|
|
209
209
|
ctx.append(key, value);
|
|
210
210
|
}
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
@@ -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
|
+
};
|
|
File without changes
|
|
File without changes
|
|
@@ -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.5.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-runtime": "2.
|
|
58
|
-
"@modern-js/utils": "2.
|
|
59
|
-
"@modern-js/types": "2.
|
|
60
|
-
"@modern-js/bff-core": "2.
|
|
56
|
+
"@modern-js/bff-runtime": "2.5.0",
|
|
57
|
+
"@modern-js/utils": "2.5.0",
|
|
58
|
+
"@modern-js/types": "2.5.0",
|
|
59
|
+
"@modern-js/bff-core": "2.5.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": "2.
|
|
77
|
-
"@scripts/jest-config": "2.
|
|
78
|
-
"@scripts/build": "2.
|
|
73
|
+
"@modern-js/core": "2.5.0",
|
|
74
|
+
"@modern-js/server-core": "2.5.0",
|
|
75
|
+
"@modern-js/app-tools": "2.5.0",
|
|
76
|
+
"@scripts/jest-config": "2.5.0",
|
|
77
|
+
"@scripts/build": "2.5.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
|
-
};
|