@midwayjs/core 3.0.0-beta.13 → 3.0.0-beta.17

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 (37) hide show
  1. package/dist/common/applicationManager.d.ts +1 -2
  2. package/dist/common/applicationManager.js +14 -10
  3. package/dist/common/dataListener.d.ts +11 -0
  4. package/dist/common/dataListener.js +43 -0
  5. package/dist/common/fileDetector.js +1 -1
  6. package/dist/common/webRouterCollector.js +1 -1
  7. package/dist/context/container.js +15 -11
  8. package/dist/context/managedResolverFactory.js +7 -0
  9. package/dist/context/requestContainer.js +2 -0
  10. package/dist/definitions/functionDefinition.d.ts +1 -0
  11. package/dist/definitions/functionDefinition.js +1 -0
  12. package/dist/definitions/objectCreator.js +6 -6
  13. package/dist/definitions/objectDefinition.d.ts +1 -0
  14. package/dist/definitions/objectDefinition.js +1 -0
  15. package/dist/error/framework.d.ts +4 -0
  16. package/dist/error/framework.js +9 -1
  17. package/dist/error/http.d.ts +3 -4
  18. package/dist/error/http.js +5 -4
  19. package/dist/error/index.d.ts +1 -1
  20. package/dist/error/index.js +4 -1
  21. package/dist/functional/configuration.d.ts +2 -0
  22. package/dist/functional/configuration.js +10 -0
  23. package/dist/index.d.ts +2 -0
  24. package/dist/index.js +4 -1
  25. package/dist/interface.d.ts +46 -31
  26. package/dist/service/aspectService.js +1 -1
  27. package/dist/service/configService.d.ts +3 -1
  28. package/dist/service/configService.js +23 -17
  29. package/dist/service/environmentService.d.ts +1 -1
  30. package/dist/service/loggerService.d.ts +1 -1
  31. package/dist/service/middlewareService.d.ts +1 -1
  32. package/dist/service/middlewareService.js +6 -6
  33. package/dist/setup.js +5 -3
  34. package/dist/util/extend.d.ts +2 -0
  35. package/dist/util/extend.js +55 -0
  36. package/package.json +10 -11
  37. package/CHANGELOG.md +0 -2252
@@ -14,9 +14,19 @@ export declare type ServiceFactoryConfigOption<OPTIONS> = {
14
14
  [key: string]: PowerPartial<OPTIONS>;
15
15
  };
16
16
  };
17
- declare type ConfigType<T> = T extends (...args: any[]) => any ? PowerPartial<ReturnType<T>> : PowerPartial<T>;
17
+ declare type ConfigType<T> = T extends (...args: any[]) => any ? Writable<PowerPartial<ReturnType<T>>> : Writable<PowerPartial<T>>;
18
+ /**
19
+ * Get definition from config
20
+ */
18
21
  export declare type FileConfigOption<T, K = unknown> = K extends keyof ConfigType<T> ? Pick<ConfigType<T>, K> : ConfigType<T>;
19
22
  /**
23
+ * Make object property writeable
24
+ */
25
+ export declare type Writable<T> = {
26
+ -readonly [P in keyof T]: T[P];
27
+ };
28
+ /**
29
+ * Lifecycle Definition
20
30
  * 生命周期定义
21
31
  */
22
32
  export interface ILifeCycle extends Partial<IObjectLifeCycle> {
@@ -29,8 +39,8 @@ export declare type ObjectContext = {
29
39
  originName?: string;
30
40
  };
31
41
  /**
42
+ * Abstract Object Factory
32
43
  * 对象容器抽象
33
- * 默认用Xml容器实现一个
34
44
  */
35
45
  export interface IObjectFactory {
36
46
  registry: IObjectDefinitionRegistry;
@@ -46,32 +56,36 @@ export declare enum ObjectLifeCycleEvent {
46
56
  AFTER_INIT = "afterObjectInit",
47
57
  BEFORE_DESTROY = "beforeObjectDestroy"
48
58
  }
59
+ interface ObjectLifeCycleOptions {
60
+ context: IMidwayContainer;
61
+ definition: IObjectDefinition;
62
+ }
63
+ export interface ObjectBeforeBindOptions extends ObjectLifeCycleOptions {
64
+ replaceCallback: (newDefinition: IObjectDefinition) => void;
65
+ }
66
+ export interface ObjectBeforeCreatedOptions extends ObjectLifeCycleOptions {
67
+ constructorArgs: any[];
68
+ }
69
+ export interface ObjectCreatedOptions<T> extends ObjectLifeCycleOptions {
70
+ replaceCallback: (ins: T) => void;
71
+ }
72
+ export interface ObjectInitOptions extends ObjectLifeCycleOptions {
73
+ }
74
+ export interface ObjectBeforeDestroyOptions extends ObjectLifeCycleOptions {
75
+ }
76
+ /**
77
+ * Object Lifecycle
78
+ * 对象生命周期
79
+ */
49
80
  export interface IObjectLifeCycle {
50
- onBeforeBind(fn: (Clzz: any, options: {
51
- context: IMidwayContainer;
52
- definition: IObjectDefinition;
53
- replaceCallback: (newDefinition: IObjectDefinition) => void;
54
- }) => void): any;
55
- onBeforeObjectCreated(fn: (Clzz: any, options: {
56
- context: IMidwayContainer;
57
- definition: IObjectDefinition;
58
- constructorArgs: any[];
59
- }) => void): any;
60
- onObjectCreated<T>(fn: (ins: T, options: {
61
- context: IMidwayContainer;
62
- definition: IObjectDefinition;
63
- replaceCallback: (ins: T) => void;
64
- }) => void): any;
65
- onObjectInit<T>(fn: (ins: T, options: {
66
- context: IMidwayContainer;
67
- definition: IObjectDefinition;
68
- }) => void): any;
69
- onBeforeObjectDestroy<T>(fn: (ins: T, options: {
70
- context: IMidwayContainer;
71
- definition: IObjectDefinition;
72
- }) => void): any;
81
+ onBeforeBind(fn: (Clzz: any, options: ObjectBeforeBindOptions) => void): any;
82
+ onBeforeObjectCreated(fn: (Clzz: any, options: ObjectBeforeCreatedOptions) => void): any;
83
+ onObjectCreated<T>(fn: (ins: T, options: ObjectCreatedOptions<T>) => void): any;
84
+ onObjectInit<T>(fn: (ins: T, options: ObjectInitOptions) => void): any;
85
+ onBeforeObjectDestroy<T>(fn: (ins: T, options: ObjectBeforeDestroyOptions) => void): any;
73
86
  }
74
87
  /**
88
+ * Object Definition
75
89
  * 对象描述定义
76
90
  */
77
91
  export interface IObjectDefinition {
@@ -112,6 +126,7 @@ export interface IObjectDefinition {
112
126
  metadata: any;
113
127
  }>;
114
128
  createFrom: 'framework' | 'file' | 'module';
129
+ allowDowngrade: boolean;
115
130
  }
116
131
  export interface IObjectCreator {
117
132
  load(): any;
@@ -123,6 +138,7 @@ export interface IObjectCreator {
123
138
  doDestroyAsync(obj: any): Promise<void>;
124
139
  }
125
140
  /**
141
+ * Object Definition Registry
126
142
  * 对象定义存储容器
127
143
  */
128
144
  export interface IObjectDefinitionRegistry {
@@ -279,7 +295,7 @@ export declare type NextFunction = () => Promise<any>;
279
295
  * Common middleware definition
280
296
  */
281
297
  export interface IMiddleware<CTX, R, N = unknown> {
282
- resolve: (app?: IMidwayApplication) => FunctionMiddleware<CTX, R, N>;
298
+ resolve: (app?: IMidwayApplication) => FunctionMiddleware<CTX, R, N> | Promise<FunctionMiddleware<CTX, R, N>>;
283
299
  match?: (ctx?: CTX) => boolean;
284
300
  ignore?: (ctx?: CTX) => boolean;
285
301
  }
@@ -396,7 +412,11 @@ export interface IMidwayBootstrapOptions {
396
412
  appDir?: string;
397
413
  applicationContext?: IMidwayContainer;
398
414
  preloadModules?: any[];
415
+ /**
416
+ * @deprecated please use 'imports'
417
+ */
399
418
  configurationModule?: any | any[];
419
+ imports?: any | any[];
400
420
  moduleDetector?: 'file' | IFileDetector | false;
401
421
  logger?: boolean | ILogger;
402
422
  ignore?: string[];
@@ -452,10 +472,5 @@ export interface MidwayAppInfo {
452
472
  export interface MidwayConfig extends FileConfigOption<typeof _default> {
453
473
  [customConfigKey: string]: unknown;
454
474
  }
455
- export interface TranslateOptions {
456
- lang?: string;
457
- group?: string;
458
- args?: any;
459
- }
460
475
  export {};
461
476
  //# sourceMappingURL=interface.d.ts.map
@@ -64,7 +64,7 @@ let MidwayAspectService = class MidwayAspectService {
64
64
  */
65
65
  interceptPrototypeMethod(Clz, methodName, aspectObject) {
66
66
  const originMethod = Clz.prototype[methodName];
67
- if ((0, decorator_1.isAsyncFunction)(Clz.prototype[methodName])) {
67
+ if (decorator_1.Types.isAsyncFunction(Clz.prototype[methodName])) {
68
68
  Clz.prototype[methodName] = async function (...args) {
69
69
  var _a, _b, _c;
70
70
  let error, result;
@@ -1,4 +1,4 @@
1
- import { IConfigService } from '../interface';
1
+ import { IConfigService, MidwayAppInfo } from '../interface';
2
2
  import { MidwayEnvironmentService } from './environmentService';
3
3
  import { MidwayInformationService } from './informationService';
4
4
  export declare class MidwayConfigService implements IConfigService {
@@ -7,8 +7,10 @@ export declare class MidwayConfigService implements IConfigService {
7
7
  protected configuration: any;
8
8
  protected isReady: boolean;
9
9
  protected externalObject: Record<string, unknown>[];
10
+ protected appInfo: MidwayAppInfo;
10
11
  protected environmentService: MidwayEnvironmentService;
11
12
  protected informationService: MidwayInformationService;
13
+ protected init(): Promise<void>;
12
14
  add(configFilePaths: any[]): void;
13
15
  addObject(obj: Record<string, unknown>): void;
14
16
  private getEnvSet;
@@ -10,7 +10,6 @@ var __metadata = (this && this.__metadata) || function (k, v) {
10
10
  };
11
11
  Object.defineProperty(exports, "__esModule", { value: true });
12
12
  exports.MidwayConfigService = void 0;
13
- const extend = require("extend2");
14
13
  const path_1 = require("path");
15
14
  const util_1 = require("../util");
16
15
  const fs_1 = require("fs");
@@ -18,6 +17,7 @@ const decorator_1 = require("@midwayjs/decorator");
18
17
  const util = require("util");
19
18
  const environmentService_1 = require("./environmentService");
20
19
  const informationService_1 = require("./informationService");
20
+ const extend_1 = require("../util/extend");
21
21
  const debug = util.debuglog('midway:debug');
22
22
  let MidwayConfigService = class MidwayConfigService {
23
23
  constructor() {
@@ -29,6 +29,17 @@ let MidwayConfigService = class MidwayConfigService {
29
29
  this.isReady = false;
30
30
  this.externalObject = [];
31
31
  }
32
+ async init() {
33
+ this.appInfo = {
34
+ pkg: this.informationService.getPkg(),
35
+ name: this.informationService.getProjectName(),
36
+ baseDir: this.informationService.getBaseDir(),
37
+ appDir: this.informationService.getAppDir(),
38
+ HOME: this.informationService.getHome(),
39
+ root: this.informationService.getRoot(),
40
+ env: this.environmentService.getCurrentEnvironment(),
41
+ };
42
+ }
32
43
  add(configFilePaths) {
33
44
  for (const dir of configFilePaths) {
34
45
  if (typeof dir === 'string') {
@@ -63,7 +74,7 @@ let MidwayConfigService = class MidwayConfigService {
63
74
  }
64
75
  addObject(obj) {
65
76
  if (this.isReady) {
66
- extend(true, this.configuration, obj);
77
+ (0, extend_1.extend)(true, this.configuration, obj);
67
78
  }
68
79
  else {
69
80
  this.externalObject.push(obj);
@@ -96,20 +107,9 @@ let MidwayConfigService = class MidwayConfigService {
96
107
  const target = {};
97
108
  for (const filename of [...defaultSet, ...currentEnvSet]) {
98
109
  let config = await this.loadConfig(filename);
99
- if ((0, decorator_1.isFunction)(config)) {
110
+ if (decorator_1.Types.isFunction(config)) {
100
111
  // eslint-disable-next-line prefer-spread
101
- config = config.apply(null, [
102
- {
103
- pkg: this.informationService.getPkg(),
104
- name: this.informationService.getProjectName(),
105
- baseDir: this.informationService.getBaseDir(),
106
- appDir: this.informationService.getAppDir(),
107
- HOME: this.informationService.getHome(),
108
- root: this.informationService.getRoot(),
109
- env: this.environmentService.getCurrentEnvironment(),
110
- },
111
- target,
112
- ]);
112
+ config = config.apply(null, [this.appInfo, target]);
113
113
  }
114
114
  if (!config) {
115
115
  continue;
@@ -120,13 +120,13 @@ let MidwayConfigService = class MidwayConfigService {
120
120
  else {
121
121
  debug('[config]: Loaded config %j', config);
122
122
  }
123
- extend(true, target, config);
123
+ (0, extend_1.extend)(true, target, config);
124
124
  }
125
125
  if (this.externalObject.length) {
126
126
  for (const externalObject of this.externalObject) {
127
127
  if (externalObject) {
128
128
  debug('[config]: Loaded external object %j', externalObject);
129
- extend(true, target, externalObject);
129
+ (0, extend_1.extend)(true, target, externalObject);
130
130
  }
131
131
  }
132
132
  }
@@ -160,6 +160,12 @@ __decorate([
160
160
  (0, decorator_1.Inject)(),
161
161
  __metadata("design:type", informationService_1.MidwayInformationService)
162
162
  ], MidwayConfigService.prototype, "informationService", void 0);
163
+ __decorate([
164
+ (0, decorator_1.Init)(),
165
+ __metadata("design:type", Function),
166
+ __metadata("design:paramtypes", []),
167
+ __metadata("design:returntype", Promise)
168
+ ], MidwayConfigService.prototype, "init", null);
163
169
  MidwayConfigService = __decorate([
164
170
  (0, decorator_1.Provide)(),
165
171
  (0, decorator_1.Scope)(decorator_1.ScopeEnum.Singleton)
@@ -1,6 +1,6 @@
1
1
  import { IEnvironmentService } from '../interface';
2
2
  export declare class MidwayEnvironmentService implements IEnvironmentService {
3
- environment: string;
3
+ protected environment: string;
4
4
  getCurrentEnvironment(): string;
5
5
  setCurrentEnvironment(environment: string): void;
6
6
  isDevelopmentEnvironment(): boolean;
@@ -7,7 +7,7 @@ export declare class MidwayLoggerService extends ServiceFactory<ILogger> {
7
7
  configService: MidwayConfigService;
8
8
  constructor(applicationContext: IMidwayContainer);
9
9
  protected init(): Promise<void>;
10
- transformEggConfig(): {
10
+ protected transformEggConfig(): {
11
11
  midwayLogger: {
12
12
  default: {};
13
13
  clients: {};
@@ -3,7 +3,7 @@ export declare class MidwayMiddlewareService<T, R, N = unknown> {
3
3
  readonly applicationContext: IMidwayContainer;
4
4
  constructor(applicationContext: IMidwayContainer);
5
5
  compose(middleware: Array<CommonMiddleware<T, R, N> | string>, app: IMidwayApplication, name?: string): Promise<{
6
- (context: any, next?: any): Promise<any>;
6
+ (context: T, next?: any): Promise<any>;
7
7
  _name: string;
8
8
  }>;
9
9
  }
@@ -23,14 +23,14 @@ let MidwayMiddlewareService = class MidwayMiddlewareService {
23
23
  }
24
24
  const newMiddlewareArr = [];
25
25
  for (let fn of middleware) {
26
- if ((0, decorator_1.isClass)(fn) || typeof fn === 'string') {
26
+ if (decorator_1.Types.isClass(fn) || typeof fn === 'string') {
27
27
  if (typeof fn === 'string' &&
28
28
  !this.applicationContext.hasDefinition(fn)) {
29
29
  throw new error_1.MidwayCommonError('Middleware definition not found in midway container');
30
30
  }
31
31
  const classMiddleware = await this.applicationContext.getAsync(fn);
32
32
  if (classMiddleware) {
33
- fn = classMiddleware.resolve(app);
33
+ fn = await classMiddleware.resolve(app);
34
34
  if (!classMiddleware.match && !classMiddleware.ignore) {
35
35
  if (!fn.name) {
36
36
  fn._name = classMiddleware.constructor.name;
@@ -88,11 +88,11 @@ let MidwayMiddlewareService = class MidwayMiddlewareService {
88
88
  index,
89
89
  })).then(result => {
90
90
  // need to set body
91
- if (context.body && !result) {
92
- result = context.body;
91
+ if (context['body'] && !result) {
92
+ result = context['body'];
93
93
  }
94
- else if (result && context.body !== result) {
95
- context.body = result;
94
+ else if (result && context['body'] !== result) {
95
+ context['body'] = result;
96
96
  }
97
97
  return result;
98
98
  });
package/dist/setup.js CHANGED
@@ -64,12 +64,14 @@ async function initializeGlobalApplicationContext(globalOptions) {
64
64
  await applicationContext.getAsync(_1.MidwayDecoratorService, [
65
65
  applicationContext,
66
66
  ]);
67
- if (!globalOptions.configurationModule) {
68
- globalOptions.configurationModule = [
67
+ if (!globalOptions.imports) {
68
+ globalOptions.imports = [
69
69
  (0, _1.safeRequire)((0, path_1.join)(globalOptions.baseDir, 'configuration')),
70
70
  ];
71
71
  }
72
- for (const configurationModule of [].concat(globalOptions.configurationModule)) {
72
+ for (const configurationModule of []
73
+ .concat(globalOptions.imports)
74
+ .concat(globalOptions.configurationModule)) {
73
75
  // load configuration and component
74
76
  applicationContext.load(configurationModule);
75
77
  }
@@ -0,0 +1,2 @@
1
+ export declare function extend(...args: any[]): any;
2
+ //# sourceMappingURL=extend.d.ts.map
@@ -0,0 +1,55 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.extend = void 0;
4
+ /**
5
+ * fork from https://github.com/eggjs/extend2
6
+ */
7
+ const decorator_1 = require("@midwayjs/decorator");
8
+ function extend(...args) {
9
+ let options, name, src, copy, clone;
10
+ let target = args[0];
11
+ let i = 1;
12
+ const length = args.length;
13
+ let deep = false;
14
+ // Handle a deep copy situation
15
+ if (typeof target === 'boolean') {
16
+ deep = target;
17
+ target = args[1] || {};
18
+ // skip the boolean and the target
19
+ i = 2;
20
+ }
21
+ else if ((typeof target !== 'object' && typeof target !== 'function') ||
22
+ target == null) {
23
+ target = {};
24
+ }
25
+ for (; i < length; ++i) {
26
+ options = args[i];
27
+ // Only deal with non-null/undefined values
28
+ if (options == null)
29
+ continue;
30
+ // Extend the base object
31
+ for (name in options) {
32
+ if (name === '__proto__')
33
+ continue;
34
+ src = target[name];
35
+ copy = options[name];
36
+ // Prevent never-ending loop
37
+ if (target === copy)
38
+ continue;
39
+ // Recurse if we're merging plain objects
40
+ if (deep && copy && decorator_1.Types.isPlainObject(copy)) {
41
+ clone = src && decorator_1.Types.isPlainObject(src) ? src : {};
42
+ // Never move original objects, clone them
43
+ target[name] = extend(deep, clone, copy);
44
+ // Don't bring in undefined values
45
+ }
46
+ else if (typeof copy !== 'undefined') {
47
+ target[name] = copy;
48
+ }
49
+ }
50
+ }
51
+ // Return the modified object
52
+ return target;
53
+ }
54
+ exports.extend = extend;
55
+ //# sourceMappingURL=extend.js.map
package/package.json CHANGED
@@ -1,13 +1,13 @@
1
1
  {
2
2
  "name": "@midwayjs/core",
3
- "version": "3.0.0-beta.13",
3
+ "version": "3.0.0-beta.17",
4
4
  "description": "midway core",
5
5
  "main": "dist/index",
6
6
  "typings": "dist/index.d.ts",
7
7
  "scripts": {
8
8
  "build": "tsc",
9
- "test": "node --require=ts-node/register ../../node_modules/.bin/jest",
10
- "cov": "node --require=ts-node/register ../../node_modules/.bin/jest --coverage --forceExit",
9
+ "test": "node --require=ts-node/register ../../node_modules/.bin/jest --runInBand",
10
+ "cov": "node --require=ts-node/register ../../node_modules/.bin/jest --runInBand --coverage --forceExit",
11
11
  "link": "npm link"
12
12
  },
13
13
  "keywords": [
@@ -21,21 +21,20 @@
21
21
  ],
22
22
  "license": "MIT",
23
23
  "devDependencies": {
24
- "@midwayjs/decorator": "^3.0.0-beta.13",
25
- "koa": "^2.13.4",
24
+ "@midwayjs/decorator": "^3.0.0-beta.17",
25
+ "koa": "2.13.4",
26
26
  "midway-test-component": "*",
27
- "mm": "3",
28
- "sinon": "^7.2.2"
27
+ "mm": "3.2.0",
28
+ "sinon": "12.0.1"
29
29
  },
30
30
  "peerDependencies": {
31
31
  "@midwayjs/decorator": "*"
32
32
  },
33
33
  "dependencies": {
34
34
  "@midwayjs/glob": "^1.0.2",
35
- "@midwayjs/logger": "^3.0.0-beta.13",
35
+ "@midwayjs/logger": "2.14.0",
36
36
  "class-transformer": "^0.5.1",
37
- "extend2": "^1.0.0",
38
- "picomatch": "^2.3.0"
37
+ "picomatch": "2.3.1"
39
38
  },
40
39
  "author": "Harry Chen <czy88840616@gmail.com>",
41
40
  "repository": {
@@ -45,5 +44,5 @@
45
44
  "engines": {
46
45
  "node": ">=12"
47
46
  },
48
- "gitHead": "d3c47770fee9dce33a8d148882173fd7782864ad"
47
+ "gitHead": "17a8b5bd3d0b0b21f24dd2f165b5df9097fc3ec4"
49
48
  }