@midwayjs/core 3.0.0-beta.5 → 3.0.0-beta.9

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/CHANGELOG.md CHANGED
@@ -3,6 +3,51 @@
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.9](https://github.com/midwayjs/midway/compare/v3.0.0-beta.8...v3.0.0-beta.9) (2021-12-09)
7
+
8
+
9
+ ### Bug Fixes
10
+
11
+ * faas missing config in framework ([#1413](https://github.com/midwayjs/midway/issues/1413)) ([7ab16a2](https://github.com/midwayjs/midway/commit/7ab16a24b29d5254a762bfffcdf18385effdf639))
12
+
13
+
14
+
15
+
16
+
17
+ # [3.0.0-beta.8](https://github.com/midwayjs/midway/compare/v3.0.0-beta.7...v3.0.0-beta.8) (2021-12-08)
18
+
19
+
20
+ ### Bug Fixes
21
+
22
+ * 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))
23
+
24
+
25
+
26
+
27
+
28
+ # [3.0.0-beta.7](https://github.com/midwayjs/midway/compare/v3.0.0-beta.6...v3.0.0-beta.7) (2021-12-03)
29
+
30
+
31
+ ### Bug Fixes
32
+
33
+ * add app.keys ([#1395](https://github.com/midwayjs/midway/issues/1395)) ([c44afc6](https://github.com/midwayjs/midway/commit/c44afc6cc6764a959d1fa7ae04d60099282d156a))
34
+ * middleware with ctx.body ([#1389](https://github.com/midwayjs/midway/issues/1389)) ([77af5c0](https://github.com/midwayjs/midway/commit/77af5c0b456f1843f4dcfd3dbfd2c0aa244c51bd))
35
+
36
+
37
+
38
+
39
+
40
+ # [3.0.0-beta.6](https://github.com/midwayjs/midway/compare/v3.0.0-beta.5...v3.0.0-beta.6) (2021-11-26)
41
+
42
+
43
+ ### Bug Fixes
44
+
45
+ * class transformer method missing ([#1387](https://github.com/midwayjs/midway/issues/1387)) ([074e839](https://github.com/midwayjs/midway/commit/074e8393598dc95e2742f735df75a2191c5fe25d))
46
+
47
+
48
+
49
+
50
+
6
51
  # [3.0.0-beta.5](https://github.com/midwayjs/midway/compare/v3.0.0-beta.4...v3.0.0-beta.5) (2021-11-25)
7
52
 
8
53
 
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
 
@@ -215,7 +215,10 @@ class BaseFramework {
215
215
  catch (err) {
216
216
  returnResult = await this.filterManager.runErrorFilter(err, ctx);
217
217
  }
218
- return returnResult;
218
+ if (returnResult.error) {
219
+ throw returnResult.error;
220
+ }
221
+ return returnResult.result;
219
222
  }));
220
223
  this.composeMiddleware = await this.middlewareService.compose(this.middlewareManager);
221
224
  await this.filterManager.init(this.applicationContext);
@@ -1,11 +1,68 @@
1
1
  import { CommonMiddleware, CommonMiddlewareUnion, IMidwayContext } from '../interface';
2
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
+ */
3
7
  insertFirst(middleware: CommonMiddlewareUnion<CTX, R, N>): void;
4
- insertBefore(middleware: CommonMiddlewareUnion<CTX, R, N>, idxOrBeforeMiddleware: number): any;
5
- insertBefore(middleware: CommonMiddlewareUnion<CTX, R, N>, idxOrBeforeMiddleware: CommonMiddlewareUnion<CTX, R, N>): any;
6
- insertAfter(middleware: CommonMiddlewareUnion<CTX, R, N>, idxOrAfterMiddleware: number): any;
7
- insertAfter(middleware: CommonMiddlewareUnion<CTX, R, N>, idxOrAfterMiddleware: CommonMiddlewareUnion<CTX, R, N>): any;
8
+ /**
9
+ * insert a middleware or middleware array to last
10
+ * @param middleware
11
+ */
8
12
  insertLast(middleware: CommonMiddlewareUnion<CTX, R, N>): void;
9
- private findItemIndex;
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[];
10
67
  }
11
68
  //# sourceMappingURL=middlewareManager.d.ts.map
@@ -2,6 +2,10 @@
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.ContextMiddlewareManager = void 0;
4
4
  class ContextMiddlewareManager extends Array {
5
+ /**
6
+ * insert a middleware or middleware array to first
7
+ * @param middleware
8
+ */
5
9
  insertFirst(middleware) {
6
10
  if (Array.isArray(middleware)) {
7
11
  this.unshift(...middleware);
@@ -10,6 +14,23 @@ class ContextMiddlewareManager extends Array {
10
14
  this.unshift(middleware);
11
15
  }
12
16
  }
17
+ /**
18
+ * insert a middleware or middleware array to last
19
+ * @param middleware
20
+ */
21
+ insertLast(middleware) {
22
+ if (Array.isArray(middleware)) {
23
+ this.push(...middleware);
24
+ }
25
+ else {
26
+ this.push(middleware);
27
+ }
28
+ }
29
+ /**
30
+ * insert a middleware or middleware array to after another middleware
31
+ * @param middleware
32
+ * @param idxOrBeforeMiddleware
33
+ */
13
34
  insertBefore(middleware, idxOrBeforeMiddleware) {
14
35
  if (typeof idxOrBeforeMiddleware !== 'number') {
15
36
  idxOrBeforeMiddleware = this.findItemIndex(idxOrBeforeMiddleware);
@@ -21,6 +42,11 @@ class ContextMiddlewareManager extends Array {
21
42
  this.splice(idxOrBeforeMiddleware, 0, middleware);
22
43
  }
23
44
  }
45
+ /**
46
+ * insert a middleware or middleware array to after another middleware
47
+ * @param middleware
48
+ * @param idxOrAfterMiddleware
49
+ */
24
50
  insertAfter(middleware, idxOrAfterMiddleware) {
25
51
  if (typeof idxOrAfterMiddleware !== 'number') {
26
52
  idxOrAfterMiddleware = this.findItemIndex(idxOrAfterMiddleware);
@@ -32,16 +58,125 @@ class ContextMiddlewareManager extends Array {
32
58
  this.splice(idxOrAfterMiddleware + 1, 0, middleware);
33
59
  }
34
60
  }
35
- insertLast(middleware) {
36
- if (Array.isArray(middleware)) {
37
- this.push(...middleware);
61
+ /**
62
+ * move a middleware after another middleware
63
+ * @param middlewareOrName
64
+ * @param afterMiddleware
65
+ */
66
+ findAndInsertAfter(middlewareOrName, afterMiddleware) {
67
+ middlewareOrName = this.findItem(middlewareOrName);
68
+ afterMiddleware = this.findItem(afterMiddleware);
69
+ if (!middlewareOrName ||
70
+ !afterMiddleware ||
71
+ middlewareOrName === afterMiddleware) {
72
+ return;
73
+ }
74
+ if (afterMiddleware) {
75
+ const mw = this.remove(middlewareOrName);
76
+ if (mw) {
77
+ this.insertAfter(mw, afterMiddleware);
78
+ }
79
+ }
80
+ }
81
+ /**
82
+ * move a middleware before another middleware
83
+ * @param middlewareOrName
84
+ * @param beforeMiddleware
85
+ */
86
+ findAndInsertBefore(middlewareOrName, beforeMiddleware) {
87
+ middlewareOrName = this.findItem(middlewareOrName);
88
+ beforeMiddleware = this.findItem(beforeMiddleware);
89
+ if (!middlewareOrName ||
90
+ !beforeMiddleware ||
91
+ middlewareOrName === beforeMiddleware) {
92
+ return;
93
+ }
94
+ if (beforeMiddleware) {
95
+ const mw = this.remove(middlewareOrName);
96
+ if (mw) {
97
+ this.insertBefore(mw, beforeMiddleware);
98
+ }
99
+ }
100
+ }
101
+ /**
102
+ * find middleware and move to first
103
+ * @param middlewareOrName
104
+ */
105
+ findAndInsertFirst(middlewareOrName) {
106
+ const mw = this.remove(middlewareOrName);
107
+ if (mw) {
108
+ this.insertFirst(mw);
109
+ }
110
+ }
111
+ /**
112
+ * find middleware and move to last
113
+ * @param middlewareOrName
114
+ */
115
+ findAndInsertLast(middlewareOrName) {
116
+ const mw = this.remove(middlewareOrName);
117
+ if (mw) {
118
+ this.insertLast(mw);
119
+ }
120
+ }
121
+ /**
122
+ * find a middleware and return index
123
+ * @param middlewareOrName
124
+ */
125
+ findItemIndex(middlewareOrName) {
126
+ if (typeof middlewareOrName === 'number') {
127
+ return middlewareOrName;
128
+ }
129
+ else if (typeof middlewareOrName === 'string') {
130
+ return this.findIndex(item => this.getMiddlewareName(item) === middlewareOrName);
38
131
  }
39
132
  else {
40
- this.push(middleware);
133
+ return this.findIndex(item => item === middlewareOrName);
134
+ }
135
+ }
136
+ findItem(middlewareOrName) {
137
+ if (typeof middlewareOrName === 'number') {
138
+ if (middlewareOrName >= 0 && middlewareOrName <= this.length - 1) {
139
+ return this[middlewareOrName];
140
+ }
141
+ }
142
+ else if (typeof middlewareOrName === 'string') {
143
+ return this.find(item => this.getMiddlewareName(item) === middlewareOrName);
144
+ }
145
+ else {
146
+ return middlewareOrName;
147
+ }
148
+ }
149
+ /**
150
+ * get name from middleware
151
+ * @param middleware
152
+ */
153
+ getMiddlewareName(middleware) {
154
+ var _a;
155
+ return (_a = middleware._name) !== null && _a !== void 0 ? _a : middleware.name;
156
+ }
157
+ /**
158
+ * remove a middleware
159
+ * @param middlewareOrNameOrIdx
160
+ */
161
+ remove(middlewareOrNameOrIdx) {
162
+ if (typeof middlewareOrNameOrIdx === 'number' &&
163
+ middlewareOrNameOrIdx !== -1) {
164
+ return this.splice(middlewareOrNameOrIdx, 1)[0];
165
+ }
166
+ else {
167
+ const idx = this.findItemIndex(middlewareOrNameOrIdx);
168
+ if (idx !== -1) {
169
+ return this.splice(idx, 1)[0];
170
+ }
41
171
  }
42
172
  }
43
- findItemIndex(middleware) {
44
- return this.findIndex(item => item === middleware);
173
+ /**
174
+ * get middleware name list
175
+ */
176
+ getNames() {
177
+ return this.map(item => {
178
+ return this.getMiddlewareName(item);
179
+ });
45
180
  }
46
181
  }
47
182
  exports.ContextMiddlewareManager = ContextMiddlewareManager;
@@ -9,6 +9,8 @@ exports.WebControllerGenerator = void 0;
9
9
  */
10
10
  const decorator_1 = require("@midwayjs/decorator");
11
11
  const index_1 = require("../index");
12
+ const util = require("util");
13
+ const debug = util.debuglog('midway:debug');
12
14
  class WebControllerGenerator {
13
15
  constructor(applicationContext, frameworkType, logger) {
14
16
  this.applicationContext = applicationContext;
@@ -67,6 +69,7 @@ class WebControllerGenerator {
67
69
  // bind controller first
68
70
  this.applicationContext.bindClass(routerInfo.routerModule);
69
71
  (_a = this.logger) === null || _a === void 0 ? void 0 : _a.debug(`Load Controller "${routerInfo.controllerId}", prefix=${routerInfo.prefix}`);
72
+ debug(`[core:webGenerator]: Load Controller "${routerInfo.controllerId}", prefix=${routerInfo.prefix}`);
70
73
  // new router
71
74
  const newRouter = this.createRouter({
72
75
  prefix: routerInfo.prefix,
@@ -97,6 +100,7 @@ class WebControllerGenerator {
97
100
  this.generateController(routeInfo),
98
101
  ];
99
102
  (_b = this.logger) === null || _b === void 0 ? void 0 : _b.debug(`Load Router "${routeInfo.requestMethod.toUpperCase()} ${routeInfo.url}"`);
103
+ debug(`[core:webGenerator]: Load Router "${routeInfo.requestMethod.toUpperCase()} ${routeInfo.url}"`);
100
104
  // apply controller from request context
101
105
  // eslint-disable-next-line prefer-spread
102
106
  newRouter[routeInfo.requestMethod].apply(newRouter, routerArgs);
@@ -6,6 +6,7 @@ const util_1 = require("../util");
6
6
  const container_1 = require("../context/container");
7
7
  const fileDetector_1 = require("./fileDetector");
8
8
  const util = require("util");
9
+ const error_1 = require("../error");
9
10
  const debug = util.debuglog('midway:debug');
10
11
  class WebRouterCollector {
11
12
  constructor(baseDir = '', options = {}) {
@@ -70,6 +71,9 @@ class WebRouterCollector {
70
71
  if (controllerIgnoreGlobalPrefix) {
71
72
  prefix = ignorePrefix;
72
73
  }
74
+ if (/\*/.test(prefix)) {
75
+ throw new error_1.MidwayCommonError(`Router prefix ${prefix} can't set string with *`);
76
+ }
73
77
  // set prefix
74
78
  if (!this.routes.has(prefix)) {
75
79
  this.routes.set(prefix, []);
@@ -64,8 +64,13 @@ class ContainerConfiguration {
64
64
  });
65
65
  }
66
66
  addImportConfigs(importConfigs) {
67
- if (importConfigs && importConfigs.length) {
68
- this.container.get(configService_1.MidwayConfigService).add(importConfigs);
67
+ if (importConfigs) {
68
+ if (Array.isArray(importConfigs)) {
69
+ this.container.get(configService_1.MidwayConfigService).add(importConfigs);
70
+ }
71
+ else {
72
+ this.container.get(configService_1.MidwayConfigService).addObject(importConfigs);
73
+ }
69
74
  }
70
75
  }
71
76
  addImports(imports = []) {
@@ -54,6 +54,7 @@ export declare enum FrameworkErrorEnum {
54
54
  PARAM_TYPE = 10002,
55
55
  DEFINITION_NOT_FOUND = 10003,
56
56
  FEATURE_NO_LONGER_SUPPORTED = 10004,
57
- VALIDATE_FAIL = 10005
57
+ VALIDATE_FAIL = 10005,
58
+ MISSING_CONFIG = 10006
58
59
  }
59
60
  //# sourceMappingURL=code.d.ts.map
@@ -60,5 +60,6 @@ var FrameworkErrorEnum;
60
60
  FrameworkErrorEnum[FrameworkErrorEnum["DEFINITION_NOT_FOUND"] = 10003] = "DEFINITION_NOT_FOUND";
61
61
  FrameworkErrorEnum[FrameworkErrorEnum["FEATURE_NO_LONGER_SUPPORTED"] = 10004] = "FEATURE_NO_LONGER_SUPPORTED";
62
62
  FrameworkErrorEnum[FrameworkErrorEnum["VALIDATE_FAIL"] = 10005] = "VALIDATE_FAIL";
63
+ FrameworkErrorEnum[FrameworkErrorEnum["MISSING_CONFIG"] = 10006] = "MISSING_CONFIG";
63
64
  })(FrameworkErrorEnum = exports.FrameworkErrorEnum || (exports.FrameworkErrorEnum = {}));
64
65
  //# sourceMappingURL=code.js.map
@@ -18,4 +18,7 @@ export declare class MidwayFeatureNoLongerSupportedError extends MidwayError {
18
18
  export declare class MidwayValidationError extends MidwayError {
19
19
  constructor(message: any, status: any, cause: any);
20
20
  }
21
+ export declare class MidwayConfigMissingError extends MidwayError {
22
+ constructor(configKey: string);
23
+ }
21
24
  //# sourceMappingURL=framework.d.ts.map
@@ -1,6 +1,6 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.MidwayValidationError = exports.MidwayFeatureNoLongerSupportedError = exports.MidwayDefinitionNotFoundError = exports.MidwayParameterError = exports.MidwayCommonError = void 0;
3
+ exports.MidwayConfigMissingError = exports.MidwayValidationError = exports.MidwayFeatureNoLongerSupportedError = exports.MidwayDefinitionNotFoundError = exports.MidwayParameterError = exports.MidwayCommonError = void 0;
4
4
  const base_1 = require("./base");
5
5
  const code_1 = require("./code");
6
6
  class MidwayCommonError extends base_1.MidwayError {
@@ -49,4 +49,10 @@ class MidwayValidationError extends base_1.MidwayError {
49
49
  }
50
50
  }
51
51
  exports.MidwayValidationError = MidwayValidationError;
52
+ class MidwayConfigMissingError extends base_1.MidwayError {
53
+ constructor(configKey) {
54
+ super(`Can't found config key "${configKey}" in your config, please set it first`, code_1.FrameworkErrorEnum.MISSING_CONFIG);
55
+ }
56
+ }
57
+ exports.MidwayConfigMissingError = MidwayConfigMissingError;
52
58
  //# sourceMappingURL=framework.js.map
package/dist/index.d.ts CHANGED
@@ -4,12 +4,11 @@ export { MidwayRequestContainer } from './context/requestContainer';
4
4
  export { BaseFramework } from './baseFramework';
5
5
  export * from './context/providerWrapper';
6
6
  export * from './common/constants';
7
- export { safelyGet, safeRequire, delegateTargetPrototypeMethod, delegateTargetMethod, delegateTargetProperties, deprecatedOutput, transformRequestObjectByType, } from './util/';
7
+ export { safelyGet, safeRequire, delegateTargetPrototypeMethod, delegateTargetMethod, delegateTargetProperties, deprecatedOutput, transformRequestObjectByType, pathMatching, wrapMiddleware, } from './util/';
8
8
  export * from './util/pathFileUtil';
9
9
  export * from './util/webRouterParam';
10
10
  export * from './common/webRouterCollector';
11
11
  export * from './common/triggerCollector';
12
- export { plainToClass, classToPlain } from 'class-transformer';
13
12
  export { createConfiguration } from './functional/configuration';
14
13
  export { MidwayConfigService } from './service/configService';
15
14
  export { MidwayEnvironmentService } from './service/environmentService';
package/dist/index.js CHANGED
@@ -10,7 +10,7 @@ var __exportStar = (this && this.__exportStar) || function(m, exports) {
10
10
  for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
11
11
  };
12
12
  Object.defineProperty(exports, "__esModule", { value: true });
13
- exports.MidwayFrameworkType = exports.MidwayDecoratorService = exports.MidwayMiddlewareService = exports.MidwayLifeCycleService = exports.MidwayAspectService = exports.MidwayFrameworkService = exports.MidwayLoggerService = exports.MidwayInformationService = exports.MidwayEnvironmentService = exports.MidwayConfigService = exports.createConfiguration = exports.classToPlain = exports.plainToClass = exports.transformRequestObjectByType = exports.deprecatedOutput = exports.delegateTargetProperties = exports.delegateTargetMethod = exports.delegateTargetPrototypeMethod = exports.safeRequire = exports.safelyGet = exports.BaseFramework = exports.MidwayRequestContainer = void 0;
13
+ exports.MidwayFrameworkType = exports.MidwayDecoratorService = exports.MidwayMiddlewareService = exports.MidwayLifeCycleService = exports.MidwayAspectService = exports.MidwayFrameworkService = exports.MidwayLoggerService = exports.MidwayInformationService = exports.MidwayEnvironmentService = exports.MidwayConfigService = exports.createConfiguration = exports.wrapMiddleware = exports.pathMatching = exports.transformRequestObjectByType = exports.deprecatedOutput = exports.delegateTargetProperties = exports.delegateTargetMethod = exports.delegateTargetPrototypeMethod = exports.safeRequire = exports.safelyGet = exports.BaseFramework = exports.MidwayRequestContainer = void 0;
14
14
  __exportStar(require("./interface"), exports);
15
15
  __exportStar(require("./context/container"), exports);
16
16
  var requestContainer_1 = require("./context/requestContainer");
@@ -27,13 +27,12 @@ Object.defineProperty(exports, "delegateTargetMethod", { enumerable: true, get:
27
27
  Object.defineProperty(exports, "delegateTargetProperties", { enumerable: true, get: function () { return util_1.delegateTargetProperties; } });
28
28
  Object.defineProperty(exports, "deprecatedOutput", { enumerable: true, get: function () { return util_1.deprecatedOutput; } });
29
29
  Object.defineProperty(exports, "transformRequestObjectByType", { enumerable: true, get: function () { return util_1.transformRequestObjectByType; } });
30
+ Object.defineProperty(exports, "pathMatching", { enumerable: true, get: function () { return util_1.pathMatching; } });
31
+ Object.defineProperty(exports, "wrapMiddleware", { enumerable: true, get: function () { return util_1.wrapMiddleware; } });
30
32
  __exportStar(require("./util/pathFileUtil"), exports);
31
33
  __exportStar(require("./util/webRouterParam"), exports);
32
34
  __exportStar(require("./common/webRouterCollector"), exports);
33
35
  __exportStar(require("./common/triggerCollector"), exports);
34
- var class_transformer_1 = require("class-transformer");
35
- Object.defineProperty(exports, "plainToClass", { enumerable: true, get: function () { return class_transformer_1.plainToClass; } });
36
- Object.defineProperty(exports, "classToPlain", { enumerable: true, get: function () { return class_transformer_1.classToPlain; } });
37
36
  var configuration_1 = require("./functional/configuration");
38
37
  Object.defineProperty(exports, "createConfiguration", { enumerable: true, get: function () { return configuration_1.createConfiguration; } });
39
38
  var configService_1 = require("./service/configService");
@@ -400,9 +400,9 @@ export interface IMidwayBootstrapOptions {
400
400
  moduleDetector?: 'file' | IFileDetector | false;
401
401
  logger?: boolean | ILogger;
402
402
  ignore?: string[];
403
- globalConfig?: {
403
+ globalConfig?: Array<{
404
404
  [environmentName: string]: Record<string, any>;
405
- };
405
+ }> | Record<string, any>;
406
406
  }
407
407
  export interface IConfigurationOptions {
408
408
  logger?: ILogger;
@@ -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) {
@@ -58,26 +78,6 @@ let MidwayDecoratorService = class MidwayDecoratorService {
58
78
  });
59
79
  }
60
80
  }
61
- // find custom method decorator metadata, include method decorator information array
62
- const methodDecoratorMetadataList = (0, decorator_1.getClassMetadata)(decorator_1.INJECT_CUSTOM_METHOD, Clzz);
63
- if (methodDecoratorMetadataList) {
64
- // loop it, save this order for decorator run
65
- for (const meta of methodDecoratorMetadataList) {
66
- const { propertyName, key, metadata } = meta;
67
- // add aspect implementation first
68
- this.aspectService.interceptPrototypeMethod(Clzz, propertyName, () => {
69
- const methodDecoratorHandler = this.methodDecoratorMap.get(key);
70
- if (!methodDecoratorHandler) {
71
- throw new error_1.MidwayCommonError(`Method Decorator "${key}" handler not found, please register first.`);
72
- }
73
- return this.methodDecoratorMap.get(key)({
74
- target: Clzz,
75
- propertyName,
76
- metadata,
77
- });
78
- });
79
- }
80
- }
81
81
  });
82
82
  // add custom property decorator listener
83
83
  this.applicationContext.onObjectCreated((instance, options) => {
@@ -6,6 +6,6 @@ export declare class MidwayMiddlewareService<T, R, N = unknown> {
6
6
  (context: any, next?: any): Promise<any>;
7
7
  _name: string;
8
8
  }>;
9
+ getMiddlewareName(mw: any): any;
9
10
  }
10
- export declare function pathMatching(options: any): (ctx?: any) => any;
11
11
  //# sourceMappingURL=middlewareService.d.ts.map
@@ -9,7 +9,7 @@ var __metadata = (this && this.__metadata) || function (k, v) {
9
9
  if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
10
10
  };
11
11
  Object.defineProperty(exports, "__esModule", { value: true });
12
- exports.pathMatching = exports.MidwayMiddlewareService = void 0;
12
+ exports.MidwayMiddlewareService = void 0;
13
13
  const decorator_1 = require("@midwayjs/decorator");
14
14
  const error_1 = require("../error");
15
15
  const util_1 = require("../util");
@@ -32,14 +32,16 @@ let MidwayMiddlewareService = class MidwayMiddlewareService {
32
32
  if (classMiddleware) {
33
33
  fn = classMiddleware.resolve();
34
34
  if (!classMiddleware.match && !classMiddleware.ignore) {
35
- fn._name = classMiddleware.constructor.name;
35
+ if (!fn.name) {
36
+ fn._name = classMiddleware.constructor.name;
37
+ }
36
38
  // just got fn
37
39
  newMiddlewareArr.push(fn);
38
40
  }
39
41
  else {
40
42
  // wrap ignore and match
41
43
  const mw = fn;
42
- const match = pathMatching({
44
+ const match = (0, util_1.pathMatching)({
43
45
  match: classMiddleware.match,
44
46
  ignore: classMiddleware.ignore,
45
47
  });
@@ -67,6 +69,7 @@ let MidwayMiddlewareService = class MidwayMiddlewareService {
67
69
  * @api public
68
70
  */
69
71
  const composeFn = (context, next) => {
72
+ const supportBody = (0, util_1.isIncludeProperty)(context, 'body');
70
73
  // last called middleware #
71
74
  let index = -1;
72
75
  return dispatch(0);
@@ -80,9 +83,25 @@ let MidwayMiddlewareService = class MidwayMiddlewareService {
80
83
  if (!fn)
81
84
  return Promise.resolve();
82
85
  try {
83
- return Promise.resolve(fn(context, dispatch.bind(null, i + 1), {
84
- index,
85
- }));
86
+ if (supportBody) {
87
+ return Promise.resolve(fn(context, dispatch.bind(null, i + 1), {
88
+ index,
89
+ })).then(result => {
90
+ // need to set body
91
+ if (context.body && !result) {
92
+ result = context.body;
93
+ }
94
+ else if (result && context.body !== result) {
95
+ context.body = result;
96
+ }
97
+ return result;
98
+ });
99
+ }
100
+ else {
101
+ return Promise.resolve(fn(context, dispatch.bind(null, i + 1), {
102
+ index,
103
+ }));
104
+ }
86
105
  }
87
106
  catch (err) {
88
107
  return Promise.reject(err);
@@ -94,6 +113,10 @@ let MidwayMiddlewareService = class MidwayMiddlewareService {
94
113
  }
95
114
  return composeFn;
96
115
  }
116
+ getMiddlewareName(mw) {
117
+ var _a;
118
+ return (_a = mw.name) !== null && _a !== void 0 ? _a : mw._name;
119
+ }
97
120
  };
98
121
  MidwayMiddlewareService = __decorate([
99
122
  (0, decorator_1.Provide)(),
@@ -101,19 +124,4 @@ MidwayMiddlewareService = __decorate([
101
124
  __metadata("design:paramtypes", [Object])
102
125
  ], MidwayMiddlewareService);
103
126
  exports.MidwayMiddlewareService = MidwayMiddlewareService;
104
- function pathMatching(options) {
105
- options = options || {};
106
- if (options.match && options.ignore)
107
- throw new error_1.MidwayCommonError('options.match and options.ignore can not both present');
108
- if (!options.match && !options.ignore)
109
- return () => true;
110
- const matchFn = options.match
111
- ? (0, util_1.toPathMatch)(options.match)
112
- : (0, util_1.toPathMatch)(options.ignore);
113
- return function pathMatch(ctx) {
114
- const matched = matchFn(ctx);
115
- return options.match ? matched : !matched;
116
- };
117
- }
118
- exports.pathMatching = pathMatching;
119
127
  //# sourceMappingURL=middlewareService.js.map
package/dist/setup.js CHANGED
@@ -75,7 +75,12 @@ async function initializeGlobalApplicationContext(globalOptions) {
75
75
  // bind user code module
76
76
  await applicationContext.ready();
77
77
  if (globalOptions.globalConfig) {
78
- configService.add([globalOptions.globalConfig]);
78
+ if (Array.isArray(globalOptions.globalConfig)) {
79
+ configService.add(globalOptions.globalConfig);
80
+ }
81
+ else {
82
+ configService.addObject(globalOptions.globalConfig);
83
+ }
79
84
  }
80
85
  // merge config
81
86
  await configService.load();
@@ -1,3 +1,4 @@
1
+ import { FunctionMiddleware } from '../interface';
1
2
  /**
2
3
  * @since 2.0.0
3
4
  * @param env
@@ -73,4 +74,12 @@ export declare const deprecatedOutput: (message: string) => void;
73
74
  */
74
75
  export declare const transformRequestObjectByType: (originValue: any, targetType?: any) => any;
75
76
  export declare function toPathMatch(pattern: any): any;
77
+ export declare function pathMatching(options: any): (ctx?: any) => any;
78
+ /**
79
+ * wrap function middleware with match and ignore
80
+ * @param mw
81
+ * @param options
82
+ */
83
+ export declare function wrapMiddleware(mw: FunctionMiddleware<any, any>, options: any): (context: any, next: any, options?: any) => any;
84
+ export declare function isIncludeProperty(obj: any, prop: string): boolean;
76
85
  //# sourceMappingURL=index.d.ts.map
@@ -1,10 +1,10 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
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;
3
+ exports.isIncludeProperty = exports.wrapMiddleware = exports.pathMatching = 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 class_transformer_1 = require("class-transformer");
7
+ const transformer = require("class-transformer");
8
8
  const pathToRegexp_1 = require("./pathToRegexp");
9
9
  const error_1 = require("../error");
10
10
  const debug = (0, util_1.debuglog)('midway:container:util');
@@ -205,7 +205,8 @@ const transformRequestObjectByType = (originValue, targetType) => {
205
205
  return originValue;
206
206
  }
207
207
  else {
208
- return (0, class_transformer_1.plainToClass)(targetType, originValue);
208
+ const transformToInstance = transformer['plainToClass'] || transformer['plainToInstance'];
209
+ return transformToInstance(targetType, originValue);
209
210
  }
210
211
  }
211
212
  };
@@ -236,4 +237,58 @@ function toPathMatch(pattern) {
236
237
  throw new error_1.MidwayCommonError('match/ignore pattern must be RegExp, Array or String, but got ' + pattern);
237
238
  }
238
239
  exports.toPathMatch = toPathMatch;
240
+ function pathMatching(options) {
241
+ options = options || {};
242
+ if (options.match && options.ignore)
243
+ throw new error_1.MidwayCommonError('options.match and options.ignore can not both present');
244
+ if (!options.match && !options.ignore)
245
+ return () => true;
246
+ const matchFn = options.match
247
+ ? toPathMatch(options.match)
248
+ : toPathMatch(options.ignore);
249
+ return function pathMatch(ctx) {
250
+ const matched = matchFn(ctx);
251
+ return options.match ? matched : !matched;
252
+ };
253
+ }
254
+ exports.pathMatching = pathMatching;
255
+ /**
256
+ * wrap function middleware with match and ignore
257
+ * @param mw
258
+ * @param options
259
+ */
260
+ function wrapMiddleware(mw, options) {
261
+ // support options.enable
262
+ if (options.enable === false)
263
+ return null;
264
+ // support options.match and options.ignore
265
+ if (!options.match && !options.ignore)
266
+ return mw;
267
+ const match = pathMatching(options);
268
+ const fn = (ctx, next) => {
269
+ if (!match(ctx))
270
+ return next();
271
+ return mw(ctx, next);
272
+ };
273
+ fn._name = mw._name + 'middlewareWrapper';
274
+ return fn;
275
+ }
276
+ exports.wrapMiddleware = wrapMiddleware;
277
+ function isOwnPropertyWritable(obj, prop) {
278
+ if (obj == null)
279
+ return false;
280
+ const type = typeof obj;
281
+ if (type !== 'object' && type !== 'function')
282
+ return false;
283
+ return !!Object.getOwnPropertyDescriptor(obj, prop);
284
+ }
285
+ function isIncludeProperty(obj, prop) {
286
+ while (obj) {
287
+ if (isOwnPropertyWritable(obj, prop))
288
+ return true;
289
+ obj = Object.getPrototypeOf(obj);
290
+ }
291
+ return false;
292
+ }
293
+ exports.isIncludeProperty = isIncludeProperty;
239
294
  //# sourceMappingURL=index.js.map
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@midwayjs/core",
3
- "version": "3.0.0-beta.5",
3
+ "version": "3.0.0-beta.9",
4
4
  "description": "midway core",
5
5
  "main": "dist/index",
6
6
  "typings": "dist/index.d.ts",
@@ -21,7 +21,8 @@
21
21
  ],
22
22
  "license": "MIT",
23
23
  "devDependencies": {
24
- "@midwayjs/decorator": "^3.0.0-beta.5",
24
+ "@midwayjs/decorator": "^3.0.0-beta.9",
25
+ "koa": "^2.13.4",
25
26
  "midway-test-component": "*",
26
27
  "mm": "3",
27
28
  "sinon": "^7.2.2"
@@ -31,8 +32,8 @@
31
32
  },
32
33
  "dependencies": {
33
34
  "@midwayjs/glob": "^1.0.2",
34
- "@midwayjs/logger": "^3.0.0-beta.5",
35
- "class-transformer": "^0.4.0",
35
+ "@midwayjs/logger": "^3.0.0-beta.9",
36
+ "class-transformer": "^0.5.1",
36
37
  "extend2": "^1.0.0",
37
38
  "picomatch": "^2.2.2",
38
39
  "reflect-metadata": "^0.1.13"
@@ -45,5 +46,5 @@
45
46
  "engines": {
46
47
  "node": ">=12"
47
48
  },
48
- "gitHead": "ab0bf05ae6d13f6435db2f7223202be61d585a1b"
49
+ "gitHead": "d23e4a4fee58097b98461625c428a37d55535cec"
49
50
  }