@midwayjs/core 3.11.1 → 3.11.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.
@@ -1,9 +1,34 @@
1
+ export interface CreateDataSourceInstanceOptions {
2
+ /**
3
+ * @default false
4
+ */
5
+ validateConnection?: boolean;
6
+ /**
7
+ * @default true
8
+ */
9
+ cacheInstance?: boolean | undefined;
10
+ }
11
+ interface DataSourceConfig extends CreateDataSourceInstanceOptions {
12
+ dataSource: {
13
+ entities?: Array<string | unknown>;
14
+ [key: string]: unknown;
15
+ };
16
+ }
1
17
  export declare abstract class DataSourceManager<T> {
2
18
  protected dataSource: Map<string, T>;
3
- protected options: {};
19
+ protected options: Partial<DataSourceConfig>;
4
20
  protected modelMapping: WeakMap<object, any>;
5
21
  private innerDefaultDataSourceName;
6
- protected initDataSource(options: any, appDir: string): Promise<void>;
22
+ protected initDataSource(dataSourceConfig: {
23
+ dataSource: {
24
+ entities?: Array<string | unknown>;
25
+ [key: string]: unknown;
26
+ };
27
+ cacheInstance?: boolean;
28
+ validateConnection?: boolean;
29
+ }, appDirOrOptions: {
30
+ appDir: string;
31
+ } | string): Promise<void>;
7
32
  /**
8
33
  * get a data source instance
9
34
  * @param dataSourceName
@@ -35,14 +60,5 @@ export declare abstract class DataSourceManager<T> {
35
60
  }
36
61
  export declare function formatGlobString(globString: string): string[];
37
62
  export declare function globModels(globString: string, appDir: string): any[];
38
- export interface CreateDataSourceInstanceOptions {
39
- /**
40
- * @default false
41
- */
42
- validateConnection?: boolean;
43
- /**
44
- * @default true
45
- */
46
- cacheInstance?: boolean | undefined;
47
- }
63
+ export {};
48
64
  //# sourceMappingURL=dataSourceManager.d.ts.map
@@ -10,43 +10,51 @@ const glob_1 = require("@midwayjs/glob");
10
10
  const path_1 = require("path");
11
11
  const types_1 = require("../util/types");
12
12
  const constants_1 = require("../constants");
13
+ const util_1 = require("util");
14
+ const debug = (0, util_1.debuglog)('midway:debug');
13
15
  class DataSourceManager {
14
16
  constructor() {
15
17
  this.dataSource = new Map();
16
18
  this.options = {};
17
19
  this.modelMapping = new WeakMap();
18
20
  }
19
- async initDataSource(options, appDir) {
20
- this.options = options;
21
- if (!options.dataSource) {
21
+ async initDataSource(dataSourceConfig, appDirOrOptions) {
22
+ this.options = dataSourceConfig;
23
+ if (!this.options.dataSource) {
22
24
  throw new error_1.MidwayParameterError('[DataSourceManager] must set options.dataSource.');
23
25
  }
24
- for (const dataSourceName in options.dataSource) {
25
- const dataSourceOptions = options.dataSource[dataSourceName];
26
+ if (typeof appDirOrOptions === 'string') {
27
+ appDirOrOptions = {
28
+ appDir: appDirOrOptions,
29
+ };
30
+ }
31
+ for (const dataSourceName in dataSourceConfig.dataSource) {
32
+ const dataSourceOptions = dataSourceConfig.dataSource[dataSourceName];
26
33
  if (dataSourceOptions['entities']) {
27
34
  const entities = new Set();
28
35
  // loop entities and glob files to model
29
36
  for (const entity of dataSourceOptions['entities']) {
30
37
  if (typeof entity === 'string') {
31
38
  // string will be glob file
32
- const models = globModels(entity, appDir);
39
+ const models = globModels(entity, appDirOrOptions.appDir);
33
40
  for (const model of models) {
34
41
  entities.add(model);
35
42
  this.modelMapping.set(model, dataSourceName);
36
43
  }
37
44
  }
38
45
  else {
39
- // model will be add to array
46
+ // model will be added to array
40
47
  entities.add(entity);
41
48
  this.modelMapping.set(entity, dataSourceName);
42
49
  }
43
50
  }
44
51
  dataSourceOptions['entities'] = Array.from(entities);
52
+ debug(`[core]: DataManager load ${dataSourceOptions['entities'].length} models from ${dataSourceName}.`);
45
53
  }
46
54
  // create data source
47
55
  const opts = {
48
- cacheInstance: options.cacheInstance,
49
- validateConnection: options.validateConnection,
56
+ cacheInstance: dataSourceConfig.cacheInstance,
57
+ validateConnection: dataSourceConfig.validateConnection,
50
58
  };
51
59
  await this.createInstance(dataSourceOptions, dataSourceName, opts);
52
60
  }
@@ -1,5 +1,5 @@
1
1
  import { FaaSMetadata, ServerlessTriggerType } from '../../interface';
2
- export declare function ServerlessFunction(options: FaaSMetadata.ServerlessFunctionOptions): MethodDecorator;
2
+ export declare function ServerlessFunction(options: FaaSMetadata.ServerlessFunctionOptions & Record<string, any>): MethodDecorator;
3
3
  export declare function ServerlessTrigger(type: ServerlessTriggerType.HTTP, metadata: FaaSMetadata.HTTPTriggerOptions): MethodDecorator;
4
4
  export declare function ServerlessTrigger(type: ServerlessTriggerType.OS, metadata: FaaSMetadata.OSTriggerOptions): MethodDecorator;
5
5
  export declare function ServerlessTrigger(type: ServerlessTriggerType.LOG, metadata: FaaSMetadata.LogTriggerOptions): MethodDecorator;
@@ -1,4 +1,4 @@
1
- import { IMidwayContext, PipeUnionTransform } from '../../interface';
1
+ import { IMidwayContext, ParamDecoratorOptions, PipeUnionTransform } from '../../interface';
2
2
  export declare enum RouteParamTypes {
3
3
  QUERY = "query",
4
4
  BODY = "body",
@@ -22,7 +22,7 @@ export interface RouterParamValue {
22
22
  export declare type KoaLikeCustomParamDecorator<T = unknown> = (ctx: IMidwayContext) => T | Promise<T>;
23
23
  export declare type ExpressLikeCustomParamDecorator<T = unknown> = (req: any, res: any) => T | Promise<T>;
24
24
  export declare type CustomParamDecorator<T = unknown> = KoaLikeCustomParamDecorator<T> | ExpressLikeCustomParamDecorator<T>;
25
- export declare const createRequestParamDecorator: (transform: CustomParamDecorator, pipes?: Array<PipeUnionTransform>) => ParameterDecorator;
25
+ export declare const createRequestParamDecorator: (transform: CustomParamDecorator, pipesOrOptions?: ParamDecoratorOptions | Array<PipeUnionTransform>) => ParameterDecorator;
26
26
  export declare const Session: (propertyOrPipes?: string | PipeUnionTransform[], pipes?: PipeUnionTransform[]) => ParameterDecorator;
27
27
  export declare const Body: (propertyOrPipes?: string | PipeUnionTransform[], pipes?: PipeUnionTransform[]) => ParameterDecorator;
28
28
  export declare const Query: (propertyOrPipes?: string | PipeUnionTransform[], pipes?: PipeUnionTransform[]) => ParameterDecorator;
@@ -33,8 +33,14 @@ const createParamMapping = function (type) {
33
33
  });
34
34
  };
35
35
  };
36
- const createRequestParamDecorator = function (transform, pipes) {
37
- return createParamMapping(RouteParamTypes.CUSTOM)(transform, pipes);
36
+ const createRequestParamDecorator = function (transform, pipesOrOptions) {
37
+ pipesOrOptions = pipesOrOptions || {};
38
+ if (Array.isArray(pipesOrOptions)) {
39
+ pipesOrOptions = {
40
+ pipes: pipesOrOptions,
41
+ };
42
+ }
43
+ return createParamMapping(RouteParamTypes.CUSTOM)(transform, pipesOrOptions.pipes);
38
44
  };
39
45
  exports.createRequestParamDecorator = createRequestParamDecorator;
40
46
  const Session = (propertyOrPipes, pipes) => createParamMapping(RouteParamTypes.SESSION)(propertyOrPipes, pipes);
@@ -3,6 +3,43 @@ import type { LoggerOptions, LoggerContextFormat } from '@midwayjs/logger';
3
3
  import * as EventEmitter from 'events';
4
4
  import type { AsyncContextManager } from './common/asyncContextManager';
5
5
  import type { LoggerFactory } from './common/loggerFactory';
6
+ export type PowerPartial<T> = {
7
+ [U in keyof T]?: T[U] extends {} ? PowerPartial<T[U]> : T[U];
8
+ };
9
+ /**
10
+ * Make object property writeable
11
+ */
12
+ export type Writable<T> = {
13
+ -readonly [P in keyof T]: T[P];
14
+ };
15
+ /**
16
+ * Utility type that adds a `fn` parameter to each method in the input type `T`,
17
+ * transforming the original method's parameter types and return type into a function type.
18
+ *
19
+ * @example
20
+ * // Input:
21
+ * interface MyInterface {
22
+ * method1(a: string, b: number): boolean;
23
+ * method2(x: Foo, y: Bar): void;
24
+ * }
25
+ *
26
+ * // Output:
27
+ * interface MyInterfaceWithFn {
28
+ * method1(fn: (a: string, b: number) => boolean): void;
29
+ * method2(fn: (x: Foo, y: Bar) => void): void;
30
+ * }
31
+ */
32
+ export type WithFn<T> = {
33
+ [K in keyof T]: T[K] extends (...args: infer P) => infer R ? (fn: (...args: P) => R) => void : T[K];
34
+ };
35
+ /**
36
+ * Transform an object type `T` with methods that have function-type parameters
37
+ * to a new object type with the same methods, but with the parameters
38
+ * extracted as separate properties.
39
+ */
40
+ export type WithoutFn<T> = {
41
+ [K in keyof T]: T[K] extends (arg: any, ...args: any[]) => any ? (...args: Parameters<T[K]>) => ReturnType<T[K]> : T[K];
42
+ };
6
43
  export type MiddlewareParamArray = Array<string | any>;
7
44
  export type ObjectIdentifier = string | Symbol;
8
45
  export type GroupModeType = 'one' | 'multi';
@@ -323,9 +360,6 @@ export interface MidwayCoreDefaultConfig {
323
360
  enable: boolean;
324
361
  };
325
362
  }
326
- export type PowerPartial<T> = {
327
- [U in keyof T]?: T[U] extends {} ? PowerPartial<T[U]> : T[U];
328
- };
329
363
  export type ServiceFactoryConfigOption<OPTIONS> = {
330
364
  default?: PowerPartial<OPTIONS>;
331
365
  client?: PowerPartial<OPTIONS>;
@@ -348,12 +382,6 @@ type ConfigType<T> = T extends (...args: any[]) => any ? Writable<PowerPartial<R
348
382
  * Get definition from config
349
383
  */
350
384
  export type FileConfigOption<T, K = unknown> = K extends keyof ConfigType<T> ? Pick<ConfigType<T>, K> : ConfigType<T>;
351
- /**
352
- * Make object property writeable
353
- */
354
- export type Writable<T> = {
355
- -readonly [P in keyof T]: T[P];
356
- };
357
385
  /**
358
386
  * Lifecycle Definition
359
387
  * 生命周期定义
@@ -407,11 +435,11 @@ export interface ObjectBeforeDestroyOptions extends ObjectLifeCycleOptions {
407
435
  * 对象生命周期
408
436
  */
409
437
  export interface IObjectLifeCycle {
410
- onBeforeBind(fn: (Clzz: any, options: ObjectBeforeBindOptions) => void): any;
411
- onBeforeObjectCreated(fn: (Clzz: any, options: ObjectBeforeCreatedOptions) => void): any;
412
- onObjectCreated<T>(fn: (ins: T, options: ObjectCreatedOptions<T>) => void): any;
413
- onObjectInit<T>(fn: (ins: T, options: ObjectInitOptions) => void): any;
414
- onBeforeObjectDestroy<T>(fn: (ins: T, options: ObjectBeforeDestroyOptions) => void): any;
438
+ onBeforeBind(Clzz: any, options: ObjectBeforeBindOptions): void;
439
+ onBeforeObjectCreated(Clzz: any, options: ObjectBeforeCreatedOptions): void;
440
+ onObjectCreated<T>(ins: T, options: ObjectCreatedOptions<T>): void;
441
+ onObjectInit<T>(ins: T, options: ObjectInitOptions): void;
442
+ onBeforeObjectDestroy<T>(ins: T, options: ObjectBeforeDestroyOptions): void;
415
443
  }
416
444
  /**
417
445
  * Object Definition
@@ -537,7 +565,7 @@ export interface IIdentifierRelationShip {
537
565
  hasRelation(id: ObjectIdentifier): boolean;
538
566
  getRelation(id: ObjectIdentifier): string;
539
567
  }
540
- export interface IMidwayContainer extends IObjectFactory, IObjectLifeCycle {
568
+ export interface IMidwayContainer extends IObjectFactory, WithFn<IObjectLifeCycle> {
541
569
  parent: IMidwayContainer;
542
570
  identifierMapping: IIdentifierRelationShip;
543
571
  objectCreateEventTarget: EventEmitter;
@@ -1,9 +1,11 @@
1
1
  /// <reference types="node" />
2
2
  /// <reference types="node" />
3
+ /// <reference types="node" />
3
4
  import http = require('http');
5
+ import https = require('https');
4
6
  type MethodType = 'GET' | 'POST';
5
7
  type MimeType = 'text' | 'json' | undefined;
6
- interface IOptions {
8
+ interface IOptions extends https.RequestOptions {
7
9
  method?: MethodType;
8
10
  headers?: any;
9
11
  contentType?: MimeType;
@@ -22,10 +22,8 @@ async function makeHttpRequest(url, options = {}) {
22
22
  debug(`request '${url}'`);
23
23
  const whatwgUrl = new URL(url);
24
24
  const client = whatwgUrl.protocol === 'https:' ? https : http;
25
- const contentType = options.contentType;
26
- const dataType = options.dataType;
27
- const method = (options.method || 'GET').toUpperCase();
28
- const timeout = options.timeout || 5000;
25
+ options.method = (options.method || 'GET').toUpperCase();
26
+ const { contentType, dataType, method, timeout = 5000, ...otherOptions } = options;
29
27
  const headers = {
30
28
  Accept: mimeMap[dataType] || mimeMap.octet,
31
29
  ...options.headers,
@@ -50,6 +48,7 @@ async function makeHttpRequest(url, options = {}) {
50
48
  const req = client.request(whatwgUrl.toString(), {
51
49
  method,
52
50
  headers,
51
+ ...otherOptions,
53
52
  }, res => {
54
53
  res.setTimeout(timeout, () => {
55
54
  res.destroy(new Error('Response Timeout'));
@@ -18,6 +18,7 @@ export declare const getCurrentEnvironment: () => string;
18
18
  */
19
19
  export declare const safeRequire: (p: any, enabledCache?: boolean) => any;
20
20
  /**
21
+ * @example
21
22
  * safelyGet(['a','b'],{a: {b: 2}}) // => 2
22
23
  * safelyGet(['a','b'],{c: {b: 2}}) // => undefined
23
24
  * safelyGet(['a','1'],{a: {"1": 2}}) // => 2
@@ -55,6 +55,7 @@ const safeRequire = (p, enabledCache = true) => {
55
55
  };
56
56
  exports.safeRequire = safeRequire;
57
57
  /**
58
+ * @example
58
59
  * safelyGet(['a','b'],{a: {b: 2}}) // => 2
59
60
  * safelyGet(['a','b'],{c: {b: 2}}) // => undefined
60
61
  * safelyGet(['a','1'],{a: {"1": 2}}) // => 2
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@midwayjs/core",
3
- "version": "3.11.1",
3
+ "version": "3.11.6",
4
4
  "description": "midway core",
5
5
  "main": "dist/index",
6
6
  "typings": "dist/index.d.ts",
@@ -23,9 +23,9 @@
23
23
  "license": "MIT",
24
24
  "devDependencies": {
25
25
  "koa": "2.14.1",
26
- "mm": "3.2.1",
26
+ "mm": "3.2.2",
27
27
  "raw-body": "2.5.2",
28
- "sinon": "15.0.3"
28
+ "sinon": "15.0.4"
29
29
  },
30
30
  "dependencies": {
31
31
  "@midwayjs/glob": "^1.0.2",
@@ -42,5 +42,5 @@
42
42
  "engines": {
43
43
  "node": ">=12"
44
44
  },
45
- "gitHead": "bd9375874eb8cfaa49fbcfaa0497021cea06a394"
45
+ "gitHead": "d1ca8ca3ee5223c1c034ad3c97d335348931404b"
46
46
  }