node-web-mvc
Version:
node spring mvc
54 lines (53 loc) • 2.31 kB
JavaScript
;
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.default = create;
const ElementType_1 = require("./ElementType");
const FunctionExtends_1 = __importDefault(require("./FunctionExtends"));
const RuntimeAnnotation_1 = __importDefault(require("./RuntimeAnnotation"));
function parseArguments(args) {
const mayAnnotation = args[args.length - 1];
if (mayAnnotation instanceof RuntimeAnnotation_1.default) {
return {
args: args.slice(0, -1),
ownerAnnotation: mayAnnotation,
};
}
return {
args,
ownerAnnotation: null,
};
}
/**
* 创建一个运行时注解
* @param { AnnotationOptions } options 注解参数
*/
function create(elementTypes, annotationType) {
const types = elementTypes instanceof Array ? elementTypes : [elementTypes];
const decorator = function (...params) {
const { args, ownerAnnotation } = parseArguments(Array.prototype.slice.call(params));
if (this instanceof decorator) {
// 如果是当做class使用,这里用于从外部继承原始的AnnotationType
return FunctionExtends_1.default.extendInstance(this, annotationType, args, decorator);
}
const maybeInitializer = args[0];
const elementType = (0, ElementType_1.reflectAnnotationType)(args);
if (elementType === 'UNKNOW') {
// 如果是执行配置,这里需要返回修饰器函数 例如: @GetMapping('/home')
return (...params) => {
const innerInfo = parseArguments(params);
// 配置后创建注解
new RuntimeAnnotation_1.default(innerInfo.args, annotationType, types, maybeInitializer, innerInfo.ownerAnnotation);
};
}
// 创建注解
new RuntimeAnnotation_1.default(args, annotationType, types, {}, ownerAnnotation);
};
decorator.NativeAnnotation = annotationType;
FunctionExtends_1.default.extendStatic(decorator, annotationType);
Object.defineProperty(decorator, 'name', { value: annotationType.name });
decorator.__proto__ = annotationType;
return decorator;
}