@modern-js/plugin-koa 1.4.7 → 1.4.8-canary.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/.wireit/6275696c64/cache/5a7fd1190b57f273bbfc05fc04fa99ef67c54e4eac07712cccd97346c9903534/.wireit/6275696c64/stdout +9 -0
- package/.wireit/6275696c64/cache/5a7fd1190b57f273bbfc05fc04fa99ef67c54e4eac07712cccd97346c9903534/dist/js/modern/cli/index.js +60 -0
- package/.wireit/6275696c64/cache/5a7fd1190b57f273bbfc05fc04fa99ef67c54e4eac07712cccd97346c9903534/dist/js/modern/context.js +6 -0
- package/.wireit/6275696c64/cache/5a7fd1190b57f273bbfc05fc04fa99ef67c54e4eac07712cccd97346c9903534/dist/js/modern/index.js +3 -0
- package/.wireit/6275696c64/cache/5a7fd1190b57f273bbfc05fc04fa99ef67c54e4eac07712cccd97346c9903534/dist/js/modern/plugin.js +129 -0
- package/.wireit/6275696c64/cache/5a7fd1190b57f273bbfc05fc04fa99ef67c54e4eac07712cccd97346c9903534/dist/js/modern/registerRoutes.js +82 -0
- package/.wireit/6275696c64/cache/5a7fd1190b57f273bbfc05fc04fa99ef67c54e4eac07712cccd97346c9903534/dist/js/modern/runtime.js +1 -0
- package/.wireit/6275696c64/cache/5a7fd1190b57f273bbfc05fc04fa99ef67c54e4eac07712cccd97346c9903534/dist/js/node/cli/index.js +76 -0
- package/.wireit/6275696c64/cache/5a7fd1190b57f273bbfc05fc04fa99ef67c54e4eac07712cccd97346c9903534/dist/js/node/context.js +15 -0
- package/.wireit/6275696c64/cache/5a7fd1190b57f273bbfc05fc04fa99ef67c54e4eac07712cccd97346c9903534/dist/js/node/index.js +28 -0
- package/.wireit/6275696c64/cache/5a7fd1190b57f273bbfc05fc04fa99ef67c54e4eac07712cccd97346c9903534/dist/js/node/plugin.js +151 -0
- package/.wireit/6275696c64/cache/5a7fd1190b57f273bbfc05fc04fa99ef67c54e4eac07712cccd97346c9903534/dist/js/node/registerRoutes.js +97 -0
- package/.wireit/6275696c64/cache/5a7fd1190b57f273bbfc05fc04fa99ef67c54e4eac07712cccd97346c9903534/dist/js/node/runtime.js +13 -0
- package/.wireit/6275696c64/cache/a48cd57db6d78fbc9aca74c669a12325d8d6adee3602be60b39b7f0aee775e0e/dist/js/modern/cli/index.js +60 -0
- package/.wireit/6275696c64/cache/a48cd57db6d78fbc9aca74c669a12325d8d6adee3602be60b39b7f0aee775e0e/dist/js/modern/context.js +6 -0
- package/.wireit/6275696c64/cache/a48cd57db6d78fbc9aca74c669a12325d8d6adee3602be60b39b7f0aee775e0e/dist/js/modern/index.js +3 -0
- package/.wireit/6275696c64/cache/a48cd57db6d78fbc9aca74c669a12325d8d6adee3602be60b39b7f0aee775e0e/dist/js/modern/plugin.js +129 -0
- package/.wireit/6275696c64/cache/a48cd57db6d78fbc9aca74c669a12325d8d6adee3602be60b39b7f0aee775e0e/dist/js/modern/registerRoutes.js +82 -0
- package/.wireit/6275696c64/cache/a48cd57db6d78fbc9aca74c669a12325d8d6adee3602be60b39b7f0aee775e0e/dist/js/modern/runtime.js +1 -0
- package/.wireit/6275696c64/cache/a48cd57db6d78fbc9aca74c669a12325d8d6adee3602be60b39b7f0aee775e0e/dist/js/node/cli/index.js +76 -0
- package/.wireit/6275696c64/cache/a48cd57db6d78fbc9aca74c669a12325d8d6adee3602be60b39b7f0aee775e0e/dist/js/node/context.js +15 -0
- package/.wireit/6275696c64/cache/a48cd57db6d78fbc9aca74c669a12325d8d6adee3602be60b39b7f0aee775e0e/dist/js/node/index.js +28 -0
- package/.wireit/6275696c64/cache/a48cd57db6d78fbc9aca74c669a12325d8d6adee3602be60b39b7f0aee775e0e/dist/js/node/plugin.js +151 -0
- package/.wireit/6275696c64/cache/a48cd57db6d78fbc9aca74c669a12325d8d6adee3602be60b39b7f0aee775e0e/dist/js/node/registerRoutes.js +97 -0
- package/.wireit/6275696c64/cache/a48cd57db6d78fbc9aca74c669a12325d8d6adee3602be60b39b7f0aee775e0e/dist/js/node/runtime.js +13 -0
- package/.wireit/6275696c64/cache/c2bcc9f69d6e88cab5556c7e21d2d96caf864704311f42923d5aac9d76903c09/dist/js/modern/cli/index.js +60 -0
- package/.wireit/6275696c64/cache/c2bcc9f69d6e88cab5556c7e21d2d96caf864704311f42923d5aac9d76903c09/dist/js/modern/context.js +6 -0
- package/.wireit/6275696c64/cache/c2bcc9f69d6e88cab5556c7e21d2d96caf864704311f42923d5aac9d76903c09/dist/js/modern/index.js +3 -0
- package/.wireit/6275696c64/cache/c2bcc9f69d6e88cab5556c7e21d2d96caf864704311f42923d5aac9d76903c09/dist/js/modern/plugin.js +129 -0
- package/.wireit/6275696c64/cache/c2bcc9f69d6e88cab5556c7e21d2d96caf864704311f42923d5aac9d76903c09/dist/js/modern/registerRoutes.js +82 -0
- package/.wireit/6275696c64/cache/c2bcc9f69d6e88cab5556c7e21d2d96caf864704311f42923d5aac9d76903c09/dist/js/modern/runtime.js +1 -0
- package/.wireit/6275696c64/cache/c2bcc9f69d6e88cab5556c7e21d2d96caf864704311f42923d5aac9d76903c09/dist/js/node/cli/index.js +76 -0
- package/.wireit/6275696c64/cache/c2bcc9f69d6e88cab5556c7e21d2d96caf864704311f42923d5aac9d76903c09/dist/js/node/context.js +15 -0
- package/.wireit/6275696c64/cache/c2bcc9f69d6e88cab5556c7e21d2d96caf864704311f42923d5aac9d76903c09/dist/js/node/index.js +28 -0
- package/.wireit/6275696c64/cache/c2bcc9f69d6e88cab5556c7e21d2d96caf864704311f42923d5aac9d76903c09/dist/js/node/plugin.js +151 -0
- package/.wireit/6275696c64/cache/c2bcc9f69d6e88cab5556c7e21d2d96caf864704311f42923d5aac9d76903c09/dist/js/node/registerRoutes.js +97 -0
- package/.wireit/6275696c64/cache/c2bcc9f69d6e88cab5556c7e21d2d96caf864704311f42923d5aac9d76903c09/dist/js/node/runtime.js +13 -0
- package/.wireit/6275696c64/cache/c702551e9ac5f99417b3d7904cde2a76ea272e9a04c27fe6afd33e73b7c25856/.wireit/6275696c64/stdout +9 -0
- package/.wireit/6275696c64/cache/c702551e9ac5f99417b3d7904cde2a76ea272e9a04c27fe6afd33e73b7c25856/dist/js/modern/cli/index.js +62 -0
- package/.wireit/6275696c64/cache/c702551e9ac5f99417b3d7904cde2a76ea272e9a04c27fe6afd33e73b7c25856/dist/js/modern/context.js +6 -0
- package/.wireit/6275696c64/cache/c702551e9ac5f99417b3d7904cde2a76ea272e9a04c27fe6afd33e73b7c25856/dist/js/modern/index.js +3 -0
- package/.wireit/6275696c64/cache/c702551e9ac5f99417b3d7904cde2a76ea272e9a04c27fe6afd33e73b7c25856/dist/js/modern/plugin.js +129 -0
- package/.wireit/6275696c64/cache/c702551e9ac5f99417b3d7904cde2a76ea272e9a04c27fe6afd33e73b7c25856/dist/js/modern/registerRoutes.js +82 -0
- package/.wireit/6275696c64/cache/c702551e9ac5f99417b3d7904cde2a76ea272e9a04c27fe6afd33e73b7c25856/dist/js/modern/runtime.js +1 -0
- package/.wireit/6275696c64/cache/c702551e9ac5f99417b3d7904cde2a76ea272e9a04c27fe6afd33e73b7c25856/dist/js/node/cli/index.js +78 -0
- package/.wireit/6275696c64/cache/c702551e9ac5f99417b3d7904cde2a76ea272e9a04c27fe6afd33e73b7c25856/dist/js/node/context.js +15 -0
- package/.wireit/6275696c64/cache/c702551e9ac5f99417b3d7904cde2a76ea272e9a04c27fe6afd33e73b7c25856/dist/js/node/index.js +28 -0
- package/.wireit/6275696c64/cache/c702551e9ac5f99417b3d7904cde2a76ea272e9a04c27fe6afd33e73b7c25856/dist/js/node/plugin.js +151 -0
- package/.wireit/6275696c64/cache/c702551e9ac5f99417b3d7904cde2a76ea272e9a04c27fe6afd33e73b7c25856/dist/js/node/registerRoutes.js +97 -0
- package/.wireit/6275696c64/cache/c702551e9ac5f99417b3d7904cde2a76ea272e9a04c27fe6afd33e73b7c25856/dist/js/node/runtime.js +13 -0
- package/.wireit/6275696c64/cache/fceab7422ff46c3bc400017a31f179fd20fb88539c1968876d37bec188823945/dist/js/modern/cli/index.js +60 -0
- package/.wireit/6275696c64/cache/fceab7422ff46c3bc400017a31f179fd20fb88539c1968876d37bec188823945/dist/js/modern/context.js +6 -0
- package/.wireit/6275696c64/cache/fceab7422ff46c3bc400017a31f179fd20fb88539c1968876d37bec188823945/dist/js/modern/index.js +3 -0
- package/.wireit/6275696c64/cache/fceab7422ff46c3bc400017a31f179fd20fb88539c1968876d37bec188823945/dist/js/modern/plugin.js +129 -0
- package/.wireit/6275696c64/cache/fceab7422ff46c3bc400017a31f179fd20fb88539c1968876d37bec188823945/dist/js/modern/registerRoutes.js +82 -0
- package/.wireit/6275696c64/cache/fceab7422ff46c3bc400017a31f179fd20fb88539c1968876d37bec188823945/dist/js/modern/runtime.js +1 -0
- package/.wireit/6275696c64/cache/fceab7422ff46c3bc400017a31f179fd20fb88539c1968876d37bec188823945/dist/js/node/cli/index.js +76 -0
- package/.wireit/6275696c64/cache/fceab7422ff46c3bc400017a31f179fd20fb88539c1968876d37bec188823945/dist/js/node/context.js +15 -0
- package/.wireit/6275696c64/cache/fceab7422ff46c3bc400017a31f179fd20fb88539c1968876d37bec188823945/dist/js/node/index.js +28 -0
- package/.wireit/6275696c64/cache/fceab7422ff46c3bc400017a31f179fd20fb88539c1968876d37bec188823945/dist/js/node/plugin.js +151 -0
- package/.wireit/6275696c64/cache/fceab7422ff46c3bc400017a31f179fd20fb88539c1968876d37bec188823945/dist/js/node/registerRoutes.js +97 -0
- package/.wireit/6275696c64/cache/fceab7422ff46c3bc400017a31f179fd20fb88539c1968876d37bec188823945/dist/js/node/runtime.js +13 -0
- package/.wireit/6275696c64/lock +0 -0
- package/.wireit/6275696c64/state +1 -0
- package/.wireit/6275696c64/stdout +9 -0
- package/dist/js/modern/cli/index.js +8 -6
- package/dist/js/node/cli/index.js +7 -5
- package/package.json +28 -4
- package/.eslintrc.js +0 -7
- package/jest.config.js +0 -7
- package/modern.config.js +0 -2
- package/tsconfig.json +0 -10
|
@@ -0,0 +1,60 @@
|
|
|
1
|
+
import * as path from 'path';
|
|
2
|
+
import { createRuntimeExportsUtils } from '@modern-js/utils';
|
|
3
|
+
import { getRelativeRuntimePath } from '@modern-js/adapter-helpers';
|
|
4
|
+
export default (() => ({
|
|
5
|
+
name: '@modern-js/plugin-koa',
|
|
6
|
+
setup: api => {
|
|
7
|
+
let bffExportsUtils;
|
|
8
|
+
const {
|
|
9
|
+
useAppContext
|
|
10
|
+
} = api;
|
|
11
|
+
const runtimeModulePath = path.resolve(__dirname, '../runtime');
|
|
12
|
+
return {
|
|
13
|
+
config() {
|
|
14
|
+
// eslint-disable-next-line react-hooks/rules-of-hooks
|
|
15
|
+
const appContext = useAppContext();
|
|
16
|
+
const {
|
|
17
|
+
appDirectory
|
|
18
|
+
} = appContext;
|
|
19
|
+
bffExportsUtils = createRuntimeExportsUtils(appContext.internalDirectory, 'server');
|
|
20
|
+
const serverRuntimePath = bffExportsUtils.getPath();
|
|
21
|
+
const relativeRuntimePath = getRelativeRuntimePath(appDirectory, serverRuntimePath);
|
|
22
|
+
return {
|
|
23
|
+
source: {
|
|
24
|
+
alias: {
|
|
25
|
+
'@modern-js/runtime/server': relativeRuntimePath
|
|
26
|
+
}
|
|
27
|
+
}
|
|
28
|
+
};
|
|
29
|
+
},
|
|
30
|
+
|
|
31
|
+
addRuntimeExports(input) {
|
|
32
|
+
// eslint-disable-next-line react-hooks/rules-of-hooks
|
|
33
|
+
const {
|
|
34
|
+
appDirectory
|
|
35
|
+
} = useAppContext();
|
|
36
|
+
|
|
37
|
+
const runtimePath = require.resolve(`@modern-js/runtime`, {
|
|
38
|
+
paths: [appDirectory]
|
|
39
|
+
});
|
|
40
|
+
|
|
41
|
+
const currentFile = bffExportsUtils.getPath();
|
|
42
|
+
const runtimeDir = path.dirname(runtimePath);
|
|
43
|
+
const relativeBffPath = path.relative(path.dirname(currentFile), path.join(runtimeDir, './exports/server'));
|
|
44
|
+
const relativeRuntimeModulePath = path.relative(path.dirname(currentFile), runtimeModulePath);
|
|
45
|
+
const relativeFramePath = path.relative(path.dirname(currentFile), require.resolve('koa'));
|
|
46
|
+
bffExportsUtils.addExport(`const bffRuntime = require('${relativeBffPath}');
|
|
47
|
+
const pluginRuntime = require('${relativeRuntimeModulePath}');
|
|
48
|
+
const Koa = require('${relativeFramePath}')
|
|
49
|
+
module.exports = {
|
|
50
|
+
Koa: Koa,
|
|
51
|
+
...bffRuntime,
|
|
52
|
+
...pluginRuntime
|
|
53
|
+
}
|
|
54
|
+
`);
|
|
55
|
+
return input;
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
};
|
|
59
|
+
}
|
|
60
|
+
}));
|
|
@@ -0,0 +1,129 @@
|
|
|
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 { requireModule } from '@modern-js/bff-utils';
|
|
6
|
+
import { fs } from '@modern-js/utils';
|
|
7
|
+
import { run } from "./context";
|
|
8
|
+
import registerRoutes from "./registerRoutes";
|
|
9
|
+
|
|
10
|
+
const findAppModule = async apiDir => {
|
|
11
|
+
const exts = ['.ts', '.js'];
|
|
12
|
+
const paths = exts.map(ext => path.join(apiDir, `app${ext}`));
|
|
13
|
+
|
|
14
|
+
for (const filename of paths) {
|
|
15
|
+
if (await fs.pathExists(filename)) {
|
|
16
|
+
return requireModule(filename);
|
|
17
|
+
}
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
return null;
|
|
21
|
+
};
|
|
22
|
+
|
|
23
|
+
const initMiddlewares = (middleware, app) => {
|
|
24
|
+
middleware.forEach(middlewareItem => {
|
|
25
|
+
const middlewareFunc = typeof middlewareItem === 'string' ? requireModule(middlewareItem) : middlewareItem;
|
|
26
|
+
app.use(middlewareFunc);
|
|
27
|
+
});
|
|
28
|
+
};
|
|
29
|
+
|
|
30
|
+
export default (() => ({
|
|
31
|
+
name: '@modern-js/plugin-koa',
|
|
32
|
+
pre: ['@modern-js/plugin-bff'],
|
|
33
|
+
setup: () => ({
|
|
34
|
+
async prepareApiServer({
|
|
35
|
+
pwd,
|
|
36
|
+
mode,
|
|
37
|
+
config,
|
|
38
|
+
prefix
|
|
39
|
+
}) {
|
|
40
|
+
let app;
|
|
41
|
+
const router = new Router();
|
|
42
|
+
const apiDir = path.join(pwd, './api');
|
|
43
|
+
|
|
44
|
+
if (mode === 'framework') {
|
|
45
|
+
app = await findAppModule(apiDir);
|
|
46
|
+
|
|
47
|
+
if (!(app instanceof Koa)) {
|
|
48
|
+
app = new Koa();
|
|
49
|
+
app.use(koaBody({
|
|
50
|
+
multipart: true
|
|
51
|
+
}));
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
if (config) {
|
|
55
|
+
const {
|
|
56
|
+
middleware
|
|
57
|
+
} = config;
|
|
58
|
+
initMiddlewares(middleware, app);
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
app.use(run);
|
|
62
|
+
registerRoutes(router, prefix);
|
|
63
|
+
} else if (mode === 'function') {
|
|
64
|
+
app = new Koa();
|
|
65
|
+
app.use(koaBody({
|
|
66
|
+
multipart: true
|
|
67
|
+
}));
|
|
68
|
+
|
|
69
|
+
if (config) {
|
|
70
|
+
const {
|
|
71
|
+
middleware
|
|
72
|
+
} = config;
|
|
73
|
+
initMiddlewares(middleware, app);
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
app.use(run);
|
|
77
|
+
registerRoutes(router, prefix);
|
|
78
|
+
} else {
|
|
79
|
+
throw new Error(`mode must be function or framework`);
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
app.use(router.routes());
|
|
83
|
+
return (req, res) => {
|
|
84
|
+
app.on('error', err => {
|
|
85
|
+
if (err) {
|
|
86
|
+
throw err;
|
|
87
|
+
}
|
|
88
|
+
});
|
|
89
|
+
return Promise.resolve(app.callback()(req, res));
|
|
90
|
+
};
|
|
91
|
+
},
|
|
92
|
+
|
|
93
|
+
prepareWebServer({
|
|
94
|
+
config
|
|
95
|
+
}) {
|
|
96
|
+
const app = new Koa();
|
|
97
|
+
app.use(async (ctx, next) => {
|
|
98
|
+
await next();
|
|
99
|
+
|
|
100
|
+
if (!ctx.body) {
|
|
101
|
+
// restore statusCode
|
|
102
|
+
if (ctx.res.statusCode === 404 && !ctx.response._explicitStatus) {
|
|
103
|
+
ctx.res.statusCode = 200;
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
ctx.respond = false;
|
|
107
|
+
}
|
|
108
|
+
});
|
|
109
|
+
app.use(koaBody());
|
|
110
|
+
|
|
111
|
+
if (config) {
|
|
112
|
+
const {
|
|
113
|
+
middleware
|
|
114
|
+
} = config;
|
|
115
|
+
initMiddlewares(middleware, app);
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
return (req, res) => {
|
|
119
|
+
app.on('error', err => {
|
|
120
|
+
if (err) {
|
|
121
|
+
throw err;
|
|
122
|
+
}
|
|
123
|
+
});
|
|
124
|
+
return Promise.resolve(app.callback()(req, res));
|
|
125
|
+
};
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
})
|
|
129
|
+
}));
|
|
@@ -0,0 +1,82 @@
|
|
|
1
|
+
import { HttpMethod, useAPIHandlerInfos } from '@modern-js/bff-utils';
|
|
2
|
+
import { isSchemaHandler } from '@modern-js/bff-runtime';
|
|
3
|
+
import typeIs from 'type-is';
|
|
4
|
+
import { sortDynamicRoutes } from '@modern-js/adapter-helpers';
|
|
5
|
+
import { createDebugger } from '@modern-js/utils';
|
|
6
|
+
const debug = createDebugger('koa');
|
|
7
|
+
|
|
8
|
+
const registerRoutes = (router, prefix) => {
|
|
9
|
+
// eslint-disable-next-line react-hooks/rules-of-hooks
|
|
10
|
+
const handlerInfos = useAPIHandlerInfos();
|
|
11
|
+
sortDynamicRoutes(handlerInfos);
|
|
12
|
+
debug('handlerInfos', handlerInfos);
|
|
13
|
+
|
|
14
|
+
if (prefix) {
|
|
15
|
+
router.prefix(prefix);
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
handlerInfos.forEach(({
|
|
19
|
+
handler,
|
|
20
|
+
method,
|
|
21
|
+
name
|
|
22
|
+
}) => {
|
|
23
|
+
const wrappedHandler = async ctx => {
|
|
24
|
+
const input = await getInputFromRequest(ctx);
|
|
25
|
+
|
|
26
|
+
if (isSchemaHandler(handler)) {
|
|
27
|
+
const result = await handler(input);
|
|
28
|
+
|
|
29
|
+
if (result.type !== 'HandleSuccess') {
|
|
30
|
+
if (result.type === 'InputValidationError') {
|
|
31
|
+
ctx.status = 400;
|
|
32
|
+
} else {
|
|
33
|
+
ctx.status = 500;
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
ctx.body = result.message;
|
|
37
|
+
} else {
|
|
38
|
+
ctx.body = result.value;
|
|
39
|
+
}
|
|
40
|
+
} else {
|
|
41
|
+
const args = Object.values(input.params).concat(input); // eslint-disable-next-line @typescript-eslint/ban-ts-comment
|
|
42
|
+
// @ts-expect-error
|
|
43
|
+
|
|
44
|
+
ctx.body = await handler(...args);
|
|
45
|
+
}
|
|
46
|
+
};
|
|
47
|
+
|
|
48
|
+
Object.defineProperties(wrappedHandler, Object.getOwnPropertyDescriptors(handler));
|
|
49
|
+
|
|
50
|
+
if (isNormalMethod(method)) {
|
|
51
|
+
const routeName = method.toLowerCase();
|
|
52
|
+
router[routeName](name, wrappedHandler);
|
|
53
|
+
} else {
|
|
54
|
+
throw new Error(`Unknown HTTP Method: ${method}`);
|
|
55
|
+
}
|
|
56
|
+
});
|
|
57
|
+
};
|
|
58
|
+
|
|
59
|
+
const isNormalMethod = method => Object.keys(HttpMethod).includes(method);
|
|
60
|
+
|
|
61
|
+
export default registerRoutes;
|
|
62
|
+
|
|
63
|
+
const getInputFromRequest = async ctx => {
|
|
64
|
+
const draft = {
|
|
65
|
+
params: ctx.params,
|
|
66
|
+
query: ctx.query,
|
|
67
|
+
headers: ctx.headers,
|
|
68
|
+
cookies: ctx.headers.cookie
|
|
69
|
+
};
|
|
70
|
+
|
|
71
|
+
if (typeIs.is(ctx.request.type, ['application/json'])) {
|
|
72
|
+
draft.data = ctx.request.body;
|
|
73
|
+
} else if (typeIs.is(ctx.request.type, ['multipart/form-data'])) {
|
|
74
|
+
draft.formData = ctx.request.files;
|
|
75
|
+
} else if (typeIs.is(ctx.request.type, ['application/x-www-form-urlencoded'])) {
|
|
76
|
+
draft.formUrlencoded = ctx.request.body;
|
|
77
|
+
} else {
|
|
78
|
+
draft.body = ctx.request.body;
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
return draft;
|
|
82
|
+
};
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export { useContext } from "./context";
|
|
@@ -0,0 +1,76 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, "__esModule", {
|
|
4
|
+
value: true
|
|
5
|
+
});
|
|
6
|
+
exports.default = void 0;
|
|
7
|
+
|
|
8
|
+
var path = _interopRequireWildcard(require("path"));
|
|
9
|
+
|
|
10
|
+
var _utils = require("@modern-js/utils");
|
|
11
|
+
|
|
12
|
+
var _adapterHelpers = require("@modern-js/adapter-helpers");
|
|
13
|
+
|
|
14
|
+
function _getRequireWildcardCache(nodeInterop) { if (typeof WeakMap !== "function") return null; var cacheBabelInterop = new WeakMap(); var cacheNodeInterop = new WeakMap(); return (_getRequireWildcardCache = function (nodeInterop) { return nodeInterop ? cacheNodeInterop : cacheBabelInterop; })(nodeInterop); }
|
|
15
|
+
|
|
16
|
+
function _interopRequireWildcard(obj, nodeInterop) { if (!nodeInterop && obj && obj.__esModule) { return obj; } if (obj === null || typeof obj !== "object" && typeof obj !== "function") { return { default: obj }; } var cache = _getRequireWildcardCache(nodeInterop); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (key !== "default" && Object.prototype.hasOwnProperty.call(obj, key)) { var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null; if (desc && (desc.get || desc.set)) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } newObj.default = obj; if (cache) { cache.set(obj, newObj); } return newObj; }
|
|
17
|
+
|
|
18
|
+
var _default = () => ({
|
|
19
|
+
name: '@modern-js/plugin-koa',
|
|
20
|
+
setup: api => {
|
|
21
|
+
let bffExportsUtils;
|
|
22
|
+
const {
|
|
23
|
+
useAppContext
|
|
24
|
+
} = api;
|
|
25
|
+
const runtimeModulePath = path.resolve(__dirname, '../runtime');
|
|
26
|
+
return {
|
|
27
|
+
config() {
|
|
28
|
+
// eslint-disable-next-line react-hooks/rules-of-hooks
|
|
29
|
+
const appContext = useAppContext();
|
|
30
|
+
const {
|
|
31
|
+
appDirectory
|
|
32
|
+
} = appContext;
|
|
33
|
+
bffExportsUtils = (0, _utils.createRuntimeExportsUtils)(appContext.internalDirectory, 'server');
|
|
34
|
+
const serverRuntimePath = bffExportsUtils.getPath();
|
|
35
|
+
const relativeRuntimePath = (0, _adapterHelpers.getRelativeRuntimePath)(appDirectory, serverRuntimePath);
|
|
36
|
+
return {
|
|
37
|
+
source: {
|
|
38
|
+
alias: {
|
|
39
|
+
'@modern-js/runtime/server': relativeRuntimePath
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
};
|
|
43
|
+
},
|
|
44
|
+
|
|
45
|
+
addRuntimeExports(input) {
|
|
46
|
+
// eslint-disable-next-line react-hooks/rules-of-hooks
|
|
47
|
+
const {
|
|
48
|
+
appDirectory
|
|
49
|
+
} = useAppContext();
|
|
50
|
+
|
|
51
|
+
const runtimePath = require.resolve(`@modern-js/runtime`, {
|
|
52
|
+
paths: [appDirectory]
|
|
53
|
+
});
|
|
54
|
+
|
|
55
|
+
const currentFile = bffExportsUtils.getPath();
|
|
56
|
+
const runtimeDir = path.dirname(runtimePath);
|
|
57
|
+
const relativeBffPath = path.relative(path.dirname(currentFile), path.join(runtimeDir, './exports/server'));
|
|
58
|
+
const relativeRuntimeModulePath = path.relative(path.dirname(currentFile), runtimeModulePath);
|
|
59
|
+
const relativeFramePath = path.relative(path.dirname(currentFile), require.resolve('koa'));
|
|
60
|
+
bffExportsUtils.addExport(`const bffRuntime = require('${relativeBffPath}');
|
|
61
|
+
const pluginRuntime = require('${relativeRuntimeModulePath}');
|
|
62
|
+
const Koa = require('${relativeFramePath}')
|
|
63
|
+
module.exports = {
|
|
64
|
+
Koa: Koa,
|
|
65
|
+
...bffRuntime,
|
|
66
|
+
...pluginRuntime
|
|
67
|
+
}
|
|
68
|
+
`);
|
|
69
|
+
return input;
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
};
|
|
73
|
+
}
|
|
74
|
+
});
|
|
75
|
+
|
|
76
|
+
exports.default = _default;
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, "__esModule", {
|
|
4
|
+
value: true
|
|
5
|
+
});
|
|
6
|
+
exports.useContext = exports.run = void 0;
|
|
7
|
+
|
|
8
|
+
var _adapterHelpers = require("@modern-js/adapter-helpers");
|
|
9
|
+
|
|
10
|
+
const {
|
|
11
|
+
run,
|
|
12
|
+
useContext
|
|
13
|
+
} = (0, _adapterHelpers.createStorage)();
|
|
14
|
+
exports.useContext = useContext;
|
|
15
|
+
exports.run = run;
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, "__esModule", {
|
|
4
|
+
value: true
|
|
5
|
+
});
|
|
6
|
+
var _exportNames = {};
|
|
7
|
+
exports.default = void 0;
|
|
8
|
+
|
|
9
|
+
var _plugin = _interopRequireDefault(require("./plugin"));
|
|
10
|
+
|
|
11
|
+
var _context = require("./context");
|
|
12
|
+
|
|
13
|
+
Object.keys(_context).forEach(function (key) {
|
|
14
|
+
if (key === "default" || key === "__esModule") return;
|
|
15
|
+
if (Object.prototype.hasOwnProperty.call(_exportNames, key)) return;
|
|
16
|
+
if (key in exports && exports[key] === _context[key]) return;
|
|
17
|
+
Object.defineProperty(exports, key, {
|
|
18
|
+
enumerable: true,
|
|
19
|
+
get: function () {
|
|
20
|
+
return _context[key];
|
|
21
|
+
}
|
|
22
|
+
});
|
|
23
|
+
});
|
|
24
|
+
|
|
25
|
+
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
|
|
26
|
+
|
|
27
|
+
var _default = _plugin.default;
|
|
28
|
+
exports.default = _default;
|
|
@@ -0,0 +1,151 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, "__esModule", {
|
|
4
|
+
value: true
|
|
5
|
+
});
|
|
6
|
+
exports.default = void 0;
|
|
7
|
+
|
|
8
|
+
var path = _interopRequireWildcard(require("path"));
|
|
9
|
+
|
|
10
|
+
var _koa = _interopRequireDefault(require("koa"));
|
|
11
|
+
|
|
12
|
+
var _koaRouter = _interopRequireDefault(require("koa-router"));
|
|
13
|
+
|
|
14
|
+
var _koaBody = _interopRequireDefault(require("koa-body"));
|
|
15
|
+
|
|
16
|
+
var _bffUtils = require("@modern-js/bff-utils");
|
|
17
|
+
|
|
18
|
+
var _utils = require("@modern-js/utils");
|
|
19
|
+
|
|
20
|
+
var _context = require("./context");
|
|
21
|
+
|
|
22
|
+
var _registerRoutes = _interopRequireDefault(require("./registerRoutes"));
|
|
23
|
+
|
|
24
|
+
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
|
|
25
|
+
|
|
26
|
+
function _getRequireWildcardCache(nodeInterop) { if (typeof WeakMap !== "function") return null; var cacheBabelInterop = new WeakMap(); var cacheNodeInterop = new WeakMap(); return (_getRequireWildcardCache = function (nodeInterop) { return nodeInterop ? cacheNodeInterop : cacheBabelInterop; })(nodeInterop); }
|
|
27
|
+
|
|
28
|
+
function _interopRequireWildcard(obj, nodeInterop) { if (!nodeInterop && obj && obj.__esModule) { return obj; } if (obj === null || typeof obj !== "object" && typeof obj !== "function") { return { default: obj }; } var cache = _getRequireWildcardCache(nodeInterop); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (key !== "default" && Object.prototype.hasOwnProperty.call(obj, key)) { var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null; if (desc && (desc.get || desc.set)) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } newObj.default = obj; if (cache) { cache.set(obj, newObj); } return newObj; }
|
|
29
|
+
|
|
30
|
+
const findAppModule = async apiDir => {
|
|
31
|
+
const exts = ['.ts', '.js'];
|
|
32
|
+
const paths = exts.map(ext => path.join(apiDir, `app${ext}`));
|
|
33
|
+
|
|
34
|
+
for (const filename of paths) {
|
|
35
|
+
if (await _utils.fs.pathExists(filename)) {
|
|
36
|
+
return (0, _bffUtils.requireModule)(filename);
|
|
37
|
+
}
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
return null;
|
|
41
|
+
};
|
|
42
|
+
|
|
43
|
+
const initMiddlewares = (middleware, app) => {
|
|
44
|
+
middleware.forEach(middlewareItem => {
|
|
45
|
+
const middlewareFunc = typeof middlewareItem === 'string' ? (0, _bffUtils.requireModule)(middlewareItem) : middlewareItem;
|
|
46
|
+
app.use(middlewareFunc);
|
|
47
|
+
});
|
|
48
|
+
};
|
|
49
|
+
|
|
50
|
+
var _default = () => ({
|
|
51
|
+
name: '@modern-js/plugin-koa',
|
|
52
|
+
pre: ['@modern-js/plugin-bff'],
|
|
53
|
+
setup: () => ({
|
|
54
|
+
async prepareApiServer({
|
|
55
|
+
pwd,
|
|
56
|
+
mode,
|
|
57
|
+
config,
|
|
58
|
+
prefix
|
|
59
|
+
}) {
|
|
60
|
+
let app;
|
|
61
|
+
const router = new _koaRouter.default();
|
|
62
|
+
const apiDir = path.join(pwd, './api');
|
|
63
|
+
|
|
64
|
+
if (mode === 'framework') {
|
|
65
|
+
app = await findAppModule(apiDir);
|
|
66
|
+
|
|
67
|
+
if (!(app instanceof _koa.default)) {
|
|
68
|
+
app = new _koa.default();
|
|
69
|
+
app.use((0, _koaBody.default)({
|
|
70
|
+
multipart: true
|
|
71
|
+
}));
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
if (config) {
|
|
75
|
+
const {
|
|
76
|
+
middleware
|
|
77
|
+
} = config;
|
|
78
|
+
initMiddlewares(middleware, app);
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
app.use(_context.run);
|
|
82
|
+
(0, _registerRoutes.default)(router, prefix);
|
|
83
|
+
} else if (mode === 'function') {
|
|
84
|
+
app = new _koa.default();
|
|
85
|
+
app.use((0, _koaBody.default)({
|
|
86
|
+
multipart: true
|
|
87
|
+
}));
|
|
88
|
+
|
|
89
|
+
if (config) {
|
|
90
|
+
const {
|
|
91
|
+
middleware
|
|
92
|
+
} = config;
|
|
93
|
+
initMiddlewares(middleware, app);
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
app.use(_context.run);
|
|
97
|
+
(0, _registerRoutes.default)(router, prefix);
|
|
98
|
+
} else {
|
|
99
|
+
throw new Error(`mode must be function or framework`);
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
app.use(router.routes());
|
|
103
|
+
return (req, res) => {
|
|
104
|
+
app.on('error', err => {
|
|
105
|
+
if (err) {
|
|
106
|
+
throw err;
|
|
107
|
+
}
|
|
108
|
+
});
|
|
109
|
+
return Promise.resolve(app.callback()(req, res));
|
|
110
|
+
};
|
|
111
|
+
},
|
|
112
|
+
|
|
113
|
+
prepareWebServer({
|
|
114
|
+
config
|
|
115
|
+
}) {
|
|
116
|
+
const app = new _koa.default();
|
|
117
|
+
app.use(async (ctx, next) => {
|
|
118
|
+
await next();
|
|
119
|
+
|
|
120
|
+
if (!ctx.body) {
|
|
121
|
+
// restore statusCode
|
|
122
|
+
if (ctx.res.statusCode === 404 && !ctx.response._explicitStatus) {
|
|
123
|
+
ctx.res.statusCode = 200;
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
ctx.respond = false;
|
|
127
|
+
}
|
|
128
|
+
});
|
|
129
|
+
app.use((0, _koaBody.default)());
|
|
130
|
+
|
|
131
|
+
if (config) {
|
|
132
|
+
const {
|
|
133
|
+
middleware
|
|
134
|
+
} = config;
|
|
135
|
+
initMiddlewares(middleware, app);
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
return (req, res) => {
|
|
139
|
+
app.on('error', err => {
|
|
140
|
+
if (err) {
|
|
141
|
+
throw err;
|
|
142
|
+
}
|
|
143
|
+
});
|
|
144
|
+
return Promise.resolve(app.callback()(req, res));
|
|
145
|
+
};
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
})
|
|
149
|
+
});
|
|
150
|
+
|
|
151
|
+
exports.default = _default;
|
|
@@ -0,0 +1,97 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, "__esModule", {
|
|
4
|
+
value: true
|
|
5
|
+
});
|
|
6
|
+
exports.default = void 0;
|
|
7
|
+
|
|
8
|
+
var _bffUtils = require("@modern-js/bff-utils");
|
|
9
|
+
|
|
10
|
+
var _bffRuntime = require("@modern-js/bff-runtime");
|
|
11
|
+
|
|
12
|
+
var _typeIs = _interopRequireDefault(require("type-is"));
|
|
13
|
+
|
|
14
|
+
var _adapterHelpers = require("@modern-js/adapter-helpers");
|
|
15
|
+
|
|
16
|
+
var _utils = require("@modern-js/utils");
|
|
17
|
+
|
|
18
|
+
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
|
|
19
|
+
|
|
20
|
+
const debug = (0, _utils.createDebugger)('koa');
|
|
21
|
+
|
|
22
|
+
const registerRoutes = (router, prefix) => {
|
|
23
|
+
// eslint-disable-next-line react-hooks/rules-of-hooks
|
|
24
|
+
const handlerInfos = (0, _bffUtils.useAPIHandlerInfos)();
|
|
25
|
+
(0, _adapterHelpers.sortDynamicRoutes)(handlerInfos);
|
|
26
|
+
debug('handlerInfos', handlerInfos);
|
|
27
|
+
|
|
28
|
+
if (prefix) {
|
|
29
|
+
router.prefix(prefix);
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
handlerInfos.forEach(({
|
|
33
|
+
handler,
|
|
34
|
+
method,
|
|
35
|
+
name
|
|
36
|
+
}) => {
|
|
37
|
+
const wrappedHandler = async ctx => {
|
|
38
|
+
const input = await getInputFromRequest(ctx);
|
|
39
|
+
|
|
40
|
+
if ((0, _bffRuntime.isSchemaHandler)(handler)) {
|
|
41
|
+
const result = await handler(input);
|
|
42
|
+
|
|
43
|
+
if (result.type !== 'HandleSuccess') {
|
|
44
|
+
if (result.type === 'InputValidationError') {
|
|
45
|
+
ctx.status = 400;
|
|
46
|
+
} else {
|
|
47
|
+
ctx.status = 500;
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
ctx.body = result.message;
|
|
51
|
+
} else {
|
|
52
|
+
ctx.body = result.value;
|
|
53
|
+
}
|
|
54
|
+
} else {
|
|
55
|
+
const args = Object.values(input.params).concat(input); // eslint-disable-next-line @typescript-eslint/ban-ts-comment
|
|
56
|
+
// @ts-expect-error
|
|
57
|
+
|
|
58
|
+
ctx.body = await handler(...args);
|
|
59
|
+
}
|
|
60
|
+
};
|
|
61
|
+
|
|
62
|
+
Object.defineProperties(wrappedHandler, Object.getOwnPropertyDescriptors(handler));
|
|
63
|
+
|
|
64
|
+
if (isNormalMethod(method)) {
|
|
65
|
+
const routeName = method.toLowerCase();
|
|
66
|
+
router[routeName](name, wrappedHandler);
|
|
67
|
+
} else {
|
|
68
|
+
throw new Error(`Unknown HTTP Method: ${method}`);
|
|
69
|
+
}
|
|
70
|
+
});
|
|
71
|
+
};
|
|
72
|
+
|
|
73
|
+
const isNormalMethod = method => Object.keys(_bffUtils.HttpMethod).includes(method);
|
|
74
|
+
|
|
75
|
+
var _default = registerRoutes;
|
|
76
|
+
exports.default = _default;
|
|
77
|
+
|
|
78
|
+
const getInputFromRequest = async ctx => {
|
|
79
|
+
const draft = {
|
|
80
|
+
params: ctx.params,
|
|
81
|
+
query: ctx.query,
|
|
82
|
+
headers: ctx.headers,
|
|
83
|
+
cookies: ctx.headers.cookie
|
|
84
|
+
};
|
|
85
|
+
|
|
86
|
+
if (_typeIs.default.is(ctx.request.type, ['application/json'])) {
|
|
87
|
+
draft.data = ctx.request.body;
|
|
88
|
+
} else if (_typeIs.default.is(ctx.request.type, ['multipart/form-data'])) {
|
|
89
|
+
draft.formData = ctx.request.files;
|
|
90
|
+
} else if (_typeIs.default.is(ctx.request.type, ['application/x-www-form-urlencoded'])) {
|
|
91
|
+
draft.formUrlencoded = ctx.request.body;
|
|
92
|
+
} else {
|
|
93
|
+
draft.body = ctx.request.body;
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
return draft;
|
|
97
|
+
};
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, "__esModule", {
|
|
4
|
+
value: true
|
|
5
|
+
});
|
|
6
|
+
Object.defineProperty(exports, "useContext", {
|
|
7
|
+
enumerable: true,
|
|
8
|
+
get: function () {
|
|
9
|
+
return _context.useContext;
|
|
10
|
+
}
|
|
11
|
+
});
|
|
12
|
+
|
|
13
|
+
var _context = require("./context");
|