npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@typeix/metadata

v8.8.0

Published

@typeix metadata apis::::

Downloads

409

Readme

@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
        ]
    }
    ...
]