@fastcar/koa 0.1.10 → 0.1.12

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/README.md CHANGED
@@ -2,7 +2,7 @@
2
2
 
3
3
  ## 快速安装
4
4
 
5
- npm install fastcar@koa
5
+ npm install @fastcar/koa
6
6
 
7
7
  ## 基本原理
8
8
 
@@ -31,12 +31,17 @@ export const app = new APP();
31
31
  ```ts
32
32
  import { Controller } from "@fastcar/core/annotation";
33
33
  import { GET } from "@fastcar/koa/annotation";
34
+ import { Context } from "koa";
35
+
34
36
 
35
37
  @Controller
36
38
  export default class HelloController {
37
39
 
38
40
  @GET("/")
39
- home() {
41
+ home(params: string, ctx: Context) {
42
+ console.log("这边请注意 params是params和body二合一的参数 重名的值会优先取body的");
43
+ console.log('body取 ctx.request.body');
44
+ console.log('路径后参数取ctx.params');
40
45
  return "hello world";
41
46
  }
42
47
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@fastcar/koa",
3
- "version": "0.1.10",
3
+ "version": "0.1.12",
4
4
  "homepage": "https://github.com/williamDazhangyu/fast-car",
5
5
  "description": "fastcar框架下对koa的包装",
6
6
  "main": "target/index.js",
@@ -33,28 +33,34 @@
33
33
  "@fastcar/core": "^0.2.38",
34
34
  "@fastcar/server": "^0.0.3",
35
35
  "@types/koa": "^2.13.5",
36
- "reflect-metadata": "^0.1.13"
36
+ "reflect-metadata": "^0.1.13",
37
+ "@types/koa-bodyparser": "^4.3.11",
38
+ "@types/koa-router": "^7.4.6"
37
39
  },
38
40
  "dependencies": {
39
- "koa": "^2.13.1"
41
+ "koa": "^2.13.1",
42
+ "koa-bodyparser": "^4.4.1",
43
+ "koa-router": "^12.0.1"
40
44
  },
41
45
  "peerDependencies": {
42
46
  "@fastcar/core": "*",
43
47
  "@fastcar/server": "*",
48
+ "@koa/multer": "^3.0.2",
44
49
  "@types/koa-bodyparser": "^4.3.3",
45
50
  "@types/koa-mount": "^4.0.1",
46
51
  "@types/koa-range": "^0.3.2",
47
- "@types/koa-router": "^7.4.4",
52
+ "@types/koa-router": "^7.4.6",
48
53
  "@types/koa-static": "^4.0.2",
49
54
  "@types/koa2-cors": "^2.0.2",
50
55
  "koa-body": "^4.2.0",
51
- "koa-bodyparser": "^4.3.0",
56
+ "koa-bodyparser": "^4.4.1",
52
57
  "koa-mount": "^4.0.0",
53
58
  "koa-range": "^0.3.0",
54
- "koa-router": "^10.1.1",
55
59
  "koa-static": "^5.0.0",
56
60
  "koa2-cors": "^2.0.6",
57
- "swagger-ui-dist": "4.5.0"
61
+ "swagger-ui-dist": "4.5.0",
62
+ "@types/koa__multer": "*",
63
+ "multer": "*"
58
64
  },
59
65
  "peerDependenciesMeta": {
60
66
  "@types/koa-bodyparser": {
@@ -98,6 +104,12 @@
98
104
  },
99
105
  "swagger-ui-dist": {
100
106
  "optional": true
107
+ },
108
+ "multer": {
109
+ "optional": true
110
+ },
111
+ "@types/koa__multer": {
112
+ "optional": true
101
113
  }
102
114
  },
103
115
  "repository": {
@@ -5,7 +5,7 @@ import * as Koa from "koa";
5
5
  import * as KoaRouter from "koa-router";
6
6
  import { MethodType } from "./type/MethodType";
7
7
  import { DesignMeta } from "./type/DesignMeta";
8
- import { TypeUtil } from "@fastcar/core/utils";
8
+ import { TypeUtil, ValidationUtil } from "@fastcar/core/utils";
9
9
  import { KoaConfig } from "./type/KoaConfig";
10
10
  import { ServerApplication } from "@fastcar/server";
11
11
 
@@ -69,10 +69,15 @@ export default class KoaApplication {
69
69
  //去除ctx的影响
70
70
  let callBack = async (ctx: any, next?: Function) => {
71
71
  //进行参数的取值
72
- let body = Object.keys(ctx.query).length > 0 ? ctx.query : ctx.request.body;
72
+ let body = {};
73
73
 
74
- if (!body) {
75
- body = {};
74
+ //自动合并传参 如果有重合的部分 需要再次单独取就好了
75
+ if (Object.keys(ctx.query).length > 0) {
76
+ Object.assign(body, ctx.query);
77
+ }
78
+
79
+ if (!!ctx.request.body) {
80
+ Object.assign(body, ctx.request.body);
76
81
  }
77
82
 
78
83
  if (!!ctx.params) {
@@ -80,7 +85,7 @@ export default class KoaApplication {
80
85
  }
81
86
 
82
87
  let res = await instance[item.method](body, ctx);
83
- if (!!res) {
88
+ if (ValidationUtil.isNotNull(res)) {
84
89
  ctx.body = res;
85
90
  }
86
91
 
package/src/annotation.ts CHANGED
@@ -8,6 +8,7 @@ import PatchMapping from "./annotation/router/PatchMapping";
8
8
  import PostMapping from "./annotation/router/PostMapping";
9
9
  import PutMapping from "./annotation/router/PutMapping";
10
10
  import RequestMapping from "./annotation/router/RequestMapping";
11
+ import KoaMulter from "./middleware/KoaMulter";
11
12
 
12
13
  //声明简化的方式
13
14
  const GET = GetMapping;
@@ -40,4 +41,5 @@ export {
40
41
  PATCH,
41
42
  ALL,
42
43
  REQUEST,
44
+ KoaMulter,
43
45
  };
@@ -0,0 +1,7 @@
1
+ import * as multer from "@koa/multer";
2
+ import { FastCarApplication } from "@fastcar/core";
3
+
4
+ //对文件内容做解析
5
+ export default function KoaMulter(app: FastCarApplication) {
6
+ return multer().single();
7
+ }
@@ -61,15 +61,19 @@ let KoaApplication = class KoaApplication {
61
61
  //去除ctx的影响
62
62
  let callBack = async (ctx, next) => {
63
63
  //进行参数的取值
64
- let body = Object.keys(ctx.query).length > 0 ? ctx.query : ctx.request.body;
65
- if (!body) {
66
- body = {};
64
+ let body = {};
65
+ //自动合并传参 如果有重合的部分 需要再次单独取就好了
66
+ if (Object.keys(ctx.query).length > 0) {
67
+ Object.assign(body, ctx.query);
68
+ }
69
+ if (!!ctx.request.body) {
70
+ Object.assign(body, ctx.request.body);
67
71
  }
68
72
  if (!!ctx.params) {
69
73
  Object.assign(body, ctx.params);
70
74
  }
71
75
  let res = await instance[item.method](body, ctx);
72
- if (!!res) {
76
+ if (utils_1.ValidationUtil.isNotNull(res)) {
73
77
  ctx.body = res;
74
78
  }
75
79
  if (next) {
@@ -1,6 +1,6 @@
1
1
  "use strict";
2
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;
3
+ exports.KoaMulter = 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
4
  const EnableKoa_1 = require("./annotation/EnableKoa");
5
5
  exports.EnableKoa = EnableKoa_1.default;
6
6
  const KoaMiddleware_1 = require("./annotation/KoaMiddleware");
@@ -21,6 +21,8 @@ const PutMapping_1 = require("./annotation/router/PutMapping");
21
21
  exports.PutMapping = PutMapping_1.default;
22
22
  const RequestMapping_1 = require("./annotation/router/RequestMapping");
23
23
  exports.RequestMapping = RequestMapping_1.default;
24
+ const KoaMulter_1 = require("./middleware/KoaMulter");
25
+ exports.KoaMulter = KoaMulter_1.default;
24
26
  //声明简化的方式
25
27
  const GET = GetMapping_1.default;
26
28
  exports.GET = GET;
@@ -0,0 +1,8 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ const multer = require("@koa/multer");
4
+ //对文件内容做解析
5
+ function KoaMulter(app) {
6
+ return multer().single();
7
+ }
8
+ exports.default = KoaMulter;
package/test/logs/sys.log CHANGED
@@ -68,3 +68,51 @@
68
68
  {"timestamp":"2022-04-23 10:22:04.326","level":"INFO","label":"sys","message":"http server is running in 12234"}
69
69
  {"timestamp":"2022-04-23 10:22:04.327","level":"INFO","label":"sys","message":"start server koaSimple is run"}
70
70
  {"timestamp":"2022-04-23 10:22:04.328","level":"INFO","label":"sys","message":"version 1.0.0"}
71
+ {"timestamp":"2023-02-18 15:57:04.345","level":"INFO","label":"sys","message":"Start scanning component"}
72
+ {"timestamp":"2023-02-18 15:57:05.478","level":"INFO","label":"sys","message":"Complete component scan"}
73
+ {"timestamp":"2023-02-18 15:57:05.479","level":"INFO","label":"sys","message":"Call application initialization method"}
74
+ {"timestamp":"2023-02-18 15:57:06.254","level":"INFO","label":"sys","message":"http server is running in 1234"}
75
+ {"timestamp":"2023-02-18 15:57:06.256","level":"INFO","label":"sys","message":"start server koaSimple is run"}
76
+ {"timestamp":"2023-02-18 15:57:06.258","level":"INFO","label":"sys","message":"version 1.0.0"}
77
+ {"timestamp":"2023-11-06 11:03:08.665","level":"INFO","label":"sys","message":"Start scanning component"}
78
+ {"timestamp":"2023-11-06 11:03:09.601","level":"INFO","label":"sys","message":"Complete component scan"}
79
+ {"timestamp":"2023-11-06 11:03:09.602","level":"INFO","label":"sys","message":"Call application initialization method"}
80
+ {"timestamp":"2023-11-06 11:03:10.19","level":"INFO","label":"sys","message":"http server is running in 1234"}
81
+ {"timestamp":"2023-11-06 11:03:10.19","level":"INFO","label":"sys","message":"start server koaSimple is run"}
82
+ {"timestamp":"2023-11-06 11:03:10.20","level":"INFO","label":"sys","message":"version 1.0.0"}
83
+ {"timestamp":"2023-11-06 11:04:45.703","level":"INFO","label":"sys","message":"Start scanning component"}
84
+ {"timestamp":"2023-11-06 11:04:46.665","level":"INFO","label":"sys","message":"Complete component scan"}
85
+ {"timestamp":"2023-11-06 11:04:46.665","level":"INFO","label":"sys","message":"Call application initialization method"}
86
+ {"timestamp":"2023-11-06 11:04:46.672","level":"INFO","label":"sys","message":"http server is running in 1234"}
87
+ {"timestamp":"2023-11-06 11:04:46.672","level":"INFO","label":"sys","message":"start server koaSimple is run"}
88
+ {"timestamp":"2023-11-06 11:04:46.673","level":"INFO","label":"sys","message":"version 1.0.0"}
89
+ {"timestamp":"2023-11-06 11:06:22.126","level":"INFO","label":"sys","message":"Start scanning component"}
90
+ {"timestamp":"2023-11-06 11:06:22.428","level":"INFO","label":"sys","message":"Complete component scan"}
91
+ {"timestamp":"2023-11-06 11:06:22.428","level":"INFO","label":"sys","message":"Call application initialization method"}
92
+ {"timestamp":"2023-11-06 11:06:30.951","level":"INFO","label":"sys","message":"http server is running in 1234"}
93
+ {"timestamp":"2023-11-06 11:06:30.952","level":"INFO","label":"sys","message":"start server koaSimple is run"}
94
+ {"timestamp":"2023-11-06 11:06:30.953","level":"INFO","label":"sys","message":"version 1.0.0"}
95
+ {"timestamp":"2023-11-06 11:06:37.815","level":"INFO","label":"sys","message":"Start scanning component"}
96
+ {"timestamp":"2023-11-06 11:06:38.123","level":"INFO","label":"sys","message":"Complete component scan"}
97
+ {"timestamp":"2023-11-06 11:06:38.124","level":"INFO","label":"sys","message":"Call application initialization method"}
98
+ {"timestamp":"2023-11-06 11:06:40.295","level":"INFO","label":"sys","message":"http server is running in 1234"}
99
+ {"timestamp":"2023-11-06 11:06:40.296","level":"INFO","label":"sys","message":"start server koaSimple is run"}
100
+ {"timestamp":"2023-11-06 11:06:40.296","level":"INFO","label":"sys","message":"version 1.0.0"}
101
+ {"timestamp":"2023-11-06 11:19:08.992","level":"INFO","label":"sys","message":"Start scanning component"}
102
+ {"timestamp":"2023-11-06 11:19:09.815","level":"INFO","label":"sys","message":"Complete component scan"}
103
+ {"timestamp":"2023-11-06 11:19:09.816","level":"INFO","label":"sys","message":"Call application initialization method"}
104
+ {"timestamp":"2023-11-06 11:19:13.80","level":"INFO","label":"sys","message":"http server is running in 1234"}
105
+ {"timestamp":"2023-11-06 11:19:13.81","level":"INFO","label":"sys","message":"start server koaSimple is run"}
106
+ {"timestamp":"2023-11-06 11:19:13.81","level":"INFO","label":"sys","message":"version 1.0.0"}
107
+ {"timestamp":"2023-11-06 11:33:57.571","level":"INFO","label":"sys","message":"Start scanning component"}
108
+ {"timestamp":"2023-11-06 11:33:57.951","level":"INFO","label":"sys","message":"Complete component scan"}
109
+ {"timestamp":"2023-11-06 11:33:57.951","level":"INFO","label":"sys","message":"Call application initialization method"}
110
+ {"timestamp":"2023-11-06 11:33:57.958","level":"INFO","label":"sys","message":"http server is running in 1234"}
111
+ {"timestamp":"2023-11-06 11:33:57.959","level":"INFO","label":"sys","message":"start server koaSimple is run"}
112
+ {"timestamp":"2023-11-06 11:33:57.959","level":"INFO","label":"sys","message":"version 1.0.0"}
113
+ {"timestamp":"2023-11-06 11:38:42.619","level":"INFO","label":"sys","message":"Start scanning component"}
114
+ {"timestamp":"2023-11-06 11:38:42.903","level":"INFO","label":"sys","message":"Complete component scan"}
115
+ {"timestamp":"2023-11-06 11:38:42.904","level":"INFO","label":"sys","message":"Call application initialization method"}
116
+ {"timestamp":"2023-11-06 11:38:42.910","level":"INFO","label":"sys","message":"http server is running in 1234"}
117
+ {"timestamp":"2023-11-06 11:38:42.911","level":"INFO","label":"sys","message":"start server koaSimple is run"}
118
+ {"timestamp":"2023-11-06 11:38:42.912","level":"INFO","label":"sys","message":"version 1.0.0"}
@@ -8,6 +8,7 @@ import * as Koa from "koa";
8
8
  import ExceptionGlobalHandler from "../../src/middleware/ExceptionGlobalHandler";
9
9
  import KoaCors from "../../src/middleware/koaCors";
10
10
  import Swagger from "../../src/middleware/Swagger";
11
+ import KoaMulter from "../../src/middleware/KoaMulter";
11
12
 
12
13
  const m1 = () => {
13
14
  return async (ctx: any, next: Function) => {
@@ -27,10 +28,8 @@ const m2 = (): Koa.Middleware => {
27
28
 
28
29
  @Application
29
30
  @EnableKoa //开启koa
30
- @KoaMiddleware(ExceptionGlobalHandler, KoaStatic, KoaBodyParser)
31
- @KoaMiddleware(KoaCors)
32
- @KoaMiddleware(Swagger)
33
- @KoaMiddleware(m1, m2)
31
+ @KoaMiddleware(ExceptionGlobalHandler, KoaBodyParser as any, KoaMulter)
32
+ // @KoaMiddleware(m1, m2)
34
33
  class APP {
35
34
  app!: FastCarApplication;
36
35
  }