Skip to content

stackra-inc/ts-metadata

Repository files navigation

@vivtel/metadata

npm version License: MIT Build Status

A lightweight utility library for enhanced metadata operations in TypeScript with improved reflect-metadata extensions.

Installation

npm install @vivtel/metadata

This package requires reflect-metadata as a peer dependency:

npm install reflect-metadata

Features

  • Enhanced metadata operations beyond basic reflect-metadata
  • Get metadata from the entire prototype chain
  • Define and update metadata with type safety
  • Clear metadata selectively or completely
  • Check metadata existence with granular control
  • Lightweight with minimal dependencies

Usage

Basic Operations

import { defineMetadata, getMetadata, hasMetadata } from '@vivtel/metadata';
import 'reflect-metadata';

class Example {
  property: string;
}

// Define metadata
defineMetadata('custom:key', 'value', Example, 'property');

// Get metadata
const value = getMetadata('custom:key', Example, 'property');
console.log(value); // 'value'

// Check if metadata exists
if (hasMetadata('custom:key', Example, 'property')) {
  console.log('Metadata exists!');
}

Getting All Metadata

import { getAllMetadata } from '@vivtel/metadata';

class Parent {
  @Reflect.metadata('parent:key', 'parent-value')
  property: string;
}

class Child extends Parent {
  @Reflect.metadata('child:key', 'child-value')
  property: string;
}

// Get all metadata from prototype chain
const allMetadata = getAllMetadata('property', Child);
console.log(allMetadata); // Contains metadata from both Parent and Child

Updating Metadata

import { updateMetadata } from '@vivtel/metadata';

// Update existing metadata or create new
updateMetadata('config', { timeout: 5000 }, Example, 'property');

Clearing Metadata

import { clearMetadata } from '@vivtel/metadata';

// Clear specific metadata
clearMetadata('custom:key', Example, 'property');

// Clear all metadata for a property
clearMetadata(undefined, Example, 'property');

API Reference

getMetadata

function getMetadata<T = any>(
  metadataKey: any,
  target: any,
  propertyKey?: string | symbol
): T | undefined;

Gets metadata value for the specified key from target or target's property.

getAllMetadata

function getAllMetadata<T = any>(
  propertyKey: string | symbol,
  target: any
): Map<any, T>;

Gets all metadata for a property from the entire prototype chain.

defineMetadata

function defineMetadata(
  metadataKey: any,
  metadataValue: any,
  target: any,
  propertyKey?: string | symbol
): void;

Defines metadata for the specified key on target or target's property.

updateMetadata

function updateMetadata<T = any>(
  metadataKey: any,
  metadataValue: T,
  target: any,
  propertyKey?: string | symbol
): void;

Updates existing metadata or creates new metadata for the specified key.

clearMetadata

function clearMetadata(
  metadataKey?: any,
  target?: any,
  propertyKey?: string | symbol
): void;

Clears metadata. If no parameters provided, clears all metadata.

hasMetadata

function hasMetadata(
  metadataKey: any,
  target: any,
  propertyKey?: string | symbol
): boolean;

Checks if metadata exists for the specified key on target or target's property.

hasOwnMetadata

function hasOwnMetadata(
  metadataKey: any,
  target: any,
  propertyKey?: string | symbol
): boolean;

Checks if metadata exists directly on target (not inherited from prototype chain).

Contributing

Please read our Contributing Guide for details on our code of conduct and the process for submitting pull requests.

Versioning

We use SemVer for versioning and Changesets to manage releases. For the versions available, see the tags on this repository.

License

This project is licensed under the MIT License - see the LICENSE file for details.

About

Typed metadata utilities for TypeScript decorators — define, get, merge, extend, and introspect reflect-metadata with a clean API

Resources

License

Stars

Watchers

Forks

Packages

 
 
 

Contributors

Languages