typespeed 2.0.23 → 2.1.2

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.
@@ -15,9 +15,9 @@ const routerMiddleware = {};
15
15
  function setRouter(app) {
16
16
  ["get", "post", "all"].forEach(method => {
17
17
  for (let key in routerMapper[method]) {
18
- let rounterFunction = routerMapper[method][key];
18
+ const rounterFunction = routerMapper[method][key];
19
19
  if (routerMiddleware[rounterFunction["name"]]) {
20
- let args = [key, ...routerMiddleware[rounterFunction["name"]], rounterFunction["invoker"]];
20
+ const args = [key, ...routerMiddleware[rounterFunction["name"]], rounterFunction["invoker"]];
21
21
  app[method].apply(app, args);
22
22
  }
23
23
  else {
@@ -32,6 +32,8 @@ function mapperFunction(method, value) {
32
32
  routerMapper[method][value] = {
33
33
  "path": value,
34
34
  "name": [target.constructor.name, propertyKey].toString(),
35
+ "target": target.constructor,
36
+ "propertyKey": propertyKey,
35
37
  "invoker": async (req, res, next) => {
36
38
  const routerBean = (0, core_decorator_1.getComponent)(target.constructor);
37
39
  try {
@@ -39,7 +41,7 @@ function mapperFunction(method, value) {
39
41
  if (routerParamsTotal[[target.constructor.name, propertyKey].toString()]) {
40
42
  paramTotal = Math.max(paramTotal, routerParamsTotal[[target.constructor.name, propertyKey].toString()]);
41
43
  }
42
- let args = [req, res, next];
44
+ const args = [req, res, next];
43
45
  if (paramTotal > 0) {
44
46
  for (let i = 0; i < paramTotal; i++) {
45
47
  if (routerParams[[target.constructor.name, propertyKey, i].toString()]) {
@@ -147,18 +149,21 @@ function reqBody(target, propertyKey, parameterIndex) {
147
149
  routerParams[key] = (req, res, next) => req.body;
148
150
  }
149
151
  exports.reqBody = reqBody;
150
- function reqParam(paramName) {
151
- return (target, propertyKey, parameterIndex) => {
152
- const key = [target.constructor.name, propertyKey, parameterIndex].toString();
153
- routerParams[key] = (req, res, next) => req.params[paramName];
154
- };
152
+ function reqParam(target, propertyKey, parameterIndex) {
153
+ const key = [target.constructor.name, propertyKey, parameterIndex].toString();
154
+ const paramName = getParamInFunction(target[propertyKey], parameterIndex);
155
+ routerParams[key] = (req, res, next) => req.params[paramName];
155
156
  }
156
157
  exports.reqParam = reqParam;
157
- function reqQuery(paramName) {
158
- return (target, propertyKey, parameterIndex) => {
159
- const key = [target.constructor.name, propertyKey, parameterIndex].toString();
160
- routerParams[key] = (req, res, next) => req.query[paramName];
161
- };
158
+ function getParamInFunction(fn, index) {
159
+ const code = fn.toString().replace(/((\/\/.*$)|(\/\*[\s\S]*?\*\/))/mg, '').replace(/=>.*$/mg, '').replace(/=[^,]+/mg, '');
160
+ const result = code.slice(code.indexOf('(') + 1, code.indexOf(')')).match(/([^\s,]+)/g);
161
+ return result[index] || null;
162
+ }
163
+ function reqQuery(target, propertyKey, parameterIndex) {
164
+ const key = [target.constructor.name, propertyKey, parameterIndex].toString();
165
+ const paramName = getParamInFunction(target[propertyKey], parameterIndex);
166
+ routerParams[key] = (req, res, next) => req.query[paramName];
162
167
  }
163
168
  exports.reqQuery = reqQuery;
164
169
  function reqForm(paramName) {
@@ -178,9 +178,9 @@ declare function next(target: any, propertyKey: string, parameterIndex: number)
178
178
  /** request.body 对象装饰器,作为路由页面方法参数,获取 request.body 对象 */
179
179
  declare function reqBody(target: any, propertyKey: string, parameterIndex: number) : void;
180
180
  /** request.param 值装饰器,作为路由页面方法参数,获取 request.params 内容 */
181
- declare function reqParam(paramName: string) : (target: any, propertyKey: string, parameterIndex: number) => void;
181
+ declare function reqParam(target: any, propertyKey: string, parameterIndex: number) : void;
182
182
  /** request.query 值装饰器,作为路由页面方法参数,获取 request.query 内容 */
183
- declare function reqQuery(paramName: string) : (target: any, propertyKey: string, parameterIndex: number) => void;
183
+ declare function reqQuery(target: any, propertyKey: string, parameterIndex: number) : void;
184
184
  /** request 表单值装饰器,作为路由页面方法参数,获取 request.body 表单内容 */
185
185
  declare function reqForm(paramName: string) : (target: any, propertyKey: string, parameterIndex: number) => void;
186
186
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "typespeed",
3
- "version": "2.0.23",
3
+ "version": "2.1.2",
4
4
  "description": "A new Framework for TypeScript.",
5
5
  "author": "speedphp",
6
6
  "license": "MIT License",
@@ -34,7 +34,7 @@
34
34
  "commander": "^9.4.0",
35
35
  "compression": "^1.7.4",
36
36
  "connect-redis": "^6.1.3",
37
- "consolidate": "^0.16.0",
37
+ "consolidate": "1.0.1",
38
38
  "cookie-parser": "^1.4.6",
39
39
  "cron": "^2.3.0",
40
40
  "express": "^4.18.1",
@@ -52,6 +52,7 @@
52
52
  "tracer": "^1.1.6",
53
53
  "typescript": "^4.7.4",
54
54
  "walk-sync": "^3.0.0",
55
- "amqplib": "0.10.3"
55
+ "amqplib": "0.10.3",
56
+ "swagger-ui-express": "4.6.3"
56
57
  }
57
58
  }
@@ -54,12 +54,12 @@ export default class ExpressServer extends ServerFactory {
54
54
 
55
55
  public start(port: number) {
56
56
  this.app.use(this.authentication.afterCompletion);
57
-
58
57
  this.middlewareList.forEach(middleware => {
59
58
  this.app.use(middleware);
60
59
  });
61
60
 
62
61
  this.setDefaultMiddleware();
62
+
63
63
  this.app.listen(port, () => {
64
64
  log("server start at port: " + port);
65
65
  });
@@ -2,7 +2,6 @@ import * as express from "express";
2
2
  import * as multiparty from "multiparty";
3
3
  import { expressjwt } from "express-jwt";
4
4
  import { getComponent } from "./core.decorator";
5
- import { param } from "./database.decorator";
6
5
 
7
6
  const routerMapper = {
8
7
  "get": {},
@@ -15,9 +14,9 @@ const routerMiddleware = {};
15
14
  function setRouter(app: express.Application) {
16
15
  ["get", "post", "all"].forEach(method => {
17
16
  for (let key in routerMapper[method]) {
18
- let rounterFunction = routerMapper[method][key];
17
+ const rounterFunction = routerMapper[method][key];
19
18
  if (routerMiddleware[rounterFunction["name"]]) {
20
- let args: Array<any> = [key, ...routerMiddleware[rounterFunction["name"]], rounterFunction["invoker"]];
19
+ const args: Array<any> = [key, ...routerMiddleware[rounterFunction["name"]], rounterFunction["invoker"]];
21
20
  app[method].apply(app, args);
22
21
  } else {
23
22
  app[method](key, rounterFunction["invoker"]);
@@ -31,6 +30,8 @@ function mapperFunction(method: string, value: string) {
31
30
  routerMapper[method][value] = {
32
31
  "path": value,
33
32
  "name": [target.constructor.name, propertyKey].toString(),
33
+ "target": target.constructor,
34
+ "propertyKey": propertyKey,
34
35
  "invoker": async (req, res, next) => {
35
36
  const routerBean = getComponent(target.constructor);
36
37
  try {
@@ -38,7 +39,7 @@ function mapperFunction(method: string, value: string) {
38
39
  if(routerParamsTotal[[target.constructor.name, propertyKey].toString()]){
39
40
  paramTotal = Math.max(paramTotal, routerParamsTotal[[target.constructor.name, propertyKey].toString()]);
40
41
  }
41
- let args = [req, res, next];
42
+ const args = [req, res, next];
42
43
  if(paramTotal > 0) {
43
44
  for(let i = 0; i < paramTotal; i++) {
44
45
  if(routerParams[[target.constructor.name, propertyKey, i].toString()]){
@@ -142,18 +143,22 @@ function reqBody(target: any, propertyKey: string, parameterIndex: number) {
142
143
  routerParams[key] = (req, res, next) => req.body;
143
144
  }
144
145
 
145
- function reqParam(paramName: string) {
146
- return (target: any, propertyKey: string, parameterIndex: number) => {
147
- const key = [target.constructor.name, propertyKey, parameterIndex].toString();
148
- routerParams[key] = (req, res, next) => req.params[paramName];
149
- }
146
+ function reqParam(target: any, propertyKey: string, parameterIndex: number) {
147
+ const key = [target.constructor.name, propertyKey, parameterIndex].toString();
148
+ const paramName = getParamInFunction(target[propertyKey], parameterIndex);
149
+ routerParams[key] = (req, res, next) => req.params[paramName];
150
150
  }
151
151
 
152
- function reqQuery(paramName: string) {
153
- return (target: any, propertyKey: string, parameterIndex: number) => {
154
- const key = [target.constructor.name, propertyKey, parameterIndex].toString();
155
- routerParams[key] = (req, res, next) => req.query[paramName];
156
- }
152
+ function getParamInFunction(fn: Function, index: number) {
153
+ const code = fn.toString().replace(/((\/\/.*$)|(\/\*[\s\S]*?\*\/))/mg, '').replace(/=>.*$/mg, '').replace(/=[^,]+/mg, '');
154
+ const result = code.slice(code.indexOf('(') + 1, code.indexOf(')')).match(/([^\s,]+)/g);
155
+ return result[index] || null;
156
+ }
157
+
158
+ function reqQuery(target: any, propertyKey: string, parameterIndex: number) {
159
+ const key = [target.constructor.name, propertyKey, parameterIndex].toString();
160
+ const paramName = getParamInFunction(target[propertyKey], parameterIndex);
161
+ routerParams[key] = (req, res, next) => req.query[paramName];
157
162
  }
158
163
 
159
164
  function reqForm(paramName: string) {
@@ -178,9 +178,9 @@ declare function next(target: any, propertyKey: string, parameterIndex: number)
178
178
  /** request.body 对象装饰器,作为路由页面方法参数,获取 request.body 对象 */
179
179
  declare function reqBody(target: any, propertyKey: string, parameterIndex: number) : void;
180
180
  /** request.param 值装饰器,作为路由页面方法参数,获取 request.params 内容 */
181
- declare function reqParam(paramName: string) : (target: any, propertyKey: string, parameterIndex: number) => void;
181
+ declare function reqParam(target: any, propertyKey: string, parameterIndex: number) : void;
182
182
  /** request.query 值装饰器,作为路由页面方法参数,获取 request.query 内容 */
183
- declare function reqQuery(paramName: string) : (target: any, propertyKey: string, parameterIndex: number) => void;
183
+ declare function reqQuery(target: any, propertyKey: string, parameterIndex: number) : void;
184
184
  /** request 表单值装饰器,作为路由页面方法参数,获取 request.body 表单内容 */
185
185
  declare function reqForm(paramName: string) : (target: any, propertyKey: string, parameterIndex: number) => void;
186
186
 
@@ -0,0 +1,5 @@
1
+ import UserDto from "./user-dto.class";
2
+
3
+ export default class MutilUsers {
4
+ constructor(public group: string, public users: UserDto[]){}
5
+ }
@@ -1,5 +1,6 @@
1
- import { component, getMapping, postMapping, log } from "../../";
2
- import { req, res, reqQuery, reqBody, reqForm, reqParam } from "../../src/route.decorator";
1
+ import { component, getMapping, postMapping, log, req, res, reqQuery, reqBody, reqForm, reqParam } from "../../";
2
+ import MutilUsers from "./entities/mutil-users.class";
3
+ import UserDto from "./entities/user-dto.class";
3
4
 
4
5
 
5
6
  @component
@@ -11,15 +12,15 @@ export default class TestRequest {
11
12
  }
12
13
 
13
14
  @getMapping("/request/query")
14
- testQuery(req, res, @reqQuery("id") id: number) {
15
+ async testQuery(req, res, @reqQuery id: number): Promise<MutilUsers> {
15
16
  log("id: " + id);
16
- res.send("test query");
17
+ return Promise.resolve(new MutilUsers("group", [new UserDto(1, "name"), new UserDto(2, "name")]));
17
18
  }
18
19
 
19
20
  @postMapping("/request/body")
20
- testBody(@res res, @reqBody body: object) {
21
+ testBody(@res res, @reqBody body: object):MutilUsers {
21
22
  log("body: " + JSON.stringify(body));
22
- res.send("test body");
23
+ return new MutilUsers("group", [new UserDto(1, "name"), new UserDto(2, "name")]);
23
24
  }
24
25
 
25
26
  @postMapping("/request/form")
@@ -29,7 +30,7 @@ export default class TestRequest {
29
30
  }
30
31
 
31
32
  @getMapping("/request/param/:id")
32
- testParam(@res res, @reqParam("id") id: number) {
33
+ testParam(@res res, @reqParam id: number) {
33
34
  log("id: " + id);
34
35
  res.send("test param");
35
36
  }