A lightweight utility library for enhanced metadata operations in TypeScript with improved reflect-metadata extensions.
npm install @vivtel/metadataThis package requires reflect-metadata as a peer dependency:
npm install reflect-metadata- 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
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!');
}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 Childimport { updateMetadata } from '@vivtel/metadata';
// Update existing metadata or create new
updateMetadata('config', { timeout: 5000 }, Example, 'property');import { clearMetadata } from '@vivtel/metadata';
// Clear specific metadata
clearMetadata('custom:key', Example, 'property');
// Clear all metadata for a property
clearMetadata(undefined, Example, 'property');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.
function getAllMetadata<T = any>(
propertyKey: string | symbol,
target: any
): Map<any, T>;Gets all metadata for a property from the entire prototype chain.
function defineMetadata(
metadataKey: any,
metadataValue: any,
target: any,
propertyKey?: string | symbol
): void;Defines metadata for the specified key on target or target's property.
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.
function clearMetadata(
metadataKey?: any,
target?: any,
propertyKey?: string | symbol
): void;Clears metadata. If no parameters provided, clears all metadata.
function hasMetadata(
metadataKey: any,
target: any,
propertyKey?: string | symbol
): boolean;Checks if metadata exists for the specified key on target or target's property.
function hasOwnMetadata(
metadataKey: any,
target: any,
propertyKey?: string | symbol
): boolean;Checks if metadata exists directly on target (not inherited from prototype chain).
Please read our Contributing Guide for details on our code of conduct and the process for submitting pull requests.
We use SemVer for versioning and Changesets to manage releases. For the versions available, see the tags on this repository.
This project is licensed under the MIT License - see the LICENSE file for details.