koatty_container
Advanced tools
Comparing version 1.3.4 to 1.3.6
@@ -15,2 +15,3 @@ "use strict"; | ||
const IContainer_1 = require("./IContainer"); | ||
const Util_1 = require("./Util"); | ||
/** | ||
@@ -142,3 +143,3 @@ * defined AOP type | ||
let aspect, name = ""; | ||
if (helper.isFunction(aopName)) { | ||
if (helper.isClass(aopName)) { | ||
// tslint:disable-next-line: no-invalid-this | ||
@@ -169,12 +170,20 @@ aspect = Container_1.IOCContainer.getInsByClass(aopName); | ||
function injectAOP(target, instance, container) { | ||
const classMetaData = Container_1.IOCContainer.getClassMetadata(IContainer_1.TAGGED_CLS, IContainer_1.TAGGED_AOP, target); | ||
if (classMetaData) { | ||
const { type, name } = classMetaData; | ||
if (type && name) { | ||
const methods = Object.getOwnPropertyNames(target.prototype).filter((m) => !["constructor", "init"].includes(m)); | ||
methods.forEach((element) => { | ||
// tslint:disable-next-line: no-unused-expression | ||
process.env.APP_DEBUG && koatty_logger_1.DefaultLogger.Custom("think", "", `Register inject AOP ${target.name} method: ${element} => ${type}`); | ||
defineAOPProperty(target, element, name, type); | ||
}); | ||
// inject default AOP method | ||
const flag = hasDefaultAOP(target); | ||
// If the class has defined the default AOP method, @BeforeEach and @AfterEach will not take effect | ||
if (flag) { | ||
injectDefaultAOP(target, instance, container); | ||
} | ||
else { | ||
const classMetaData = Container_1.IOCContainer.getClassMetadata(IContainer_1.TAGGED_CLS, IContainer_1.TAGGED_AOP, target); | ||
if (classMetaData) { | ||
const { type, name } = classMetaData; | ||
if (type && name) { | ||
const methods = Util_1.getMethodNames(target, true).filter((m) => !["constructor", "init", "__before", "__after"].includes(m)); | ||
methods.forEach((element) => { | ||
// tslint:disable-next-line: no-unused-expression | ||
process.env.APP_DEBUG && koatty_logger_1.DefaultLogger.Custom("think", "", `Register inject AOP ${target.name} method: ${element} => ${type}`); | ||
defineAOPProperty(target, element, name, type); | ||
}); | ||
} | ||
} | ||
@@ -185,2 +194,43 @@ } | ||
/** | ||
* Determine whether the class contains the default AOP method | ||
* | ||
* @param {*} target | ||
* @returns {*} {boolean} | ||
*/ | ||
function hasDefaultAOP(target) { | ||
const allMethods = Util_1.getMethodNames(target).filter((m) => !["constructor", "init"].includes(m)); | ||
// class contains the default AOP method | ||
if (allMethods.includes("__before") || allMethods.includes("__after")) { | ||
return true; | ||
} | ||
return false; | ||
} | ||
/** | ||
* inject default AOP | ||
* | ||
* @export | ||
* @param {*} target | ||
* @param {*} instance | ||
* @param {Container} container | ||
* @returns {*} | ||
*/ | ||
function injectDefaultAOP(target, instance, container) { | ||
// class methods | ||
const methods = Util_1.getMethodNames(target, true).filter((m) => !["constructor", "init"].includes(m)); | ||
// tslint:disable-next-line: no-unused-expression | ||
process.env.APP_DEBUG && koatty_logger_1.DefaultLogger.Warn(`The ${target.name} class has a default AOP method, @BeforeEach and @AfterEach maybe not take effect`); | ||
methods.forEach((element) => { | ||
if (element !== "__before" && element !== "__after") { | ||
// tslint:disable-next-line: no-unused-expression | ||
process.env.APP_DEBUG && koatty_logger_1.DefaultLogger.Custom("think", "", `Register inject default AOP ${target.name} method: ${element} => __before`); | ||
if (helper.isFunction(instance.__before)) { | ||
defineAOPProperty(target, element, "__before", AOPType.Before); | ||
} | ||
if (helper.isFunction(instance.__after)) { | ||
defineAOPProperty(target, element, "__after", AOPType.After); | ||
} | ||
} | ||
}); | ||
} | ||
/** | ||
* Dynamically add methods for target class types | ||
@@ -194,2 +244,5 @@ * | ||
const oldMethod = Reflect.get(classes.prototype, protoName); | ||
if (!oldMethod) { | ||
throw Error(`${protoName} method does not exist.`); | ||
} | ||
Reflect.defineProperty(classes.prototype, protoName, { | ||
@@ -199,4 +252,11 @@ writable: true, | ||
if (type === AOPType.Before) { | ||
if (oldMethod) { | ||
await executeAspect(aopName, props); | ||
if (aopName) { | ||
if (aopName === "__before") { | ||
koatty_logger_1.DefaultLogger.Info(`Execute the aspect ${classes.name}.__before`); | ||
// tslint:disable-next-line: no-invalid-this | ||
await this.__before(props); | ||
} | ||
else { | ||
await executeAspect(aopName, props); | ||
} | ||
} | ||
@@ -210,3 +270,10 @@ // tslint:disable-next-line: no-invalid-this | ||
if (aopName) { | ||
await executeAspect(aopName, props); | ||
if (aopName === "__after") { | ||
koatty_logger_1.DefaultLogger.Info(`Execute the aspect ${classes.name}.__after`); | ||
// tslint:disable-next-line: no-invalid-this | ||
await this.__after(props); | ||
} | ||
else { | ||
await executeAspect(aopName, props); | ||
} | ||
} | ||
@@ -213,0 +280,0 @@ return res; |
/** | ||
* get metadata value of a metadata key on the prototype chain of an object and property | ||
* @param metadataKey metadata's key | ||
* @param metadataKey metadata key | ||
* @param target the target of metadataKey | ||
*/ | ||
export declare function RecursiveGetMetadata(metadataKey: any, target: any, propertyKey?: string | symbol): any[]; | ||
/** | ||
* Find all methods on a given ES6 class | ||
* | ||
* @param {*} target | ||
* @param {boolean} isSelfProperties | ||
* @returns {string[]} | ||
*/ | ||
export declare function getMethodNames(target: any, isSelfProperties?: boolean): string[]; | ||
/** | ||
* Find all property on a given ES6 class | ||
* | ||
* @export | ||
* @param {*} target | ||
* @param {boolean} isSelfProperties | ||
* @returns {string[]} | ||
*/ | ||
export declare function getPropertyNames(target: any, isSelfProperties?: boolean): string[]; |
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.getPropertyNames = exports.getMethodNames = exports.RecursiveGetMetadata = void 0; | ||
const tslib_1 = require("tslib"); | ||
/* | ||
@@ -6,8 +9,7 @@ * @Author: richen | ||
* @LastEditors: linyyyang<linyyyang@tencent.com> | ||
* @LastEditTime: 2020-12-17 20:05:19 | ||
* @LastEditTime: 2020-12-21 10:57:54 | ||
* @License: BSD (3-Clause) | ||
* @Copyright (c) - <richenlin(at)gmail.com> | ||
*/ | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.RecursiveGetMetadata = void 0; | ||
const helper = tslib_1.__importStar(require("koatty_lib")); | ||
const Container_1 = require("./Container"); | ||
@@ -27,3 +29,3 @@ // get property of an object | ||
// points back to the constructor. | ||
// If this is not the same as Function.[[Prototype]], then this is definately inherited. | ||
// If this is not the same as Function.[[Prototype]], then this is definitely inherited. | ||
// This is the case when in ES6 or when using __proto__ in a compatible browser. | ||
@@ -54,3 +56,3 @@ if (proto !== functionPrototype) { | ||
* get metadata value of a metadata key on the prototype chain of an object and property | ||
* @param metadataKey metadata's key | ||
* @param metadataKey metadata key | ||
* @param target the target of metadataKey | ||
@@ -67,7 +69,7 @@ */ | ||
// metadata = Reflect.getOwnMetadata(metadataKey, parent, propertyKey); | ||
const pmetadata = IOCContainer.listPropertyData(metadataKey, parent); | ||
if (pmetadata) { | ||
for (const n in pmetadata) { | ||
const metadata = IOCContainer.listPropertyData(metadataKey, parent); | ||
if (metadata) { | ||
for (const n in metadata) { | ||
if (!metadata.hasOwnProperty(n)) { | ||
metadata[n] = pmetadata[n]; | ||
metadata[n] = metadata[n]; | ||
} | ||
@@ -81,2 +83,69 @@ } | ||
exports.RecursiveGetMetadata = RecursiveGetMetadata; | ||
/** | ||
* Find all methods on a given ES6 class | ||
* | ||
* @param {*} target | ||
* @param {boolean} isSelfProperties | ||
* @returns {string[]} | ||
*/ | ||
function getMethodNames(target, isSelfProperties = false) { | ||
const result = []; | ||
const enumerableOwnKeys = Object.getOwnPropertyNames(target.prototype); | ||
if (!isSelfProperties) { | ||
// searching prototype chain for methods | ||
let parent = ordinaryGetPrototypeOf(target); | ||
while (helper.isClass(parent) && parent.constructor) { | ||
const allOwnKeysOnPrototype = Object.getOwnPropertyNames(parent.prototype); | ||
// get methods from es6 class | ||
allOwnKeysOnPrototype.forEach((k) => { | ||
if (!result.includes(k) && helper.isFunction(parent.prototype[k])) { | ||
result.push(k); | ||
} | ||
}); | ||
parent = ordinaryGetPrototypeOf(parent); | ||
} | ||
} | ||
// leave out those methods on Object's prototype | ||
enumerableOwnKeys.forEach((k) => { | ||
if (!result.includes(k) && helper.isFunction(target.prototype[k])) { | ||
result.push(k); | ||
} | ||
}); | ||
return result; | ||
} | ||
exports.getMethodNames = getMethodNames; | ||
/** | ||
* Find all property on a given ES6 class | ||
* | ||
* @export | ||
* @param {*} target | ||
* @param {boolean} isSelfProperties | ||
* @returns {string[]} | ||
*/ | ||
function getPropertyNames(target, isSelfProperties = false) { | ||
const result = []; | ||
const enumerableOwnKeys = Object.getOwnPropertyNames(target); | ||
if (!isSelfProperties) { | ||
// searching prototype chain for methods | ||
let parent = ordinaryGetPrototypeOf(target); | ||
while (helper.isClass(parent) && parent.constructor) { | ||
const allOwnKeysOnPrototype = Object.getOwnPropertyNames(parent); | ||
// get methods from es6 class | ||
allOwnKeysOnPrototype.forEach((k) => { | ||
if (!result.includes(k) && !helper.isFunction(parent.prototype[k])) { | ||
result.push(k); | ||
} | ||
}); | ||
parent = ordinaryGetPrototypeOf(parent); | ||
} | ||
} | ||
// leave out those methods on Object's prototype | ||
enumerableOwnKeys.forEach((k) => { | ||
if (!result.includes(k) && !helper.isFunction(target.prototype[k])) { | ||
result.push(k); | ||
} | ||
}); | ||
return result; | ||
} | ||
exports.getPropertyNames = getPropertyNames; | ||
//# sourceMappingURL=Util.js.map |
{ | ||
"name": "koatty_container", | ||
"version": "1.3.4", | ||
"version": "1.3.6", | ||
"description": "IOC Container for Koatty.", | ||
@@ -5,0 +5,0 @@ "scripts": { |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
84852
1696
5