@zuu/mirror
    TypeScript icon, indicating that this package has built-in type declarations

    4.0.2 • Public • Published

    @zuu/mirror

    Gitter Version Downloads/week License

    What is Mirror?

    Mirror is a very powerfull reflection api created for typescript, mainly used with the zuu framework (for dependency injection, assertion and other cool stuff).

    Want to contribute?

    Here's how!

    Usage

    The basic principle is easy. Decorations are attached to classes, properties, methods and method parameters and can be reflected after by a very powerfull reflection engine (that also provides aditional information about the types, names, positions, etc).

    The first step is to create a new decoration:

    // just extend the IDecoration interface
    interface GParameters extends IDecoration {
        x: number;
        y?: string;
    };

    After that we can define our decorator logic:

    // just extend the AbstractDecorator<T> and provide a IDecoration type
    class GDecorator extends AbstractDecorator<GParameters> {
        constructor() {
            // provide information about the MirrorType [CLASS, PROPERTY, METHOD, PARAMETER] and a namespace (must be unique)
            super(MirrorType.CLASS, "demos.class.g");
        };
    
        public annotate(instance: GParameters, target: any, key?: string | symbol, index?: number) {
            // here you can process your decoration placement
            console.log(instance);
        };
    };

    Now we can ask mirror to build our decorator: (don't forget to say please)

    // Hey Mirror, sorry for bothering you, but can you please make a new decorator?
    //  It will be a ClassDecorator and will bind to GParameters decoration :) Thanks
    let G = Mirror.decorator<GParameters, ClassDecorator>(new GDecorator);

    That's all :) Now you can use it on any class like so:

    @G({x: 3, y: "abcd"})
    class Dummy { };

    It's a decorator factory actually... but you can convert it to a decorator just by calling it with the decoration:

    let g = G({x: 0});
    
    @g
    class Dummy2 {};

    You can now reflect on your class or class instances (also on specific methods and properties):

    let reflection: ReflectedClass = Mirror.reflect<ReflectedClass>(Dummy);
    console.log(reflection);
    
    let instance = new Dummy2()
    let instanceReflection: ReflectedClass = Mirror.reflectInstance<ReflectedClass>(instance);
    console.log(instanceReflection);

    Objects

    Object Reflection Decorator
    type ReflectedType -
    class ReflectedClass ClassDecorator
    property ReflectedProperty PropertyDecorator
    method ReflectedMethod MethodDecorator
    parameter ReflectedParameter ParamterDecorator

    Install

    npm i @zuu/mirror

    DownloadsWeekly Downloads

    4

    Version

    4.0.2

    License

    MIT

    Unpacked Size

    40.1 kB

    Total Files

    31

    Last publish

    Collaborators

    • iamthevex