@midwayjs/core 3.6.0 → 3.8.0

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.
@@ -2,6 +2,7 @@ export declare abstract class DataSourceManager<T> {
2
2
  protected dataSource: Map<string, T>;
3
3
  protected options: {};
4
4
  protected modelMapping: WeakMap<object, any>;
5
+ private innerDefaultDataSourceName;
5
6
  protected initDataSource(options: any, appDir: string): Promise<void>;
6
7
  /**
7
8
  * get a data source instance
@@ -30,6 +31,7 @@ export declare abstract class DataSourceManager<T> {
30
31
  protected abstract checkConnected(dataSource: T): Promise<boolean>;
31
32
  protected abstract destroyDataSource(dataSource: T): Promise<void>;
32
33
  stop(): Promise<void>;
34
+ getDefaultDataSourceName(): string;
33
35
  }
34
36
  export declare function globModels(globString: string, appDir: string): any[];
35
37
  export interface CreateDataSourceInstanceOptions {
@@ -112,6 +112,22 @@ class DataSourceManager {
112
112
  }));
113
113
  this.dataSource.clear();
114
114
  }
115
+ getDefaultDataSourceName() {
116
+ if (this.innerDefaultDataSourceName === undefined) {
117
+ if (this.options['defaultDataSourceName']) {
118
+ this.innerDefaultDataSourceName = this.options['defaultDataSourceName'];
119
+ }
120
+ else if (this.dataSource.size === 1) {
121
+ // Set the default source name when there is only one data source
122
+ this.innerDefaultDataSourceName = Array.from(this.dataSource.keys())[0];
123
+ }
124
+ else {
125
+ // Set empty string for cache
126
+ this.innerDefaultDataSourceName = '';
127
+ }
128
+ }
129
+ return this.innerDefaultDataSourceName;
130
+ }
115
131
  }
116
132
  exports.DataSourceManager = DataSourceManager;
117
133
  function globModels(globString, appDir) {
@@ -124,7 +140,6 @@ function globModels(globString, appDir) {
124
140
  if (/\*/.test(globString)) {
125
141
  cwd = appDir;
126
142
  pattern = [...constants_1.DEFAULT_PATTERN.map(p => (0, path_1.join)(globString, p))];
127
- pattern.push(globString);
128
143
  }
129
144
  else {
130
145
  pattern = [...constants_1.DEFAULT_PATTERN];
@@ -12,5 +12,6 @@ export declare abstract class ServiceFactory<T> {
12
12
  protected abstract createClient(config: any, clientName: any): Promise<T | void> | (T | void);
13
13
  protected destroyClient(client: T): Promise<void>;
14
14
  stop(): Promise<void>;
15
+ getDefaultClientName(): string;
15
16
  }
16
17
  //# sourceMappingURL=serviceFactory.d.ts.map
@@ -17,7 +17,6 @@ class ServiceFactory {
17
17
  options.clients = options.clients || {};
18
18
  options.clients['default'] = options.clients['default'] || {};
19
19
  (0, extend_1.extend)(true, options.clients['default'], options.client);
20
- delete options.client;
21
20
  }
22
21
  // multi client
23
22
  if (options.clients) {
@@ -49,6 +48,9 @@ class ServiceFactory {
49
48
  await this.destroyClient(value);
50
49
  }
51
50
  }
51
+ getDefaultClientName() {
52
+ return this.options['defaultClientName'];
53
+ }
52
54
  }
53
55
  exports.ServiceFactory = ServiceFactory;
54
56
  //# sourceMappingURL=serviceFactory.js.map
@@ -78,6 +78,7 @@ export declare class MidwayContainer implements IMidwayContainer, IModuleStore {
78
78
  listModule(key: string): unknown[];
79
79
  transformModule(moduleMap: Map<string, Set<any>>): void;
80
80
  hasNamespace(ns: string): boolean;
81
+ getNamespaceList(): string[];
81
82
  hasDefinition(identifier: ObjectIdentifier): boolean;
82
83
  hasObject(identifier: ObjectIdentifier): boolean;
83
84
  }
@@ -490,6 +490,9 @@ class MidwayContainer {
490
490
  hasNamespace(ns) {
491
491
  return this.namespaceSet.has(ns);
492
492
  }
493
+ getNamespaceList() {
494
+ return Array.from(this.namespaceSet);
495
+ }
493
496
  hasDefinition(identifier) {
494
497
  return this.registry.hasDefinition(identifier);
495
498
  }
@@ -235,4 +235,8 @@ export interface IModuleStore {
235
235
  saveModule(key: string, module: any): any;
236
236
  transformModule?(moduleMap: Map<string, Set<any>>): any;
237
237
  }
238
+ export interface PipeTransform<T = any, R = any> {
239
+ transform(value: T): R;
240
+ }
241
+ export declare type PipeTransformFunction<T = any, R = any> = (value: T) => R;
238
242
  //# sourceMappingURL=interface.d.ts.map
@@ -33,7 +33,6 @@ class MidwayFrameworkType extends FrameworkType {
33
33
  super();
34
34
  this.name = name;
35
35
  }
36
- ;
37
36
  }
38
37
  exports.MidwayFrameworkType = MidwayFrameworkType;
39
38
  MidwayFrameworkType.WEB = new MidwayFrameworkType('@midwayjs/web');
@@ -1,3 +1,4 @@
1
+ import { IMidwayContext } from '../../interface';
1
2
  export declare enum RouteParamTypes {
2
3
  QUERY = 0,
3
4
  BODY = 1,
@@ -10,13 +11,18 @@ export declare enum RouteParamTypes {
10
11
  REQUEST_PATH = 8,
11
12
  REQUEST_IP = 9,
12
13
  QUERIES = 10,
13
- FIELDS = 11
14
+ FIELDS = 11,
15
+ CUSTOM = 12
14
16
  }
15
17
  export interface RouterParamValue {
16
18
  index: number;
17
19
  type: RouteParamTypes;
18
20
  propertyData?: any;
19
21
  }
22
+ export declare type KoaLikeCustomParamDecorator<T = unknown> = (ctx: IMidwayContext) => T | Promise<T>;
23
+ export declare type ExpressLikeCustomParamDecorator<T = unknown> = (req: any, res: any) => T | Promise<T>;
24
+ export declare type CustomParamDecorator<T = unknown> = KoaLikeCustomParamDecorator<T> | ExpressLikeCustomParamDecorator<T>;
25
+ export declare const createRequestParamDecorator: (transform: CustomParamDecorator) => ParameterDecorator;
20
26
  export declare const Session: (property?: string) => ParameterDecorator;
21
27
  export declare const Body: (property?: string) => ParameterDecorator;
22
28
  export declare const Query: (property?: string) => ParameterDecorator;
@@ -1,6 +1,6 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.Fields = exports.Queries = exports.RequestIP = exports.RequestPath = exports.Files = exports.File = exports.Headers = exports.Param = exports.Query = exports.Body = exports.Session = exports.RouteParamTypes = void 0;
3
+ exports.Fields = exports.Queries = exports.RequestIP = exports.RequestPath = exports.Files = exports.File = exports.Headers = exports.Param = exports.Query = exports.Body = exports.Session = exports.createRequestParamDecorator = exports.RouteParamTypes = void 0;
4
4
  const __1 = require("../");
5
5
  var RouteParamTypes;
6
6
  (function (RouteParamTypes) {
@@ -16,15 +16,21 @@ var RouteParamTypes;
16
16
  RouteParamTypes[RouteParamTypes["REQUEST_IP"] = 9] = "REQUEST_IP";
17
17
  RouteParamTypes[RouteParamTypes["QUERIES"] = 10] = "QUERIES";
18
18
  RouteParamTypes[RouteParamTypes["FIELDS"] = 11] = "FIELDS";
19
+ RouteParamTypes[RouteParamTypes["CUSTOM"] = 12] = "CUSTOM";
19
20
  })(RouteParamTypes = exports.RouteParamTypes || (exports.RouteParamTypes = {}));
20
21
  const createParamMapping = function (type) {
21
- return (propertyData) => {
22
+ return (propertyData, pipes) => {
22
23
  return (0, __1.createCustomParamDecorator)(__1.WEB_ROUTER_PARAM_KEY, {
23
24
  type,
24
25
  propertyData,
26
+ pipes,
25
27
  });
26
28
  };
27
29
  };
30
+ const createRequestParamDecorator = function (transform) {
31
+ return createParamMapping(RouteParamTypes.CUSTOM)(transform);
32
+ };
33
+ exports.createRequestParamDecorator = createRequestParamDecorator;
28
34
  const Session = (property) => createParamMapping(RouteParamTypes.SESSION)(property);
29
35
  exports.Session = Session;
30
36
  const Body = (property) => createParamMapping(RouteParamTypes.BODY)(property);
@@ -22,9 +22,11 @@ export declare type ServiceFactoryConfigOption<OPTIONS> = {
22
22
  clients?: {
23
23
  [key: string]: PowerPartial<OPTIONS>;
24
24
  };
25
+ defaultClientName?: string;
25
26
  };
26
27
  export declare type DataSourceManagerConfigOption<OPTIONS> = {
27
28
  default?: PowerPartial<OPTIONS>;
29
+ defaultDataSourceName?: string;
28
30
  dataSource?: {
29
31
  [key: string]: PowerPartial<{
30
32
  entities: any[];
@@ -234,6 +236,7 @@ export interface IMidwayContainer extends IObjectFactory, IObjectLifeCycle {
234
236
  registerObject(identifier: ObjectIdentifier, target: any): any;
235
237
  load(module?: any): any;
236
238
  hasNamespace(namespace: string): boolean;
239
+ getNamespaceList(): string[];
237
240
  hasDefinition(identifier: ObjectIdentifier): any;
238
241
  hasObject(identifier: ObjectIdentifier): any;
239
242
  bind<T>(target: T, options?: Partial<IObjectDefinition>): void;
@@ -94,10 +94,20 @@ let MidwayFrameworkService = class MidwayFrameworkService {
94
94
  this.applicationManager.addFramework((_a = definition === null || definition === void 0 ? void 0 : definition.namespace) !== null && _a !== void 0 ? _a : frameworkInstance.getFrameworkName(), frameworkInstance);
95
95
  this.globalFrameworkList.push(frameworkInstance);
96
96
  }
97
- const nsSet = this.applicationContext['namespaceSet'];
98
97
  let mainNs;
99
- if (nsSet.size > 0) {
100
- [mainNs] = nsSet;
98
+ /**
99
+ * 这里处理引入组件的顺序,在主框架之前是否包含其他的 framework
100
+ * 1、装饰器的顺序和 import 的写的顺序有关
101
+ * 2、主框架和 configuration 中的配置加载顺序有关
102
+ * 3、两者不符合的话,App 装饰器获取的 app 会不一致,导致中间件等无法正常使用
103
+ */
104
+ const namespaceList = this.applicationContext.getNamespaceList();
105
+ for (const namespace of namespaceList) {
106
+ const framework = this.applicationManager.getApplication(namespace);
107
+ if (framework) {
108
+ mainNs = namespace;
109
+ break;
110
+ }
101
111
  }
102
112
  global['MIDWAY_MAIN_FRAMEWORK'] = this.mainFramework =
103
113
  (_b = this.applicationManager.getFramework(mainNs)) !== null && _b !== void 0 ? _b : this.globalFrameworkList[0];
@@ -114,8 +114,6 @@ export declare class MidwayWebRouterService {
114
114
  private isReady;
115
115
  protected routes: Map<string, RouterInfo[]>;
116
116
  protected routesPriority: RouterPriority[];
117
- private cachedFlattenRouteList;
118
- private includeCompileUrlPattern;
119
117
  constructor(options?: RouterCollectorOptions);
120
118
  protected analyze(): Promise<void>;
121
119
  protected analyzeController(): void;
@@ -239,7 +237,6 @@ export declare class MidwayWebRouterService {
239
237
  getRouterTable(): Promise<Map<string, RouterInfo[]>>;
240
238
  getFlattenRouterTable(options?: {
241
239
  compileUrlPattern?: boolean;
242
- noCache?: boolean;
243
240
  }): Promise<RouterInfo[]>;
244
241
  getMatchedRouterInfo(routerUrl: string, method: string): Promise<RouterInfo | undefined>;
245
242
  protected checkDuplicateAndPush(prefix: any, routerInfo: RouterInfo): void;
@@ -23,7 +23,6 @@ let MidwayWebRouterService = class MidwayWebRouterService {
23
23
  this.isReady = false;
24
24
  this.routes = new Map();
25
25
  this.routesPriority = [];
26
- this.includeCompileUrlPattern = false;
27
26
  }
28
27
  async analyze() {
29
28
  this.analyzeController();
@@ -182,6 +181,8 @@ let MidwayWebRouterService = class MidwayWebRouterService {
182
181
  this.checkDuplicateAndPush(prefix, Object.assign(routerInfoOption, {
183
182
  method: routerFunction,
184
183
  }));
184
+ // sort again
185
+ this.sortPrefixAndRouter();
185
186
  }
186
187
  sortRouter(urlMatchList) {
187
188
  // 1. 绝对路径规则优先级最高如 /ab/cb/e
@@ -260,18 +261,6 @@ let MidwayWebRouterService = class MidwayWebRouterService {
260
261
  return this.routes;
261
262
  }
262
263
  async getFlattenRouterTable(options = {}) {
263
- if (this.cachedFlattenRouteList && !options.noCache) {
264
- if (options.compileUrlPattern && !this.includeCompileUrlPattern) {
265
- this.includeCompileUrlPattern = true;
266
- // attach match pattern function
267
- for (const item of this.cachedFlattenRouteList) {
268
- if (item.fullUrlFlattenString) {
269
- item.fullUrlCompiledRegexp = pathToRegexp_1.PathToRegexpUtil.toRegexp(item.fullUrlFlattenString);
270
- }
271
- }
272
- }
273
- return this.cachedFlattenRouteList;
274
- }
275
264
  if (!this.isReady) {
276
265
  await this.analyze();
277
266
  this.isReady = true;
@@ -281,7 +270,6 @@ let MidwayWebRouterService = class MidwayWebRouterService {
281
270
  routeArr = routeArr.concat(this.routes.get(routerPriority.prefix));
282
271
  }
283
272
  if (options.compileUrlPattern) {
284
- this.includeCompileUrlPattern = true;
285
273
  // attach match pattern function
286
274
  for (const item of routeArr) {
287
275
  if (item.fullUrlFlattenString) {
@@ -289,7 +277,6 @@ let MidwayWebRouterService = class MidwayWebRouterService {
289
277
  }
290
278
  }
291
279
  }
292
- this.cachedFlattenRouteList = routeArr;
293
280
  return routeArr;
294
281
  }
295
282
  async getMatchedRouterInfo(routerUrl, method) {
package/dist/setup.d.ts CHANGED
@@ -3,7 +3,7 @@ import { MidwayContainer, IMidwayBootstrapOptions, IMidwayContainer } from './';
3
3
  * midway framework main entry, this method bootstrap all service and framework.
4
4
  * @param globalOptions
5
5
  */
6
- export declare function initializeGlobalApplicationContext(globalOptions: IMidwayBootstrapOptions): Promise<IMidwayContainer | MidwayContainer>;
6
+ export declare function initializeGlobalApplicationContext(globalOptions: IMidwayBootstrapOptions): Promise<IMidwayContainer>;
7
7
  export declare function destroyGlobalApplicationContext(applicationContext: IMidwayContainer): Promise<void>;
8
8
  /**
9
9
  * prepare applicationContext, it use in egg framework.
@@ -19,7 +19,7 @@ async function makeHttpRequest(url, options = {}) {
19
19
  const client = whatwgUrl.protocol === 'https:' ? https : http;
20
20
  const contentType = options.contentType;
21
21
  const dataType = options.dataType;
22
- const method = options.method || 'GET';
22
+ const method = (options.method || 'GET').toUpperCase();
23
23
  const timeout = options.timeout || 5000;
24
24
  const headers = {
25
25
  Accept: mimeMap[dataType] || mimeMap.octet,
@@ -5,11 +5,6 @@ const util = require("util");
5
5
  const ToString = Function.prototype.toString;
6
6
  const hasOwn = Object.prototype.hasOwnProperty;
7
7
  const toStr = Object.prototype.toString;
8
- function fnBody(fn) {
9
- return ToString.call(fn)
10
- .replace(/^[^{]*{\s*/, '')
11
- .replace(/\s*}[^}]*$/, '');
12
- }
13
8
  function isString(value) {
14
9
  return typeof value === 'string';
15
10
  }
@@ -21,10 +16,6 @@ function isClass(fn) {
21
16
  if (/^class[\s{]/.test(ToString.call(fn))) {
22
17
  return true;
23
18
  }
24
- // babel.js classCallCheck() & inlined
25
- const body = fnBody(fn);
26
- return (/classCallCheck\(/.test(body) ||
27
- /TypeError\("Cannot call a class as a function"\)/.test(body));
28
19
  }
29
20
  exports.isClass = isClass;
30
21
  function isAsyncFunction(value) {
@@ -1,3 +1,5 @@
1
- export declare const extractKoaLikeValue: (key: any, data: any, paramType?: any) => (ctx: any, next: any) => any;
2
- export declare const extractExpressLikeValue: (key: any, data: any, paramType?: any) => (req: any, res: any, next: any) => any;
1
+ import { PipeTransform, PipeTransformFunction } from '../decorator';
2
+ export declare function callPipes(pipes: Array<PipeTransform | PipeTransformFunction>, value: any): Promise<any>;
3
+ export declare const extractKoaLikeValue: (key: any, data: any, paramType?: any, pipes?: PipeTransform[]) => (ctx: any, next: any) => Promise<any>;
4
+ export declare const extractExpressLikeValue: (key: any, data: any, paramType?: any, pipes?: PipeTransform[]) => (req: any, res: any, next: any) => Promise<any>;
3
5
  //# sourceMappingURL=webRouterParam.d.ts.map
@@ -1,13 +1,27 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.extractExpressLikeValue = exports.extractKoaLikeValue = void 0;
4
- const index_1 = require("./index");
3
+ exports.extractExpressLikeValue = exports.extractKoaLikeValue = exports.callPipes = void 0;
5
4
  const decorator_1 = require("../decorator");
6
- const extractKoaLikeValue = (key, data, paramType) => {
5
+ const index_1 = require("./index");
6
+ async function callPipes(pipes, value) {
7
+ if (pipes && pipes.length) {
8
+ for (const pipe of pipes) {
9
+ if ('transform' in pipe) {
10
+ value = await pipe.transform(value);
11
+ }
12
+ else {
13
+ value = await pipe(value);
14
+ }
15
+ }
16
+ }
17
+ return value;
18
+ }
19
+ exports.callPipes = callPipes;
20
+ const extractKoaLikeValue = (key, data, paramType, pipes) => {
7
21
  if (decorator_1.ALL === data) {
8
22
  data = undefined;
9
23
  }
10
- return function (ctx, next) {
24
+ const value = async function (ctx, next) {
11
25
  switch (key) {
12
26
  case decorator_1.RouteParamTypes.NEXT:
13
27
  return next;
@@ -54,17 +68,23 @@ const extractKoaLikeValue = (key, data, paramType) => {
54
68
  }
55
69
  case decorator_1.RouteParamTypes.FIELDS:
56
70
  return data ? ctx.fields[data] : ctx.fields;
71
+ case decorator_1.RouteParamTypes.CUSTOM:
72
+ return data ? data(ctx) : undefined;
57
73
  default:
58
74
  return null;
59
75
  }
60
76
  };
77
+ return async function (ctx, next) {
78
+ const result = await value(ctx, next);
79
+ return await callPipes(pipes || [], result);
80
+ };
61
81
  };
62
82
  exports.extractKoaLikeValue = extractKoaLikeValue;
63
- const extractExpressLikeValue = (key, data, paramType) => {
83
+ const extractExpressLikeValue = (key, data, paramType, pipes) => {
64
84
  if (decorator_1.ALL === data) {
65
85
  data = undefined;
66
86
  }
67
- return function (req, res, next) {
87
+ const value = (req, res, next) => {
68
88
  switch (key) {
69
89
  case decorator_1.RouteParamTypes.NEXT:
70
90
  return next;
@@ -95,10 +115,16 @@ const extractExpressLikeValue = (key, data, paramType) => {
95
115
  }
96
116
  case decorator_1.RouteParamTypes.FIELDS:
97
117
  return data ? req.fields[data] : req.fields;
118
+ case decorator_1.RouteParamTypes.CUSTOM:
119
+ return data ? data(req, res) : undefined;
98
120
  default:
99
121
  return null;
100
122
  }
101
123
  };
124
+ return async function (req, res, next) {
125
+ const result = await value(req, res, next);
126
+ return await callPipes(pipes || [], result);
127
+ };
102
128
  };
103
129
  exports.extractExpressLikeValue = extractExpressLikeValue;
104
130
  //# sourceMappingURL=webRouterParam.js.map
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@midwayjs/core",
3
- "version": "3.6.0",
3
+ "version": "3.8.0",
4
4
  "description": "midway core",
5
5
  "main": "dist/index",
6
6
  "typings": "dist/index.d.ts",
@@ -26,7 +26,7 @@
26
26
  "mm": "3.2.0",
27
27
  "pg": "8.8.0",
28
28
  "raw-body": "2.5.1",
29
- "sinon": "14.0.1"
29
+ "sinon": "14.0.2"
30
30
  },
31
31
  "dependencies": {
32
32
  "@midwayjs/glob": "^1.0.2",
@@ -38,10 +38,10 @@
38
38
  "author": "Harry Chen <czy88840616@gmail.com>",
39
39
  "repository": {
40
40
  "type": "git",
41
- "url": "http://github.com/midwayjs/midway.git"
41
+ "url": "https://github.com/midwayjs/midway.git"
42
42
  },
43
43
  "engines": {
44
44
  "node": ">=12"
45
45
  },
46
- "gitHead": "22643b0e8519766bb7c68b975930199fc136336e"
46
+ "gitHead": "5c640c7182923587139f9f9c0aecf50585798e1e"
47
47
  }