@essential-projects/metadata
Version:
the core metadata service for using the metadata from inside the domain
56 lines (39 loc) • 2.12 kB
text/typescript
import {ClaimActionType, IPromotionRoles, IRequiredClaim} from '@essential-projects/core_contracts';
import {IPromoteDecorator, MetadataType} from '@essential-projects/metadata_contracts';
import {MetadataProvider} from './../provider';
export function promote(roles: IPromotionRoles, namespace?: string): IPromoteDecorator {
if (!roles || !roles.roles || roles.roles.length === 0) {
throw new Error('roles for promotion are missing');
}
// if this is decorating a class, typeof args[0] is the type of the decorated class
return function promoteFactory(...args: Array<any>): typeof args[0] | void | PropertyDescriptor {
switch (args.length) {
case 1:
return classPromote.apply(this, [args[0], roles, namespace]);
case 2:
return propertyPromote.apply(this, [args[0], args[1], roles, namespace]);
case 3:
if (typeof args[2] === 'number') {
// this is a parameter descriptor
// those should not support promotions
}
return methodPromote.apply(this, [args[0], args[1], args[2], roles, namespace]);
default:
throw new Error('Decorators are not valid here!');
}
};
}
function classPromote(target: any, roles: IPromotionRoles, namespace?: string): typeof target {
const type: string = target.prototype ? target.prototype.constructor.name : target.constructor.name;
MetadataProvider.setForType(MetadataType.Promotion, roles, namespace, type);
return target;
}
function propertyPromote(target: any, key: string, roles: IPromotionRoles, namespace?: string): void {
const type: string = target.prototype ? target.prototype.constructor.name : target.constructor.name;
MetadataProvider.setForType(MetadataType.Promotion, roles, namespace, type, key);
}
function methodPromote(target: any, key: string, descriptor: any, roles: IPromotionRoles, namespace?: string): PropertyDescriptor {
const type: string = target.prototype ? target.prototype.constructor.name : target.constructor.name;
MetadataProvider.setForType(MetadataType.Promotion, roles, namespace, type, key);
return descriptor;
}