@midwayjs/core 3.0.0-beta.1 → 3.0.0-beta.10

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 (57) hide show
  1. package/CHANGELOG.md +106 -0
  2. package/README.md +1 -1
  3. package/dist/baseFramework.d.ts +13 -10
  4. package/dist/baseFramework.js +32 -23
  5. package/dist/common/applicationManager.d.ts +12 -0
  6. package/dist/common/applicationManager.js +66 -0
  7. package/dist/{util → common}/fileDetector.d.ts +0 -0
  8. package/dist/{util → common}/fileDetector.js +0 -0
  9. package/dist/common/filterManager.d.ts +19 -0
  10. package/dist/common/filterManager.js +85 -0
  11. package/dist/common/middlewareManager.d.ts +68 -0
  12. package/dist/common/middlewareManager.js +183 -0
  13. package/dist/{util → common}/serviceFactory.d.ts +0 -0
  14. package/dist/{util → common}/serviceFactory.js +0 -0
  15. package/dist/{util → common}/triggerCollector.d.ts +0 -0
  16. package/dist/{util → common}/triggerCollector.js +0 -0
  17. package/dist/common/webGenerator.d.ts +16 -0
  18. package/dist/{util → common}/webGenerator.js +34 -52
  19. package/dist/{util → common}/webRouterCollector.d.ts +9 -4
  20. package/dist/{util → common}/webRouterCollector.js +52 -28
  21. package/dist/config/config.default.d.ts +3 -17
  22. package/dist/context/container.js +13 -2
  23. package/dist/context/managedResolverFactory.js +2 -2
  24. package/dist/error/base.d.ts +3 -1
  25. package/dist/error/base.js +1 -0
  26. package/dist/error/code.d.ts +2 -1
  27. package/dist/error/code.js +2 -1
  28. package/dist/error/framework.d.ts +5 -2
  29. package/dist/error/framework.js +14 -5
  30. package/dist/error/http.d.ts +2 -1
  31. package/dist/error/http.js +3 -3
  32. package/dist/error/index.d.ts +1 -0
  33. package/dist/error/index.js +1 -0
  34. package/dist/index.d.ts +9 -9
  35. package/dist/index.js +13 -11
  36. package/dist/interface.d.ts +58 -30
  37. package/dist/service/configService.js +4 -5
  38. package/dist/service/decoratorService.js +26 -24
  39. package/dist/service/frameworkService.d.ts +6 -4
  40. package/dist/service/frameworkService.js +26 -11
  41. package/dist/service/lifeCycleService.js +7 -5
  42. package/dist/service/loggerService.d.ts +1 -2
  43. package/dist/service/loggerService.js +1 -10
  44. package/dist/service/middlewareService.d.ts +4 -4
  45. package/dist/service/middlewareService.js +32 -46
  46. package/dist/setup.js +10 -2
  47. package/dist/util/contextUtil.d.ts +1 -1
  48. package/dist/util/index.d.ts +46 -0
  49. package/dist/util/index.js +150 -1
  50. package/dist/util/webRouterParam.d.ts +2 -2
  51. package/dist/util/webRouterParam.js +17 -18
  52. package/package.json +7 -7
  53. package/dist/util/exceptionFilterManager.d.ts +0 -13
  54. package/dist/util/exceptionFilterManager.js +0 -53
  55. package/dist/util/middlewareManager.d.ts +0 -11
  56. package/dist/util/middlewareManager.js +0 -48
  57. package/dist/util/webGenerator.d.ts +0 -30
package/CHANGELOG.md CHANGED
@@ -3,6 +3,112 @@
3
3
  All notable changes to this project will be documented in this file.
4
4
  See [Conventional Commits](https://conventionalcommits.org) for commit guidelines.
5
5
 
6
+ # [3.0.0-beta.10](https://github.com/midwayjs/midway/compare/v3.0.0-beta.9...v3.0.0-beta.10) (2021-12-20)
7
+
8
+
9
+ ### Features
10
+
11
+ * default add session & bodyparser support for koa/express/faas ([#1420](https://github.com/midwayjs/midway/issues/1420)) ([cdaff31](https://github.com/midwayjs/midway/commit/cdaff317c3e862a95494a167995a28280af639bf))
12
+ * implement i18n for validate ([#1426](https://github.com/midwayjs/midway/issues/1426)) ([4c7ed2f](https://github.com/midwayjs/midway/commit/4c7ed2ff2e7ccf10f88f62abad230f92f5e76b97))
13
+
14
+
15
+
16
+
17
+
18
+ # [3.0.0-beta.9](https://github.com/midwayjs/midway/compare/v3.0.0-beta.8...v3.0.0-beta.9) (2021-12-09)
19
+
20
+
21
+ ### Bug Fixes
22
+
23
+ * faas missing config in framework ([#1413](https://github.com/midwayjs/midway/issues/1413)) ([7ab16a2](https://github.com/midwayjs/midway/commit/7ab16a24b29d5254a762bfffcdf18385effdf639))
24
+
25
+
26
+
27
+
28
+
29
+ # [3.0.0-beta.8](https://github.com/midwayjs/midway/compare/v3.0.0-beta.7...v3.0.0-beta.8) (2021-12-08)
30
+
31
+
32
+ ### Bug Fixes
33
+
34
+ * express routing middleware takes effect at the controller level ([#1364](https://github.com/midwayjs/midway/issues/1364)) ([b9272e0](https://github.com/midwayjs/midway/commit/b9272e0971003443304b0c53815be31a0061b4bd))
35
+
36
+
37
+
38
+
39
+
40
+ # [3.0.0-beta.7](https://github.com/midwayjs/midway/compare/v3.0.0-beta.6...v3.0.0-beta.7) (2021-12-03)
41
+
42
+
43
+ ### Bug Fixes
44
+
45
+ * add app.keys ([#1395](https://github.com/midwayjs/midway/issues/1395)) ([c44afc6](https://github.com/midwayjs/midway/commit/c44afc6cc6764a959d1fa7ae04d60099282d156a))
46
+ * middleware with ctx.body ([#1389](https://github.com/midwayjs/midway/issues/1389)) ([77af5c0](https://github.com/midwayjs/midway/commit/77af5c0b456f1843f4dcfd3dbfd2c0aa244c51bd))
47
+
48
+
49
+
50
+
51
+
52
+ # [3.0.0-beta.6](https://github.com/midwayjs/midway/compare/v3.0.0-beta.5...v3.0.0-beta.6) (2021-11-26)
53
+
54
+
55
+ ### Bug Fixes
56
+
57
+ * class transformer method missing ([#1387](https://github.com/midwayjs/midway/issues/1387)) ([074e839](https://github.com/midwayjs/midway/commit/074e8393598dc95e2742f735df75a2191c5fe25d))
58
+
59
+
60
+
61
+
62
+
63
+ # [3.0.0-beta.5](https://github.com/midwayjs/midway/compare/v3.0.0-beta.4...v3.0.0-beta.5) (2021-11-25)
64
+
65
+
66
+ ### Bug Fixes
67
+
68
+ * [@match](https://github.com/match) empty args ([#1384](https://github.com/midwayjs/midway/issues/1384)) ([6f90fc9](https://github.com/midwayjs/midway/commit/6f90fc993ff01e078288ff664833c61c02dede51))
69
+ * router sort ([#1383](https://github.com/midwayjs/midway/issues/1383)) ([f253887](https://github.com/midwayjs/midway/commit/f2538876d3eaf7dec55173d86b5b9caeeeb7be64))
70
+
71
+
72
+
73
+
74
+
75
+ # [3.0.0-beta.4](https://github.com/midwayjs/midway/compare/v3.0.0-beta.3...v3.0.0-beta.4) (2021-11-24)
76
+
77
+
78
+ ### Bug Fixes
79
+
80
+ * logger close before bootstrap close ([#1370](https://github.com/midwayjs/midway/issues/1370)) ([6cc2720](https://github.com/midwayjs/midway/commit/6cc2720ed3445e8ffccc96d124b80ed7e2517f08))
81
+
82
+
83
+ ### Features
84
+
85
+ * add i18n ([#1375](https://github.com/midwayjs/midway/issues/1375)) ([bffefe0](https://github.com/midwayjs/midway/commit/bffefe07afe45777d49b5a76b9ab17fc2b9d9a55))
86
+ * auto transform args to type ([#1372](https://github.com/midwayjs/midway/issues/1372)) ([bb3f7d2](https://github.com/midwayjs/midway/commit/bb3f7d2028a034e1926d9df554849332354c3762))
87
+ * support global prefix url ([#1371](https://github.com/midwayjs/midway/issues/1371)) ([cc5fe44](https://github.com/midwayjs/midway/commit/cc5fe44e1d221590562dc71e1f33ae96093e0da7))
88
+
89
+
90
+
91
+
92
+
93
+ # [3.0.0-beta.3](https://github.com/midwayjs/midway/compare/v3.0.0-beta.2...v3.0.0-beta.3) (2021-11-18)
94
+
95
+
96
+ ### Features
97
+
98
+ * add component and framework config definition ([#1367](https://github.com/midwayjs/midway/issues/1367)) ([b2fe615](https://github.com/midwayjs/midway/commit/b2fe6157f99659471ff1333eca0b86bb889f61a3))
99
+
100
+
101
+
102
+
103
+
104
+ # [3.0.0-beta.2](https://github.com/midwayjs/midway/compare/v3.0.0-beta.1...v3.0.0-beta.2) (2021-11-16)
105
+
106
+ **Note:** Version bump only for package @midwayjs/core
107
+
108
+
109
+
110
+
111
+
6
112
  # [3.0.0-beta.1](https://github.com/midwayjs/midway/compare/v2.12.4...v3.0.0-beta.1) (2021-11-14)
7
113
 
8
114
 
package/README.md CHANGED
@@ -5,7 +5,7 @@
5
5
 
6
6
  this is a sub package for midway.
7
7
 
8
- Document: [https://midwayjs.org/midway](https://midwayjs.org/midway)
8
+ Document: [https://midwayjs.org](https://midwayjs.org)
9
9
 
10
10
  ## License
11
11
 
@@ -1,14 +1,14 @@
1
- import { CommonMiddlewareUnion, IConfigurationOptions, IMidwayApplication, IMidwayBootstrapOptions, IMidwayContainer, IMidwayContext, IMidwayFramework, CommonExceptionFilterUnion, CommonMiddleware, MiddlewareRespond } from './interface';
1
+ import { CommonMiddlewareUnion, IConfigurationOptions, IMidwayApplication, IMidwayBootstrapOptions, IMidwayContainer, IMidwayContext, IMidwayFramework, CommonFilterUnion, CommonMiddleware, MiddlewareRespond } from './interface';
2
2
  import { FrameworkType } from '@midwayjs/decorator';
3
3
  import { ILogger, LoggerOptions } from '@midwayjs/logger';
4
4
  import { MidwayEnvironmentService } from './service/environmentService';
5
5
  import { MidwayConfigService } from './service/configService';
6
6
  import { MidwayInformationService } from './service/informationService';
7
7
  import { MidwayLoggerService } from './service/loggerService';
8
- import { ContextMiddlewareManager } from './util/middlewareManager';
8
+ import { ContextMiddlewareManager } from './common/middlewareManager';
9
9
  import { MidwayMiddlewareService } from './service/middlewareService';
10
- import { ExceptionFilterManager } from './util/exceptionFilterManager';
11
- export declare abstract class BaseFramework<APP extends IMidwayApplication<CTX>, CTX extends IMidwayContext, OPT extends IConfigurationOptions> implements IMidwayFramework<APP, OPT> {
10
+ import { FilterManager } from './common/filterManager';
11
+ export declare abstract class BaseFramework<APP extends IMidwayApplication<CTX>, CTX extends IMidwayContext, OPT extends IConfigurationOptions, ResOrNext = unknown, Next = unknown> implements IMidwayFramework<APP, CTX, OPT, ResOrNext, Next> {
12
12
  readonly applicationContext: IMidwayContainer;
13
13
  app: APP;
14
14
  configurationOptions: OPT;
@@ -17,14 +17,14 @@ export declare abstract class BaseFramework<APP extends IMidwayApplication<CTX>,
17
17
  protected defaultContext: {};
18
18
  protected BaseContextLoggerClass: any;
19
19
  protected ContextLoggerApplyLogger: string;
20
- protected middlewareManager: ContextMiddlewareManager<CTX, any, any>;
21
- protected exceptionFilterManager: ExceptionFilterManager<CTX, any, any>;
20
+ protected middlewareManager: ContextMiddlewareManager<CTX, ResOrNext, Next>;
21
+ protected filterManager: FilterManager<CTX, ResOrNext, Next>;
22
22
  protected composeMiddleware: any;
23
23
  loggerService: MidwayLoggerService;
24
24
  environmentService: MidwayEnvironmentService;
25
25
  configService: MidwayConfigService;
26
26
  informationService: MidwayInformationService;
27
- middlewareService: MidwayMiddlewareService<CTX>;
27
+ middlewareService: MidwayMiddlewareService<CTX, ResOrNext, Next>;
28
28
  constructor(applicationContext: IMidwayContainer);
29
29
  init(): Promise<this>;
30
30
  abstract configure(options?: OPT): any;
@@ -72,14 +72,17 @@ export declare abstract class BaseFramework<APP extends IMidwayApplication<CTX>,
72
72
  * @deprecated
73
73
  */
74
74
  protected afterContainerReady(options: Partial<IMidwayBootstrapOptions>): Promise<void>;
75
- getMiddleware<R, N>(lastMiddleware?: CommonMiddleware<CTX, R, N>): Promise<MiddlewareRespond<CTX, R, N>>;
75
+ applyMiddleware<R, N>(lastMiddleware?: CommonMiddleware<CTX, R, N>): Promise<MiddlewareRespond<CTX, R, N>>;
76
76
  getLogger(name?: string): ILogger;
77
77
  getCoreLogger(): ILogger;
78
78
  createLogger(name: string, option?: LoggerOptions): ILogger;
79
79
  getProjectName(): string;
80
80
  getFrameworkName(): string;
81
81
  getDefaultContextLoggerClass(): any;
82
- useMiddleware(Middleware: CommonMiddlewareUnion<CTX>): void;
83
- useFilter(Filter: CommonExceptionFilterUnion<CTX>): void;
82
+ useMiddleware(Middleware: CommonMiddlewareUnion<CTX, ResOrNext, Next>): void;
83
+ getMiddleware(): ContextMiddlewareManager<CTX, ResOrNext, Next>;
84
+ useFilter(Filter: CommonFilterUnion<CTX, ResOrNext, Next>): void;
85
+ protected createMiddlewareManager(): ContextMiddlewareManager<CTX, ResOrNext, Next>;
86
+ protected createFilterManager(): FilterManager<CTX, ResOrNext, Next>;
84
87
  }
85
88
  //# sourceMappingURL=baseFramework.d.ts.map
@@ -18,15 +18,17 @@ const environmentService_1 = require("./service/environmentService");
18
18
  const configService_1 = require("./service/configService");
19
19
  const informationService_1 = require("./service/informationService");
20
20
  const loggerService_1 = require("./service/loggerService");
21
- const middlewareManager_1 = require("./util/middlewareManager");
21
+ const middlewareManager_1 = require("./common/middlewareManager");
22
22
  const middlewareService_1 = require("./service/middlewareService");
23
- const exceptionFilterManager_1 = require("./util/exceptionFilterManager");
23
+ const filterManager_1 = require("./common/filterManager");
24
+ const util = require("util");
25
+ const debug = util.debuglog('midway:debug');
24
26
  class BaseFramework {
25
27
  constructor(applicationContext) {
26
28
  this.applicationContext = applicationContext;
27
29
  this.defaultContext = {};
28
- this.middlewareManager = new middlewareManager_1.ContextMiddlewareManager();
29
- this.exceptionFilterManager = new exceptionFilterManager_1.ExceptionFilterManager();
30
+ this.middlewareManager = this.createMiddlewareManager();
31
+ this.filterManager = this.createFilterManager();
30
32
  this.composeMiddleware = null;
31
33
  }
32
34
  async init() {
@@ -173,13 +175,13 @@ class BaseFramework {
173
175
  return this.getApplicationContext().getAttr(key);
174
176
  },
175
177
  useMiddleware: (middleware) => {
176
- this.middlewareManager.insertLast(middleware);
178
+ return this.useMiddleware(middleware);
177
179
  },
178
180
  getMiddleware: () => {
179
- return this.middlewareManager;
181
+ return this.getMiddleware();
180
182
  },
181
183
  useFilter: (Filter) => {
182
- this.exceptionFilterManager.useFilter(Filter);
184
+ return this.useFilter(Filter);
183
185
  },
184
186
  };
185
187
  for (const method of whiteList) {
@@ -204,30 +206,28 @@ class BaseFramework {
204
206
  * @deprecated
205
207
  */
206
208
  async afterContainerReady(options) { }
207
- async getMiddleware(lastMiddleware) {
209
+ async applyMiddleware(lastMiddleware) {
208
210
  if (!this.composeMiddleware) {
209
- this.middlewareManager.insertFirst(async (ctx, next) => {
211
+ this.middlewareManager.insertFirst((async (ctx, next) => {
210
212
  let returnResult = undefined;
211
213
  try {
212
214
  const result = await next();
213
- returnResult = {
214
- result,
215
- error: undefined,
216
- };
215
+ returnResult = await this.filterManager.runResultFilter(result, ctx);
217
216
  }
218
217
  catch (err) {
219
- returnResult = await this.exceptionFilterManager.run(err, ctx);
218
+ returnResult = await this.filterManager.runErrorFilter(err, ctx);
219
+ }
220
+ if (returnResult.error) {
221
+ throw returnResult.error;
220
222
  }
221
- return returnResult;
222
- });
223
- this.composeMiddleware = await this.middlewareService.compose(this.middlewareManager);
224
- await this.exceptionFilterManager.init(this.applicationContext);
223
+ return returnResult.result;
224
+ }));
225
+ debug(`[core]: Compose middleware = [${this.middlewareManager.getNames()}]`);
226
+ this.composeMiddleware = await this.middlewareService.compose(this.middlewareManager, this.app);
227
+ await this.filterManager.init(this.applicationContext);
225
228
  }
226
229
  if (lastMiddleware) {
227
- return await this.middlewareService.compose([
228
- this.composeMiddleware,
229
- lastMiddleware,
230
- ]);
230
+ return await this.middlewareService.compose([this.composeMiddleware, lastMiddleware], this.app);
231
231
  }
232
232
  else {
233
233
  return this.composeMiddleware;
@@ -255,8 +255,17 @@ class BaseFramework {
255
255
  useMiddleware(Middleware) {
256
256
  this.middlewareManager.insertLast(Middleware);
257
257
  }
258
+ getMiddleware() {
259
+ return this.middlewareManager;
260
+ }
258
261
  useFilter(Filter) {
259
- this.exceptionFilterManager.useFilter(Filter);
262
+ return this.filterManager.useFilter(Filter);
263
+ }
264
+ createMiddlewareManager() {
265
+ return new middlewareManager_1.ContextMiddlewareManager();
266
+ }
267
+ createFilterManager() {
268
+ return new filterManager_1.FilterManager();
260
269
  }
261
270
  }
262
271
  __decorate([
@@ -0,0 +1,12 @@
1
+ import { IMidwayApplication, IMidwayFramework } from '../interface';
2
+ import { FrameworkType } from '@midwayjs/decorator';
3
+ export declare class MidwayApplicationManager {
4
+ private globalFrameworkMap;
5
+ private globalFrameworkTypeMap;
6
+ addFramework(namespace: any, framework: IMidwayFramework<any, any, any>): void;
7
+ getFramework(namespaceOrFrameworkType: string | FrameworkType): IMidwayFramework<any, any, any, unknown, unknown>;
8
+ getApplication(namespaceOrFrameworkType: string | FrameworkType): IMidwayApplication;
9
+ getApplications(namespaces: Array<string | FrameworkType>): IMidwayApplication[];
10
+ getWebLikeApplication(): IMidwayApplication[];
11
+ }
12
+ //# sourceMappingURL=applicationManager.d.ts.map
@@ -0,0 +1,66 @@
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
+ Object.defineProperty(exports, "__esModule", { value: true });
9
+ exports.MidwayApplicationManager = void 0;
10
+ const decorator_1 = require("@midwayjs/decorator");
11
+ let MidwayApplicationManager = class MidwayApplicationManager {
12
+ constructor() {
13
+ this.globalFrameworkMap = new Map();
14
+ this.globalFrameworkTypeMap = new WeakMap();
15
+ }
16
+ addFramework(namespace, framework) {
17
+ this.globalFrameworkMap.set(namespace, framework);
18
+ this.globalFrameworkTypeMap.set(framework.getFrameworkType(), framework);
19
+ }
20
+ getFramework(namespaceOrFrameworkType) {
21
+ if (typeof namespaceOrFrameworkType === 'string') {
22
+ if (this.globalFrameworkMap.has(namespaceOrFrameworkType)) {
23
+ return this.globalFrameworkMap.get(namespaceOrFrameworkType);
24
+ }
25
+ }
26
+ else {
27
+ if (this.globalFrameworkTypeMap.has(namespaceOrFrameworkType)) {
28
+ return this.globalFrameworkTypeMap.get(namespaceOrFrameworkType);
29
+ }
30
+ }
31
+ }
32
+ getApplication(namespaceOrFrameworkType) {
33
+ if (typeof namespaceOrFrameworkType === 'string') {
34
+ if (this.globalFrameworkMap.has(namespaceOrFrameworkType)) {
35
+ return this.globalFrameworkMap
36
+ .get(namespaceOrFrameworkType)
37
+ .getApplication();
38
+ }
39
+ }
40
+ else {
41
+ if (this.globalFrameworkTypeMap.has(namespaceOrFrameworkType)) {
42
+ return this.globalFrameworkTypeMap
43
+ .get(namespaceOrFrameworkType)
44
+ .getApplication();
45
+ }
46
+ }
47
+ }
48
+ getApplications(namespaces) {
49
+ return namespaces
50
+ .map(namespace => {
51
+ return this.getApplication(namespace);
52
+ })
53
+ .filter(app => {
54
+ return !!app;
55
+ });
56
+ }
57
+ getWebLikeApplication() {
58
+ return this.getApplications(['express', 'koa', 'egg', 'faas']);
59
+ }
60
+ };
61
+ MidwayApplicationManager = __decorate([
62
+ (0, decorator_1.Provide)(),
63
+ (0, decorator_1.Scope)(decorator_1.ScopeEnum.Singleton)
64
+ ], MidwayApplicationManager);
65
+ exports.MidwayApplicationManager = MidwayApplicationManager;
66
+ //# sourceMappingURL=applicationManager.js.map
File without changes
File without changes
@@ -0,0 +1,19 @@
1
+ import { CommonFilterUnion, IMidwayContainer, IMidwayContext } from '../interface';
2
+ export declare class FilterManager<CTX extends IMidwayContext = IMidwayContext, R = any, N = any> {
3
+ private errFilterList;
4
+ private successFilterList;
5
+ private exceptionMap;
6
+ private defaultErrFilter;
7
+ private matchFnList;
8
+ useFilter(Filters: CommonFilterUnion<CTX, R, N>): void;
9
+ init(applicationContext: IMidwayContainer): Promise<void>;
10
+ runErrorFilter(err: Error, ctx: CTX, res?: R, next?: N): Promise<{
11
+ result: any;
12
+ error: any;
13
+ }>;
14
+ runResultFilter(result: any, ctx: CTX, res?: R, next?: N): Promise<{
15
+ result: any;
16
+ error: any;
17
+ }>;
18
+ }
19
+ //# sourceMappingURL=filterManager.d.ts.map
@@ -0,0 +1,85 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.FilterManager = void 0;
4
+ const decorator_1 = require("@midwayjs/decorator");
5
+ const util_1 = require("../util");
6
+ class FilterManager {
7
+ constructor() {
8
+ this.errFilterList = [];
9
+ this.successFilterList = [];
10
+ this.exceptionMap = new WeakMap();
11
+ this.defaultErrFilter = undefined;
12
+ this.matchFnList = [];
13
+ }
14
+ useFilter(Filters) {
15
+ if (!Array.isArray(Filters)) {
16
+ Filters = [Filters];
17
+ }
18
+ for (const Filter of Filters) {
19
+ if ((0, decorator_1.getClassMetadata)(decorator_1.CATCH_KEY, Filter)) {
20
+ this.errFilterList.push(Filter);
21
+ }
22
+ if ((0, decorator_1.getClassMetadata)(decorator_1.MATCH_KEY, Filter)) {
23
+ this.successFilterList.push(Filter);
24
+ }
25
+ }
26
+ }
27
+ async init(applicationContext) {
28
+ // for catch exception
29
+ for (const FilterClass of this.errFilterList) {
30
+ const filter = await applicationContext.getAsync(FilterClass);
31
+ const exceptionMetadata = (0, decorator_1.getClassMetadata)(decorator_1.CATCH_KEY, FilterClass);
32
+ if (exceptionMetadata && exceptionMetadata.catchTargets) {
33
+ for (const Exception of exceptionMetadata.catchTargets) {
34
+ this.exceptionMap.set(Exception, filter);
35
+ }
36
+ }
37
+ else {
38
+ // default filter
39
+ this.defaultErrFilter = filter;
40
+ }
41
+ }
42
+ // for success return
43
+ for (const FilterClass of this.successFilterList) {
44
+ const filter = await applicationContext.getAsync(FilterClass);
45
+ const matchMetadata = (0, decorator_1.getClassMetadata)(decorator_1.MATCH_KEY, FilterClass);
46
+ if (matchMetadata && matchMetadata.matchPattern) {
47
+ this.matchFnList.push({
48
+ matchFn: (0, util_1.toPathMatch)(matchMetadata.matchPattern),
49
+ target: filter,
50
+ });
51
+ }
52
+ }
53
+ }
54
+ async runErrorFilter(err, ctx, res, next) {
55
+ let result, error;
56
+ if (this.exceptionMap.has(err.constructor)) {
57
+ const filter = this.exceptionMap.get(err.constructor);
58
+ result = await filter.catch(err, ctx, res, next);
59
+ }
60
+ else if (this.defaultErrFilter) {
61
+ result = await this.defaultErrFilter.catch(err, ctx, res, next);
62
+ }
63
+ else {
64
+ error = err;
65
+ }
66
+ return {
67
+ result,
68
+ error,
69
+ };
70
+ }
71
+ async runResultFilter(result, ctx, res, next) {
72
+ let returnValue = result;
73
+ for (const matchData of this.matchFnList) {
74
+ if (matchData.matchFn(ctx, res)) {
75
+ returnValue = await matchData.target.match(returnValue, ctx, res, next);
76
+ }
77
+ }
78
+ return {
79
+ result: returnValue,
80
+ error: undefined,
81
+ };
82
+ }
83
+ }
84
+ exports.FilterManager = FilterManager;
85
+ //# sourceMappingURL=filterManager.js.map
@@ -0,0 +1,68 @@
1
+ import { CommonMiddleware, CommonMiddlewareUnion, IMidwayContext } from '../interface';
2
+ export declare class ContextMiddlewareManager<CTX extends IMidwayContext, R, N> extends Array<CommonMiddleware<CTX, R, N>> {
3
+ /**
4
+ * insert a middleware or middleware array to first
5
+ * @param middleware
6
+ */
7
+ insertFirst(middleware: CommonMiddlewareUnion<CTX, R, N>): void;
8
+ /**
9
+ * insert a middleware or middleware array to last
10
+ * @param middleware
11
+ */
12
+ insertLast(middleware: CommonMiddlewareUnion<CTX, R, N>): void;
13
+ /**
14
+ * insert a middleware or middleware array to after another middleware
15
+ * @param middleware
16
+ * @param idxOrBeforeMiddleware
17
+ */
18
+ insertBefore(middleware: CommonMiddlewareUnion<CTX, R, N>, idxOrBeforeMiddleware: CommonMiddleware<CTX, R, N> | string | number): void;
19
+ /**
20
+ * insert a middleware or middleware array to after another middleware
21
+ * @param middleware
22
+ * @param idxOrAfterMiddleware
23
+ */
24
+ insertAfter(middleware: CommonMiddlewareUnion<CTX, R, N>, idxOrAfterMiddleware: CommonMiddleware<CTX, R, N> | string | number): void;
25
+ /**
26
+ * move a middleware after another middleware
27
+ * @param middlewareOrName
28
+ * @param afterMiddleware
29
+ */
30
+ findAndInsertAfter(middlewareOrName: CommonMiddleware<CTX, R, N> | string, afterMiddleware: CommonMiddleware<CTX, R, N> | string | number): void;
31
+ /**
32
+ * move a middleware before another middleware
33
+ * @param middlewareOrName
34
+ * @param beforeMiddleware
35
+ */
36
+ findAndInsertBefore(middlewareOrName: CommonMiddleware<CTX, R, N> | string, beforeMiddleware: CommonMiddleware<CTX, R, N> | string | number): void;
37
+ /**
38
+ * find middleware and move to first
39
+ * @param middlewareOrName
40
+ */
41
+ findAndInsertFirst(middlewareOrName: CommonMiddleware<CTX, R, N> | string): void;
42
+ /**
43
+ * find middleware and move to last
44
+ * @param middlewareOrName
45
+ */
46
+ findAndInsertLast(middlewareOrName: CommonMiddleware<CTX, R, N> | string): void;
47
+ /**
48
+ * find a middleware and return index
49
+ * @param middlewareOrName
50
+ */
51
+ findItemIndex(middlewareOrName: CommonMiddleware<CTX, R, N> | string | number): number;
52
+ findItem(middlewareOrName: CommonMiddleware<CTX, R, N> | string | number): CommonMiddleware<CTX, R, N>;
53
+ /**
54
+ * get name from middleware
55
+ * @param middleware
56
+ */
57
+ getMiddlewareName(middleware: CommonMiddleware<CTX, R, N>): string;
58
+ /**
59
+ * remove a middleware
60
+ * @param middlewareOrNameOrIdx
61
+ */
62
+ remove(middlewareOrNameOrIdx: CommonMiddleware<CTX, R, N> | string | number): CommonMiddleware<CTX, R, N>;
63
+ /**
64
+ * get middleware name list
65
+ */
66
+ getNames(): string[];
67
+ }
68
+ //# sourceMappingURL=middlewareManager.d.ts.map