@modern-js/plugin-koa 2.0.0-beta.2 → 2.0.0-beta.4
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 +86 -0
- package/dist/js/modern/cli/index.js +34 -23
- package/dist/js/modern/context.js +4 -4
- package/dist/js/modern/index.js +4 -1
- package/dist/js/modern/plugin.js +94 -81
- package/dist/js/modern/registerRoutes.js +5 -6
- package/dist/js/modern/runtime.js +7 -3
- package/dist/js/modern/utils.js +60 -27
- package/dist/js/node/cli/index.js +57 -30
- package/dist/js/node/context.js +24 -12
- package/dist/js/node/index.js +31 -22
- package/dist/js/node/plugin.js +123 -95
- package/dist/js/node/registerRoutes.js +25 -14
- package/dist/js/node/runtime.js +24 -28
- package/dist/js/node/utils.js +84 -40
- package/dist/js/treeshaking/cli/index.js +49 -0
- package/dist/js/treeshaking/context.js +3 -0
- package/dist/js/treeshaking/index.js +4 -0
- package/dist/js/treeshaking/plugin.js +363 -0
- package/dist/js/treeshaking/registerRoutes.js +11 -0
- package/dist/js/treeshaking/runtime.js +6 -0
- package/dist/js/treeshaking/utils.js +410 -0
- package/dist/types/cli/index.d.ts +2 -0
- package/dist/types/plugin.d.ts +2 -0
- package/package.json +9 -14
package/CHANGELOG.md
CHANGED
|
@@ -1,5 +1,91 @@
|
|
|
1
1
|
# @modern-js/plugin-koa
|
|
2
2
|
|
|
3
|
+
## 2.0.0-beta.4
|
|
4
|
+
|
|
5
|
+
### Major Changes
|
|
6
|
+
|
|
7
|
+
- dda38c9c3e: 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 [7879e8f]
|
|
16
|
+
- Updated dependencies [6aca875]
|
|
17
|
+
- Updated dependencies [2e6031955e]
|
|
18
|
+
- Updated dependencies [2344eb26ed]
|
|
19
|
+
- Updated dependencies [a2509bfbdb]
|
|
20
|
+
- Updated dependencies [7b7d12c]
|
|
21
|
+
- Updated dependencies [92f0eade39]
|
|
22
|
+
- Updated dependencies [edd1cfb1af]
|
|
23
|
+
- Updated dependencies [cc971eabfc]
|
|
24
|
+
- Updated dependencies [5b9049f2e9]
|
|
25
|
+
- Updated dependencies [6bda14ed71]
|
|
26
|
+
- Updated dependencies [92004d1906]
|
|
27
|
+
- Updated dependencies [b8bbe036c7]
|
|
28
|
+
- Updated dependencies [40ed5874c6]
|
|
29
|
+
- Updated dependencies [87c1ff86b9]
|
|
30
|
+
- Updated dependencies [d5a31df781]
|
|
31
|
+
- Updated dependencies [dda38c9c3e]
|
|
32
|
+
- Updated dependencies [102d32e4ba]
|
|
33
|
+
- Updated dependencies [8b8e1bb571]
|
|
34
|
+
- Updated dependencies [3bbea92b2a]
|
|
35
|
+
- Updated dependencies [b710adb843]
|
|
36
|
+
- Updated dependencies [ea7cf06]
|
|
37
|
+
- Updated dependencies [bbe4c4a]
|
|
38
|
+
- Updated dependencies [e4558a0]
|
|
39
|
+
- Updated dependencies [abf3421a75]
|
|
40
|
+
- Updated dependencies [543be9558e]
|
|
41
|
+
- Updated dependencies [14b712da84]
|
|
42
|
+
- @modern-js/types@2.0.0-beta.4
|
|
43
|
+
- @modern-js/utils@2.0.0-beta.4
|
|
44
|
+
- @modern-js/bff-core@2.0.0-beta.4
|
|
45
|
+
- @modern-js/bff-runtime@2.0.0-beta.4
|
|
46
|
+
|
|
47
|
+
## 2.0.0-beta.3
|
|
48
|
+
|
|
49
|
+
### Major Changes
|
|
50
|
+
|
|
51
|
+
- dda38c9c3e: chore: v2
|
|
52
|
+
|
|
53
|
+
### Patch Changes
|
|
54
|
+
|
|
55
|
+
- 8d24bed25b: fix: compat Hook API in /server namespace
|
|
56
|
+
fix: 在 @modern-js/runtime/server 命名空间下兼容 Hook API
|
|
57
|
+
- 3bbea92b2a: feat: support Hook、Middleware new API
|
|
58
|
+
feat: 支持 Hook、Middleware 的新 API
|
|
59
|
+
- Updated dependencies [6aca875]
|
|
60
|
+
- Updated dependencies [2e60319]
|
|
61
|
+
- Updated dependencies [2344eb26ed]
|
|
62
|
+
- Updated dependencies [a2509bfbdb]
|
|
63
|
+
- Updated dependencies [92f0eade39]
|
|
64
|
+
- Updated dependencies [edd1cfb1af]
|
|
65
|
+
- Updated dependencies [cc971eabfc]
|
|
66
|
+
- Updated dependencies [5b9049f2e9]
|
|
67
|
+
- Updated dependencies [6bda14ed71]
|
|
68
|
+
- Updated dependencies [92004d1906]
|
|
69
|
+
- Updated dependencies [b8bbe036c7]
|
|
70
|
+
- Updated dependencies [40ed5874c6]
|
|
71
|
+
- Updated dependencies [87c1ff86b9]
|
|
72
|
+
- Updated dependencies [d5a31df781]
|
|
73
|
+
- Updated dependencies [dda38c9c3e]
|
|
74
|
+
- Updated dependencies [102d32e4ba]
|
|
75
|
+
- Updated dependencies [8b8e1bb571]
|
|
76
|
+
- Updated dependencies [3bbea92b2a]
|
|
77
|
+
- Updated dependencies [b710adb]
|
|
78
|
+
- Updated dependencies [ea7cf06]
|
|
79
|
+
- Updated dependencies [bbe4c4a]
|
|
80
|
+
- Updated dependencies [e4558a0]
|
|
81
|
+
- Updated dependencies [abf3421a75]
|
|
82
|
+
- Updated dependencies [543be9558e]
|
|
83
|
+
- Updated dependencies [14b712da84]
|
|
84
|
+
- @modern-js/utils@2.0.0-beta.3
|
|
85
|
+
- @modern-js/types@2.0.0-beta.3
|
|
86
|
+
- @modern-js/bff-core@2.0.0-beta.3
|
|
87
|
+
- @modern-js/bff-runtime@2.0.0-beta.3
|
|
88
|
+
|
|
3
89
|
## 2.0.0-beta.2
|
|
4
90
|
|
|
5
91
|
### Major Changes
|
|
@@ -1,29 +1,31 @@
|
|
|
1
|
-
import * as path from
|
|
2
|
-
import { createRuntimeExportsUtils } from
|
|
3
|
-
import { getRelativeRuntimePath } from
|
|
4
|
-
|
|
5
|
-
name:
|
|
6
|
-
setup: api => {
|
|
1
|
+
import * as path from "path";
|
|
2
|
+
import { createRuntimeExportsUtils } from "@modern-js/utils";
|
|
3
|
+
import { getRelativeRuntimePath } from "@modern-js/bff-core";
|
|
4
|
+
var cli_default = () => ({
|
|
5
|
+
name: "@modern-js/plugin-koa",
|
|
6
|
+
setup: (api) => {
|
|
7
7
|
let bffExportsUtils;
|
|
8
|
-
const {
|
|
9
|
-
|
|
10
|
-
} = api;
|
|
11
|
-
const runtimeModulePath = path.resolve(__dirname, '../runtime');
|
|
8
|
+
const { useAppContext } = api;
|
|
9
|
+
const runtimeModulePath = path.resolve(__dirname, "../runtime");
|
|
12
10
|
return {
|
|
13
11
|
config() {
|
|
14
12
|
const appContext = useAppContext();
|
|
15
|
-
const {
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
13
|
+
const { appDirectory } = appContext;
|
|
14
|
+
bffExportsUtils = createRuntimeExportsUtils(
|
|
15
|
+
appContext.internalDirectory,
|
|
16
|
+
"server"
|
|
17
|
+
);
|
|
19
18
|
const serverRuntimePath = bffExportsUtils.getPath();
|
|
20
|
-
const relativeRuntimePath = getRelativeRuntimePath(
|
|
21
|
-
|
|
19
|
+
const relativeRuntimePath = getRelativeRuntimePath(
|
|
20
|
+
appDirectory,
|
|
21
|
+
serverRuntimePath
|
|
22
|
+
);
|
|
23
|
+
if (process.env.NODE_ENV === "production") {
|
|
22
24
|
return {
|
|
23
25
|
source: {
|
|
24
26
|
alias: {
|
|
25
|
-
|
|
26
|
-
|
|
27
|
+
"@modern-js/runtime/server": relativeRuntimePath,
|
|
28
|
+
"@modern-js/runtime/koa": relativeRuntimePath
|
|
27
29
|
}
|
|
28
30
|
}
|
|
29
31
|
};
|
|
@@ -31,8 +33,8 @@ export default (() => ({
|
|
|
31
33
|
return {
|
|
32
34
|
source: {
|
|
33
35
|
alias: {
|
|
34
|
-
|
|
35
|
-
|
|
36
|
+
"@modern-js/runtime/server": serverRuntimePath,
|
|
37
|
+
"@modern-js/runtime/koa": serverRuntimePath
|
|
36
38
|
}
|
|
37
39
|
}
|
|
38
40
|
};
|
|
@@ -40,8 +42,14 @@ export default (() => ({
|
|
|
40
42
|
},
|
|
41
43
|
addRuntimeExports(input) {
|
|
42
44
|
const currentFile = bffExportsUtils.getPath();
|
|
43
|
-
const relativeRuntimeModulePath = path.relative(
|
|
44
|
-
|
|
45
|
+
const relativeRuntimeModulePath = path.relative(
|
|
46
|
+
path.dirname(currentFile),
|
|
47
|
+
runtimeModulePath
|
|
48
|
+
);
|
|
49
|
+
const relativeFramePath = path.relative(
|
|
50
|
+
path.dirname(currentFile),
|
|
51
|
+
require.resolve("koa")
|
|
52
|
+
);
|
|
45
53
|
bffExportsUtils.addExport(`const pluginRuntime = require('${relativeRuntimeModulePath}');
|
|
46
54
|
const Koa = require('${relativeFramePath}')
|
|
47
55
|
module.exports = {
|
|
@@ -53,4 +61,7 @@ export default (() => ({
|
|
|
53
61
|
}
|
|
54
62
|
};
|
|
55
63
|
}
|
|
56
|
-
})
|
|
64
|
+
});
|
|
65
|
+
export {
|
|
66
|
+
cli_default as default
|
|
67
|
+
};
|
package/dist/js/modern/index.js
CHANGED
package/dist/js/modern/plugin.js
CHANGED
|
@@ -1,114 +1,124 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
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";
|
|
6
26
|
import { run } from "./context";
|
|
7
27
|
import registerRoutes from "./registerRoutes";
|
|
8
|
-
const findAppModule =
|
|
9
|
-
const exts = [
|
|
10
|
-
const paths = exts.map(ext => path.join(apiDir, `app${ext}`));
|
|
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}`));
|
|
11
31
|
for (const filename of paths) {
|
|
12
|
-
if (
|
|
13
|
-
// 每次获取 app.ts 的时候,避免使用缓存的 app.ts
|
|
32
|
+
if (yield fs.pathExists(filename)) {
|
|
14
33
|
delete require.cache[filename];
|
|
15
34
|
return compatRequire(filename);
|
|
16
35
|
}
|
|
17
36
|
}
|
|
18
37
|
return null;
|
|
19
|
-
};
|
|
38
|
+
});
|
|
20
39
|
const initMiddlewares = (middleware, app) => {
|
|
21
|
-
middleware.forEach(middlewareItem => {
|
|
22
|
-
const middlewareFunc = typeof middlewareItem ===
|
|
40
|
+
middleware.forEach((middlewareItem) => {
|
|
41
|
+
const middlewareFunc = typeof middlewareItem === "string" ? compatRequire(middlewareItem) : middlewareItem;
|
|
23
42
|
app.use(middlewareFunc);
|
|
24
43
|
});
|
|
25
44
|
};
|
|
26
|
-
|
|
27
|
-
name:
|
|
28
|
-
pre: [
|
|
29
|
-
post: [
|
|
30
|
-
setup: api => ({
|
|
31
|
-
|
|
32
|
-
pwd,
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
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") {
|
|
44
75
|
app = new Koa();
|
|
45
|
-
app.use(
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
middleware
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
app = new Koa();
|
|
59
|
-
app.use(koaBody({
|
|
60
|
-
multipart: true
|
|
61
|
-
}));
|
|
62
|
-
if (config) {
|
|
63
|
-
const {
|
|
64
|
-
middleware
|
|
65
|
-
} = config;
|
|
66
|
-
initMiddlewares(middleware, app);
|
|
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`);
|
|
67
89
|
}
|
|
68
|
-
app.use(
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
}
|
|
73
|
-
app.use(router.routes());
|
|
74
|
-
return (req, res) => {
|
|
75
|
-
return Promise.resolve(app.callback()(req, res));
|
|
76
|
-
};
|
|
90
|
+
app.use(router.routes());
|
|
91
|
+
return (req, res) => {
|
|
92
|
+
return Promise.resolve(app.callback()(req, res));
|
|
93
|
+
};
|
|
94
|
+
});
|
|
77
95
|
},
|
|
78
|
-
prepareWebServer({
|
|
79
|
-
|
|
80
|
-
}, next) {
|
|
81
|
-
var _userConfig$server;
|
|
96
|
+
prepareWebServer({ config }, next) {
|
|
97
|
+
var _a2;
|
|
82
98
|
const userConfig = api.useConfigContext();
|
|
83
|
-
if (userConfig
|
|
99
|
+
if ((_a2 = userConfig == null ? void 0 : userConfig.server) == null ? void 0 : _a2.disableFrameworkExt) {
|
|
84
100
|
return next();
|
|
85
101
|
}
|
|
86
102
|
const app = new Koa();
|
|
87
|
-
app.use(
|
|
88
|
-
|
|
103
|
+
app.use((ctx, next2) => __async(this, null, function* () {
|
|
104
|
+
yield next2();
|
|
89
105
|
if (!ctx.body) {
|
|
90
|
-
// restore statusCode
|
|
91
106
|
if (ctx.res.statusCode === 404 && !ctx.response._explicitStatus) {
|
|
92
107
|
ctx.res.statusCode = 200;
|
|
93
108
|
}
|
|
94
109
|
ctx.respond = false;
|
|
95
110
|
}
|
|
96
|
-
});
|
|
111
|
+
}));
|
|
97
112
|
app.use(koaBody());
|
|
98
113
|
if (config) {
|
|
99
|
-
const {
|
|
100
|
-
middleware
|
|
101
|
-
} = config;
|
|
114
|
+
const { middleware } = config;
|
|
102
115
|
initMiddlewares(middleware, app);
|
|
103
116
|
}
|
|
104
|
-
return ctx => {
|
|
117
|
+
return (ctx) => {
|
|
105
118
|
const {
|
|
106
|
-
source: {
|
|
107
|
-
req,
|
|
108
|
-
res
|
|
109
|
-
}
|
|
119
|
+
source: { req, res }
|
|
110
120
|
} = ctx;
|
|
111
|
-
app.on(
|
|
121
|
+
app.on("error", (err) => {
|
|
112
122
|
if (err) {
|
|
113
123
|
throw err;
|
|
114
124
|
}
|
|
@@ -117,4 +127,7 @@ export default (() => ({
|
|
|
117
127
|
};
|
|
118
128
|
}
|
|
119
129
|
})
|
|
120
|
-
})
|
|
130
|
+
});
|
|
131
|
+
export {
|
|
132
|
+
plugin_default as default
|
|
133
|
+
};
|
|
@@ -1,13 +1,12 @@
|
|
|
1
1
|
import { createRouteHandler } from "./utils";
|
|
2
2
|
const registerRoutes = (router, handlerInfos) => {
|
|
3
|
-
handlerInfos.forEach(({
|
|
4
|
-
routePath,
|
|
5
|
-
handler,
|
|
6
|
-
httpMethod
|
|
7
|
-
}) => {
|
|
3
|
+
handlerInfos.forEach(({ routePath, handler, httpMethod }) => {
|
|
8
4
|
const routeHandler = createRouteHandler(handler);
|
|
9
5
|
const method = httpMethod.toLowerCase();
|
|
10
6
|
router[method](routePath, routeHandler);
|
|
11
7
|
});
|
|
12
8
|
};
|
|
13
|
-
|
|
9
|
+
var registerRoutes_default = registerRoutes;
|
|
10
|
+
export {
|
|
11
|
+
registerRoutes_default as default
|
|
12
|
+
};
|
|
@@ -1,3 +1,7 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
export * from
|
|
1
|
+
const hook = (attacher) => attacher;
|
|
2
|
+
import { useContext } from "./context";
|
|
3
|
+
export * from "@modern-js/bff-core";
|
|
4
|
+
export {
|
|
5
|
+
hook,
|
|
6
|
+
useContext
|
|
7
|
+
};
|
package/dist/js/modern/utils.js
CHANGED
|
@@ -1,28 +1,55 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
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
|
+
import {
|
|
22
|
+
httpMethods,
|
|
23
|
+
isWithMetaHandler,
|
|
24
|
+
ResponseMetaType,
|
|
25
|
+
HttpMetadata
|
|
26
|
+
} from "@modern-js/bff-core";
|
|
27
|
+
import { isSchemaHandler } from "@modern-js/bff-runtime";
|
|
28
|
+
import typeIs from "type-is";
|
|
4
29
|
const handleResponseMeta = (ctx, handler) => {
|
|
5
|
-
const responseMeta = Reflect.getMetadata(
|
|
30
|
+
const responseMeta = Reflect.getMetadata(
|
|
31
|
+
HttpMetadata.Response,
|
|
32
|
+
handler
|
|
33
|
+
);
|
|
6
34
|
if (Array.isArray(responseMeta)) {
|
|
7
35
|
for (const meta of responseMeta) {
|
|
8
36
|
const metaType = meta.type;
|
|
9
37
|
const metaValue = meta.value;
|
|
10
38
|
switch (metaType) {
|
|
11
39
|
case ResponseMetaType.Headers:
|
|
12
|
-
// eslint-disable-next-line @typescript-eslint/ban-types
|
|
13
40
|
for (const [key, value] of Object.entries(metaValue)) {
|
|
14
|
-
if (typeof value ===
|
|
41
|
+
if (typeof value === "string") {
|
|
15
42
|
ctx.append(key, value);
|
|
16
43
|
}
|
|
17
44
|
}
|
|
18
45
|
break;
|
|
19
46
|
case ResponseMetaType.Redirect:
|
|
20
|
-
if (typeof metaValue ===
|
|
47
|
+
if (typeof metaValue === "string") {
|
|
21
48
|
ctx.redirect(metaValue);
|
|
22
49
|
}
|
|
23
50
|
break;
|
|
24
51
|
case ResponseMetaType.StatusCode:
|
|
25
|
-
if (typeof metaValue ===
|
|
52
|
+
if (typeof metaValue === "number") {
|
|
26
53
|
ctx.status = metaValue;
|
|
27
54
|
}
|
|
28
55
|
break;
|
|
@@ -32,15 +59,14 @@ const handleResponseMeta = (ctx, handler) => {
|
|
|
32
59
|
}
|
|
33
60
|
}
|
|
34
61
|
};
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
const input = await getInputFromRequest(ctx);
|
|
62
|
+
const createRouteHandler = (handler) => {
|
|
63
|
+
const apiHandler = (ctx) => __async(void 0, null, function* () {
|
|
64
|
+
const input = yield getInputFromRequest(ctx);
|
|
39
65
|
if (isWithMetaHandler(handler)) {
|
|
40
66
|
try {
|
|
41
67
|
handleResponseMeta(ctx, handler);
|
|
42
|
-
const body =
|
|
43
|
-
if (typeof body !==
|
|
68
|
+
const body = yield handler(input);
|
|
69
|
+
if (typeof body !== "undefined") {
|
|
44
70
|
ctx.body = body;
|
|
45
71
|
}
|
|
46
72
|
} catch (error) {
|
|
@@ -57,9 +83,9 @@ export const createRouteHandler = handler => {
|
|
|
57
83
|
}
|
|
58
84
|
}
|
|
59
85
|
} else if (isSchemaHandler(handler)) {
|
|
60
|
-
const result =
|
|
61
|
-
if (result.type !==
|
|
62
|
-
if (result.type ===
|
|
86
|
+
const result = yield handler(input);
|
|
87
|
+
if (result.type !== "HandleSuccess") {
|
|
88
|
+
if (result.type === "InputValidationError") {
|
|
63
89
|
ctx.status = 400;
|
|
64
90
|
} else {
|
|
65
91
|
ctx.status = 500;
|
|
@@ -70,31 +96,38 @@ export const createRouteHandler = handler => {
|
|
|
70
96
|
}
|
|
71
97
|
} else {
|
|
72
98
|
const args = Object.values(input.params).concat(input);
|
|
73
|
-
const body =
|
|
74
|
-
if (typeof body !==
|
|
99
|
+
const body = yield handler(...args);
|
|
100
|
+
if (typeof body !== "undefined") {
|
|
75
101
|
ctx.body = body;
|
|
76
102
|
}
|
|
77
103
|
}
|
|
78
|
-
};
|
|
79
|
-
Object.defineProperties(
|
|
104
|
+
});
|
|
105
|
+
Object.defineProperties(
|
|
106
|
+
apiHandler,
|
|
107
|
+
Object.getOwnPropertyDescriptors(handler)
|
|
108
|
+
);
|
|
80
109
|
return apiHandler;
|
|
81
110
|
};
|
|
82
|
-
|
|
83
|
-
const getInputFromRequest =
|
|
111
|
+
const isNormalMethod = (httpMethod) => httpMethods.includes(httpMethod);
|
|
112
|
+
const getInputFromRequest = (ctx) => __async(void 0, null, function* () {
|
|
84
113
|
const draft = {
|
|
85
114
|
params: ctx.params,
|
|
86
115
|
query: ctx.query,
|
|
87
116
|
headers: ctx.headers,
|
|
88
117
|
cookies: ctx.headers.cookie
|
|
89
118
|
};
|
|
90
|
-
if (typeIs.is(ctx.request.type, [
|
|
119
|
+
if (typeIs.is(ctx.request.type, ["application/json"])) {
|
|
91
120
|
draft.data = ctx.request.body;
|
|
92
|
-
} else if (typeIs.is(ctx.request.type, [
|
|
121
|
+
} else if (typeIs.is(ctx.request.type, ["multipart/form-data"])) {
|
|
93
122
|
draft.formData = ctx.request.files;
|
|
94
|
-
} else if (typeIs.is(ctx.request.type, [
|
|
123
|
+
} else if (typeIs.is(ctx.request.type, ["application/x-www-form-urlencoded"])) {
|
|
95
124
|
draft.formUrlencoded = ctx.request.body;
|
|
96
125
|
} else {
|
|
97
126
|
draft.body = ctx.request.body;
|
|
98
127
|
}
|
|
99
128
|
return draft;
|
|
100
|
-
};
|
|
129
|
+
});
|
|
130
|
+
export {
|
|
131
|
+
createRouteHandler,
|
|
132
|
+
isNormalMethod
|
|
133
|
+
};
|