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

nestjs-auto-reflect-metadata-emitter

v1.2.1

Published

So, I got this crazy idea of making every class, property and method emitted through reflect-metadata when compiling a trypescript code. Long story short I did some research and didn't find a good solution, but figured out that I could use **nest build**,

Downloads

1,235

Readme

nestjs-auto-reflect-metadata-emitter

So, I got this crazy idea of making every class, property and method emitted through reflect-metadata when compiling a trypescript code. Long story short I did some research and didn't find a good solution, but figured out that I could use nest build, of @nestjs/cli, to manipulate the code before compiling it, so I did.

This is a working in progress, though, and I'd love to get some suggestions or contributions.

But why?

There're some situations where having access to every class metadata during runtime is quite useful. Let's say you have an application separated in layers: domain, application, infrastructure, and so son, but you fill up your domain with nestjs, inversify, or tsyringe decorators, or even mongoose, but you want to do a external reference free domain layer. If you have the metadata, it's possible to create all these classes decorations programmatically at infrastructure layer!

Let's say you're using @nestjs/swagger, and you have to put in every property the @ApiProperty decorator, even though you don't specify nothing special. Having all de metadata emitted, you can create a decorator where you just decorate the class, and automatizes the decoration of all the properties!

How to use it?

As I said, this is a @nestjs/cli plugin, so you need to use nest build to compile your solution. There's no problem if you're not using nestjs in anything else, the nest build can be used stand alone.

Now, to use this plugin, do the following steps:

  • Install the plugin:
  npm i -D nestjs-auto-reflect-metadata-emitter
  • Add the plugin in the .nest-cli.json
  "compilerOptions": {
    "plugins": [
      "nestjs-auto-reflect-metadata-emitter/plugin"
    ]
  }
  • Make sure tsconfig is set with the following properties:
    "emitDecoratorMetadata": true,
    "experimentalDecorators": true,
  • Use, at least, typescript 5.3.3.
  • Compile your application using nest build, and test it using nest start

How to access metadata

Metadata of all classes is accessible through the method getClassMetadata, where you just need to inform the class which you want the metadata of. You can also iterate over all metadata registered through iterateMetadata. Finally, metadata may be a sensitive data of your application, so, you can erase all its information using clearAllMetadata. We recommend you to do so, if you use this library, don't keep any hard logic depending on what this package will register, just construct whenever you need and clear it all at the end.

How to use it with Jest?

You can set the transformer of this library to run with jest following the example below:

"transform": {
      "^.+\\.(t|j)s$": [
        "ts-jest",
        {
          "astTransformers": {
            "before": ["node_modules/nestjs-auto-reflect-metadata-emitter/plugin"]
          }
        }
      ]
    }

This will be enough to make it apply it during transpilation.

Helpers to apply decorators

One of the advantages of having all this metadata emitted is that you can apply decorators for existing classes in separated scopes! To do that, there're two helper functions this library offers:

the first one is applyPropertyAndMethodsDecorators:

applyPropertyAndMethodsDecorators(MyClass, {
  prop1: [
    @ApiProperty({
      example: '123'
    })
  ],
  prop2: [
    @ApiProperty()
    @IsEnum(MyEnum)
  ],
  [DEFAULT]: [@ApiProperty()]
})

Notice the symbol DEFAULT, it's a symbol imported from our library and serves to purpose of define a decorator that'll be applied to every property or method that doesn't have a specific set os decorators informed. This second parameter is a strongly typed object and it'll only allow names of properties and methods of MyClass (static or not), or the DEFAULT symbol. Executing this code will have the same effect as applying the decorators directly in the class.

The second helper method is simpler, applyClassDecorators:

applyClassDecorators(MyClass, [
    @Model()
    @PrimaryKey({ field1: 1, field2: 2 })
  ]
})

This one will apply the decorators to the class itself, not its properties.

What we're not doing yet.

  • We're not generating metadata of get and set accessors;

This is a point of evolution of this library and we'll address them as soon as possible. If you have any suggestions or contributions to do, feel free to contact us!