@midwayjs/decorator 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.
@@ -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;
@@ -22,10 +22,12 @@ export interface RouterOption {
22
22
  middleware?: MiddlewareParamArray;
23
23
  /**
24
24
  * router summary, for swagger
25
+ * @deprecated
25
26
  */
26
27
  summary?: string;
27
28
  /**
28
29
  * router description, for swagger
30
+ * @deprecated
29
31
  */
30
32
  description?: string;
31
33
  /**
@@ -4,6 +4,7 @@ exports.createCustomParamDecorator = exports.createCustomMethodDecorator = expor
4
4
  require("reflect-metadata");
5
5
  const constant_1 = require("./constant");
6
6
  const util_1 = require("./util");
7
+ const camelCase_1 = require("./util/camelCase");
7
8
  const debug = require('util').debuglog('midway:decorator');
8
9
  exports.PRELOAD_MODULE_KEY = 'INJECTION_PRELOAD_MODULE_KEY';
9
10
  exports.INJECT_CLASS_KEY_PREFIX = 'INJECTION_CLASS_META_DATA';
@@ -565,7 +566,7 @@ function saveProviderId(identifier, target) {
565
566
  id: identifier,
566
567
  originName: target.name,
567
568
  uuid,
568
- name: (0, util_1.classNamed)(target.name),
569
+ name: (0, camelCase_1.camelCase)(target.name),
569
570
  }, target);
570
571
  debug(`save provide: ${target.name} -> ${uuid}`);
571
572
  }
package/dist/index.d.ts CHANGED
@@ -2,6 +2,7 @@ export * from './decorator';
2
2
  export * from './interface';
3
3
  export * from './constant';
4
4
  export * from './decoratorManager';
5
- export * from './util/index';
5
+ export { Types, Utils, sleep } from './util/index';
6
+ export { FileUtils } from './util/fs';
6
7
  export * from './util/format';
7
8
  //# sourceMappingURL=index.d.ts.map
package/dist/index.js CHANGED
@@ -10,10 +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.FileUtils = exports.sleep = exports.Utils = exports.Types = void 0;
13
14
  __exportStar(require("./decorator"), exports);
14
15
  __exportStar(require("./interface"), exports);
15
16
  __exportStar(require("./constant"), exports);
16
17
  __exportStar(require("./decoratorManager"), exports);
17
- __exportStar(require("./util/index"), exports);
18
+ var index_1 = require("./util/index");
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; } });
21
+ Object.defineProperty(exports, "sleep", { enumerable: true, get: function () { return index_1.sleep; } });
22
+ var fs_1 = require("./util/fs");
23
+ Object.defineProperty(exports, "FileUtils", { enumerable: true, get: function () { return fs_1.FileUtils; } });
18
24
  __exportStar(require("./util/format"), exports);
19
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,3 @@
1
+ export declare function camelCase(input: string): string;
2
+ export declare function pascalCase(input: string): string;
3
+ //# sourceMappingURL=camelCase.d.ts.map
@@ -0,0 +1,88 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.pascalCase = exports.camelCase = void 0;
4
+ const UPPERCASE = /[\p{Lu}]/u;
5
+ const LOWERCASE = /[\p{Ll}]/u;
6
+ const IDENTIFIER = /([\p{Alpha}\p{N}_]|$)/u;
7
+ const SEPARATORS = /[_.\- ]+/;
8
+ const LEADING_SEPARATORS = new RegExp('^' + SEPARATORS.source);
9
+ const SEPARATORS_AND_IDENTIFIER = new RegExp(SEPARATORS.source + IDENTIFIER.source, 'gu');
10
+ const NUMBERS_AND_IDENTIFIER = new RegExp('\\d+' + IDENTIFIER.source, 'gu');
11
+ const preserveCamelCase = (string, toLowerCase, toUpperCase) => {
12
+ let isLastCharLower = false;
13
+ let isLastCharUpper = false;
14
+ let isLastLastCharUpper = false;
15
+ for (let i = 0; i < string.length; i++) {
16
+ const character = string[i];
17
+ if (isLastCharLower && UPPERCASE.test(character)) {
18
+ string = string.slice(0, i) + '-' + string.slice(i);
19
+ isLastCharLower = false;
20
+ isLastLastCharUpper = isLastCharUpper;
21
+ isLastCharUpper = true;
22
+ i++;
23
+ }
24
+ else if (isLastCharUpper &&
25
+ isLastLastCharUpper &&
26
+ LOWERCASE.test(character)) {
27
+ string = string.slice(0, i - 1) + '-' + string.slice(i - 1);
28
+ isLastLastCharUpper = isLastCharUpper;
29
+ isLastCharUpper = false;
30
+ isLastCharLower = true;
31
+ }
32
+ else {
33
+ isLastCharLower =
34
+ toLowerCase(character) === character &&
35
+ toUpperCase(character) !== character;
36
+ isLastLastCharUpper = isLastCharUpper;
37
+ isLastCharUpper =
38
+ toUpperCase(character) === character &&
39
+ toLowerCase(character) !== character;
40
+ }
41
+ }
42
+ return string;
43
+ };
44
+ const postProcess = (input, toUpperCase) => {
45
+ SEPARATORS_AND_IDENTIFIER.lastIndex = 0;
46
+ NUMBERS_AND_IDENTIFIER.lastIndex = 0;
47
+ return input
48
+ .replace(SEPARATORS_AND_IDENTIFIER, (_, identifier) => toUpperCase(identifier))
49
+ .replace(NUMBERS_AND_IDENTIFIER, m => toUpperCase(m));
50
+ };
51
+ function camelCaseOrigin(input, options) {
52
+ options = {
53
+ pascalCase: false,
54
+ ...options,
55
+ };
56
+ input = input.trim();
57
+ if (input.length === 0) {
58
+ return '';
59
+ }
60
+ const toLowerCase = string => string.toLowerCase();
61
+ const toUpperCase = string => string.toUpperCase();
62
+ if (input.length === 1) {
63
+ return options.pascalCase ? toUpperCase(input) : toLowerCase(input);
64
+ }
65
+ const hasUpperCase = input !== toLowerCase(input);
66
+ if (hasUpperCase) {
67
+ input = preserveCamelCase(input, toLowerCase, toUpperCase);
68
+ }
69
+ input = input.replace(LEADING_SEPARATORS, '');
70
+ input = toLowerCase(input);
71
+ if (options.pascalCase) {
72
+ input = toUpperCase(input.charAt(0)) + input.slice(1);
73
+ }
74
+ return postProcess(input, toUpperCase);
75
+ }
76
+ function camelCase(input) {
77
+ return camelCaseOrigin(input, {
78
+ pascalCase: false,
79
+ });
80
+ }
81
+ exports.camelCase = camelCase;
82
+ function pascalCase(input) {
83
+ return camelCaseOrigin(input, {
84
+ pascalCase: true,
85
+ });
86
+ }
87
+ exports.pascalCase = pascalCase;
88
+ //# sourceMappingURL=camelCase.js.map
@@ -18,18 +18,18 @@ const MS = {
18
18
  };
19
19
  // crontab pre format
20
20
  const CRONTAB = {
21
- EVERY_SECOND: '* * * * * ?',
22
- EVERY_MINUTE: '0 * * * * ?',
23
- EVERY_HOUR: '0 0 * * * ?',
24
- EVERY_DAY: '0 0 0 * * ?',
25
- EVERY_DAY_ZERO_FIFTEEN: '0 15 0 * * ?',
26
- EVERY_DAY_ONE_FIFTEEN: '0 15 1 * * ?',
27
- EVERY_PER_5_SECOND: '/5 * * * * ?',
28
- EVERY_PER_10_SECOND: '/10 * * * * ?',
29
- EVERY_PER_30_SECOND: '/30 * * * * ?',
30
- EVERY_PER_5_MINUTE: '0 /5 * * * ?',
31
- EVERY_PER_10_MINUTE: '0 /10 * * * ?',
32
- EVERY_PER_30_MINUTE: '0 /30 * * * ?',
21
+ EVERY_SECOND: '* * * * * *',
22
+ EVERY_MINUTE: '0 * * * * *',
23
+ EVERY_HOUR: '0 0 * * * *',
24
+ EVERY_DAY: '0 0 0 * * *',
25
+ EVERY_DAY_ZERO_FIFTEEN: '0 15 0 * * *',
26
+ EVERY_DAY_ONE_FIFTEEN: '0 15 1 * * *',
27
+ EVERY_PER_5_SECOND: '*/5 * * * * *',
28
+ EVERY_PER_10_SECOND: '*/10 * * * * *',
29
+ EVERY_PER_30_SECOND: '*/30 * * * * *',
30
+ EVERY_PER_5_MINUTE: '0 */5 * * * *',
31
+ EVERY_PER_10_MINUTE: '0 */10 * * * *',
32
+ EVERY_PER_30_MINUTE: '0 */30 * * * *',
33
33
  };
34
34
  exports.FORMAT = {
35
35
  MS,
@@ -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,13 +23,37 @@ export declare function sleep(sleepTime?: number): Promise<void>;
19
23
  */
20
24
  export declare function getParamNames(func: any): string[];
21
25
  /**
22
- * 按照框架规则返回类名字
23
- * @param name 类名称
24
- */
25
- export declare function classNamed(name: string): string;
26
- /**
27
- * generate a lightweight random id, enough for ioc container
26
+ * generate a lightweight 32 bit random id, enough for ioc container
28
27
  */
29
28
  export declare function generateRandomId(): string;
30
29
  export declare function merge(target: any, src: any): any;
30
+ export declare function toAsyncFunction<T extends (...args: any[]) => any>(method: T): (...args: Parameters<T>) => Promise<ReturnType<T>>;
31
+ export declare const Types: {
32
+ isClass: typeof isClass;
33
+ isAsyncFunction: typeof isAsyncFunction;
34
+ isGeneratorFunction: typeof isGeneratorFunction;
35
+ isString: typeof isString;
36
+ isPromise: typeof isPromise;
37
+ isFunction: typeof isFunction;
38
+ isObject: typeof isObject;
39
+ isPlainObject: typeof isPlainObject;
40
+ isNumber: typeof isNumber;
41
+ isProxy: typeof isProxy;
42
+ isMap: typeof isMap;
43
+ isSet: typeof isSet;
44
+ isRegExp: typeof isRegExp;
45
+ isUndefined: typeof isUndefined;
46
+ isNull: typeof isNull;
47
+ isNullOrUndefined: typeof isNullOrUndefined;
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
+ };
31
59
  //# sourceMappingURL=index.d.ts.map
@@ -1,15 +1,22 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.merge = exports.generateRandomId = exports.classNamed = 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
- const camelcase = require("camelcase");
6
5
  const crypto = require("crypto");
6
+ const camelCase_1 = require("./camelCase");
7
+ const uuid_1 = require("./uuid");
7
8
  const ToString = Function.prototype.toString;
9
+ const hasOwn = Object.prototype.hasOwnProperty;
10
+ const toStr = Object.prototype.toString;
8
11
  function fnBody(fn) {
9
12
  return ToString.call(fn)
10
13
  .replace(/^[^{]*{\s*/, '')
11
14
  .replace(/\s*}[^}]*$/, '');
12
15
  }
16
+ function isString(value) {
17
+ return typeof value === 'string';
18
+ }
19
+ exports.isString = isString;
13
20
  function isClass(fn) {
14
21
  if (typeof fn !== 'function') {
15
22
  return false;
@@ -43,6 +50,27 @@ function isObject(value) {
43
50
  return value !== null && typeof value === 'object';
44
51
  }
45
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;
46
74
  function isNumber(value) {
47
75
  return typeof value === 'number';
48
76
  }
@@ -103,15 +131,7 @@ function getParamNames(func) {
103
131
  }
104
132
  exports.getParamNames = getParamNames;
105
133
  /**
106
- * 按照框架规则返回类名字
107
- * @param name 类名称
108
- */
109
- function classNamed(name) {
110
- return camelcase(name);
111
- }
112
- exports.classNamed = classNamed;
113
- /**
114
- * generate a lightweight random id, enough for ioc container
134
+ * generate a lightweight 32 bit random id, enough for ioc container
115
135
  */
116
136
  function generateRandomId() {
117
137
  // => f9b327e70bbcf42494ccb28b2d98e00e
@@ -135,4 +155,43 @@ function merge(target, src) {
135
155
  throw new Error('can not merge meta that type of ' + typeof target);
136
156
  }
137
157
  exports.merge = merge;
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 = {
170
+ isClass,
171
+ isAsyncFunction,
172
+ isGeneratorFunction,
173
+ isString,
174
+ isPromise,
175
+ isFunction,
176
+ isObject,
177
+ isPlainObject,
178
+ isNumber,
179
+ isProxy,
180
+ isMap,
181
+ isSet,
182
+ isRegExp,
183
+ isUndefined,
184
+ isNull,
185
+ isNullOrUndefined,
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
+ };
138
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,22 +1,21 @@
1
1
  {
2
2
  "name": "@midwayjs/decorator",
3
- "version": "3.0.0-beta.13",
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
  },
14
14
  "dependencies": {
15
- "camelcase": "^6.2.0",
16
15
  "reflect-metadata": "^0.1.13"
17
16
  },
18
17
  "devDependencies": {
19
- "mm": "3"
18
+ "mm": "3.2.0"
20
19
  },
21
20
  "keywords": [
22
21
  "midway",
@@ -37,5 +36,5 @@
37
36
  "publishConfig": {
38
37
  "access": "public"
39
38
  },
40
- "gitHead": "d3c47770fee9dce33a8d148882173fd7782864ad"
39
+ "gitHead": "17a8b5bd3d0b0b21f24dd2f165b5df9097fc3ec4"
41
40
  }