New Research: Supply Chain Attack on Axios Pulls Malicious Dependency from npm.Details
Socket
Book a DemoSign in
Socket

@typinghare/ts-reflect

Package Overview
Dependencies
Maintainers
1
Versions
8
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@typinghare/ts-reflect

A useful library for making your own typescript decorators.

npmnpm
Version
0.2.2
Version published
Weekly downloads
4
Maintainers
1
Weekly downloads
 
Created
Source

Reflect

Get Started

Create and export decorators

At the very first beginning, we create two decorators, one of which is a class decorator and the other is a method decorator, as follows by using DecoratorGenerator. We can save some fields if needed. Those fields are named context in ts-reflect and can be accessed when decorators are applied. Notice that function myMethodDecorator accepts a parameter label then is passed to context. Users can custom the value of the label when applying this decorator.

// create.ts

import { DecoratorGenerator } from 'ts-reflect';

// create a decorator generator
const decoratorGenerator = new DecoratorGenerator();

// create a class decorator
function myClassDecorator(): ClassDecorator {
  return decoratorGenerator.classDecorator({
    // saves some fields
    owner: 'James'
  });
}

// create a method decorator
function myMethodDecorator(label: string): MethodDecorator {
  return decoratorGenerator.methodDecorator({ label });
}

// export the decorators
export { myClassDecorator, myMethodDecorator };

Apply decorators and get context

We apply the decorators as follows. After a decorated class is loaded, its corresponding reflector can be obtained by the classContainer. Use getContext to access the context we save when creating the decorator. We can also obtain the reflector of the method myMethod. Similar operations of getting and setting the context of all kinds of reflectors are the same.

Prerequisite knowledge of typescript decorators is that they will be invoked when the generated js file is imported (or required). They will not be invoked again the second time of importing (or requiring).

// apply.ts

import { MyClassDecorator, myMethodDecorator } from './create';
import { classContainer } from 'ts-reflect';

// apply decorators to your class
@MyClassDecorator()
class MyClass {
  @myMethodDecorator('My label')
  public myMethod() {
    // some codes
  }
}

// get class context
const myClassReflector = classContainer.getByConstructor(MyClass);
console.log(myClassReflector?.getContext('owner')); // James

// get method context
const myMethodReflector = myClassReflector?.getMethod('myMethod');
console.log(myMethodReflector?.getContext('label'));  // My label

Keywords

typescript

FAQs

Package last updated on 15 Apr 2023

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts