@fastcar/koa 0.1.9
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/LICENSE +20 -0
- package/README.md +110 -0
- package/annotation.d.ts +46 -0
- package/index.d.ts +63 -0
- package/package.json +123 -0
- package/src/KoaApplication.ts +139 -0
- package/src/annotation/EnableKoa.ts +9 -0
- package/src/annotation/KoaMiddleware.ts +19 -0
- package/src/annotation/router/AddMapping.ts +31 -0
- package/src/annotation/router/AllMapping.ts +12 -0
- package/src/annotation/router/DeleteMapping.ts +12 -0
- package/src/annotation/router/GetMapping.ts +12 -0
- package/src/annotation/router/PatchMapping.ts +12 -0
- package/src/annotation/router/PostMapping.ts +12 -0
- package/src/annotation/router/PutMapping.ts +12 -0
- package/src/annotation/router/RequestMapping.ts +23 -0
- package/src/annotation.ts +43 -0
- package/src/index.ts +11 -0
- package/src/middleware/ExceptionGlobalHandler.ts +28 -0
- package/src/middleware/KoaBody.ts +11 -0
- package/src/middleware/KoaBodyParser.ts +11 -0
- package/src/middleware/KoaCors.ts +38 -0
- package/src/middleware/KoaStatic.ts +42 -0
- package/src/middleware/Swagger.ts +63 -0
- package/src/type/DesignMeta.ts +4 -0
- package/src/type/KoaConfig.ts +13 -0
- package/src/type/MethodType.ts +8 -0
- package/src/type/RouteMethods.ts +8 -0
- package/target/KoaApplication.js +139 -0
- package/target/annotation/EnableKoa.js +11 -0
- package/target/annotation/KoaMiddleware.js +19 -0
- package/target/annotation/router/AddMapping.js +29 -0
- package/target/annotation/router/AllMapping.js +14 -0
- package/target/annotation/router/DeleteMapping.js +14 -0
- package/target/annotation/router/GetMapping.js +14 -0
- package/target/annotation/router/PatchMapping.js +14 -0
- package/target/annotation/router/PostMapping.js +14 -0
- package/target/annotation/router/PutMapping.js +14 -0
- package/target/annotation/router/RequestMapping.js +22 -0
- package/target/annotation.js +38 -0
- package/target/index.js +19 -0
- package/target/middleware/ExceptionGlobalHandler.js +32 -0
- package/target/middleware/KoaBody.js +10 -0
- package/target/middleware/KoaBodyParser.js +10 -0
- package/target/middleware/KoaCors.js +33 -0
- package/target/middleware/KoaStatic.js +38 -0
- package/target/middleware/Swagger.js +53 -0
- package/target/npmlist.json +1 -0
- package/target/type/DesignMeta.js +7 -0
- package/target/type/KoaConfig.js +2 -0
- package/target/type/MethodType.js +2 -0
- package/target/type/RouteMethods.js +12 -0
- package/test/logs/koa.log +0 -0
- package/test/logs/serverlogger.log +3 -0
- package/test/logs/sys.log +70 -0
- package/test/resource/application.yml +18 -0
- package/test/resource/public/api/index.yaml +37 -0
- package/test/resource/public/hello.txt +1 -0
- package/test/simple/app.ts +38 -0
- package/test/simple/controller/HelloController.ts +37 -0
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
import EnableKoa from "./annotation/EnableKoa";
|
|
2
|
+
import KoaMiddleware from "./annotation/KoaMiddleware";
|
|
3
|
+
import AddMapping from "./annotation/router/AddMapping";
|
|
4
|
+
import AllMapping from "./annotation/router/AllMapping";
|
|
5
|
+
import DeleteMapping from "./annotation/router/DeleteMapping";
|
|
6
|
+
import GetMapping from "./annotation/router/GetMapping";
|
|
7
|
+
import PatchMapping from "./annotation/router/PatchMapping";
|
|
8
|
+
import PostMapping from "./annotation/router/PostMapping";
|
|
9
|
+
import PutMapping from "./annotation/router/PutMapping";
|
|
10
|
+
import RequestMapping from "./annotation/router/RequestMapping";
|
|
11
|
+
|
|
12
|
+
//声明简化的方式
|
|
13
|
+
const GET = GetMapping;
|
|
14
|
+
const POST = PostMapping;
|
|
15
|
+
const DELETE = DeleteMapping;
|
|
16
|
+
const PUT = PutMapping;
|
|
17
|
+
const PATCH = PatchMapping;
|
|
18
|
+
const ALL = AllMapping;
|
|
19
|
+
const REQUEST = RequestMapping;
|
|
20
|
+
|
|
21
|
+
export {
|
|
22
|
+
//关于请求方式注解
|
|
23
|
+
AddMapping,
|
|
24
|
+
AllMapping,
|
|
25
|
+
DeleteMapping,
|
|
26
|
+
GetMapping,
|
|
27
|
+
PatchMapping,
|
|
28
|
+
PostMapping,
|
|
29
|
+
PutMapping,
|
|
30
|
+
RequestMapping,
|
|
31
|
+
//开启koa应用
|
|
32
|
+
EnableKoa,
|
|
33
|
+
//追加koa中间件
|
|
34
|
+
KoaMiddleware,
|
|
35
|
+
//简化声明方式
|
|
36
|
+
GET,
|
|
37
|
+
POST,
|
|
38
|
+
DELETE,
|
|
39
|
+
PUT,
|
|
40
|
+
PATCH,
|
|
41
|
+
ALL,
|
|
42
|
+
REQUEST,
|
|
43
|
+
};
|
package/src/index.ts
ADDED
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import KoaApplication from "./KoaApplication";
|
|
2
|
+
import { KoaConfig } from "./type/KoaConfig";
|
|
3
|
+
import ExceptionGlobalHandler from "./middleware/ExceptionGlobalHandler";
|
|
4
|
+
import KoaBody from "./middleware/KoaBody";
|
|
5
|
+
import KoaBodyParser from "./middleware/KoaBodyParser";
|
|
6
|
+
import KoaCors from "./middleware/KoaCors";
|
|
7
|
+
import KoaStatic from "./middleware/KoaStatic";
|
|
8
|
+
import Swagger from "./middleware/Swagger";
|
|
9
|
+
import { DesignMeta } from "./type/DesignMeta";
|
|
10
|
+
|
|
11
|
+
export { KoaApplication, KoaConfig, ExceptionGlobalHandler, KoaBody, KoaBodyParser, KoaCors, KoaStatic, Swagger, DesignMeta };
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
import { FastCarApplication, ValidError } from "@fastcar/core";
|
|
2
|
+
|
|
3
|
+
//默认错误捕捉
|
|
4
|
+
export default function ExceptionGlobalHandler(app: FastCarApplication) {
|
|
5
|
+
let logger = app.getSysLogger();
|
|
6
|
+
return async (ctx: any, next: Function) => {
|
|
7
|
+
try {
|
|
8
|
+
await next();
|
|
9
|
+
} catch (e) {
|
|
10
|
+
//新增如果是校验的错误则进行输出
|
|
11
|
+
if (e instanceof ValidError) {
|
|
12
|
+
ctx.body = {
|
|
13
|
+
code: 400,
|
|
14
|
+
msg: e.message || "parameter error",
|
|
15
|
+
};
|
|
16
|
+
} else {
|
|
17
|
+
logger.error(`${ctx.url} is error`);
|
|
18
|
+
if (e) {
|
|
19
|
+
logger.error(e);
|
|
20
|
+
}
|
|
21
|
+
ctx.body = {
|
|
22
|
+
code: 500,
|
|
23
|
+
msg: "Service internal error",
|
|
24
|
+
};
|
|
25
|
+
}
|
|
26
|
+
}
|
|
27
|
+
};
|
|
28
|
+
}
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import * as koaBody from "koa-body";
|
|
2
|
+
import { FastCarApplication } from "@fastcar/core";
|
|
3
|
+
import { KoaConfig } from "../type/KoaConfig";
|
|
4
|
+
|
|
5
|
+
//对于文件上传做限定
|
|
6
|
+
export default function KoaBody(app: FastCarApplication) {
|
|
7
|
+
let koaConfig: KoaConfig = app.getSetting("koa");
|
|
8
|
+
let bodyConfig = koaConfig?.koaBodyOptions;
|
|
9
|
+
|
|
10
|
+
return koaBody(bodyConfig);
|
|
11
|
+
}
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import * as bodyParser from "koa-bodyparser";
|
|
2
|
+
import { FastCarApplication } from "@fastcar/core";
|
|
3
|
+
import { KoaConfig } from "../type/KoaConfig";
|
|
4
|
+
|
|
5
|
+
//对文件内容做解析
|
|
6
|
+
export default function KoaBodyParser(app: FastCarApplication) {
|
|
7
|
+
let koaConfig: KoaConfig = app.getSetting("koa");
|
|
8
|
+
let bodyConfig = koaConfig?.koaBodyParser;
|
|
9
|
+
|
|
10
|
+
return bodyParser(bodyConfig);
|
|
11
|
+
}
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
import { FastCarApplication } from "@fastcar/core";
|
|
2
|
+
import * as koa2Cors from "koa2-cors";
|
|
3
|
+
import { KoaConfig } from "../type/KoaConfig";
|
|
4
|
+
import { Context } from "koa";
|
|
5
|
+
|
|
6
|
+
export default function KoaCors(app: FastCarApplication) {
|
|
7
|
+
let koaConfig: KoaConfig = app.getSetting("koa");
|
|
8
|
+
if (koaConfig?.extra) {
|
|
9
|
+
let corsConfig: koa2Cors.Options = Reflect.get(koaConfig.extra, "cors");
|
|
10
|
+
if (!!corsConfig) {
|
|
11
|
+
//兼容支持多个跨域
|
|
12
|
+
if (typeof corsConfig.origin == "string") {
|
|
13
|
+
let origins = corsConfig.origin.split(";");
|
|
14
|
+
Reflect.set(corsConfig, "origin", (ctx: Context): boolean | string => {
|
|
15
|
+
let orign = ctx?.request?.header?.origin;
|
|
16
|
+
if (!orign) {
|
|
17
|
+
if (!origins.includes("*")) {
|
|
18
|
+
return false;
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
return "*";
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
for (let o of origins) {
|
|
25
|
+
if (orign.startsWith(o) || o == "*") {
|
|
26
|
+
return o;
|
|
27
|
+
}
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
return false;
|
|
31
|
+
});
|
|
32
|
+
}
|
|
33
|
+
}
|
|
34
|
+
return koa2Cors(corsConfig);
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
return [];
|
|
38
|
+
}
|
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
import * as koaStatic from "koa-static";
|
|
2
|
+
import * as KoaRange from "koa-range";
|
|
3
|
+
import * as KoaMount from "koa-mount";
|
|
4
|
+
import { FastCarApplication } from "@fastcar/core";
|
|
5
|
+
import { KoaConfig } from "../type/KoaConfig";
|
|
6
|
+
import * as fs from "fs";
|
|
7
|
+
import * as path from "path";
|
|
8
|
+
import * as Koa from "koa";
|
|
9
|
+
|
|
10
|
+
//支持静态文件访问
|
|
11
|
+
export default function KoaStatic(app: FastCarApplication): Koa.Middleware[] {
|
|
12
|
+
let mlist: Koa.Middleware[] = [];
|
|
13
|
+
|
|
14
|
+
//采用koa-range使文件可以流式传播
|
|
15
|
+
mlist.push(KoaRange as any);
|
|
16
|
+
|
|
17
|
+
let koaConfig: KoaConfig = app.getSetting("koa");
|
|
18
|
+
|
|
19
|
+
if (!!koaConfig?.koaStatic) {
|
|
20
|
+
let keys = Object.keys(koaConfig?.koaStatic);
|
|
21
|
+
if (keys.length > 0) {
|
|
22
|
+
for (let key of keys) {
|
|
23
|
+
let fp = koaConfig.koaStatic[key];
|
|
24
|
+
let rp = path.join(app.getResourcePath(), fp);
|
|
25
|
+
if (!fs.existsSync(fp)) {
|
|
26
|
+
if (!fs.existsSync(rp)) {
|
|
27
|
+
console.error(`${fp} is not found`);
|
|
28
|
+
continue;
|
|
29
|
+
} else {
|
|
30
|
+
fp = rp;
|
|
31
|
+
}
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
if (!key.startsWith("/")) {
|
|
35
|
+
key = `/${key}`;
|
|
36
|
+
}
|
|
37
|
+
mlist.push(KoaMount(key, koaStatic(fp)) as any);
|
|
38
|
+
}
|
|
39
|
+
}
|
|
40
|
+
}
|
|
41
|
+
return mlist;
|
|
42
|
+
}
|
|
@@ -0,0 +1,63 @@
|
|
|
1
|
+
import type { Context, Middleware, Next } from "koa";
|
|
2
|
+
import * as fs from "fs";
|
|
3
|
+
import * as path from "path";
|
|
4
|
+
import { FastCarApplication } from "@fastcar/core";
|
|
5
|
+
import { KoaConfig } from "../type/KoaConfig";
|
|
6
|
+
import * as koaStatic from "koa-static";
|
|
7
|
+
import * as KoaMount from "koa-mount";
|
|
8
|
+
|
|
9
|
+
const swaggerDefalutUrl = "https://petstore.swagger.io/v2/swagger.json";
|
|
10
|
+
//api显示和管理
|
|
11
|
+
export default function Swagger(app: FastCarApplication): Middleware[] {
|
|
12
|
+
let mlist: Middleware[] = [];
|
|
13
|
+
|
|
14
|
+
let koaConfig: KoaConfig = app.getSetting("koa");
|
|
15
|
+
|
|
16
|
+
if (koaConfig.swagger && koaConfig.swagger.enable) {
|
|
17
|
+
let apiMap = new Map<string, string>();
|
|
18
|
+
let fileMap = new Map<string, string>();
|
|
19
|
+
let apis = koaConfig.swagger.api;
|
|
20
|
+
|
|
21
|
+
if (apis) {
|
|
22
|
+
Object.keys(apis).forEach((key) => {
|
|
23
|
+
let value = apis[key];
|
|
24
|
+
if (!key.startsWith("/")) {
|
|
25
|
+
key = `/${key}`;
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
let realValue = value.startsWith("/") ? value : "/" + value;
|
|
29
|
+
apiMap.set(key, realValue);
|
|
30
|
+
fileMap.set(realValue, path.join(app.getResourcePath(), value));
|
|
31
|
+
});
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
//进行设置静态访问路径
|
|
35
|
+
const swaggerUiAssetPath = require("swagger-ui-dist").getAbsoluteFSPath();
|
|
36
|
+
mlist.push(KoaMount("/swagger-ui", koaStatic(swaggerUiAssetPath)) as any);
|
|
37
|
+
|
|
38
|
+
const swaggerTemplate = fs.readFileSync(path.join(swaggerUiAssetPath, "index.html"), "utf-8");
|
|
39
|
+
const fn = async (ctx: Context, next: Next) => {
|
|
40
|
+
let url = ctx.url;
|
|
41
|
+
let item = apiMap.get(url);
|
|
42
|
+
if (!!item) {
|
|
43
|
+
//输出路径
|
|
44
|
+
ctx.type = "text/html";
|
|
45
|
+
ctx.body = swaggerTemplate.replace(swaggerDefalutUrl, item).replace(/\.\//g, "./swagger-ui/");
|
|
46
|
+
return;
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
let fp = fileMap.get(url);
|
|
50
|
+
if (fp) {
|
|
51
|
+
if (fs.existsSync(fp)) {
|
|
52
|
+
ctx.body = fs.readFileSync(fp, "utf-8");
|
|
53
|
+
return;
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
await next();
|
|
58
|
+
};
|
|
59
|
+
mlist.unshift(fn);
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
return mlist;
|
|
63
|
+
}
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import * as koaBody from "koa-body";
|
|
2
|
+
import * as bodyParser from "koa-bodyparser";
|
|
3
|
+
import { ServerConfig } from "@fastcar/server";
|
|
4
|
+
|
|
5
|
+
//和koa的约定配置
|
|
6
|
+
export type KoaConfig = {
|
|
7
|
+
server: ServerConfig[] | ServerConfig; //监听的端口号
|
|
8
|
+
koaStatic?: { [key: string]: string }; //相对路径为resource下的 或者绝对文件路径
|
|
9
|
+
koaBodyOptions?: koaBody.IKoaBodyOptions; //文件上传的解析
|
|
10
|
+
koaBodyParser?: bodyParser.Options; //解析请求
|
|
11
|
+
extra?: { [key: string]: any }; //拓展设置
|
|
12
|
+
swagger?: { enable: boolean; api: { [alias: string]: string } }; //别名:路径
|
|
13
|
+
};
|
|
@@ -0,0 +1,139 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
|
|
3
|
+
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
|
|
4
|
+
if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
|
|
5
|
+
else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
|
|
6
|
+
return c > 3 && r && Object.defineProperty(target, key, r), r;
|
|
7
|
+
};
|
|
8
|
+
var __metadata = (this && this.__metadata) || function (k, v) {
|
|
9
|
+
if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
|
|
10
|
+
};
|
|
11
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
12
|
+
require("reflect-metadata");
|
|
13
|
+
const annotation_1 = require("@fastcar/core/annotation");
|
|
14
|
+
const core_1 = require("@fastcar/core");
|
|
15
|
+
const Koa = require("koa");
|
|
16
|
+
const KoaRouter = require("koa-router");
|
|
17
|
+
const DesignMeta_1 = require("./type/DesignMeta");
|
|
18
|
+
const utils_1 = require("@fastcar/core/utils");
|
|
19
|
+
const server_1 = require("@fastcar/server");
|
|
20
|
+
/***
|
|
21
|
+
* @version 1.0 koa基础组件启动
|
|
22
|
+
*
|
|
23
|
+
*/
|
|
24
|
+
let KoaApplication = class KoaApplication {
|
|
25
|
+
constructor() {
|
|
26
|
+
this.koaApp = new Koa();
|
|
27
|
+
}
|
|
28
|
+
/***
|
|
29
|
+
* @version 1.0 加载中间件
|
|
30
|
+
*
|
|
31
|
+
*/
|
|
32
|
+
loadMiddleWare(list) {
|
|
33
|
+
if (Array.isArray(list)) {
|
|
34
|
+
list.forEach((item) => {
|
|
35
|
+
if (utils_1.TypeUtil.isFunction(item)) {
|
|
36
|
+
this.koaApp.use(item);
|
|
37
|
+
}
|
|
38
|
+
});
|
|
39
|
+
}
|
|
40
|
+
else {
|
|
41
|
+
if (utils_1.TypeUtil.isFunction(list)) {
|
|
42
|
+
this.koaApp.use(list);
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
/***
|
|
47
|
+
* @version 1.0 加载路由
|
|
48
|
+
*
|
|
49
|
+
*/
|
|
50
|
+
loadRoute() {
|
|
51
|
+
let router = new KoaRouter();
|
|
52
|
+
let instanceList = this.app.getComponentByType(core_1.ComponentKind.Controller);
|
|
53
|
+
//查找绑定的url
|
|
54
|
+
instanceList.forEach((instance) => {
|
|
55
|
+
let routerMap = Reflect.getMetadata(DesignMeta_1.DesignMeta.ROUTER_MAP, instance);
|
|
56
|
+
//移除空的map结构
|
|
57
|
+
if (!routerMap || routerMap.size == 0) {
|
|
58
|
+
return;
|
|
59
|
+
}
|
|
60
|
+
routerMap.forEach((item) => {
|
|
61
|
+
//去除ctx的影响
|
|
62
|
+
let callBack = async (ctx, next) => {
|
|
63
|
+
//进行参数的取值
|
|
64
|
+
let body = Object.keys(ctx.query).length > 0 ? ctx.query : ctx.request.body;
|
|
65
|
+
if (!body) {
|
|
66
|
+
body = {};
|
|
67
|
+
}
|
|
68
|
+
if (!!ctx.params) {
|
|
69
|
+
Object.assign(body, ctx.params);
|
|
70
|
+
}
|
|
71
|
+
let res = await instance[item.method](body, ctx);
|
|
72
|
+
if (!!res) {
|
|
73
|
+
ctx.body = res;
|
|
74
|
+
}
|
|
75
|
+
if (next) {
|
|
76
|
+
await next();
|
|
77
|
+
}
|
|
78
|
+
};
|
|
79
|
+
for (let r of item.request) {
|
|
80
|
+
//进行绑定 加载router路由的执行方法
|
|
81
|
+
Reflect.apply(router[`${r}`], router, [item.url, callBack]);
|
|
82
|
+
}
|
|
83
|
+
});
|
|
84
|
+
});
|
|
85
|
+
return router.routes();
|
|
86
|
+
}
|
|
87
|
+
start() {
|
|
88
|
+
const koaApp = this.koaApp;
|
|
89
|
+
//加载中间件
|
|
90
|
+
let middlewareList = this.app.getSetting(DesignMeta_1.DesignMeta.KoaMIDDLEWARE);
|
|
91
|
+
if (Array.isArray(middlewareList)) {
|
|
92
|
+
for (let m of middlewareList) {
|
|
93
|
+
if (utils_1.TypeUtil.isPromise(m)) {
|
|
94
|
+
Reflect.apply(m, this, [this.app, koaApp]).then((tmpList) => {
|
|
95
|
+
this.loadMiddleWare(tmpList);
|
|
96
|
+
});
|
|
97
|
+
}
|
|
98
|
+
else {
|
|
99
|
+
let tmpList = Reflect.apply(m, this, [this.app, koaApp]);
|
|
100
|
+
this.loadMiddleWare(tmpList);
|
|
101
|
+
}
|
|
102
|
+
}
|
|
103
|
+
}
|
|
104
|
+
//加载路由
|
|
105
|
+
koaApp.use(this.loadRoute());
|
|
106
|
+
//读取配置文件 创建服务器
|
|
107
|
+
let appCallback = koaApp.callback();
|
|
108
|
+
let koaConfig = this.app.getSetting("koa");
|
|
109
|
+
if (!!koaConfig.server) {
|
|
110
|
+
if (Array.isArray(koaConfig.server)) {
|
|
111
|
+
koaConfig.server.forEach((server) => {
|
|
112
|
+
this.serverApplication.createServer(server, appCallback);
|
|
113
|
+
});
|
|
114
|
+
}
|
|
115
|
+
else {
|
|
116
|
+
this.serverApplication.createServer(koaConfig.server, appCallback);
|
|
117
|
+
}
|
|
118
|
+
}
|
|
119
|
+
}
|
|
120
|
+
async stop() { }
|
|
121
|
+
};
|
|
122
|
+
__decorate([
|
|
123
|
+
annotation_1.Autowired,
|
|
124
|
+
__metadata("design:type", core_1.FastCarApplication)
|
|
125
|
+
], KoaApplication.prototype, "app", void 0);
|
|
126
|
+
__decorate([
|
|
127
|
+
(0, annotation_1.Log)("koa"),
|
|
128
|
+
__metadata("design:type", core_1.Logger)
|
|
129
|
+
], KoaApplication.prototype, "koaLogger", void 0);
|
|
130
|
+
__decorate([
|
|
131
|
+
annotation_1.Autowired,
|
|
132
|
+
__metadata("design:type", server_1.ServerApplication)
|
|
133
|
+
], KoaApplication.prototype, "serverApplication", void 0);
|
|
134
|
+
KoaApplication = __decorate([
|
|
135
|
+
(0, annotation_1.ApplicationStart)(core_1.BootPriority.Lowest, "start"),
|
|
136
|
+
(0, annotation_1.ApplicationStop)(core_1.BootPriority.Base, "stop"),
|
|
137
|
+
__metadata("design:paramtypes", [])
|
|
138
|
+
], KoaApplication);
|
|
139
|
+
exports.default = KoaApplication;
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
const annotation_1 = require("@fastcar/core/annotation");
|
|
4
|
+
const server_1 = require("@fastcar/server");
|
|
5
|
+
//开启koa应用
|
|
6
|
+
function EnableKoa(target) {
|
|
7
|
+
let fp = require.resolve("../KoaApplication");
|
|
8
|
+
(0, annotation_1.ComponentInjection)(target, fp);
|
|
9
|
+
(0, server_1.EnableServer)(target);
|
|
10
|
+
}
|
|
11
|
+
exports.default = EnableKoa;
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
require("reflect-metadata");
|
|
4
|
+
const DesignMeta_1 = require("../type/DesignMeta");
|
|
5
|
+
//加载koa中间件
|
|
6
|
+
function KoaMiddleware(...args) {
|
|
7
|
+
return function (target) {
|
|
8
|
+
let middlewareList = Reflect.get(target.prototype, DesignMeta_1.DesignMeta.KoaMIDDLEWARE);
|
|
9
|
+
if (!middlewareList) {
|
|
10
|
+
middlewareList = args;
|
|
11
|
+
}
|
|
12
|
+
else {
|
|
13
|
+
//由于注解方式是从下至上运行 和我们理解的书写习惯不一样,所以这边做了一个反序
|
|
14
|
+
middlewareList = [...args, ...middlewareList];
|
|
15
|
+
}
|
|
16
|
+
Reflect.set(target.prototype, DesignMeta_1.DesignMeta.KoaMIDDLEWARE, middlewareList);
|
|
17
|
+
};
|
|
18
|
+
}
|
|
19
|
+
exports.default = KoaMiddleware;
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
require("reflect-metadata");
|
|
4
|
+
const DesignMeta_1 = require("../../type/DesignMeta");
|
|
5
|
+
function AddMapping(target, info) {
|
|
6
|
+
if (!info.url) {
|
|
7
|
+
info.url = info.method;
|
|
8
|
+
}
|
|
9
|
+
//格式化url 以/开头
|
|
10
|
+
if (!info.url.startsWith("/")) {
|
|
11
|
+
info.url = "/" + info.url;
|
|
12
|
+
}
|
|
13
|
+
let routerMap = Reflect.getMetadata(DesignMeta_1.DesignMeta.ROUTER_MAP, target);
|
|
14
|
+
if (!routerMap) {
|
|
15
|
+
routerMap = new Map();
|
|
16
|
+
Reflect.defineMetadata(DesignMeta_1.DesignMeta.ROUTER_MAP, routerMap, target);
|
|
17
|
+
}
|
|
18
|
+
let curr = routerMap.get(info.url);
|
|
19
|
+
if (!curr) {
|
|
20
|
+
routerMap.set(info.url, info);
|
|
21
|
+
}
|
|
22
|
+
else {
|
|
23
|
+
if (info.url != curr.url) {
|
|
24
|
+
console.warn(`The two URL names are inconsisten in (${info.url},${curr.url})`);
|
|
25
|
+
}
|
|
26
|
+
curr.request = [...info.request, ...curr.request];
|
|
27
|
+
}
|
|
28
|
+
}
|
|
29
|
+
exports.default = AddMapping;
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
const RouteMethods_1 = require("../../type/RouteMethods");
|
|
4
|
+
const AddMapping_1 = require("./AddMapping");
|
|
5
|
+
function AllMapping(url) {
|
|
6
|
+
return function (target, name, descriptor) {
|
|
7
|
+
(0, AddMapping_1.default)(target, {
|
|
8
|
+
url,
|
|
9
|
+
method: name,
|
|
10
|
+
request: [RouteMethods_1.RouteMethods.AllMapping],
|
|
11
|
+
});
|
|
12
|
+
};
|
|
13
|
+
}
|
|
14
|
+
exports.default = AllMapping;
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
const RouteMethods_1 = require("../../type/RouteMethods");
|
|
4
|
+
const AddMapping_1 = require("./AddMapping");
|
|
5
|
+
function DeleteMapping(url) {
|
|
6
|
+
return function (target, name, descriptor) {
|
|
7
|
+
(0, AddMapping_1.default)(target, {
|
|
8
|
+
url,
|
|
9
|
+
method: name,
|
|
10
|
+
request: [RouteMethods_1.RouteMethods.DeleteMapping],
|
|
11
|
+
});
|
|
12
|
+
};
|
|
13
|
+
}
|
|
14
|
+
exports.default = DeleteMapping;
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
const RouteMethods_1 = require("../../type/RouteMethods");
|
|
4
|
+
const AddMapping_1 = require("./AddMapping");
|
|
5
|
+
function GetMapping(url) {
|
|
6
|
+
return function (target, name, descriptor) {
|
|
7
|
+
(0, AddMapping_1.default)(target, {
|
|
8
|
+
url,
|
|
9
|
+
method: name,
|
|
10
|
+
request: [RouteMethods_1.RouteMethods.GetMapping],
|
|
11
|
+
});
|
|
12
|
+
};
|
|
13
|
+
}
|
|
14
|
+
exports.default = GetMapping;
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
const RouteMethods_1 = require("../../type/RouteMethods");
|
|
4
|
+
const AddMapping_1 = require("./AddMapping");
|
|
5
|
+
function PatchMapping(url) {
|
|
6
|
+
return function (target, name, descriptor) {
|
|
7
|
+
(0, AddMapping_1.default)(target, {
|
|
8
|
+
url,
|
|
9
|
+
method: name,
|
|
10
|
+
request: [RouteMethods_1.RouteMethods.PatchMapping],
|
|
11
|
+
});
|
|
12
|
+
};
|
|
13
|
+
}
|
|
14
|
+
exports.default = PatchMapping;
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
const RouteMethods_1 = require("../../type/RouteMethods");
|
|
4
|
+
const AddMapping_1 = require("./AddMapping");
|
|
5
|
+
function PostMapping(url) {
|
|
6
|
+
return function (target, name, descriptor) {
|
|
7
|
+
(0, AddMapping_1.default)(target, {
|
|
8
|
+
url,
|
|
9
|
+
method: name,
|
|
10
|
+
request: [RouteMethods_1.RouteMethods.PostMapping],
|
|
11
|
+
});
|
|
12
|
+
};
|
|
13
|
+
}
|
|
14
|
+
exports.default = PostMapping;
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
const RouteMethods_1 = require("../../type/RouteMethods");
|
|
4
|
+
const AddMapping_1 = require("./AddMapping");
|
|
5
|
+
function PutMapping(url) {
|
|
6
|
+
return function (target, name, descriptor) {
|
|
7
|
+
(0, AddMapping_1.default)(target, {
|
|
8
|
+
url,
|
|
9
|
+
method: name,
|
|
10
|
+
request: [RouteMethods_1.RouteMethods.PutMapping],
|
|
11
|
+
});
|
|
12
|
+
};
|
|
13
|
+
}
|
|
14
|
+
exports.default = PutMapping;
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
require("reflect-metadata");
|
|
4
|
+
const DesignMeta_1 = require("../../type/DesignMeta");
|
|
5
|
+
const utils_1 = require("@fastcar/core/utils");
|
|
6
|
+
//加载值头部的url
|
|
7
|
+
function RequestMapping(url) {
|
|
8
|
+
return function (target) {
|
|
9
|
+
let tname = utils_1.FormatStr.formatFirstToLow(target.name);
|
|
10
|
+
let headUrl = url || tname;
|
|
11
|
+
if (!headUrl.startsWith("/")) {
|
|
12
|
+
headUrl = "/" + headUrl;
|
|
13
|
+
}
|
|
14
|
+
let routerMap = Reflect.getMetadata(DesignMeta_1.DesignMeta.ROUTER_MAP, target.prototype);
|
|
15
|
+
if (!!routerMap) {
|
|
16
|
+
routerMap.forEach((item) => {
|
|
17
|
+
item.url = headUrl + item.url;
|
|
18
|
+
});
|
|
19
|
+
}
|
|
20
|
+
};
|
|
21
|
+
}
|
|
22
|
+
exports.default = RequestMapping;
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.REQUEST = exports.ALL = exports.PATCH = exports.PUT = exports.DELETE = exports.POST = exports.GET = exports.KoaMiddleware = exports.EnableKoa = exports.RequestMapping = exports.PutMapping = exports.PostMapping = exports.PatchMapping = exports.GetMapping = exports.DeleteMapping = exports.AllMapping = exports.AddMapping = void 0;
|
|
4
|
+
const EnableKoa_1 = require("./annotation/EnableKoa");
|
|
5
|
+
exports.EnableKoa = EnableKoa_1.default;
|
|
6
|
+
const KoaMiddleware_1 = require("./annotation/KoaMiddleware");
|
|
7
|
+
exports.KoaMiddleware = KoaMiddleware_1.default;
|
|
8
|
+
const AddMapping_1 = require("./annotation/router/AddMapping");
|
|
9
|
+
exports.AddMapping = AddMapping_1.default;
|
|
10
|
+
const AllMapping_1 = require("./annotation/router/AllMapping");
|
|
11
|
+
exports.AllMapping = AllMapping_1.default;
|
|
12
|
+
const DeleteMapping_1 = require("./annotation/router/DeleteMapping");
|
|
13
|
+
exports.DeleteMapping = DeleteMapping_1.default;
|
|
14
|
+
const GetMapping_1 = require("./annotation/router/GetMapping");
|
|
15
|
+
exports.GetMapping = GetMapping_1.default;
|
|
16
|
+
const PatchMapping_1 = require("./annotation/router/PatchMapping");
|
|
17
|
+
exports.PatchMapping = PatchMapping_1.default;
|
|
18
|
+
const PostMapping_1 = require("./annotation/router/PostMapping");
|
|
19
|
+
exports.PostMapping = PostMapping_1.default;
|
|
20
|
+
const PutMapping_1 = require("./annotation/router/PutMapping");
|
|
21
|
+
exports.PutMapping = PutMapping_1.default;
|
|
22
|
+
const RequestMapping_1 = require("./annotation/router/RequestMapping");
|
|
23
|
+
exports.RequestMapping = RequestMapping_1.default;
|
|
24
|
+
//声明简化的方式
|
|
25
|
+
const GET = GetMapping_1.default;
|
|
26
|
+
exports.GET = GET;
|
|
27
|
+
const POST = PostMapping_1.default;
|
|
28
|
+
exports.POST = POST;
|
|
29
|
+
const DELETE = DeleteMapping_1.default;
|
|
30
|
+
exports.DELETE = DELETE;
|
|
31
|
+
const PUT = PutMapping_1.default;
|
|
32
|
+
exports.PUT = PUT;
|
|
33
|
+
const PATCH = PatchMapping_1.default;
|
|
34
|
+
exports.PATCH = PATCH;
|
|
35
|
+
const ALL = AllMapping_1.default;
|
|
36
|
+
exports.ALL = ALL;
|
|
37
|
+
const REQUEST = RequestMapping_1.default;
|
|
38
|
+
exports.REQUEST = REQUEST;
|