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

@e22m4u/ts-reflector

v0.1.6

Published

Typed wrapper for The Metadata Reflection API

Downloads

321

Readme

@e22m4u/ts-reflector

English | Русский

A typed wrapper for The Metadata Reflection API

Installation

npm install @e22m4u/ts-reflector

Experimental decorators

To enable experimental decorators support add the following options to your tsconfig.json file.

{
  "emitDecoratorMetadata": true,
  "experimentalDecorators": true
}

MetadataKey

Here's an example of using MetadataKey for storing metadata. Values are validated against the given key type before storage. The metadata type is also automatically inferred when retrieving values.

import {Reflector} from '@e22m4u/ts-metadata';
import {MetadataKey} from '@e22m4u/ts-metadata';

type MyData = {foo: string};
class Target {/* ... */}

// Create a MetadataKey<T> for MyData type
const key = new MetadataKey<MyData>();

// Assign metadata to the Target class
Reflector.defineMetadata(key, {foo: 'bar'}, Target);

// The defineMetadata method validates the value type
// against the MetadataKey<MyData> type constraint
Reflector.defineMetadata(key, 'string', Target);
// TypeError: Argument of type 'string' is not assignable
// to parameter of type MyData.

// Return types from getMetadata and getOwnMetadata
// are automatically inferred from the key type
const result = Reflector.getMetadata(key, Target); // MyData

getDecoratorTargetType

The getDecoratorTargetType utility provides a simple way to identify where a decorator is applied in your code. When writing custom decorators, it's often necessary to implement different behavior based on whether the decorator is used on a class, method, property, or parameter. This utility returns an enum value indicating the exact location of the decorator.

import {getDecoratorTargetType} from '@e22m4u/ts-metadata';
import {DecoratorTargetType as DTT} from '@e22m4u/ts-metadata';

// Define a decorator
function myDecorator(
  target: object,
  propertyKey?: string,
  descriptorOrIndex?: PropertyDescriptor | number,
) {
  // Pass the decorator arguments to the utility
  const type = getDecoratorTargetType(
    target,
    propertyKey,
    descriptorOrIndex,
  );
  // Check the returned type to determine the decorator's location
  if (type === DTT.CONSTRUCTOR)
    console.log('@myDecorator is applied to a class');
  if (type === DTT.STATIC_METHOD)
    console.log('@myDecorator is applied to a static method');
  if (type === DTT.INSTANCE_METHOD)
    console.log('@myDecorator is applied to an instance method');
  if (type === DTT.STATIC_PROPERTY)
    console.log('@myDecorator is applied to a static property');
  if (type === DTT.INSTANCE_PROPERTY)
    console.log('@myDecorator is applied to an instance property');
  if (type === DTT.CONSTRUCTOR_PARAMETER)
    console.log('@myDecorator is applied to a constructor parameter');
  if (type === DTT.STATIC_METHOD_PARAMETER)
    console.log('@myDecorator is applied to a static method parameter');
  if (type === DTT.INSTANCE_METHOD_PARAMETER)
    console.log('@myDecorator is applied to an instance method parameter');
};

Tests

npm run test

License

MIT