@midwayjs/core 3.0.0-beta.2 → 3.0.0-beta.6

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.
Files changed (50) hide show
  1. package/CHANGELOG.md +52 -0
  2. package/dist/baseFramework.d.ts +11 -9
  3. package/dist/baseFramework.js +17 -14
  4. package/dist/{util → common}/fileDetector.d.ts +0 -0
  5. package/dist/{util → common}/fileDetector.js +0 -0
  6. package/dist/common/filterManager.d.ts +19 -0
  7. package/dist/common/filterManager.js +85 -0
  8. package/dist/common/middlewareManager.d.ts +11 -0
  9. package/dist/{util → common}/middlewareManager.js +0 -0
  10. package/dist/{util → common}/serviceFactory.d.ts +0 -0
  11. package/dist/{util → common}/serviceFactory.js +0 -0
  12. package/dist/{util → common}/triggerCollector.d.ts +0 -0
  13. package/dist/{util → common}/triggerCollector.js +0 -0
  14. package/dist/{util → common}/webGenerator.d.ts +6 -9
  15. package/dist/{util → common}/webGenerator.js +14 -21
  16. package/dist/{util → common}/webRouterCollector.d.ts +9 -4
  17. package/dist/{util → common}/webRouterCollector.js +47 -27
  18. package/dist/config/config.default.d.ts +3 -17
  19. package/dist/error/base.d.ts +3 -1
  20. package/dist/error/base.js +1 -0
  21. package/dist/error/code.d.ts +1 -1
  22. package/dist/error/code.js +1 -1
  23. package/dist/error/framework.d.ts +2 -2
  24. package/dist/error/framework.js +8 -5
  25. package/dist/error/http.d.ts +2 -1
  26. package/dist/error/http.js +3 -3
  27. package/dist/error/index.d.ts +1 -0
  28. package/dist/error/index.js +1 -0
  29. package/dist/index.d.ts +8 -9
  30. package/dist/index.js +10 -11
  31. package/dist/interface.d.ts +50 -28
  32. package/dist/service/configService.js +4 -5
  33. package/dist/service/decoratorService.js +23 -21
  34. package/dist/service/frameworkService.d.ts +4 -3
  35. package/dist/service/frameworkService.js +10 -0
  36. package/dist/service/lifeCycleService.js +2 -0
  37. package/dist/service/loggerService.d.ts +1 -2
  38. package/dist/service/loggerService.js +1 -10
  39. package/dist/service/middlewareService.d.ts +2 -2
  40. package/dist/service/middlewareService.js +3 -25
  41. package/dist/setup.js +2 -0
  42. package/dist/util/contextUtil.d.ts +1 -1
  43. package/dist/util/index.d.ts +37 -0
  44. package/dist/util/index.js +96 -1
  45. package/dist/util/webRouterParam.d.ts +2 -2
  46. package/dist/util/webRouterParam.js +17 -18
  47. package/package.json +5 -5
  48. package/dist/util/exceptionFilterManager.d.ts +0 -13
  49. package/dist/util/exceptionFilterManager.js +0 -53
  50. package/dist/util/middlewareManager.d.ts +0 -11
@@ -24,7 +24,27 @@ let MidwayDecoratorService = class MidwayDecoratorService {
24
24
  }
25
25
  async init() {
26
26
  // add custom method decorator listener
27
- this.applicationContext.onBeforeBind((Clzz, options) => {
27
+ this.applicationContext.onBeforeBind(Clzz => {
28
+ // find custom method decorator metadata, include method decorator information array
29
+ const methodDecoratorMetadataList = (0, decorator_1.getClassMetadata)(decorator_1.INJECT_CUSTOM_METHOD, Clzz);
30
+ if (methodDecoratorMetadataList) {
31
+ // loop it, save this order for decorator run
32
+ for (const meta of methodDecoratorMetadataList) {
33
+ const { propertyName, key, metadata } = meta;
34
+ // add aspect implementation first
35
+ this.aspectService.interceptPrototypeMethod(Clzz, propertyName, () => {
36
+ const methodDecoratorHandler = this.methodDecoratorMap.get(key);
37
+ if (!methodDecoratorHandler) {
38
+ throw new error_1.MidwayCommonError(`Method Decorator "${key}" handler not found, please register first.`);
39
+ }
40
+ return this.methodDecoratorMap.get(key)({
41
+ target: Clzz,
42
+ propertyName,
43
+ metadata,
44
+ });
45
+ });
46
+ }
47
+ }
28
48
  // find custom param decorator metadata
29
49
  const parameterDecoratorMetadata = (0, decorator_1.getClassMetadata)(decorator_1.INJECT_CUSTOM_PARAM, Clzz);
30
50
  if (parameterDecoratorMetadata) {
@@ -42,12 +62,14 @@ let MidwayDecoratorService = class MidwayDecoratorService {
42
62
  if (!parameterDecoratorHandler) {
43
63
  throw new error_1.MidwayCommonError(`Parameter Decorator "${key}" handler not found, please register first.`);
44
64
  }
65
+ const paramTypes = (0, decorator_1.getMethodParamTypes)(Clzz, propertyName);
45
66
  newArgs[parameterIndex] = await parameterDecoratorHandler({
46
67
  metadata,
47
68
  propertyName,
48
69
  parameterIndex,
49
70
  target: Clzz,
50
71
  originArgs: joinPoint.args,
72
+ originParamType: paramTypes[parameterIndex],
51
73
  });
52
74
  }
53
75
  joinPoint.args = newArgs;
@@ -56,26 +78,6 @@ let MidwayDecoratorService = class MidwayDecoratorService {
56
78
  });
57
79
  }
58
80
  }
59
- // find custom method decorator metadata, include method decorator information array
60
- const methodDecoratorMetadataList = (0, decorator_1.getClassMetadata)(decorator_1.INJECT_CUSTOM_METHOD, Clzz);
61
- if (methodDecoratorMetadataList) {
62
- // loop it, save this order for decorator run
63
- for (const meta of methodDecoratorMetadataList) {
64
- const { propertyName, key, metadata } = meta;
65
- // add aspect implementation first
66
- this.aspectService.interceptPrototypeMethod(Clzz, propertyName, () => {
67
- const methodDecoratorHandler = this.methodDecoratorMap.get(key);
68
- if (!methodDecoratorHandler) {
69
- throw new error_1.MidwayCommonError(`Method Decorator "${key}" handler not found, please register first.`);
70
- }
71
- return this.methodDecoratorMap.get(key)({
72
- target: Clzz,
73
- propertyName,
74
- metadata,
75
- });
76
- });
77
- }
78
- }
79
81
  });
80
82
  // add custom property decorator listener
81
83
  this.applicationContext.onObjectCreated((instance, options) => {
@@ -15,10 +15,11 @@ export declare class MidwayFrameworkService {
15
15
  private mainFramework;
16
16
  private globalFrameworkMap;
17
17
  private globalFrameworkList;
18
- init(): Promise<void>;
18
+ protected init(): Promise<void>;
19
19
  getMainApp(): any;
20
- getMainFramework(): IMidwayFramework<any, any>;
21
- getFramework(type: MidwayFrameworkType): IMidwayFramework<any, any>;
20
+ getMainFramework(): IMidwayFramework<any, any, any, unknown, unknown>;
21
+ getFramework(type: MidwayFrameworkType): IMidwayFramework<any, any, any, unknown, unknown>;
22
+ runFramework(): Promise<void>;
22
23
  stopFramework(): Promise<void>;
23
24
  }
24
25
  //# sourceMappingURL=frameworkService.d.ts.map
@@ -110,6 +110,16 @@ let MidwayFrameworkService = class MidwayFrameworkService {
110
110
  getFramework(type) {
111
111
  return this.globalFrameworkMap.get(type);
112
112
  }
113
+ async runFramework() {
114
+ for (const frameworkInstance of this.globalFrameworkList) {
115
+ // if enable, just init framework
116
+ if (frameworkInstance.isEnable()) {
117
+ // app init
118
+ await frameworkInstance.run();
119
+ debug(`[core:framework]: Found Framework "${frameworkInstance.getFrameworkName()}" and run.`);
120
+ }
121
+ }
122
+ }
113
123
  async stopFramework() {
114
124
  await Promise.all(Array.from(this.globalFrameworkList).map(frameworkInstance => {
115
125
  return frameworkInstance.stop();
@@ -54,6 +54,8 @@ let MidwayLifeCycleService = class MidwayLifeCycleService {
54
54
  });
55
55
  // exec onReady()
56
56
  await this.runContainerLifeCycle(lifecycleInstanceList, 'onReady');
57
+ // exec framework.run()
58
+ await this.frameworkService.runFramework();
57
59
  // exec onServerReady()
58
60
  await this.runContainerLifeCycle(lifecycleInstanceList, 'onServerReady');
59
61
  }
@@ -1,5 +1,5 @@
1
1
  import { MidwayConfigService } from './configService';
2
- import { ServiceFactory } from '../util/serviceFactory';
2
+ import { ServiceFactory } from '../common/serviceFactory';
3
3
  import { ILogger } from '@midwayjs/logger';
4
4
  import { IMidwayContainer } from '../interface';
5
5
  export declare class MidwayLoggerService extends ServiceFactory<ILogger> {
@@ -15,7 +15,6 @@ export declare class MidwayLoggerService extends ServiceFactory<ILogger> {
15
15
  };
16
16
  protected createClient(config: any, name?: string): Promise<void>;
17
17
  getName(): string;
18
- destroy(): Promise<void>;
19
18
  createLogger(name: any, config: any): ILogger;
20
19
  getLogger(name: string): ILogger;
21
20
  transformEggLogger(options: any): {
@@ -12,7 +12,7 @@ Object.defineProperty(exports, "__esModule", { value: true });
12
12
  exports.MidwayLoggerService = void 0;
13
13
  const decorator_1 = require("@midwayjs/decorator");
14
14
  const configService_1 = require("./configService");
15
- const serviceFactory_1 = require("../util/serviceFactory");
15
+ const serviceFactory_1 = require("../common/serviceFactory");
16
16
  const logger_1 = require("@midwayjs/logger");
17
17
  const levelTransform = level => {
18
18
  if (!level) {
@@ -73,9 +73,6 @@ let MidwayLoggerService = class MidwayLoggerService extends serviceFactory_1.Ser
73
73
  getName() {
74
74
  return 'logger';
75
75
  }
76
- async destroy() {
77
- logger_1.loggers.close();
78
- }
79
76
  createLogger(name, config) {
80
77
  return logger_1.loggers.createLogger(name, config);
81
78
  }
@@ -119,12 +116,6 @@ __decorate([
119
116
  __metadata("design:paramtypes", []),
120
117
  __metadata("design:returntype", Promise)
121
118
  ], MidwayLoggerService.prototype, "init", null);
122
- __decorate([
123
- (0, decorator_1.Destroy)(),
124
- __metadata("design:type", Function),
125
- __metadata("design:paramtypes", []),
126
- __metadata("design:returntype", Promise)
127
- ], MidwayLoggerService.prototype, "destroy", null);
128
119
  MidwayLoggerService = __decorate([
129
120
  (0, decorator_1.Provide)(),
130
121
  (0, decorator_1.Scope)(decorator_1.ScopeEnum.Singleton),
@@ -1,8 +1,8 @@
1
1
  import { CommonMiddleware, IMidwayContainer } from '../interface';
2
- export declare class MidwayMiddlewareService<T> {
2
+ export declare class MidwayMiddlewareService<T, R, N = unknown> {
3
3
  readonly applicationContext: IMidwayContainer;
4
4
  constructor(applicationContext: IMidwayContainer);
5
- compose(middleware: Array<CommonMiddleware<T> | string>, name?: string): Promise<{
5
+ compose(middleware: Array<CommonMiddleware<T, R, N> | string>, name?: string): Promise<{
6
6
  (context: any, next?: any): Promise<any>;
7
7
  _name: string;
8
8
  }>;
@@ -11,8 +11,8 @@ var __metadata = (this && this.__metadata) || function (k, v) {
11
11
  Object.defineProperty(exports, "__esModule", { value: true });
12
12
  exports.pathMatching = exports.MidwayMiddlewareService = void 0;
13
13
  const decorator_1 = require("@midwayjs/decorator");
14
- const pathToRegexp_1 = require("../util/pathToRegexp");
15
14
  const error_1 = require("../error");
15
+ const util_1 = require("../util");
16
16
  let MidwayMiddlewareService = class MidwayMiddlewareService {
17
17
  constructor(applicationContext) {
18
18
  this.applicationContext = applicationContext;
@@ -108,34 +108,12 @@ function pathMatching(options) {
108
108
  if (!options.match && !options.ignore)
109
109
  return () => true;
110
110
  const matchFn = options.match
111
- ? toPathMatch(options.match)
112
- : toPathMatch(options.ignore);
111
+ ? (0, util_1.toPathMatch)(options.match)
112
+ : (0, util_1.toPathMatch)(options.ignore);
113
113
  return function pathMatch(ctx) {
114
114
  const matched = matchFn(ctx);
115
115
  return options.match ? matched : !matched;
116
116
  };
117
117
  }
118
118
  exports.pathMatching = pathMatching;
119
- function toPathMatch(pattern) {
120
- if (typeof pattern === 'string') {
121
- const reg = (0, pathToRegexp_1.pathToRegexp)(pattern, [], { end: false });
122
- if (reg.global)
123
- reg.lastIndex = 0;
124
- return ctx => reg.test(ctx.path);
125
- }
126
- if (pattern instanceof RegExp) {
127
- return ctx => {
128
- if (pattern.global)
129
- pattern.lastIndex = 0;
130
- return pattern.test(ctx.path);
131
- };
132
- }
133
- if (typeof pattern === 'function')
134
- return pattern;
135
- if (Array.isArray(pattern)) {
136
- const matchs = pattern.map(item => toPathMatch(item));
137
- return ctx => matchs.some(match => match(ctx));
138
- }
139
- throw new error_1.MidwayCommonError('match/ignore pattern must be RegExp, Array or String, but got ' + pattern);
140
- }
141
119
  //# sourceMappingURL=middlewareService.js.map
package/dist/setup.js CHANGED
@@ -6,6 +6,7 @@ const config_default_1 = require("./config/config.default");
6
6
  const decorator_1 = require("@midwayjs/decorator");
7
7
  const util = require("util");
8
8
  const path_1 = require("path");
9
+ const logger_1 = require("@midwayjs/logger");
9
10
  const debug = util.debuglog('midway:debug');
10
11
  async function initializeGlobalApplicationContext(globalOptions) {
11
12
  var _a, _b, _c, _d;
@@ -104,6 +105,7 @@ async function destroyGlobalApplicationContext(applicationContext) {
104
105
  // stop container
105
106
  await applicationContext.stop();
106
107
  (0, decorator_1.clearBindContainer)();
108
+ logger_1.loggers.close();
107
109
  global['MIDWAY_APPLICATION_CONTEXT'] = undefined;
108
110
  global['MIDWAY_MAIN_FRAMEWORK'] = undefined;
109
111
  }
@@ -1,5 +1,5 @@
1
1
  import { IConfigurationOptions, IMidwayContainer, IMidwayFramework } from '../interface';
2
2
  export declare const getCurrentApplicationContext: () => IMidwayContainer;
3
- export declare const getCurrentMainFramework: <APP extends import("../interface").IMidwayBaseApplication<import("../interface").Context>, T extends IConfigurationOptions>() => IMidwayFramework<APP, T>;
3
+ export declare const getCurrentMainFramework: <APP extends import("../interface").IMidwayBaseApplication<CTX>, CTX extends import("../interface").Context, CONFIG extends IConfigurationOptions>() => IMidwayFramework<APP, CTX, CONFIG, unknown, unknown>;
4
4
  export declare const getCurrentMainApp: <APP extends import("../interface").IMidwayBaseApplication<import("../interface").Context>>() => APP;
5
5
  //# sourceMappingURL=contextUtil.d.ts.map
@@ -1,5 +1,17 @@
1
+ /**
2
+ * @since 2.0.0
3
+ * @param env
4
+ */
1
5
  export declare const isDevelopmentEnvironment: (env: any) => boolean;
6
+ /**
7
+ * @since 2.0.0
8
+ */
2
9
  export declare const getCurrentEnvironment: () => string;
10
+ /**
11
+ * @param p
12
+ * @param enabledCache
13
+ * @since 2.0.0
14
+ */
3
15
  export declare const safeRequire: (p: any, enabledCache?: boolean) => any;
4
16
  /**
5
17
  * safelyGet(['a','b'],{a: {b: 2}}) // => 2
@@ -8,11 +20,13 @@ export declare const safeRequire: (p: any, enabledCache?: boolean) => any;
8
20
  * safelyGet(['a','1'],{a: {b: 2}}) // => undefined
9
21
  * safelyGet('a.b',{a: {b: 2}}) // => 2
10
22
  * safelyGet('a.b',{c: {b: 2}}) // => undefined
23
+ * @since 2.0.0
11
24
  */
12
25
  export declare function safelyGet(list: string | string[], obj?: Record<string, unknown>): any;
13
26
  /**
14
27
  * 剔除 @ 符号
15
28
  * @param provideId provideId
29
+ * @since 2.0.0
16
30
  */
17
31
  export declare function parsePrefix(provideId: string): string;
18
32
  export declare function getUserHome(): string;
@@ -21,19 +35,42 @@ export declare function joinURLPath(...strArray: any[]): string;
21
35
  * 代理目标所有的原型方法,不包括构造器和内部隐藏方法
22
36
  * @param derivedCtor
23
37
  * @param constructors
38
+ * @since 2.0.0
24
39
  */
25
40
  export declare function delegateTargetPrototypeMethod(derivedCtor: any, constructors: any[]): void;
26
41
  /**
27
42
  * 代理目标原型上的特定方法
28
43
  * @param derivedCtor
29
44
  * @param methods
45
+ * @since 2.0.0
30
46
  */
31
47
  export declare function delegateTargetMethod(derivedCtor: any, methods: string[]): void;
32
48
  /**
33
49
  * 代理目标原型属性
34
50
  * @param derivedCtor
35
51
  * @param properties
52
+ * @since 2.0.0
36
53
  */
37
54
  export declare function delegateTargetProperties(derivedCtor: any, properties: string[]): void;
55
+ /**
56
+ * 代理目标原型属性
57
+ * @since 2.0.0
58
+ * @param timestamp
59
+ */
38
60
  export declare const getCurrentDateString: (timestamp?: number) => string;
61
+ /**
62
+ *
63
+ * @param message
64
+ * @since 3.0.0
65
+ */
66
+ export declare const deprecatedOutput: (message: string) => void;
67
+ /**
68
+ * transform request object to definition type
69
+ *
70
+ * @param originValue
71
+ * @param targetType
72
+ * @since 3.0.0
73
+ */
74
+ export declare const transformRequestObjectByType: (originValue: any, targetType?: any) => any;
75
+ export declare function toPathMatch(pattern: any): any;
39
76
  //# sourceMappingURL=index.d.ts.map
@@ -1,18 +1,33 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.getCurrentDateString = exports.delegateTargetProperties = exports.delegateTargetMethod = exports.delegateTargetPrototypeMethod = exports.joinURLPath = exports.getUserHome = exports.parsePrefix = exports.safelyGet = exports.safeRequire = exports.getCurrentEnvironment = exports.isDevelopmentEnvironment = void 0;
3
+ exports.toPathMatch = exports.transformRequestObjectByType = exports.deprecatedOutput = exports.getCurrentDateString = exports.delegateTargetProperties = exports.delegateTargetMethod = exports.delegateTargetPrototypeMethod = exports.joinURLPath = exports.getUserHome = exports.parsePrefix = exports.safelyGet = exports.safeRequire = exports.getCurrentEnvironment = exports.isDevelopmentEnvironment = void 0;
4
4
  const path_1 = require("path");
5
5
  const fs_1 = require("fs");
6
6
  const util_1 = require("util");
7
+ const transformer = require("class-transformer");
8
+ const pathToRegexp_1 = require("./pathToRegexp");
9
+ const error_1 = require("../error");
7
10
  const debug = (0, util_1.debuglog)('midway:container:util');
11
+ /**
12
+ * @since 2.0.0
13
+ * @param env
14
+ */
8
15
  const isDevelopmentEnvironment = env => {
9
16
  return ['local', 'test', 'unittest'].includes(env);
10
17
  };
11
18
  exports.isDevelopmentEnvironment = isDevelopmentEnvironment;
19
+ /**
20
+ * @since 2.0.0
21
+ */
12
22
  const getCurrentEnvironment = () => {
13
23
  return process.env['MIDWAY_SERVER_ENV'] || process.env['NODE_ENV'] || 'prod';
14
24
  };
15
25
  exports.getCurrentEnvironment = getCurrentEnvironment;
26
+ /**
27
+ * @param p
28
+ * @param enabledCache
29
+ * @since 2.0.0
30
+ */
16
31
  const safeRequire = (p, enabledCache = true) => {
17
32
  if (p.startsWith(`.${path_1.sep}`) || p.startsWith(`..${path_1.sep}`)) {
18
33
  p = (0, path_1.resolve)((0, path_1.dirname)(module.parent.filename), p);
@@ -41,6 +56,7 @@ exports.safeRequire = safeRequire;
41
56
  * safelyGet(['a','1'],{a: {b: 2}}) // => undefined
42
57
  * safelyGet('a.b',{a: {b: 2}}) // => 2
43
58
  * safelyGet('a.b',{c: {b: 2}}) // => undefined
59
+ * @since 2.0.0
44
60
  */
45
61
  function safelyGet(list, obj) {
46
62
  if (arguments.length === 1) {
@@ -66,6 +82,7 @@ exports.safelyGet = safelyGet;
66
82
  /**
67
83
  * 剔除 @ 符号
68
84
  * @param provideId provideId
85
+ * @since 2.0.0
69
86
  */
70
87
  function parsePrefix(provideId) {
71
88
  if (provideId.includes('@')) {
@@ -79,6 +96,7 @@ function getUserHome() {
79
96
  }
80
97
  exports.getUserHome = getUserHome;
81
98
  function joinURLPath(...strArray) {
99
+ strArray = strArray.filter(item => !!item);
82
100
  if (strArray.length === 0) {
83
101
  return '';
84
102
  }
@@ -94,6 +112,7 @@ exports.joinURLPath = joinURLPath;
94
112
  * 代理目标所有的原型方法,不包括构造器和内部隐藏方法
95
113
  * @param derivedCtor
96
114
  * @param constructors
115
+ * @since 2.0.0
97
116
  */
98
117
  function delegateTargetPrototypeMethod(derivedCtor, constructors) {
99
118
  constructors.forEach(baseCtor => {
@@ -111,6 +130,7 @@ exports.delegateTargetPrototypeMethod = delegateTargetPrototypeMethod;
111
130
  * 代理目标原型上的特定方法
112
131
  * @param derivedCtor
113
132
  * @param methods
133
+ * @since 2.0.0
114
134
  */
115
135
  function delegateTargetMethod(derivedCtor, methods) {
116
136
  methods.forEach(name => {
@@ -124,6 +144,7 @@ exports.delegateTargetMethod = delegateTargetMethod;
124
144
  * 代理目标原型属性
125
145
  * @param derivedCtor
126
146
  * @param properties
147
+ * @since 2.0.0
127
148
  */
128
149
  function delegateTargetProperties(derivedCtor, properties) {
129
150
  properties.forEach(name => {
@@ -135,6 +156,11 @@ function delegateTargetProperties(derivedCtor, properties) {
135
156
  });
136
157
  }
137
158
  exports.delegateTargetProperties = delegateTargetProperties;
159
+ /**
160
+ * 代理目标原型属性
161
+ * @since 2.0.0
162
+ * @param timestamp
163
+ */
138
164
  const getCurrentDateString = (timestamp = Date.now()) => {
139
165
  const d = new Date(timestamp);
140
166
  return `${d.getFullYear()}-${(d.getMonth() + 1)
@@ -142,4 +168,73 @@ const getCurrentDateString = (timestamp = Date.now()) => {
142
168
  .padStart(2, '0')}-${d.getDate().toString().padStart(2, '0')}`;
143
169
  };
144
170
  exports.getCurrentDateString = getCurrentDateString;
171
+ /**
172
+ *
173
+ * @param message
174
+ * @since 3.0.0
175
+ */
176
+ const deprecatedOutput = (message) => {
177
+ console.warn('DeprecationWarning: ' + message);
178
+ };
179
+ exports.deprecatedOutput = deprecatedOutput;
180
+ /**
181
+ * transform request object to definition type
182
+ *
183
+ * @param originValue
184
+ * @param targetType
185
+ * @since 3.0.0
186
+ */
187
+ const transformRequestObjectByType = (originValue, targetType) => {
188
+ if (targetType === undefined ||
189
+ targetType === null ||
190
+ targetType === Object) {
191
+ return originValue;
192
+ }
193
+ switch (targetType) {
194
+ case Number:
195
+ return Number(originValue);
196
+ case String:
197
+ return String(originValue);
198
+ case Boolean:
199
+ if (originValue === '0' || originValue === 'false') {
200
+ return false;
201
+ }
202
+ return Boolean(originValue);
203
+ default:
204
+ if (originValue instanceof targetType) {
205
+ return originValue;
206
+ }
207
+ else {
208
+ const transformToInstance = transformer['plainToClass'] || transformer['plainToInstance'];
209
+ return transformToInstance(targetType, originValue);
210
+ }
211
+ }
212
+ };
213
+ exports.transformRequestObjectByType = transformRequestObjectByType;
214
+ function toPathMatch(pattern) {
215
+ if (typeof pattern === 'boolean') {
216
+ return ctx => pattern;
217
+ }
218
+ if (typeof pattern === 'string') {
219
+ const reg = (0, pathToRegexp_1.pathToRegexp)(pattern, [], { end: false });
220
+ if (reg.global)
221
+ reg.lastIndex = 0;
222
+ return ctx => reg.test(ctx.path);
223
+ }
224
+ if (pattern instanceof RegExp) {
225
+ return ctx => {
226
+ if (pattern.global)
227
+ pattern.lastIndex = 0;
228
+ return pattern.test(ctx.path);
229
+ };
230
+ }
231
+ if (typeof pattern === 'function')
232
+ return pattern;
233
+ if (Array.isArray(pattern)) {
234
+ const matchs = pattern.map(item => toPathMatch(item));
235
+ return ctx => matchs.some(match => match(ctx));
236
+ }
237
+ throw new error_1.MidwayCommonError('match/ignore pattern must be RegExp, Array or String, but got ' + pattern);
238
+ }
239
+ exports.toPathMatch = toPathMatch;
145
240
  //# sourceMappingURL=index.js.map
@@ -1,3 +1,3 @@
1
- export declare const extractKoaLikeValue: (key: any, data: any) => (ctx: any, next: any) => any;
2
- export declare const extractExpressLikeValue: (key: any, data: any) => (req: any, res: any, next: any) => any;
1
+ export declare const extractKoaLikeValue: (key: any, data: any, paramType?: any) => (ctx: any, next: any) => any;
2
+ export declare const extractExpressLikeValue: (key: any, data: any, paramType?: any) => (req: any, res: any, next: any) => any;
3
3
  //# sourceMappingURL=webRouterParam.d.ts.map
@@ -2,7 +2,8 @@
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.extractExpressLikeValue = exports.extractKoaLikeValue = void 0;
4
4
  const decorator_1 = require("@midwayjs/decorator");
5
- const extractKoaLikeValue = (key, data) => {
5
+ const index_1 = require("./index");
6
+ const extractKoaLikeValue = (key, data, paramType) => {
6
7
  if (decorator_1.ALL === data) {
7
8
  data = undefined;
8
9
  }
@@ -11,17 +12,15 @@ const extractKoaLikeValue = (key, data) => {
11
12
  case decorator_1.RouteParamTypes.NEXT:
12
13
  return next;
13
14
  case decorator_1.RouteParamTypes.BODY:
14
- return data && ctx.request.body
15
- ? ctx.request.body[data]
16
- : ctx.request.body;
15
+ return (0, index_1.transformRequestObjectByType)(data && ctx.request.body ? ctx.request.body[data] : ctx.request.body, paramType);
17
16
  case decorator_1.RouteParamTypes.PARAM:
18
- return data ? ctx.params[data] : ctx.params;
17
+ return (0, index_1.transformRequestObjectByType)(data ? ctx.params[data] : ctx.params, paramType);
19
18
  case decorator_1.RouteParamTypes.QUERY:
20
- return data ? ctx.query[data] : ctx.query;
19
+ return (0, index_1.transformRequestObjectByType)(data ? ctx.query[data] : ctx.query, paramType);
21
20
  case decorator_1.RouteParamTypes.HEADERS:
22
- return data ? ctx.get(data) : ctx.headers;
21
+ return (0, index_1.transformRequestObjectByType)(data ? ctx.get(data) : ctx.headers, paramType);
23
22
  case decorator_1.RouteParamTypes.SESSION:
24
- return data ? ctx.session[data] : ctx.session;
23
+ return (0, index_1.transformRequestObjectByType)(data ? ctx.session[data] : ctx.session, paramType);
25
24
  case decorator_1.RouteParamTypes.FILESTREAM:
26
25
  return ctx.getFileStream && ctx.getFileStream(data);
27
26
  case decorator_1.RouteParamTypes.FILESSTREAM:
@@ -32,10 +31,10 @@ const extractKoaLikeValue = (key, data) => {
32
31
  return ctx['ip'];
33
32
  case decorator_1.RouteParamTypes.QUERIES:
34
33
  if (ctx.queries) {
35
- return data ? ctx.queries[data] : ctx.queries;
34
+ return (0, index_1.transformRequestObjectByType)(data ? ctx.queries[data] : ctx.queries, paramType);
36
35
  }
37
36
  else {
38
- return data ? ctx.query[data] : ctx.query;
37
+ return (0, index_1.transformRequestObjectByType)(data ? ctx.query[data] : ctx.query, paramType);
39
38
  }
40
39
  default:
41
40
  return null;
@@ -43,7 +42,7 @@ const extractKoaLikeValue = (key, data) => {
43
42
  };
44
43
  };
45
44
  exports.extractKoaLikeValue = extractKoaLikeValue;
46
- const extractExpressLikeValue = (key, data) => {
45
+ const extractExpressLikeValue = (key, data, paramType) => {
47
46
  if (decorator_1.ALL === data) {
48
47
  data = undefined;
49
48
  }
@@ -52,15 +51,15 @@ const extractExpressLikeValue = (key, data) => {
52
51
  case decorator_1.RouteParamTypes.NEXT:
53
52
  return next;
54
53
  case decorator_1.RouteParamTypes.BODY:
55
- return data && req.body ? req.body[data] : req.body;
54
+ return (0, index_1.transformRequestObjectByType)(data && req.body ? req.body[data] : req.body, paramType);
56
55
  case decorator_1.RouteParamTypes.PARAM:
57
- return data ? req.params[data] : req.params;
56
+ return (0, index_1.transformRequestObjectByType)(data ? req.params[data] : req.params, paramType);
58
57
  case decorator_1.RouteParamTypes.QUERY:
59
- return data ? req.query[data] : req.query;
58
+ return (0, index_1.transformRequestObjectByType)(data ? req.query[data] : req.query, paramType);
60
59
  case decorator_1.RouteParamTypes.HEADERS:
61
- return data ? req.get(data) : req.headers;
60
+ return (0, index_1.transformRequestObjectByType)(data ? req.get(data) : req.headers, paramType);
62
61
  case decorator_1.RouteParamTypes.SESSION:
63
- return data ? req.session[data] : req.session;
62
+ return (0, index_1.transformRequestObjectByType)(data ? req.session[data] : req.session, paramType);
64
63
  case decorator_1.RouteParamTypes.FILESTREAM:
65
64
  return req.getFileStream && req.getFileStream(data);
66
65
  case decorator_1.RouteParamTypes.FILESSTREAM:
@@ -71,10 +70,10 @@ const extractExpressLikeValue = (key, data) => {
71
70
  return req['ip'];
72
71
  case decorator_1.RouteParamTypes.QUERIES:
73
72
  if (req.queries) {
74
- return data ? req.queries[data] : req.queries;
73
+ return (0, index_1.transformRequestObjectByType)(data ? req.queries[data] : req.queries, paramType);
75
74
  }
76
75
  else {
77
- return data ? req.query[data] : req.query;
76
+ return (0, index_1.transformRequestObjectByType)(data ? req.query[data] : req.query, paramType);
78
77
  }
79
78
  default:
80
79
  return null;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@midwayjs/core",
3
- "version": "3.0.0-beta.2",
3
+ "version": "3.0.0-beta.6",
4
4
  "description": "midway core",
5
5
  "main": "dist/index",
6
6
  "typings": "dist/index.d.ts",
@@ -21,7 +21,7 @@
21
21
  ],
22
22
  "license": "MIT",
23
23
  "devDependencies": {
24
- "@midwayjs/decorator": "^3.0.0-beta.2",
24
+ "@midwayjs/decorator": "^3.0.0-beta.6",
25
25
  "midway-test-component": "*",
26
26
  "mm": "3",
27
27
  "sinon": "^7.2.2"
@@ -31,8 +31,8 @@
31
31
  },
32
32
  "dependencies": {
33
33
  "@midwayjs/glob": "^1.0.2",
34
- "@midwayjs/logger": "^3.0.0-beta.2",
35
- "class-transformer": "^0.3.1",
34
+ "@midwayjs/logger": "^3.0.0-beta.6",
35
+ "class-transformer": "^0.5.1",
36
36
  "extend2": "^1.0.0",
37
37
  "picomatch": "^2.2.2",
38
38
  "reflect-metadata": "^0.1.13"
@@ -45,5 +45,5 @@
45
45
  "engines": {
46
46
  "node": ">=12"
47
47
  },
48
- "gitHead": "7f07de960da1155a9f7df554e1789c7a97bdd3fe"
48
+ "gitHead": "e4595d30b369e36bef21b36f2b3737d8bc2f802d"
49
49
  }
@@ -1,13 +0,0 @@
1
- import { CommonExceptionFilterUnion, IMidwayContainer, IMidwayContext } from '../interface';
2
- export declare class ExceptionFilterManager<T extends IMidwayContext = IMidwayContext, R = any, N = any> {
3
- private filterList;
4
- private exceptionMap;
5
- private defaultFilter;
6
- useFilter(Filter: CommonExceptionFilterUnion<T, R, N>): void;
7
- init(applicationContext: IMidwayContainer): Promise<void>;
8
- run(err: Error, ctx: T, res?: R, next?: N): Promise<{
9
- result: any;
10
- error: any;
11
- }>;
12
- }
13
- //# sourceMappingURL=exceptionFilterManager.d.ts.map