typespeed 2.1.1 → 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.
@@ -149,18 +149,21 @@ function reqBody(target, propertyKey, parameterIndex) {
149
149
  routerParams[key] = (req, res, next) => req.body;
150
150
  }
151
151
  exports.reqBody = reqBody;
152
- function reqParam(paramName) {
153
- return (target, propertyKey, parameterIndex) => {
154
- const key = [target.constructor.name, propertyKey, parameterIndex].toString();
155
- routerParams[key] = (req, res, next) => req.params[paramName];
156
- };
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];
157
156
  }
158
157
  exports.reqParam = reqParam;
159
- function reqQuery(paramName) {
160
- return (target, propertyKey, parameterIndex) => {
161
- const key = [target.constructor.name, propertyKey, parameterIndex].toString();
162
- routerParams[key] = (req, res, next) => req.query[paramName];
163
- };
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];
164
167
  }
165
168
  exports.reqQuery = reqQuery;
166
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.1.1",
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",
@@ -143,18 +143,22 @@ function reqBody(target: any, propertyKey: string, parameterIndex: number) {
143
143
  routerParams[key] = (req, res, next) => req.body;
144
144
  }
145
145
 
146
- function reqParam(paramName: string) {
147
- return (target: any, propertyKey: string, parameterIndex: number) => {
148
- const key = [target.constructor.name, propertyKey, parameterIndex].toString();
149
- routerParams[key] = (req, res, next) => req.params[paramName];
150
- }
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];
151
150
  }
152
151
 
153
- function reqQuery(paramName: string) {
154
- return (target: any, propertyKey: string, parameterIndex: number) => {
155
- const key = [target.constructor.name, propertyKey, parameterIndex].toString();
156
- routerParams[key] = (req, res, next) => req.query[paramName];
157
- }
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];
158
162
  }
159
163
 
160
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
 
@@ -12,7 +12,7 @@ export default class TestRequest {
12
12
  }
13
13
 
14
14
  @getMapping("/request/query")
15
- async testQuery(req, res, @reqQuery("id") id: number): Promise<MutilUsers> {
15
+ async testQuery(req, res, @reqQuery id: number): Promise<MutilUsers> {
16
16
  log("id: " + id);
17
17
  return Promise.resolve(new MutilUsers("group", [new UserDto(1, "name"), new UserDto(2, "name")]));
18
18
  }
@@ -30,7 +30,7 @@ export default class TestRequest {
30
30
  }
31
31
 
32
32
  @getMapping("/request/param/:id")
33
- testParam(@res res, @reqParam("id") id: number) {
33
+ testParam(@res res, @reqParam id: number) {
34
34
  log("id: " + id);
35
35
  res.send("test param");
36
36
  }