@midwayjs/core 3.3.2 → 3.3.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.
@@ -0,0 +1,28 @@
1
+ export declare abstract class DataSourceManager<T> {
2
+ protected dataSource: Map<string, T>;
3
+ protected options: {};
4
+ protected initDataSource(options?: any): Promise<void>;
5
+ /**
6
+ * get a data source instance
7
+ * @param dataSourceName
8
+ */
9
+ getDataSource(dataSourceName: string): T;
10
+ /**
11
+ * check data source has exists
12
+ * @param dataSourceName
13
+ */
14
+ hasDataSource(dataSourceName: string): boolean;
15
+ getDataSourceNames(): string[];
16
+ /**
17
+ * check the data source is connected
18
+ * @param dataSourceName
19
+ */
20
+ isConnected(dataSourceName: string): Promise<boolean>;
21
+ createInstance(config: any, clientName: any): Promise<T | void>;
22
+ abstract getName(): string;
23
+ protected abstract createDataSource(config: any, dataSourceName: string): Promise<T | void> | (T | void);
24
+ protected abstract checkConnected(dataSource: T): Promise<boolean>;
25
+ protected destroyDataSource(dataSource: T): Promise<void>;
26
+ stop(): Promise<void>;
27
+ }
28
+ //# sourceMappingURL=dataSourceManager.d.ts.map
@@ -0,0 +1,69 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.DataSourceManager = void 0;
4
+ /**
5
+ * 数据源管理器实现
6
+ */
7
+ const extend_1 = require("../util/extend");
8
+ const error_1 = require("../error");
9
+ class DataSourceManager {
10
+ constructor() {
11
+ this.dataSource = new Map();
12
+ this.options = {};
13
+ }
14
+ async initDataSource(options = {}) {
15
+ this.options = options;
16
+ if (options.dataSource) {
17
+ for (const dataSourceName in options.dataSource) {
18
+ // create data source
19
+ await this.createInstance(options.dataSource[dataSourceName], dataSourceName);
20
+ }
21
+ }
22
+ else {
23
+ throw new error_1.MidwayParameterError('DataSourceManager must set options.dataSource.');
24
+ }
25
+ }
26
+ /**
27
+ * get a data source instance
28
+ * @param dataSourceName
29
+ */
30
+ getDataSource(dataSourceName) {
31
+ return this.dataSource.get(dataSourceName);
32
+ }
33
+ /**
34
+ * check data source has exists
35
+ * @param dataSourceName
36
+ */
37
+ hasDataSource(dataSourceName) {
38
+ return this.dataSource.has(dataSourceName);
39
+ }
40
+ getDataSourceNames() {
41
+ return Array.from(this.dataSource.keys());
42
+ }
43
+ /**
44
+ * check the data source is connected
45
+ * @param dataSourceName
46
+ */
47
+ async isConnected(dataSourceName) {
48
+ return this.checkConnected(this.getDataSource(dataSourceName));
49
+ }
50
+ async createInstance(config, clientName) {
51
+ // options.default will be merge in to options.clients[id]
52
+ config = (0, extend_1.extend)(true, {}, this.options['default'], config);
53
+ const client = await this.createDataSource(config, clientName);
54
+ if (client) {
55
+ if (clientName) {
56
+ this.dataSource.set(clientName, client);
57
+ }
58
+ return client;
59
+ }
60
+ }
61
+ async destroyDataSource(dataSource) { }
62
+ async stop() {
63
+ for (const value of this.dataSource.values()) {
64
+ await this.destroyDataSource(value);
65
+ }
66
+ }
67
+ }
68
+ exports.DataSourceManager = DataSourceManager;
69
+ //# sourceMappingURL=dataSourceManager.js.map
@@ -15,6 +15,7 @@ export declare const FrameworkErrorEnum: {
15
15
  readonly MISSING_IMPORTS: "MIDWAY_10011";
16
16
  readonly UTIL_HTTP_TIMEOUT: "MIDWAY_10012";
17
17
  readonly INCONSISTENT_VERSION: "MIDWAY_10013";
18
+ readonly INVALID_CONFIG: "MIDWAY_10014";
18
19
  };
19
20
  export declare class MidwayCommonError extends MidwayError {
20
21
  constructor(message: string);
@@ -37,6 +38,9 @@ export declare class MidwayFeatureNotImplementedError extends MidwayError {
37
38
  export declare class MidwayConfigMissingError extends MidwayError {
38
39
  constructor(configKey: string);
39
40
  }
41
+ export declare class MidwayInvalidConfigError extends MidwayError {
42
+ constructor(message?: string);
43
+ }
40
44
  export declare class MidwayResolverMissingError extends MidwayError {
41
45
  constructor(type: string);
42
46
  }
@@ -1,6 +1,6 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.MidwayInconsistentVersionError = exports.MidwayUtilHttpClientTimeoutError = exports.MidwayMissingImportComponentError = exports.MidwaySingletonInjectRequestError = exports.MidwayUseWrongMethodError = exports.MidwayDuplicateRouteError = exports.MidwayResolverMissingError = exports.MidwayConfigMissingError = exports.MidwayFeatureNotImplementedError = exports.MidwayFeatureNoLongerSupportedError = exports.MidwayDefinitionNotFoundError = exports.MidwayParameterError = exports.MidwayCommonError = exports.FrameworkErrorEnum = void 0;
3
+ exports.MidwayInconsistentVersionError = exports.MidwayUtilHttpClientTimeoutError = exports.MidwayMissingImportComponentError = exports.MidwaySingletonInjectRequestError = exports.MidwayUseWrongMethodError = exports.MidwayDuplicateRouteError = exports.MidwayResolverMissingError = exports.MidwayInvalidConfigError = exports.MidwayConfigMissingError = exports.MidwayFeatureNotImplementedError = exports.MidwayFeatureNoLongerSupportedError = exports.MidwayDefinitionNotFoundError = exports.MidwayParameterError = exports.MidwayCommonError = exports.FrameworkErrorEnum = void 0;
4
4
  const base_1 = require("./base");
5
5
  exports.FrameworkErrorEnum = (0, base_1.registerErrorCode)('midway', {
6
6
  UNKNOWN: 10000,
@@ -17,6 +17,7 @@ exports.FrameworkErrorEnum = (0, base_1.registerErrorCode)('midway', {
17
17
  MISSING_IMPORTS: 10011,
18
18
  UTIL_HTTP_TIMEOUT: 10012,
19
19
  INCONSISTENT_VERSION: 10013,
20
+ INVALID_CONFIG: 10014,
20
21
  });
21
22
  class MidwayCommonError extends base_1.MidwayError {
22
23
  constructor(message) {
@@ -67,6 +68,12 @@ class MidwayConfigMissingError extends base_1.MidwayError {
67
68
  }
68
69
  }
69
70
  exports.MidwayConfigMissingError = MidwayConfigMissingError;
71
+ class MidwayInvalidConfigError extends base_1.MidwayError {
72
+ constructor(message) {
73
+ super('Invalid config file \n' + message, exports.FrameworkErrorEnum.INVALID_CONFIG);
74
+ }
75
+ }
76
+ exports.MidwayInvalidConfigError = MidwayInvalidConfigError;
70
77
  class MidwayResolverMissingError extends base_1.MidwayError {
71
78
  constructor(type) {
72
79
  super(`${type} resolver is not exists!`, exports.FrameworkErrorEnum.MISSING_RESOLVER);
@@ -18,6 +18,7 @@ const util = require("util");
18
18
  const environmentService_1 = require("./environmentService");
19
19
  const informationService_1 = require("./informationService");
20
20
  const extend_1 = require("../util/extend");
21
+ const error_1 = require("../error");
21
22
  const debug = util.debuglog('midway:debug');
22
23
  let MidwayConfigService = class MidwayConfigService {
23
24
  constructor() {
@@ -174,8 +175,11 @@ let MidwayConfigService = class MidwayConfigService {
174
175
  let exports = typeof configFilename === 'string'
175
176
  ? require(configFilename)
176
177
  : configFilename;
177
- if (exports && exports['default'] && Object.keys(exports).length === 1) {
178
- exports = exports['default'];
178
+ if (exports && exports.default) {
179
+ if (Object.keys(exports).length > 1) {
180
+ throw new error_1.MidwayInvalidConfigError(`${configFilename} should not have both a default export and named export`);
181
+ }
182
+ exports = exports.default;
179
183
  }
180
184
  return exports;
181
185
  }
@@ -49,6 +49,23 @@ let MidwayFrameworkService = class MidwayFrameworkService {
49
49
  var _a, _b;
50
50
  return new pipelineService_1.MidwayPipelineService((_b = (_a = instance[interface_1.REQUEST_OBJ_CTX_KEY]) === null || _a === void 0 ? void 0 : _a.requestContext) !== null && _b !== void 0 ? _b : this.applicationContext, meta.valves);
51
51
  });
52
+ // register @App decorator handler
53
+ this.decoratorService.registerPropertyHandler(decorator_1.APPLICATION_KEY, (propertyName, meta) => {
54
+ if (meta.type) {
55
+ const framework = this.applicationManager.getApplication(meta.type);
56
+ if (!framework) {
57
+ throw new error_1.MidwayCommonError(`Framework ${meta.type} not Found`);
58
+ }
59
+ return framework;
60
+ }
61
+ else {
62
+ return this.getMainApp();
63
+ }
64
+ });
65
+ this.decoratorService.registerPropertyHandler(decorator_1.PLUGIN_KEY, (propertyName, meta) => {
66
+ var _a;
67
+ return this.getMainApp()[(_a = meta.identifier) !== null && _a !== void 0 ? _a : propertyName];
68
+ });
52
69
  let frameworks = (0, decorator_1.listModule)(decorator_1.FRAMEWORK_KEY);
53
70
  // filter proto
54
71
  frameworks = filterProtoFramework(frameworks);
@@ -77,22 +94,6 @@ let MidwayFrameworkService = class MidwayFrameworkService {
77
94
  this.applicationManager.addFramework((_a = definition === null || definition === void 0 ? void 0 : definition.namespace) !== null && _a !== void 0 ? _a : frameworkInstance.getFrameworkName(), frameworkInstance);
78
95
  this.globalFrameworkList.push(frameworkInstance);
79
96
  }
80
- // register @App decorator handler
81
- this.decoratorService.registerPropertyHandler(decorator_1.APPLICATION_KEY, (propertyName, mete) => {
82
- if (mete.type) {
83
- const framework = this.applicationManager.getApplication(mete.type);
84
- if (!framework) {
85
- throw new error_1.MidwayCommonError(`Framework ${mete.type} not Found`);
86
- }
87
- return framework;
88
- }
89
- else {
90
- return this.getMainApp();
91
- }
92
- });
93
- this.decoratorService.registerPropertyHandler(decorator_1.PLUGIN_KEY, (key, target) => {
94
- return this.getMainApp()[key];
95
- });
96
97
  const nsSet = this.applicationContext['namespaceSet'];
97
98
  let mainNs;
98
99
  if (nsSet.size > 0) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@midwayjs/core",
3
- "version": "3.3.2",
3
+ "version": "3.3.6",
4
4
  "description": "midway core",
5
5
  "main": "dist/index",
6
6
  "typings": "dist/index.d.ts",
@@ -21,12 +21,12 @@
21
21
  ],
22
22
  "license": "MIT",
23
23
  "devDependencies": {
24
- "@midwayjs/decorator": "^3.1.6",
24
+ "@midwayjs/decorator": "^3.3.4",
25
25
  "koa": "2.13.4",
26
26
  "midway-test-component": "*",
27
27
  "mm": "3.2.0",
28
28
  "raw-body": "2.5.1",
29
- "sinon": "13.0.1"
29
+ "sinon": "13.0.2"
30
30
  },
31
31
  "peerDependencies": {
32
32
  "@midwayjs/decorator": "*"
@@ -45,5 +45,5 @@
45
45
  "engines": {
46
46
  "node": ">=12"
47
47
  },
48
- "gitHead": "2c7c235421ba904f8943efe31a71f452089d5d53"
48
+ "gitHead": "a603d2348d6141f8f723901498f03a162a037708"
49
49
  }
package/LICENSE DELETED
@@ -1,21 +0,0 @@
1
- MIT License
2
-
3
- Copyright (c) 2013 - Now midwayjs
4
-
5
- Permission is hereby granted, free of charge, to any person obtaining a copy
6
- of this software and associated documentation files (the "Software"), to deal
7
- in the Software without restriction, including without limitation the rights
8
- to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
- copies of the Software, and to permit persons to whom the Software is
10
- furnished to do so, subject to the following conditions:
11
-
12
- The above copyright notice and this permission notice shall be included in all
13
- copies or substantial portions of the Software.
14
-
15
- THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
- IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
- FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
- AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
- LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
- OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
- SOFTWARE.