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

metadata-booster

v0.4.3

Published

Emits detailed metadata of your types. You can then get it in runtime to deal with schema-aware operation, like defining GraphQL schemas, ORM operations, etc.

Downloads

9

Readme

Metadata Booster

This is a transformer (also known as plugin) for Typescript to generate detailed metadata for all your classes.

Why?

There are many applications that need to know the schema of your classes to work properly, like ORMs (to know the names of the database tables and columns), GraphQL APIs (to generate the GraphQL schema), etc.

Currently, Typescript emits some metadata when you add decorators to your classes and enable the following compiler options: "experimentalDecorators": true and "emitDecoratorMetadata": true.

However, that metadata is very limited, as it doesn't include property names and information about the type parameters.

For example, if we had the following code:

@AnyDecorator //We need to decorate the class to emit metadata
class User {
    constructor(
        public name: string,
        public friends: Set<User>
    ) {}
}

enum Size {
    Small,
    Medium,
    Big,
}
@AnyDecorator //We need to decorate the class to emit metadata
class Car {
    constructor(
        public size: Size,
        public driversByName: Map<string, User>
    ) {}
    
    public engageAutoPilot(): Promise<boolean> {
        // Asume a log task here
        return Promise.resolve(true)
    }
}

And now we call Reflect.getMetadata('design:paramtypes') on each class, we would get the following:

Reflect.getMetadata('design:paramtypes', User)
> [ [Function: String], [Function: Set] ]

Reflect.getMetadata('design:paramtypes', Car)
> [ [Function: Number], [Function: Map] ]

Not very helpful: we are lacking a lot of information about property names, type parameters and methods.

Welcome to full detailed metadata

With this transformer, you will get much more detailed metadata for all your classes, without the need of using any decorator.

Following with the previous example, you can get this detailed metadata using the key 'booster:typeinfo':

Reflect.getMetadata('booster:typeinfo', User)
> {
    name: 'User',
    type: [Function: User],
    fields: [
        {
            name: 'name',
            typeInfo: { 
                type: [Function: String], 
                parameters: []
            }
        },{
            name: 'friends',
            typeInfo: {
                type: [Function: Set],
                parameters: [
                    { 
                        type: [Function: User], 
                        parameters: []
                    } 
                ]
            }
        }
    ],
    methods: []
}

Reflect.getMetadata('booster:typeinfo', Car)
> {
    name: 'Car',
    type: [Function: Car],
    fields: [
        {
            name: 'size',
            typeInfo: {
                type: {
                    '0': 'Small',
                    '1': 'Medium',
                    '2': 'Big',
                    Small: 0,
                    Medium: 1,
                    Big: 2
                },
                parameters: []
            }
        },
        {
            name: 'driversByName',
            typeInfo: {
                type: [Function: Map],
                parameters: [
                    {
                        type: [Function: String], 
                        parameters: []
                    },{
                        type: [Function: User],
                        parameters: []
                    }
                ]
            }
        }
    ],
    methods: [
        {
            name: 'engageAutoPilot',
            typeInfo: {
                type: [Function: Promise],
                parameters: [
                    {
                        type: [Function: Boolean], 
                        parameters: []
                    }
                ]
            }
        }
    ]
}

As you can see, you can now have runtime access to the information about all the properties, type parameters, methods, return types, etc. of your classes.

How to use it

"metadata-booster" is a transformer so, until the Typescript team decides to accept plugins (you can track the status in this issue), you would want to use the Typescript wrapper "TTypescript" to be able to use any transformer. That being said, it works really well.

Here are the steps:

  1. Add the "metadata-booster" transformer and "ttypescript" to your "devDependencies"
npm install --save-dev "metadata-booster"
npm install --save-dev "ttypescript"
  1. Add the official module "reflect-metadata" to your "dependencies" (you need this to access the metadata)
npm install --save-prod "reflect-metadata"
  1. Go to your tsconfig.json file and
    • a) Ensure you have the option "experimentalDecorators": true. The reason is that the metadata is automatically added as a decorator to the class. In any case, you don't need to write any decorator.
    • b) Add "metadata-booster" as a transformer plugin inside the "compilerOptions" section
{
  "compilerOptions": {
    (...)
    "experimentalDecorators": true
    "plugins": [
      { "transform": "metadata-booster"}
    ]
  },
}
  1. [Optional] From now on, to compile your code you need to use the command ttsc (the TTypescript wrapper), instead of tsc. I normally have a "compile" script in my "package.json" file that calls "tsc -b tsconfig.json", so I compile my code by running npm run compile. I would recommend you to do that and change "tsc" by "ttsc". Like this:
  ... other "package.json" options fields ...
  "scripts": {
    "compile": "ttsc -b tsconfig.json",
    "test": "..."
  }

Now you can compile your project by running npm run compile and have access to a full detailed metadata for all your Typescript classes.

Compatibility

This transformer is compatible with Typescript version 4.x.x

Missing features

  • [ ] Gather interfaces metadata
  • [ ] Gather method parameters metadata