8.6.0 • Published 6 months ago

@typeix/metadata v8.6.0

Weekly downloads
1
License
MIT
Repository
github
Last release
6 months ago

@typeix/metadata

Metadata API for typescript decorators

Build Status Coverage Status npm

Metadata

With the introduction of Classes in TypeScript and ES6, there now exist certain scenarios that require additional features to support annotating or modifying classes and class members. Decorators provide a way to add both annotations and a meta-programming syntax for class declarations and members. Decorators are a stage 2 proposal for JavaScript and are available as an experimental feature of TypeScript.

Installing

npm i @typeix/metadata --save

Creating Decorators

Defining custom decorators has not been easier, API will take care that metadataKeys are created correctly and passed to low level metadata api.

import {
    createClassDecorator,
    createParameterDecorator,
    createPropertyDecorator,
    createParameterAndPropertyDecorator,
    createMethodDecorator
} from "@typeix/metadata";

const Injectable = () => createClassDecorator(Injectable);
const Inject = (token?) => createParameterAndPropertyDecorator(Inject, {token});
const Produces = (type) => createMethodDecorator(Produces, {type});
const Render = (type) => createMethodDecorator(Produces, {type});
const PathParam = (value) => createParameterDecorator(Produces, {value});

@Injectable()
class AService {
}

@Injectable()
class BService {
}

@Injectable()
class RootController {
    @Inject() aService: AService;
    @Inject() bService: BService;

    @Produces("application/json")
    actionIndex(@Inject() first: AService, @Inject() second: BService) {
    }
}

@Injectable()
class HomeController extends RootController {

    @Render("home")
    actionHome(
        @Inject() first: AService,
        @Inject(BService) second: BService,
        @PathParam("name") name: string
    ) {
    }
}

const metadata: Array<IMetadata> = getAllMetadataForTarget(HomeController);

Interface IMetadata

  • args - are custom arguments passed by decorator
  • metadataKey - is unique decorator key auto generated by metadata api
  • type - class, method, property, parameter
  • decoratorType - (above type) + mixed is createParameterAndPropertyDecorator
  • decorator - actual reference to decorator Function
  • propertyKey - user defined method name or constructor
  • paramIndex - it's defined if decorator is parameter type
  • designType - value from design:type typescript metadata
  • designParam - values from design:paramtypes typescript metadata
  • designReturn - values from design:returntype typescript metadata
export interface IMetadata {
    args: any;
    metadataKey: string;
    type?: string;
    decoratorType?: string;
    decorator?: Function;
    propertyKey?: string | symbol;
    paramIndex?: number;
    designType?: any;
    designParam?: any;
    designReturn?: any;
}

All metadata for target can be requested via getAllMetadataForTarget function, which returns list of metadata. eg.:

[
    {
        args: {
            token: BService
        },
        decoratorType: "mixed",
        decorator: Inject,
        type: "parameter",
        metadataKey: `@typeix:parameter:Inject:1:${getDecoratorUUID(Inject)}`,
        paramIndex: 1,
        propertyKey: "actionHome",
        designParam: [
            AService,
            BService,
            String
        ]
    }
    ...
]
8.6.0

6 months ago

8.5.2-rc.1

6 months ago

8.5.2-rc.2

6 months ago

8.4.5

11 months ago

8.4.4

11 months ago

8.4.6

11 months ago

8.4.1

11 months ago

8.4.0

11 months ago

8.4.3

11 months ago

8.4.2

11 months ago

8.5.0

11 months ago

8.5.1

11 months ago

8.3.0-rc.1

1 year ago

8.3.0

1 year ago

8.2.4

2 years ago

8.2.4-rc.2

2 years ago

8.2.3

2 years ago

8.2.2

2 years ago

8.1.3

2 years ago

8.2.1

2 years ago

8.2.0

2 years ago

8.1.2

3 years ago

8.1.2-rc.2

3 years ago

8.0.0-alpha.0

3 years ago

8.1.0-rc.0

3 years ago

8.0.2-alpha.0

3 years ago

8.1.0-rc.2

3 years ago

8.0.7

3 years ago

8.1.0-rc.1

3 years ago

8.0.6

3 years ago

8.0.0-alpha.1

3 years ago

7.3.0

3 years ago

7.1.0

3 years ago

7.4.1-alpha.3

3 years ago

7.4.1-alpha.1

3 years ago

7.4.1

3 years ago

7.4.1-alpha.2

3 years ago

7.4.1-alpha.0

3 years ago

7.2.4-alpha.0

3 years ago

8.1.0

3 years ago

8.0.3-alpha.3

3 years ago

8.0.3-alpha.2

3 years ago

8.0.1-beta.0

3 years ago

8.0.0-beta.0

3 years ago

8.1.1

3 years ago

8.0.3-alpha.1

3 years ago

8.0.3-alpha.0

3 years ago

7.2.8-alpha.1

3 years ago

7.2.6-alpha.0

3 years ago

8.0.0-y.0

3 years ago

8.0.0-alpha.2.1

3 years ago

8.0.1-alpha.0

3 years ago

7.4.0

3 years ago

7.2.3-alpha.0

3 years ago

8.0.1

3 years ago

7.4.0-alpha.0

3 years ago

8.0.0

3 years ago

7.4.0-alpha.1

3 years ago

8.0.2

3 years ago

7.2.7-alpha.0

3 years ago

7.2.5-alpha.0

3 years ago

8.1.1-rc.2

3 years ago

8.1.1-rc.3

3 years ago

8.1.1-rc.0

3 years ago

8.0.7-rc.1

3 years ago

8.0.7-rc.2

3 years ago

7.0.0

3 years ago

6.0.0

3 years ago

5.1.0

3 years ago

5.0.0

3 years ago

4.0.5

3 years ago

4.0.4

3 years ago

4.0.3

3 years ago

4.0.2

3 years ago

4.0.1

3 years ago

4.0.0

3 years ago