@fastcar/koa 0.1.19 → 0.1.20
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/index.d.ts +14 -0
- package/package.json +5 -2
- package/src/annotation/router/AddMapping.ts +14 -10
- package/src/middleware/KoaProxy.ts +29 -0
- package/src/type/KoaConfig.ts +11 -0
- package/target/annotation/router/AddMapping.js +13 -9
- package/target/middleware/KoaProxy.js +23 -0
- package/test/logs/sys.log +36 -0
- package/test/resource/application.yml +1 -0
package/index.d.ts
CHANGED
|
@@ -38,6 +38,17 @@ export type KoaConfig = {
|
|
|
38
38
|
koaBodyOptions?: { [key: string]: any }; //文件上传的解析
|
|
39
39
|
koaBodyParser?: { [key: string]: any }; //解析请求
|
|
40
40
|
extra?: { [key: string]: any }; //拓展设置
|
|
41
|
+
koaProxy?: {
|
|
42
|
+
//基于http-proxy-middleware来进行拓展
|
|
43
|
+
[key: string]: {
|
|
44
|
+
target: string;
|
|
45
|
+
changeOrigin?: boolean;
|
|
46
|
+
pathRewrite?: {
|
|
47
|
+
[key: string]: string;
|
|
48
|
+
};
|
|
49
|
+
ws: boolean;
|
|
50
|
+
} & any;
|
|
51
|
+
};
|
|
41
52
|
};
|
|
42
53
|
|
|
43
54
|
//全局异常捕捉 可以用自定义的替换这个函数
|
|
@@ -55,6 +66,9 @@ export function KoaCors(app: FastCarApplication): MiddleWareType;
|
|
|
55
66
|
//解析静态文件
|
|
56
67
|
export function KoaStatic(app: FastCarApplication): MiddleWareType;
|
|
57
68
|
|
|
69
|
+
//反向代理文件
|
|
70
|
+
export function KoaProxy(app: FastCarApplication): MiddleWareType;
|
|
71
|
+
|
|
58
72
|
//支持api说明
|
|
59
73
|
export function Swagger(app: FastCarApplication): MiddleWareType;
|
|
60
74
|
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@fastcar/koa",
|
|
3
|
-
"version": "0.1.
|
|
3
|
+
"version": "0.1.20",
|
|
4
4
|
"homepage": "https://github.com/williamDazhangyu/fast-car",
|
|
5
5
|
"description": "fastcar框架下对koa的包装",
|
|
6
6
|
"main": "target/index.js",
|
|
@@ -51,12 +51,15 @@
|
|
|
51
51
|
"@types/koa-range": "^0.3.2",
|
|
52
52
|
"@types/koa-static": "^4.0.2",
|
|
53
53
|
"@types/koa2-cors": "^2.0.2",
|
|
54
|
+
"http-proxy-middleware": "^3.0.5",
|
|
54
55
|
"koa-body": "^4.2.0",
|
|
55
56
|
"koa-mount": "^4.0.0",
|
|
56
57
|
"koa-range": "^0.3.0",
|
|
57
58
|
"koa-static": "^5.0.0",
|
|
59
|
+
"koa2-connect": "^1.0.2",
|
|
58
60
|
"koa2-cors": "^2.0.6",
|
|
59
|
-
"multer": "*"
|
|
61
|
+
"multer": "*",
|
|
62
|
+
"path-to-regexp": "^8.3.0"
|
|
60
63
|
},
|
|
61
64
|
"peerDependenciesMeta": {
|
|
62
65
|
"@types/koa-mount": {
|
|
@@ -3,8 +3,7 @@ import { MethodType } from "../../type/MethodType";
|
|
|
3
3
|
import { DesignMeta } from "../../type/DesignMeta";
|
|
4
4
|
|
|
5
5
|
export default function AddMapping(target: any, info: MethodType) {
|
|
6
|
-
|
|
7
|
-
if(!info.url) {
|
|
6
|
+
if (!info.url) {
|
|
8
7
|
info.url = info.method;
|
|
9
8
|
}
|
|
10
9
|
|
|
@@ -19,13 +18,18 @@ export default function AddMapping(target: any, info: MethodType) {
|
|
|
19
18
|
Reflect.defineMetadata(DesignMeta.ROUTER_MAP, routerMap, target);
|
|
20
19
|
}
|
|
21
20
|
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
21
|
+
//修改主键 根据url-method作为唯一主键
|
|
22
|
+
info.request.forEach((m) => {
|
|
23
|
+
let urlKey = `${info.url}:${m}`;
|
|
24
|
+
|
|
25
|
+
let curr = routerMap.get(urlKey);
|
|
26
|
+
if (!curr) {
|
|
27
|
+
routerMap.set(urlKey, info);
|
|
28
|
+
} else {
|
|
29
|
+
if (info.url != curr.url) {
|
|
30
|
+
console.warn(`The two URL names are inconsisten in (${info.url},${curr.url})`);
|
|
31
|
+
}
|
|
32
|
+
curr.request = [...info.request, ...curr.request];
|
|
28
33
|
}
|
|
29
|
-
|
|
30
|
-
}
|
|
34
|
+
});
|
|
31
35
|
}
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
import { FastCarApplication } from "@fastcar/core";
|
|
2
|
+
import * as Koa from "koa";
|
|
3
|
+
import { KoaConfig } from "../type/KoaConfig";
|
|
4
|
+
|
|
5
|
+
//反向代理扩展
|
|
6
|
+
export default function KoaProxy(app: FastCarApplication): Koa.Middleware {
|
|
7
|
+
const httpProxy = require("http-proxy-middleware");
|
|
8
|
+
const k2c = require("koa2-connect");
|
|
9
|
+
const { match } = require("path-to-regexp");
|
|
10
|
+
|
|
11
|
+
return async function (ctx, next) {
|
|
12
|
+
let koaConfig: KoaConfig = app.getSetting("koa");
|
|
13
|
+
|
|
14
|
+
if (koaConfig && koaConfig.koaProxy) {
|
|
15
|
+
const { path } = ctx;
|
|
16
|
+
for (const route of Object.keys(koaConfig.koaProxy)) {
|
|
17
|
+
if (
|
|
18
|
+
match(route, {
|
|
19
|
+
decode: decodeURIComponent,
|
|
20
|
+
})(path)
|
|
21
|
+
) {
|
|
22
|
+
return await k2c(httpProxy.createProxyMiddleware(koaConfig.koaProxy[route]))(ctx, next);
|
|
23
|
+
}
|
|
24
|
+
}
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
await next();
|
|
28
|
+
};
|
|
29
|
+
}
|
package/src/type/KoaConfig.ts
CHANGED
|
@@ -7,4 +7,15 @@ export type KoaConfig = {
|
|
|
7
7
|
koaBodyOptions?: { [key: string]: any }; //文件上传的解析
|
|
8
8
|
koaBodyParser?: { [key: string]: any }; //bodyParser.Options; //解析请求
|
|
9
9
|
extra?: { [key: string]: any }; //拓展设置
|
|
10
|
+
koaProxy?: {
|
|
11
|
+
//基于http-proxy-middleware来进行拓展
|
|
12
|
+
[key: string]: {
|
|
13
|
+
target: string;
|
|
14
|
+
changeOrigin?: boolean;
|
|
15
|
+
pathRewrite?: {
|
|
16
|
+
[key: string]: string;
|
|
17
|
+
};
|
|
18
|
+
ws: boolean;
|
|
19
|
+
} & any;
|
|
20
|
+
};
|
|
10
21
|
};
|
|
@@ -16,14 +16,18 @@ function AddMapping(target, info) {
|
|
|
16
16
|
routerMap = new Map();
|
|
17
17
|
Reflect.defineMetadata(DesignMeta_1.DesignMeta.ROUTER_MAP, routerMap, target);
|
|
18
18
|
}
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
console.warn(`The two URL names are inconsisten in (${info.url},${curr.url})`);
|
|
19
|
+
//修改主键 根据url-method作为唯一主键
|
|
20
|
+
info.request.forEach((m) => {
|
|
21
|
+
let urlKey = `${info.url}:${m}`;
|
|
22
|
+
let curr = routerMap.get(urlKey);
|
|
23
|
+
if (!curr) {
|
|
24
|
+
routerMap.set(urlKey, info);
|
|
26
25
|
}
|
|
27
|
-
|
|
28
|
-
|
|
26
|
+
else {
|
|
27
|
+
if (info.url != curr.url) {
|
|
28
|
+
console.warn(`The two URL names are inconsisten in (${info.url},${curr.url})`);
|
|
29
|
+
}
|
|
30
|
+
curr.request = [...info.request, ...curr.request];
|
|
31
|
+
}
|
|
32
|
+
});
|
|
29
33
|
}
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.default = KoaProxy;
|
|
4
|
+
//反向代理扩展
|
|
5
|
+
function KoaProxy(app) {
|
|
6
|
+
const httpProxy = require("http-proxy-middleware");
|
|
7
|
+
const k2c = require("koa2-connect");
|
|
8
|
+
const { match } = require("path-to-regexp");
|
|
9
|
+
return async function (ctx, next) {
|
|
10
|
+
let koaConfig = app.getSetting("koa");
|
|
11
|
+
if (koaConfig && koaConfig.koaProxy) {
|
|
12
|
+
const { path } = ctx;
|
|
13
|
+
for (const route of Object.keys(koaConfig.koaProxy)) {
|
|
14
|
+
if (match(route, {
|
|
15
|
+
decode: decodeURIComponent,
|
|
16
|
+
})(path)) {
|
|
17
|
+
return await k2c(httpProxy.createProxyMiddleware(koaConfig.koaProxy[route]))(ctx, next);
|
|
18
|
+
}
|
|
19
|
+
}
|
|
20
|
+
}
|
|
21
|
+
await next();
|
|
22
|
+
};
|
|
23
|
+
}
|
package/test/logs/sys.log
CHANGED
|
@@ -147,3 +147,39 @@
|
|
|
147
147
|
{"timestamp":"2025-07-29 16:31:11.648","level":"INFO","label":"sys","message":"http server is running in 1234"}
|
|
148
148
|
{"timestamp":"2025-07-29 16:31:11.648","level":"INFO","label":"sys","message":"start server koaSimple is run"}
|
|
149
149
|
{"timestamp":"2025-07-29 16:31:11.649","level":"INFO","label":"sys","message":"version 1.0.0"}
|
|
150
|
+
{"timestamp":"2025-12-22 17:28:39.115","level":"INFO","label":"sys","message":"Start scanning component"}
|
|
151
|
+
{"timestamp":"2025-12-22 17:28:41.189","level":"INFO","label":"sys","message":"Complete component scan"}
|
|
152
|
+
{"timestamp":"2025-12-22 17:28:41.190","level":"INFO","label":"sys","message":"Call application initialization method"}
|
|
153
|
+
{"timestamp":"2025-12-22 17:28:43.337","level":"INFO","label":"sys","message":"http server is running in 1234"}
|
|
154
|
+
{"timestamp":"2025-12-22 17:28:43.338","level":"INFO","label":"sys","message":"start server koaSimple is run"}
|
|
155
|
+
{"timestamp":"2025-12-22 17:28:43.339","level":"INFO","label":"sys","message":"version 1.0.0"}
|
|
156
|
+
{"timestamp":"2025-12-22 17:31:22.709","level":"INFO","label":"sys","message":"Start scanning component"}
|
|
157
|
+
{"timestamp":"2025-12-22 17:31:23.51","level":"INFO","label":"sys","message":"Complete component scan"}
|
|
158
|
+
{"timestamp":"2025-12-22 17:31:23.52","level":"INFO","label":"sys","message":"Call application initialization method"}
|
|
159
|
+
{"timestamp":"2025-12-22 17:31:23.100","level":"INFO","label":"sys","message":"http server is running in 1234"}
|
|
160
|
+
{"timestamp":"2025-12-22 17:31:23.101","level":"INFO","label":"sys","message":"start server koaSimple is run"}
|
|
161
|
+
{"timestamp":"2025-12-22 17:31:23.101","level":"INFO","label":"sys","message":"version 1.0.0"}
|
|
162
|
+
{"timestamp":"2025-12-22 17:32:19.934","level":"INFO","label":"sys","message":"Start scanning component"}
|
|
163
|
+
{"timestamp":"2025-12-22 17:32:20.252","level":"INFO","label":"sys","message":"Complete component scan"}
|
|
164
|
+
{"timestamp":"2025-12-22 17:32:20.253","level":"INFO","label":"sys","message":"Call application initialization method"}
|
|
165
|
+
{"timestamp":"2025-12-22 17:32:20.303","level":"INFO","label":"sys","message":"http server is running in 1234"}
|
|
166
|
+
{"timestamp":"2025-12-22 17:32:20.304","level":"INFO","label":"sys","message":"start server koaSimple is run"}
|
|
167
|
+
{"timestamp":"2025-12-22 17:32:20.305","level":"INFO","label":"sys","message":"version 1.0.0"}
|
|
168
|
+
{"timestamp":"2025-12-23 10:47:56.901","level":"INFO","label":"sys","message":"Start scanning component"}
|
|
169
|
+
{"timestamp":"2025-12-23 10:47:59.717","level":"INFO","label":"sys","message":"Complete component scan"}
|
|
170
|
+
{"timestamp":"2025-12-23 10:47:59.718","level":"INFO","label":"sys","message":"Call application initialization method"}
|
|
171
|
+
{"timestamp":"2025-12-23 10:47:59.730","level":"INFO","label":"sys","message":"http server is running in 1234"}
|
|
172
|
+
{"timestamp":"2025-12-23 10:47:59.731","level":"INFO","label":"sys","message":"start server koaSimple is run"}
|
|
173
|
+
{"timestamp":"2025-12-23 10:47:59.731","level":"INFO","label":"sys","message":"version 1.0.0"}
|
|
174
|
+
{"timestamp":"2025-12-23 10:49:47.715","level":"INFO","label":"sys","message":"Start scanning component"}
|
|
175
|
+
{"timestamp":"2025-12-23 10:49:48.98","level":"INFO","label":"sys","message":"Complete component scan"}
|
|
176
|
+
{"timestamp":"2025-12-23 10:49:48.99","level":"INFO","label":"sys","message":"Call application initialization method"}
|
|
177
|
+
{"timestamp":"2025-12-23 10:49:48.106","level":"INFO","label":"sys","message":"http server is running in 1234"}
|
|
178
|
+
{"timestamp":"2025-12-23 10:49:48.106","level":"INFO","label":"sys","message":"start server koaSimple is run"}
|
|
179
|
+
{"timestamp":"2025-12-23 10:49:48.107","level":"INFO","label":"sys","message":"version 1.0.0"}
|
|
180
|
+
{"timestamp":"2025-12-23 10:59:33.290","level":"INFO","label":"sys","message":"Start scanning component"}
|
|
181
|
+
{"timestamp":"2025-12-23 10:59:33.820","level":"INFO","label":"sys","message":"Complete component scan"}
|
|
182
|
+
{"timestamp":"2025-12-23 10:59:33.822","level":"INFO","label":"sys","message":"Call application initialization method"}
|
|
183
|
+
{"timestamp":"2025-12-23 10:59:33.833","level":"INFO","label":"sys","message":"http server is running in 1234"}
|
|
184
|
+
{"timestamp":"2025-12-23 10:59:33.834","level":"INFO","label":"sys","message":"start server koaSimple is run"}
|
|
185
|
+
{"timestamp":"2025-12-23 10:59:33.835","level":"INFO","label":"sys","message":"version 1.0.0"}
|
|
@@ -8,6 +8,7 @@ settings:
|
|
|
8
8
|
swagger:
|
|
9
9
|
enable: true
|
|
10
10
|
api: { "index": "/public/api/index.yaml" }
|
|
11
|
+
koaProxy: { "/:path(.*).php": { target: "http://localhost.better.lg", changeOrigin: true }, "/api/:path*": { target: "https://localhost.better.lg", "changeOrigin": true } }
|
|
11
12
|
extra: #额外配置支持
|
|
12
13
|
cors: #跨域支持示范
|
|
13
14
|
origin: "https://localhost:1234"
|