Skip to content

Decorator Helpers

This file provides helper functions for setting and retrieving metadata on classes or class properties, typically used for decorators in the @sodacore/di package. It also includes a utility for generating standardized metadata keys.


API

setMeta(key: string, namespace?: string, noPrefix = false)

Creates a higher-order function to set metadata on a class or property.

  • Parameters:
    • key: The key to store the metadata under.
    • namespace (optional): Namespace for the metadata key.
    • noPrefix (default: false): If true, omits the @sodacore prefix.
  • Returns:(target: any, value: any, propertyKey?: string | symbol) => void
  • Example:
    typescript
    setMeta('role', 'auth')(MyClass.prototype, 'admin');

getMeta<T = any | undefined>(key: string, namespace?: string, noPrefix = false)

Creates a higher-order function to retrieve metadata from a class or property.

  • Parameters:
    • key: The key to retrieve the metadata for.
    • namespace (optional): Namespace for the metadata key.
    • noPrefix (default: false): If true, omits the @sodacore prefix.
  • Returns:(target: any, propertyKey?: string | symbol, fallback?: T) => T
  • Example:
    typescript
    const role = getMeta<string>('role', 'auth')(MyClass.prototype) || 'guest';

getMetaPrefix(key: string, namespace?: string, noPrefix = false): string

Generates a standardized metadata key, optionally namespaced and/or prefixed.

  • Parameters:
    • key: The key to prefix.
    • namespace (optional): Namespace for the key.
    • noPrefix (default: false): If true, omits the @sodacore prefix.
  • Returns:string
  • Example:
    typescript
    const metaKey = getMetaPrefix('role', 'auth'); // "@sodacore:auth:role"

Notes

  • These helpers rely on the Reflect metadata API.
  • Useful for implementing custom decorators that need to store or retrieve metadata on classes or properties.

Released under the Apache-2.0 License.