@midwayjs/decorator 3.0.0-beta.16 → 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.
@@ -1,5 +1,7 @@
1
1
  import { ScopeEnum } from '../../index';
2
2
  export declare function Init(): MethodDecorator;
3
3
  export declare function Destroy(): MethodDecorator;
4
- export declare function Scope(scope: ScopeEnum): ClassDecorator;
4
+ export declare function Scope(scope: ScopeEnum, scopeOptions?: {
5
+ allowDowngrade?: boolean;
6
+ }): ClassDecorator;
5
7
  //# sourceMappingURL=objectDef.d.ts.map
@@ -16,9 +16,9 @@ function Destroy() {
16
16
  };
17
17
  }
18
18
  exports.Destroy = Destroy;
19
- function Scope(scope) {
19
+ function Scope(scope, scopeOptions) {
20
20
  return function (target) {
21
- (0, index_1.saveObjectDefinition)(target, { scope });
21
+ (0, index_1.saveObjectDefinition)(target, { scope, ...scopeOptions });
22
22
  };
23
23
  }
24
24
  exports.Scope = Scope;
package/dist/index.d.ts CHANGED
@@ -2,7 +2,7 @@ export * from './decorator';
2
2
  export * from './interface';
3
3
  export * from './constant';
4
4
  export * from './decoratorManager';
5
- export { TYPES, sleep, getParamNames, generateRandomId, merge, } from './util/index';
5
+ export { Types, Utils, sleep } from './util/index';
6
+ export { FileUtils } from './util/fs';
6
7
  export * from './util/format';
7
- export { camelCase, pascalCase } from './util/camelCase';
8
8
  //# sourceMappingURL=index.d.ts.map
package/dist/index.js CHANGED
@@ -10,19 +10,16 @@ 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.pascalCase = exports.camelCase = exports.merge = exports.generateRandomId = exports.getParamNames = exports.sleep = exports.TYPES = void 0;
13
+ exports.FileUtils = exports.sleep = exports.Utils = exports.Types = void 0;
14
14
  __exportStar(require("./decorator"), exports);
15
15
  __exportStar(require("./interface"), exports);
16
16
  __exportStar(require("./constant"), exports);
17
17
  __exportStar(require("./decoratorManager"), exports);
18
18
  var index_1 = require("./util/index");
19
- Object.defineProperty(exports, "TYPES", { enumerable: true, get: function () { return index_1.TYPES; } });
19
+ Object.defineProperty(exports, "Types", { enumerable: true, get: function () { return index_1.Types; } });
20
+ Object.defineProperty(exports, "Utils", { enumerable: true, get: function () { return index_1.Utils; } });
20
21
  Object.defineProperty(exports, "sleep", { enumerable: true, get: function () { return index_1.sleep; } });
21
- Object.defineProperty(exports, "getParamNames", { enumerable: true, get: function () { return index_1.getParamNames; } });
22
- Object.defineProperty(exports, "generateRandomId", { enumerable: true, get: function () { return index_1.generateRandomId; } });
23
- Object.defineProperty(exports, "merge", { enumerable: true, get: function () { return index_1.merge; } });
22
+ var fs_1 = require("./util/fs");
23
+ Object.defineProperty(exports, "FileUtils", { enumerable: true, get: function () { return fs_1.FileUtils; } });
24
24
  __exportStar(require("./util/format"), exports);
25
- var camelCase_1 = require("./util/camelCase");
26
- Object.defineProperty(exports, "camelCase", { enumerable: true, get: function () { return camelCase_1.camelCase; } });
27
- Object.defineProperty(exports, "pascalCase", { enumerable: true, get: function () { return camelCase_1.pascalCase; } });
28
25
  //# sourceMappingURL=index.js.map
@@ -22,6 +22,7 @@ export interface ObjectDefinitionOptions {
22
22
  constructorArgs?: any[];
23
23
  namespace?: string;
24
24
  srcPath?: string;
25
+ allowDowngrade?: boolean;
25
26
  }
26
27
  export interface TagPropsMetadata {
27
28
  key: string | number | symbol;
@@ -0,0 +1,5 @@
1
+ export declare function exists(p: any): Promise<boolean>;
2
+ export declare const FileUtils: {
3
+ exists: typeof exists;
4
+ };
5
+ //# sourceMappingURL=fs.d.ts.map
@@ -0,0 +1,15 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.FileUtils = exports.exists = void 0;
4
+ const fs_1 = require("fs");
5
+ async function exists(p) {
6
+ return fs_1.promises
7
+ .access(p, fs_1.constants.F_OK)
8
+ .then(() => true)
9
+ .catch(() => false);
10
+ }
11
+ exports.exists = exists;
12
+ exports.FileUtils = {
13
+ exists,
14
+ };
15
+ //# sourceMappingURL=fs.js.map
@@ -1,9 +1,13 @@
1
+ import { camelCase, pascalCase } from './camelCase';
2
+ import { randomUUID } from './uuid';
3
+ export declare function isString(value: any): boolean;
1
4
  export declare function isClass(fn: any): boolean;
2
5
  export declare function isAsyncFunction(value: any): boolean;
3
6
  export declare function isGeneratorFunction(value: any): boolean;
4
7
  export declare function isPromise(value: any): boolean;
5
8
  export declare function isFunction(value: any): boolean;
6
9
  export declare function isObject(value: any): boolean;
10
+ export declare function isPlainObject(obj: any): any;
7
11
  export declare function isNumber(value: any): boolean;
8
12
  export declare function isProxy(value: any): boolean;
9
13
  export declare function isMap(value: any): boolean;
@@ -19,17 +23,20 @@ export declare function sleep(sleepTime?: number): Promise<void>;
19
23
  */
20
24
  export declare function getParamNames(func: any): string[];
21
25
  /**
22
- * generate a lightweight random id, enough for ioc container
26
+ * generate a lightweight 32 bit random id, enough for ioc container
23
27
  */
24
28
  export declare function generateRandomId(): string;
25
29
  export declare function merge(target: any, src: any): any;
26
- export declare const TYPES: {
30
+ export declare function toAsyncFunction<T extends (...args: any[]) => any>(method: T): (...args: Parameters<T>) => Promise<ReturnType<T>>;
31
+ export declare const Types: {
27
32
  isClass: typeof isClass;
28
33
  isAsyncFunction: typeof isAsyncFunction;
29
34
  isGeneratorFunction: typeof isGeneratorFunction;
35
+ isString: typeof isString;
30
36
  isPromise: typeof isPromise;
31
37
  isFunction: typeof isFunction;
32
38
  isObject: typeof isObject;
39
+ isPlainObject: typeof isPlainObject;
33
40
  isNumber: typeof isNumber;
34
41
  isProxy: typeof isProxy;
35
42
  isMap: typeof isMap;
@@ -39,4 +46,14 @@ export declare const TYPES: {
39
46
  isNull: typeof isNull;
40
47
  isNullOrUndefined: typeof isNullOrUndefined;
41
48
  };
49
+ export declare const Utils: {
50
+ sleep: typeof sleep;
51
+ getParamNames: typeof getParamNames;
52
+ generateRandomId: typeof generateRandomId;
53
+ merge: typeof merge;
54
+ camelCase: typeof camelCase;
55
+ pascalCase: typeof pascalCase;
56
+ randomUUID: typeof randomUUID;
57
+ toAsyncFunction: typeof toAsyncFunction;
58
+ };
42
59
  //# sourceMappingURL=index.d.ts.map
@@ -1,14 +1,22 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.TYPES = exports.merge = exports.generateRandomId = exports.getParamNames = exports.sleep = exports.isNullOrUndefined = exports.isNull = exports.isUndefined = exports.isRegExp = exports.isSet = exports.isMap = exports.isProxy = exports.isNumber = exports.isObject = exports.isFunction = exports.isPromise = exports.isGeneratorFunction = exports.isAsyncFunction = exports.isClass = void 0;
3
+ exports.Utils = exports.Types = exports.toAsyncFunction = exports.merge = exports.generateRandomId = exports.getParamNames = exports.sleep = exports.isNullOrUndefined = exports.isNull = exports.isUndefined = exports.isRegExp = exports.isSet = exports.isMap = exports.isProxy = exports.isNumber = exports.isPlainObject = exports.isObject = exports.isFunction = exports.isPromise = exports.isGeneratorFunction = exports.isAsyncFunction = exports.isClass = exports.isString = void 0;
4
4
  const util = require("util");
5
5
  const crypto = require("crypto");
6
+ const camelCase_1 = require("./camelCase");
7
+ const uuid_1 = require("./uuid");
6
8
  const ToString = Function.prototype.toString;
9
+ const hasOwn = Object.prototype.hasOwnProperty;
10
+ const toStr = Object.prototype.toString;
7
11
  function fnBody(fn) {
8
12
  return ToString.call(fn)
9
13
  .replace(/^[^{]*{\s*/, '')
10
14
  .replace(/\s*}[^}]*$/, '');
11
15
  }
16
+ function isString(value) {
17
+ return typeof value === 'string';
18
+ }
19
+ exports.isString = isString;
12
20
  function isClass(fn) {
13
21
  if (typeof fn !== 'function') {
14
22
  return false;
@@ -42,6 +50,27 @@ function isObject(value) {
42
50
  return value !== null && typeof value === 'object';
43
51
  }
44
52
  exports.isObject = isObject;
53
+ function isPlainObject(obj) {
54
+ if (!obj || toStr.call(obj) !== '[object Object]') {
55
+ return false;
56
+ }
57
+ const hasOwnConstructor = hasOwn.call(obj, 'constructor');
58
+ const hasIsPrototypeOf = obj.constructor &&
59
+ obj.constructor.prototype &&
60
+ hasOwn.call(obj.constructor.prototype, 'isPrototypeOf');
61
+ // Not own constructor property must be Object
62
+ if (obj.constructor && !hasOwnConstructor && !hasIsPrototypeOf) {
63
+ return false;
64
+ }
65
+ // Own properties are enumerated firstly, so to speed up,
66
+ // if last one is own, then all properties are own.
67
+ let key;
68
+ for (key in obj) {
69
+ /**/
70
+ }
71
+ return typeof key === 'undefined' || hasOwn.call(obj, key);
72
+ }
73
+ exports.isPlainObject = isPlainObject;
45
74
  function isNumber(value) {
46
75
  return typeof value === 'number';
47
76
  }
@@ -102,7 +131,7 @@ function getParamNames(func) {
102
131
  }
103
132
  exports.getParamNames = getParamNames;
104
133
  /**
105
- * generate a lightweight random id, enough for ioc container
134
+ * generate a lightweight 32 bit random id, enough for ioc container
106
135
  */
107
136
  function generateRandomId() {
108
137
  // => f9b327e70bbcf42494ccb28b2d98e00e
@@ -126,13 +155,26 @@ function merge(target, src) {
126
155
  throw new Error('can not merge meta that type of ' + typeof target);
127
156
  }
128
157
  exports.merge = merge;
129
- exports.TYPES = {
158
+ function toAsyncFunction(method) {
159
+ if (isAsyncFunction(method)) {
160
+ return method;
161
+ }
162
+ else {
163
+ return async function (...args) {
164
+ return Promise.resolve(method.call(this, ...args));
165
+ };
166
+ }
167
+ }
168
+ exports.toAsyncFunction = toAsyncFunction;
169
+ exports.Types = {
130
170
  isClass,
131
171
  isAsyncFunction,
132
172
  isGeneratorFunction,
173
+ isString,
133
174
  isPromise,
134
175
  isFunction,
135
176
  isObject,
177
+ isPlainObject,
136
178
  isNumber,
137
179
  isProxy,
138
180
  isMap,
@@ -142,4 +184,14 @@ exports.TYPES = {
142
184
  isNull,
143
185
  isNullOrUndefined,
144
186
  };
187
+ exports.Utils = {
188
+ sleep,
189
+ getParamNames,
190
+ generateRandomId,
191
+ merge,
192
+ camelCase: camelCase_1.camelCase,
193
+ pascalCase: camelCase_1.pascalCase,
194
+ randomUUID: uuid_1.randomUUID,
195
+ toAsyncFunction,
196
+ };
145
197
  //# sourceMappingURL=index.js.map
@@ -0,0 +1,5 @@
1
+ /**
2
+ * a easy uuid v4 generator
3
+ */
4
+ export declare function randomUUID(): string;
5
+ //# sourceMappingURL=uuid.d.ts.map
@@ -0,0 +1,64 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.randomUUID = void 0;
4
+ const crypto = require("crypto");
5
+ /**
6
+ * code fork from https://github.com/uuidjs/uuid/blob/main/src/stringify.js
7
+ */
8
+ const rnds8Pool = new Uint8Array(256); // # of random values to pre-allocate
9
+ let poolPtr = rnds8Pool.length;
10
+ function rng() {
11
+ if (poolPtr > rnds8Pool.length - 16) {
12
+ crypto.randomFillSync(rnds8Pool);
13
+ poolPtr = 0;
14
+ }
15
+ return rnds8Pool.slice(poolPtr, (poolPtr += 16));
16
+ }
17
+ /**
18
+ * Convert array of 16 byte values to UUID string format of the form:
19
+ * XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX
20
+ */
21
+ const byteToHex = [];
22
+ for (let i = 0; i < 256; ++i) {
23
+ byteToHex.push((i + 0x100).toString(16).substr(1));
24
+ }
25
+ function unsafeStringify(arr, offset = 0) {
26
+ // Note: Be careful editing this code! It's been tuned for performance
27
+ // and works in ways you may not expect. See https://github.com/uuidjs/uuid/pull/434
28
+ return (byteToHex[arr[offset + 0]] +
29
+ byteToHex[arr[offset + 1]] +
30
+ byteToHex[arr[offset + 2]] +
31
+ byteToHex[arr[offset + 3]] +
32
+ '-' +
33
+ byteToHex[arr[offset + 4]] +
34
+ byteToHex[arr[offset + 5]] +
35
+ '-' +
36
+ byteToHex[arr[offset + 6]] +
37
+ byteToHex[arr[offset + 7]] +
38
+ '-' +
39
+ byteToHex[arr[offset + 8]] +
40
+ byteToHex[arr[offset + 9]] +
41
+ '-' +
42
+ byteToHex[arr[offset + 10]] +
43
+ byteToHex[arr[offset + 11]] +
44
+ byteToHex[arr[offset + 12]] +
45
+ byteToHex[arr[offset + 13]] +
46
+ byteToHex[arr[offset + 14]] +
47
+ byteToHex[arr[offset + 15]]).toLowerCase();
48
+ }
49
+ /**
50
+ * a easy uuid v4 generator
51
+ */
52
+ function randomUUID() {
53
+ // node > v14.17
54
+ if (crypto['randomUUID']) {
55
+ return crypto['randomUUID']();
56
+ }
57
+ const rnds = rng();
58
+ // Per 4.4, set bits for version and `clock_seq_hi_and_reserved`
59
+ rnds[6] = (rnds[6] & 0x0f) | 0x40;
60
+ rnds[8] = (rnds[8] & 0x3f) | 0x80;
61
+ return unsafeStringify(rnds);
62
+ }
63
+ exports.randomUUID = randomUUID;
64
+ //# sourceMappingURL=uuid.js.map
package/package.json CHANGED
@@ -1,13 +1,13 @@
1
1
  {
2
2
  "name": "@midwayjs/decorator",
3
- "version": "3.0.0-beta.16",
3
+ "version": "3.0.0-beta.17",
4
4
  "description": "definition decorator for midway project",
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
  "ci": "npm run test",
12
12
  "link": "npm link"
13
13
  },
@@ -36,5 +36,5 @@
36
36
  "publishConfig": {
37
37
  "access": "public"
38
38
  },
39
- "gitHead": "43a32745ac45164dfef1494cba979f9652a6692b"
39
+ "gitHead": "17a8b5bd3d0b0b21f24dd2f165b5df9097fc3ec4"
40
40
  }
@@ -1 +0,0 @@
1
- //# sourceMappingURL=decide.d.ts.map
@@ -1,36 +0,0 @@
1
- // import { isAsyncFunction } from '../util';
2
- //
3
- // export function OnUndefined(data: Error | number) {
4
- // return (target, key, descriptor: PropertyDescriptor) => {
5
- // const oldMethod = descriptor.value;
6
- // if (isAsyncFunction(oldMethod)) {
7
- // descriptor.value = async (...args) => {
8
- // const result = await oldMethod(...args);
9
- // if(typeof result === 'undefined') {
10
- //
11
- // }
12
- // };
13
- // } else {
14
- // descriptor.value = () => {
15
- //
16
- // };
17
- // }
18
- //
19
- // return descriptor;
20
- // };
21
- // }
22
- //
23
- // export function OnNull(data: Error | number) {
24
- // return (target, key, descriptor: PropertyDescriptor) => {
25
- //
26
- // return descriptor;
27
- // };
28
- // }
29
- //
30
- // export function OnEmpty(data: Error | number) {
31
- // return (target, key, descriptor: PropertyDescriptor) => {
32
- //
33
- // return descriptor;
34
- // };
35
- // }
36
- //# sourceMappingURL=decide.js.map