@modern-js/plugin-koa 1.2.1-rc.0 → 1.4.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 +64 -9
- package/dist/js/modern/cli/index.js +1 -1
- package/dist/js/modern/plugin.js +1 -1
- package/dist/js/node/cli/index.js +1 -1
- package/dist/js/node/plugin.js +2 -2
- package/dist/types/cli/index.d.ts +2 -0
- package/jest.config.js +8 -0
- package/modern.config.js +1 -9
- package/package.json +23 -16
- package/tests/fixtures/function-mode/api/nest/user.ts +1 -1
- package/tests/fixtures/lambda-mode/api/lambda/nest/user.ts +1 -1
- package/tests/functionMode.test.ts +1 -1
- package/tests/helpers.ts +1 -1
- package/tests/lambdaMode.test.ts +4 -4
- package/tests/tsconfig.json +1 -3
- package/tests/webServer.test.ts +1 -1
- package/types.d.ts +12 -2
- package/src/cli/index.ts +0 -85
- package/src/context.ts +0 -9
- package/src/index.ts +0 -5
- package/src/plugin.ts +0 -125
- package/src/registerRoutes.ts +0 -109
- package/src/runtime.ts +0 -1
package/CHANGELOG.md
CHANGED
|
@@ -1,17 +1,72 @@
|
|
|
1
1
|
# @modern-js/plugin-koa
|
|
2
2
|
|
|
3
|
-
## 1.
|
|
3
|
+
## 1.4.0
|
|
4
|
+
|
|
5
|
+
### Minor Changes
|
|
6
|
+
|
|
7
|
+
- ec4dbffb: feat: support as a pure api service
|
|
4
8
|
|
|
5
9
|
### Patch Changes
|
|
6
10
|
|
|
7
|
-
-
|
|
8
|
-
- Updated dependencies [
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
11
|
+
- 272cab15: refactor server plugin manager
|
|
12
|
+
- Updated dependencies [d9cc5ea9]
|
|
13
|
+
- Updated dependencies [bd819a8d]
|
|
14
|
+
- Updated dependencies [ec4dbffb]
|
|
15
|
+
- Updated dependencies [d099e5c5]
|
|
16
|
+
- Updated dependencies [bada2879]
|
|
17
|
+
- Updated dependencies [24f616ca]
|
|
18
|
+
- Updated dependencies [bd819a8d]
|
|
19
|
+
- Updated dependencies [272cab15]
|
|
20
|
+
- @modern-js/core@1.4.0
|
|
21
|
+
- @modern-js/utils@1.3.0
|
|
22
|
+
- @modern-js/server-core@1.2.2
|
|
23
|
+
|
|
24
|
+
## 1.3.1
|
|
25
|
+
|
|
26
|
+
### Patch Changes
|
|
27
|
+
|
|
28
|
+
- 83166714: change .npmignore
|
|
29
|
+
- Updated dependencies [83166714]
|
|
30
|
+
- Updated dependencies [c3de9882]
|
|
31
|
+
- Updated dependencies [33ff48af]
|
|
32
|
+
- @modern-js/core@1.3.2
|
|
33
|
+
- @modern-js/adapter-helpers@1.2.1
|
|
34
|
+
- @modern-js/bff-runtime@1.2.1
|
|
35
|
+
- @modern-js/bff-utils@1.2.2
|
|
36
|
+
- @modern-js/server-plugin@1.2.1
|
|
37
|
+
- @modern-js/utils@1.2.2
|
|
38
|
+
|
|
39
|
+
## 1.3.0
|
|
40
|
+
|
|
41
|
+
### Minor Changes
|
|
42
|
+
|
|
43
|
+
- cfe11628: Make Modern.js self bootstraping
|
|
44
|
+
|
|
45
|
+
### Patch Changes
|
|
46
|
+
|
|
47
|
+
- 146dcd85: modify server framework plugin hook types and hook context
|
|
48
|
+
- 146dcd85: modify server framework plugin hook types
|
|
49
|
+
- 146dcd85: fix test case in babel compiler
|
|
50
|
+
- Updated dependencies [2da09c69]
|
|
51
|
+
- Updated dependencies [fc71e36f]
|
|
52
|
+
- Updated dependencies [146dcd85]
|
|
53
|
+
- Updated dependencies [c3d46ee4]
|
|
54
|
+
- Updated dependencies [cfe11628]
|
|
55
|
+
- Updated dependencies [146dcd85]
|
|
56
|
+
- Updated dependencies [8e7603ee]
|
|
57
|
+
- Updated dependencies [146dcd85]
|
|
58
|
+
- @modern-js/utils@1.2.0
|
|
59
|
+
- @modern-js/core@1.3.0
|
|
60
|
+
- @modern-js/bff-runtime@1.2.0
|
|
61
|
+
- @modern-js/adapter-helpers@1.2.0
|
|
62
|
+
- @modern-js/bff-utils@1.2.0
|
|
63
|
+
- @modern-js/server-plugin@1.2.0
|
|
64
|
+
|
|
65
|
+
## 1.2.1
|
|
66
|
+
|
|
67
|
+
### Patch Changes
|
|
68
|
+
|
|
69
|
+
- ad107726: feat: new action support apppend type define
|
|
15
70
|
|
|
16
71
|
## 1.2.0
|
|
17
72
|
|
package/dist/js/modern/plugin.js
CHANGED
|
@@ -2,7 +2,7 @@ import * as path from 'path';
|
|
|
2
2
|
import Koa from 'koa';
|
|
3
3
|
import Router from 'koa-router';
|
|
4
4
|
import koaBody from 'koa-body';
|
|
5
|
-
import { createPlugin } from '@modern-js/server-
|
|
5
|
+
import { createPlugin } from '@modern-js/server-core';
|
|
6
6
|
import { requireModule } from '@modern-js/bff-utils';
|
|
7
7
|
import { fs } from '@modern-js/utils';
|
|
8
8
|
import { run } from "./context";
|
package/dist/js/node/plugin.js
CHANGED
|
@@ -13,7 +13,7 @@ var _koaRouter = _interopRequireDefault(require("koa-router"));
|
|
|
13
13
|
|
|
14
14
|
var _koaBody = _interopRequireDefault(require("koa-body"));
|
|
15
15
|
|
|
16
|
-
var
|
|
16
|
+
var _serverCore = require("@modern-js/server-core");
|
|
17
17
|
|
|
18
18
|
var _bffUtils = require("@modern-js/bff-utils");
|
|
19
19
|
|
|
@@ -49,7 +49,7 @@ const initMiddlewares = (middleware, app) => {
|
|
|
49
49
|
});
|
|
50
50
|
};
|
|
51
51
|
|
|
52
|
-
var _default = (0,
|
|
52
|
+
var _default = (0, _serverCore.createPlugin)(() => ({
|
|
53
53
|
// eslint-disable-next-line max-statements
|
|
54
54
|
async prepareApiServer({
|
|
55
55
|
pwd,
|
|
@@ -11,8 +11,10 @@ declare const _default: import("@modern-js/core").AsyncPlugin<Partial<import("@m
|
|
|
11
11
|
watchFiles: import("@modern-js/core").ParallelWorkflow<void, unknown>;
|
|
12
12
|
fileChange: import("@modern-js/core").AsyncWorkflow<{
|
|
13
13
|
filename: string;
|
|
14
|
+
eventType: "add" | "unlink" | "change";
|
|
14
15
|
}, void>;
|
|
15
16
|
beforeExit: import("@modern-js/core").AsyncWorkflow<void, void>;
|
|
17
|
+
beforeRestart: import("@modern-js/core").AsyncWorkflow<void, void>;
|
|
16
18
|
} & import("@modern-js/core").ClearDraftProgress<import("@modern-js/core").Hooks>>>>;
|
|
17
19
|
|
|
18
20
|
export default _default;
|
package/jest.config.js
ADDED
package/modern.config.js
CHANGED
package/package.json
CHANGED
|
@@ -11,26 +11,31 @@
|
|
|
11
11
|
"modern",
|
|
12
12
|
"modern.js"
|
|
13
13
|
],
|
|
14
|
-
"version": "1.
|
|
14
|
+
"version": "1.4.0",
|
|
15
15
|
"jsnext:source": "./src/index.ts",
|
|
16
16
|
"types": "./dist/types/index.d.ts",
|
|
17
17
|
"main": "./dist/js/node/index.js",
|
|
18
18
|
"module": "./dist/js/treeshaking/index.js",
|
|
19
19
|
"jsnext:modern": "./dist/js/modern/index.js",
|
|
20
20
|
"exports": {
|
|
21
|
+
"./types": "./types.d.ts",
|
|
21
22
|
".": {
|
|
22
23
|
"node": {
|
|
24
|
+
"jsnext:source": "./src/index.ts",
|
|
23
25
|
"import": "./dist/js/modern/index.js",
|
|
24
26
|
"require": "./dist/js/node/index.js"
|
|
25
27
|
},
|
|
26
28
|
"default": "./dist/js/treeshaking/index.js"
|
|
27
29
|
},
|
|
28
|
-
"./cli":
|
|
30
|
+
"./cli": {
|
|
31
|
+
"jsnext:source": "./src/cli/index.ts",
|
|
32
|
+
"default": "./dist/js/node/cli/index.js"
|
|
33
|
+
}
|
|
29
34
|
},
|
|
30
35
|
"dependencies": {
|
|
31
36
|
"@babel/runtime": "^7.15.3",
|
|
32
|
-
"@modern-js/adapter-helpers": "^1.
|
|
33
|
-
"@modern-js/utils": "^1.
|
|
37
|
+
"@modern-js/adapter-helpers": "^1.2.1",
|
|
38
|
+
"@modern-js/utils": "^1.3.0",
|
|
34
39
|
"formidable": "^1.2.2",
|
|
35
40
|
"koa-body": "^4.2.0",
|
|
36
41
|
"koa-router": "^10.0.0",
|
|
@@ -47,12 +52,13 @@
|
|
|
47
52
|
"@types/type-is": "^1.6.3",
|
|
48
53
|
"supertest": "^6.1.6",
|
|
49
54
|
"typescript": "^4",
|
|
50
|
-
"@modern-js/bff-runtime": "^1.
|
|
51
|
-
"@modern-js/bff-utils": "^1.
|
|
52
|
-
"@modern-js/core": "^1.
|
|
53
|
-
"@modern-js/server-
|
|
54
|
-
"@
|
|
55
|
-
"
|
|
55
|
+
"@modern-js/bff-runtime": "^1.2.1",
|
|
56
|
+
"@modern-js/bff-utils": "^1.2.2",
|
|
57
|
+
"@modern-js/core": "^1.4.0",
|
|
58
|
+
"@modern-js/server-core": "^1.2.2",
|
|
59
|
+
"@scripts/build": "0.0.0",
|
|
60
|
+
"jest": "^27",
|
|
61
|
+
"@scripts/jest-config": "0.0.0"
|
|
56
62
|
},
|
|
57
63
|
"modernConfig": {
|
|
58
64
|
"output": {
|
|
@@ -60,20 +66,21 @@
|
|
|
60
66
|
}
|
|
61
67
|
},
|
|
62
68
|
"peerDependencies": {
|
|
63
|
-
"@modern-js/bff-utils": "^1.
|
|
64
|
-
"@modern-js/core": "^1.
|
|
65
|
-
"@modern-js/server-
|
|
66
|
-
"@modern-js/bff-runtime": "^1.
|
|
69
|
+
"@modern-js/bff-utils": "^1.2.2",
|
|
70
|
+
"@modern-js/core": "^1.4.0",
|
|
71
|
+
"@modern-js/server-core": "^1.2.2",
|
|
72
|
+
"@modern-js/bff-runtime": "^1.2.1",
|
|
67
73
|
"koa": "^2.13.3"
|
|
68
74
|
},
|
|
69
75
|
"publishConfig": {
|
|
70
76
|
"registry": "https://registry.npmjs.org/",
|
|
71
|
-
"access": "public"
|
|
77
|
+
"access": "public",
|
|
78
|
+
"types": "./dist/types/index.d.ts"
|
|
72
79
|
},
|
|
73
80
|
"scripts": {
|
|
74
81
|
"new": "modern new",
|
|
75
82
|
"build": "modern build",
|
|
76
|
-
"test": "
|
|
83
|
+
"test": "jest --passWithNoTests"
|
|
77
84
|
},
|
|
78
85
|
"readme": "\n<p align=\"center\">\n <a href=\"https://modernjs.dev\" target=\"blank\"><img src=\"https://lf3-static.bytednsdoc.com/obj/eden-cn/ylaelkeh7nuhfnuhf/modernjs-cover.png\" width=\"300\" alt=\"Modern.js Logo\" /></a>\n</p>\n<p align=\"center\">\n现代 Web 工程体系\n <br/>\n <a href=\"https://modernjs.dev\" target=\"blank\">\n modernjs.dev\n </a>\n</p>\n<p align=\"center\">\n The meta-framework suite designed from scratch for frontend-focused modern web development\n</p>\n\n# Introduction\n\n> The doc site ([modernjs.dev](https://modernjs.dev)) and articles are only available in Chinese for now, we are planning to add English versions soon.\n\n- [Modern.js: Hello, World!](https://zhuanlan.zhihu.com/p/426707646)\n\n## Getting Started\n\n- [Quick Start](https://modernjs.dev/docs/start)\n- [Guides](https://modernjs.dev/docs/guides)\n- [API References](https://modernjs.dev/docs/apis)\n\n## Contributing\n\n- [Contributing Guide](https://github.com/modern-js-dev/modern.js/blob/main/CONTRIBUTING.md)\n"
|
|
79
86
|
}
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import * as path from 'path';
|
|
2
2
|
import request from 'supertest';
|
|
3
|
-
import { serverManager } from '@modern-js/server-
|
|
3
|
+
import { serverManager } from '@modern-js/server-core';
|
|
4
4
|
import plugin from '../src/plugin';
|
|
5
5
|
import { APIPlugin } from './helpers';
|
|
6
6
|
import './common';
|
package/tests/helpers.ts
CHANGED
package/tests/lambdaMode.test.ts
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import * as path from 'path';
|
|
2
2
|
import request from 'supertest';
|
|
3
|
-
import Koa
|
|
4
|
-
import { serverManager } from '@modern-js/server-
|
|
3
|
+
import Koa from 'koa';
|
|
4
|
+
import { serverManager } from '@modern-js/server-core';
|
|
5
5
|
import Router from 'koa-router';
|
|
6
6
|
import koaBody from 'koa-body';
|
|
7
7
|
import plugin from '../src/plugin';
|
|
@@ -111,11 +111,11 @@ describe('add middlewares', () => {
|
|
|
111
111
|
|
|
112
112
|
test('should works', async () => {
|
|
113
113
|
const foo = 'foo';
|
|
114
|
-
const fakeMiddleware = jest.fn(async (ctx:
|
|
114
|
+
const fakeMiddleware = jest.fn(async (ctx: any, next: any) => {
|
|
115
115
|
await next();
|
|
116
116
|
ctx.body = foo;
|
|
117
117
|
});
|
|
118
|
-
const fakeMiddleware2 = jest.fn(async (ctx:
|
|
118
|
+
const fakeMiddleware2 = jest.fn(async (ctx: any, next: any) => {
|
|
119
119
|
await next();
|
|
120
120
|
});
|
|
121
121
|
|
package/tests/tsconfig.json
CHANGED
package/tests/webServer.test.ts
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import * as path from 'path';
|
|
2
2
|
import fs from 'fs';
|
|
3
3
|
import request from 'supertest';
|
|
4
|
-
import { serverManager } from '@modern-js/server-
|
|
4
|
+
import { serverManager } from '@modern-js/server-core';
|
|
5
5
|
import plugin from '../src/plugin';
|
|
6
6
|
import { APIPlugin } from './helpers';
|
|
7
7
|
import './common';
|
package/types.d.ts
CHANGED
|
@@ -1,7 +1,17 @@
|
|
|
1
1
|
/// <reference path="./dist/types/index.d.ts" />
|
|
2
|
-
|
|
3
2
|
declare module '@modern-js/runtime/server' {
|
|
4
|
-
|
|
3
|
+
|
|
4
|
+
import { Context, Middleware } from 'koa';
|
|
5
|
+
|
|
6
|
+
type KoaOptions = {
|
|
7
|
+
addMiddleware: (...input: Middleware[]) => void;
|
|
8
|
+
};
|
|
9
|
+
|
|
10
|
+
type KoaAttacher = (options: KoaOptions) => void;
|
|
5
11
|
|
|
6
12
|
export function useContext(): Context;
|
|
13
|
+
|
|
14
|
+
export function hook(attacher: KoaAttacher): KoaAttacher;
|
|
15
|
+
|
|
16
|
+
export * from '@modern-js/bff-runtime';
|
|
7
17
|
}
|
package/src/cli/index.ts
DELETED
|
@@ -1,85 +0,0 @@
|
|
|
1
|
-
import * as path from 'path';
|
|
2
|
-
import { useAppContext, createPlugin } from '@modern-js/core';
|
|
3
|
-
import { createRuntimeExportsUtils } from '@modern-js/utils';
|
|
4
|
-
|
|
5
|
-
export default createPlugin(
|
|
6
|
-
() => {
|
|
7
|
-
let bffExportsUtils: any;
|
|
8
|
-
const runtimeModulePath = path.resolve(__dirname, '../runtime');
|
|
9
|
-
|
|
10
|
-
return {
|
|
11
|
-
config() {
|
|
12
|
-
// eslint-disable-next-line react-hooks/rules-of-hooks
|
|
13
|
-
const appContext = useAppContext();
|
|
14
|
-
const { appDirectory } = appContext;
|
|
15
|
-
bffExportsUtils = createRuntimeExportsUtils(
|
|
16
|
-
appContext.internalDirectory,
|
|
17
|
-
'server',
|
|
18
|
-
);
|
|
19
|
-
|
|
20
|
-
const serverRuntimePath = bffExportsUtils.getPath();
|
|
21
|
-
|
|
22
|
-
// Look up one level, because the artifacts after build have dist directories
|
|
23
|
-
let relativeRuntimePath = path.join(
|
|
24
|
-
'../',
|
|
25
|
-
path.relative(appDirectory, serverRuntimePath),
|
|
26
|
-
);
|
|
27
|
-
|
|
28
|
-
if (
|
|
29
|
-
process.env.NODE_ENV === 'development' ||
|
|
30
|
-
process.env.NODE_ENV === 'test'
|
|
31
|
-
) {
|
|
32
|
-
relativeRuntimePath = `./${path.relative(
|
|
33
|
-
appDirectory,
|
|
34
|
-
serverRuntimePath,
|
|
35
|
-
)}`;
|
|
36
|
-
}
|
|
37
|
-
|
|
38
|
-
return {
|
|
39
|
-
source: {
|
|
40
|
-
alias: {
|
|
41
|
-
'@modern-js/runtime/server': relativeRuntimePath,
|
|
42
|
-
},
|
|
43
|
-
},
|
|
44
|
-
};
|
|
45
|
-
},
|
|
46
|
-
modifyEntryImports(input) {
|
|
47
|
-
// eslint-disable-next-line react-hooks/rules-of-hooks
|
|
48
|
-
const { appDirectory } = useAppContext();
|
|
49
|
-
const runtimePath = require.resolve(`@modern-js/runtime`, {
|
|
50
|
-
paths: [appDirectory],
|
|
51
|
-
});
|
|
52
|
-
|
|
53
|
-
const currentFile = bffExportsUtils.getPath();
|
|
54
|
-
|
|
55
|
-
const runtimeDir = path.dirname(runtimePath);
|
|
56
|
-
|
|
57
|
-
const relativeBffPath = path.relative(
|
|
58
|
-
path.dirname(currentFile),
|
|
59
|
-
path.join(runtimeDir, './exports/server'),
|
|
60
|
-
);
|
|
61
|
-
const relativeRuntimeModulePath = path.relative(
|
|
62
|
-
path.dirname(currentFile),
|
|
63
|
-
runtimeModulePath,
|
|
64
|
-
);
|
|
65
|
-
|
|
66
|
-
const relativeFramePath = path.relative(
|
|
67
|
-
path.dirname(currentFile),
|
|
68
|
-
require.resolve('koa'),
|
|
69
|
-
);
|
|
70
|
-
|
|
71
|
-
bffExportsUtils.addExport(`const bffRuntime = require('${relativeBffPath}');
|
|
72
|
-
const pluginRuntime = require('${relativeRuntimeModulePath}');
|
|
73
|
-
const Koa = require('${relativeFramePath}')
|
|
74
|
-
module.exports = {
|
|
75
|
-
Koa: Koa,
|
|
76
|
-
...bffRuntime,
|
|
77
|
-
...pluginRuntime
|
|
78
|
-
}
|
|
79
|
-
`);
|
|
80
|
-
return input;
|
|
81
|
-
},
|
|
82
|
-
};
|
|
83
|
-
},
|
|
84
|
-
{ name: '@modern-js/plugin-koa' },
|
|
85
|
-
);
|
package/src/context.ts
DELETED
package/src/index.ts
DELETED
package/src/plugin.ts
DELETED
|
@@ -1,125 +0,0 @@
|
|
|
1
|
-
import * as path from 'path';
|
|
2
|
-
import Koa, { Middleware } from 'koa';
|
|
3
|
-
import type Application from 'koa';
|
|
4
|
-
import Router from 'koa-router';
|
|
5
|
-
import koaBody from 'koa-body';
|
|
6
|
-
import { createPlugin } from '@modern-js/server-plugin';
|
|
7
|
-
import { requireModule } from '@modern-js/bff-utils';
|
|
8
|
-
import { fs } from '@modern-js/utils';
|
|
9
|
-
import { run } from './context';
|
|
10
|
-
import registerRoutes from './registerRoutes';
|
|
11
|
-
|
|
12
|
-
interface FrameConfig {
|
|
13
|
-
middleware: (Middleware | string)[];
|
|
14
|
-
}
|
|
15
|
-
|
|
16
|
-
const findAppModule = async (apiDir: string) => {
|
|
17
|
-
const exts = ['.ts', '.js'];
|
|
18
|
-
const paths = exts.map(ext => path.join(apiDir, `app${ext}`));
|
|
19
|
-
|
|
20
|
-
for (const filename of paths) {
|
|
21
|
-
if (await fs.pathExists(filename)) {
|
|
22
|
-
return requireModule(filename);
|
|
23
|
-
}
|
|
24
|
-
}
|
|
25
|
-
|
|
26
|
-
return null;
|
|
27
|
-
};
|
|
28
|
-
|
|
29
|
-
const initMiddlewares = (
|
|
30
|
-
middleware: (Middleware | string)[],
|
|
31
|
-
app: Application,
|
|
32
|
-
) => {
|
|
33
|
-
middleware.forEach(middlewareItem => {
|
|
34
|
-
const middlewareFunc =
|
|
35
|
-
typeof middlewareItem === 'string'
|
|
36
|
-
? requireModule(middlewareItem)
|
|
37
|
-
: middlewareItem;
|
|
38
|
-
app.use(middlewareFunc);
|
|
39
|
-
});
|
|
40
|
-
};
|
|
41
|
-
|
|
42
|
-
export type Mode = 'function' | 'framework';
|
|
43
|
-
|
|
44
|
-
export default createPlugin(
|
|
45
|
-
() => ({
|
|
46
|
-
// eslint-disable-next-line max-statements
|
|
47
|
-
async prepareApiServer({ pwd, mode, config, prefix }) {
|
|
48
|
-
let app: Application;
|
|
49
|
-
const router = new Router();
|
|
50
|
-
const apiDir = path.join(pwd, './api');
|
|
51
|
-
|
|
52
|
-
if (mode === 'framework') {
|
|
53
|
-
app = await findAppModule(apiDir);
|
|
54
|
-
if (!(app instanceof Koa)) {
|
|
55
|
-
app = new Koa();
|
|
56
|
-
app.use(koaBody());
|
|
57
|
-
}
|
|
58
|
-
|
|
59
|
-
if (config) {
|
|
60
|
-
const { middleware } = config as FrameConfig;
|
|
61
|
-
initMiddlewares(middleware, app);
|
|
62
|
-
}
|
|
63
|
-
|
|
64
|
-
app.use(run);
|
|
65
|
-
registerRoutes(router, prefix as string);
|
|
66
|
-
} else if (mode === 'function') {
|
|
67
|
-
app = new Koa();
|
|
68
|
-
app.use(koaBody());
|
|
69
|
-
if (config) {
|
|
70
|
-
const { middleware } = config as FrameConfig;
|
|
71
|
-
initMiddlewares(middleware, app);
|
|
72
|
-
}
|
|
73
|
-
|
|
74
|
-
app.use(run);
|
|
75
|
-
registerRoutes(router, prefix as string);
|
|
76
|
-
} else {
|
|
77
|
-
throw new Error(`mode must be function or framework`);
|
|
78
|
-
}
|
|
79
|
-
|
|
80
|
-
app.use(router.routes());
|
|
81
|
-
|
|
82
|
-
return (req, res) => {
|
|
83
|
-
app.on('error', err => {
|
|
84
|
-
if (err) {
|
|
85
|
-
throw err;
|
|
86
|
-
}
|
|
87
|
-
});
|
|
88
|
-
return Promise.resolve(app.callback()(req, res));
|
|
89
|
-
};
|
|
90
|
-
},
|
|
91
|
-
prepareWebServer({ config }) {
|
|
92
|
-
const app: Application = new Koa();
|
|
93
|
-
|
|
94
|
-
app.use(async (ctx, next) => {
|
|
95
|
-
await next();
|
|
96
|
-
if (!ctx.body) {
|
|
97
|
-
// restore statusCode
|
|
98
|
-
if (ctx.res.statusCode === 404) {
|
|
99
|
-
ctx.res.statusCode = 200;
|
|
100
|
-
}
|
|
101
|
-
ctx.respond = false;
|
|
102
|
-
}
|
|
103
|
-
});
|
|
104
|
-
|
|
105
|
-
app.use(koaBody());
|
|
106
|
-
if (config) {
|
|
107
|
-
const { middleware } = config as FrameConfig;
|
|
108
|
-
initMiddlewares(middleware, app);
|
|
109
|
-
}
|
|
110
|
-
|
|
111
|
-
return (req, res) => {
|
|
112
|
-
app.on('error', err => {
|
|
113
|
-
if (err) {
|
|
114
|
-
throw err;
|
|
115
|
-
}
|
|
116
|
-
});
|
|
117
|
-
return Promise.resolve(app.callback()(req, res));
|
|
118
|
-
};
|
|
119
|
-
},
|
|
120
|
-
}),
|
|
121
|
-
{
|
|
122
|
-
name: '@modern-js/plugin-koa',
|
|
123
|
-
pre: ['@modern-js/plugin-bff'],
|
|
124
|
-
},
|
|
125
|
-
) as any;
|
package/src/registerRoutes.ts
DELETED
|
@@ -1,109 +0,0 @@
|
|
|
1
|
-
import { HttpMethod, useAPIHandlerInfos } from '@modern-js/bff-utils';
|
|
2
|
-
import { isSchemaHandler, InputType } from '@modern-js/bff-runtime';
|
|
3
|
-
import Router from 'koa-router';
|
|
4
|
-
import { Context } from 'koa';
|
|
5
|
-
import typeIs from 'type-is';
|
|
6
|
-
import formidable from 'formidable';
|
|
7
|
-
import { sortDynamicRoutes } from '@modern-js/adapter-helpers';
|
|
8
|
-
import { createDebugger } from '@modern-js/utils';
|
|
9
|
-
|
|
10
|
-
const debug = createDebugger('koa');
|
|
11
|
-
|
|
12
|
-
const registerRoutes = (router: Router, prefix?: string) => {
|
|
13
|
-
// eslint-disable-next-line react-hooks/rules-of-hooks
|
|
14
|
-
const handlerInfos = useAPIHandlerInfos();
|
|
15
|
-
sortDynamicRoutes(handlerInfos);
|
|
16
|
-
debug('handlerInfos', handlerInfos);
|
|
17
|
-
|
|
18
|
-
if (prefix) {
|
|
19
|
-
router.prefix(prefix);
|
|
20
|
-
}
|
|
21
|
-
|
|
22
|
-
handlerInfos.forEach(({ handler, method, name }) => {
|
|
23
|
-
const wrapedHandler = async (ctx: Context) => {
|
|
24
|
-
const input = await getInputFromRequest(ctx);
|
|
25
|
-
|
|
26
|
-
if (isSchemaHandler(handler)) {
|
|
27
|
-
const result = await handler(input);
|
|
28
|
-
if (result.type !== 'HandleSuccess') {
|
|
29
|
-
if (result.type === 'InputValidationError') {
|
|
30
|
-
// eslint-disable-next-line require-atomic-updates
|
|
31
|
-
ctx.status = 400;
|
|
32
|
-
} else {
|
|
33
|
-
// eslint-disable-next-line require-atomic-updates
|
|
34
|
-
ctx.status = 500;
|
|
35
|
-
}
|
|
36
|
-
// eslint-disable-next-line require-atomic-updates
|
|
37
|
-
ctx.body = result.message;
|
|
38
|
-
} else {
|
|
39
|
-
// eslint-disable-next-line require-atomic-updates
|
|
40
|
-
ctx.body = result.value;
|
|
41
|
-
}
|
|
42
|
-
} else {
|
|
43
|
-
const args = Object.values(input.params as any).concat(input);
|
|
44
|
-
// eslint-disable-next-line require-atomic-updates
|
|
45
|
-
ctx.type = 'json';
|
|
46
|
-
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
|
|
47
|
-
// @ts-expect-error
|
|
48
|
-
// eslint-disable-next-line require-atomic-updates
|
|
49
|
-
ctx.body = await handler(...args);
|
|
50
|
-
}
|
|
51
|
-
};
|
|
52
|
-
|
|
53
|
-
Object.defineProperties(
|
|
54
|
-
wrapedHandler,
|
|
55
|
-
Object.getOwnPropertyDescriptors(handler),
|
|
56
|
-
);
|
|
57
|
-
|
|
58
|
-
if (isNormalMethod(method)) {
|
|
59
|
-
const routeName = method.toLowerCase();
|
|
60
|
-
(router as any)[routeName](name, wrapedHandler);
|
|
61
|
-
} else {
|
|
62
|
-
throw new Error(`Unknown HTTP Method: ${method}`);
|
|
63
|
-
}
|
|
64
|
-
});
|
|
65
|
-
};
|
|
66
|
-
|
|
67
|
-
const isNormalMethod = (method: string): method is HttpMethod =>
|
|
68
|
-
Object.keys(HttpMethod).includes(method);
|
|
69
|
-
|
|
70
|
-
export default registerRoutes;
|
|
71
|
-
|
|
72
|
-
const getInputFromRequest = async (ctx: Context): Promise<InputType> => {
|
|
73
|
-
const draft: Record<string, any> = {
|
|
74
|
-
params: ctx.params,
|
|
75
|
-
query: ctx.query,
|
|
76
|
-
headers: ctx.headers,
|
|
77
|
-
cookies: ctx.headers.cookie,
|
|
78
|
-
};
|
|
79
|
-
|
|
80
|
-
if (typeIs.is(ctx.request.type, ['application/json'])) {
|
|
81
|
-
draft.data = ctx.request.body;
|
|
82
|
-
} else if (typeIs.is(ctx.request.type, ['multipart/form-data'])) {
|
|
83
|
-
draft.formData = await resvoleFormData(ctx);
|
|
84
|
-
} else if (
|
|
85
|
-
typeIs.is(ctx.request.type, ['application/x-www-form-urlencoded'])
|
|
86
|
-
) {
|
|
87
|
-
draft.formUrlencoded = ctx.request.body;
|
|
88
|
-
} else {
|
|
89
|
-
draft.body = ctx.request.body;
|
|
90
|
-
}
|
|
91
|
-
|
|
92
|
-
return draft as any;
|
|
93
|
-
};
|
|
94
|
-
|
|
95
|
-
const resvoleFormData = (ctx: Context): Promise<Record<string, any>> => {
|
|
96
|
-
const form = formidable({ multiples: true });
|
|
97
|
-
return new Promise((resolve, reject) => {
|
|
98
|
-
form.parse(ctx.req, (err, fields, files) => {
|
|
99
|
-
if (err) {
|
|
100
|
-
reject(err);
|
|
101
|
-
}
|
|
102
|
-
|
|
103
|
-
resolve({
|
|
104
|
-
...fields,
|
|
105
|
-
...files,
|
|
106
|
-
});
|
|
107
|
-
});
|
|
108
|
-
});
|
|
109
|
-
};
|
package/src/runtime.ts
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export { useContext } from './context';
|