IonIon
Packages
  • next
  • current
Changelog
GitHub
Packages
  • next
  • current
Changelog
GitHub
  • Version 0.x

    • Release Notes
    • Upgrade Guide
    • Contribution Guide
    • Security Policy
    • Code of Conduct
    • Origin
  • Packages

    • Introduction
    • Container

      • Introduction
      • Prerequisites
      • How to install
      • Container Instance
      • Bindings
      • Dependencies
      • Resolving
      • Contextual Bindings
    • Contracts

      • Introduction
      • How to install
    • Support

      • Introduction
      • How to install
      • Arrays

        • About Arrays
        • Includes All
        • Includes Any
        • Is Array Like
        • Is Concat Spreadable
        • Is Safe Array Like
        • Is Typed Array
        • Merge
      • Concerns

        • About Concerns
        • Prerequisites
        • Concern Class
        • Using Concerns
        • Aliases
        • Conflict Resolution
        • Booting
        • Hooks
        • Edge Cases
        • JSDoc
      • Exceptions

        • About Exceptions
        • Configure Custom Error
        • Configure Stack Trace
        • Get Error Message
        • Custom Errors
      • Facades

        • About Facades
      • Meta

        • About Meta
        • Prerequisites
        • Supported Elements
        • Set & Get
        • Inheritance
        • Outside Changes
        • TC39 Proposal
        • Target Meta
      • Mixins

        • About Mixins
        • New Mixin
        • Apply Mixins
        • Instanceof
        • Inheritance
        • Onward
      • Object

        • About Objects
        • Forget
        • Forget All
        • Get
        • Has
        • Has All
        • Has Any
        • Has Unique ID
        • Is Cloneable
        • Is Populatable
        • Isset
        • Merge
        • Populate
        • Set
        • Unique ID
      • Reflections

        • About reflections
        • Assert Has Prototype Prop.
        • Class Looks Like
        • Class Own Keys
        • Get All Parents Of Class
        • Get Class Prop. Descriptor
        • Get Class Prop. Descriptors
        • Get Constructor Name
        • Get Name Or Desc. Tag
        • Get Parent Of Class
        • Has All Methods
        • Has Method
        • Has Prototype Property
        • Is Callable
        • Is Class Constructor
        • Is Class Method Reference
        • Is Constructor
        • Is Key Safe
        • Is Key Unsafe
        • Is Method
        • Is Subclass
        • Is Subclass Or Looks Like
        • Is WeakKind
      • Misc

        • About Misc.
        • Desc. Tag
        • Empty
        • Is Key
        • Is Primitive
        • Is Property Key
        • Isset
        • Merge Keys
        • To Weak Ref.
      • Callback Wrapper
    • Vuepress Utils

      • Introduction
      • How to install
      • Navigation

        • Archive
      • Plugins

        • Last Updated
      • Components

        • Version Disclaimer
    • XYZ (test package)

Set and Get Metadata

  • Set Metadata
  • Get Metadata
    • Default Value
  • Callback

Set Metadata

To define metadata on a class or its elements, use meta(). It accepts the following arguments:

  • key: name of metadata identifier. Can also be a path (see set).
  • value: arbitrary data. Can be a primitive value, an object, or a function.

To obtain metadata, use the getMeta() method. You can also use getAllMeta(), if you wish to obtain all available metadata for a target class.

import { meta } from '@aedart/support/meta';

@meta('service_alias', 'locationSearcher')
class Service
{
    @meta('name', 'Name of service') name;
    
    @meta('fetch.desc', 'Fetches resource via a gateway')
    @meta('fetch.dependencies', [ 'my-gateway' ])
    async fetch(gateway)
    {
        // ...implementation not shown...
    }
}

Get Metadata

Use getMeta() or getAllMeta() to retrieve metadata.

import { getMeta, getAllMeta } from '@aedart/support/meta';

const service = new Service();

const desc = getMeta(Service, 'fetch.desc');
const dependencies = getMeta(Service, 'fetch.dependencies');

// Or, obtain all metadata
const allMeta = getAllMeta(Service);

Metadata Availability

Depending on the kind of element that is decorated, metadata might only become available for reading, after a new class instance has been instantiated. This is true for the following elements:

  • method
  • getter
  • setter
  • field
  • accessor

Static Elements

If an element is declared as static, then it's metadata becomes available as soon as the class has been defined.

Default Value

The getMeta() method also offers a defaultValue argument, which is returned, in case that a metadata value does not exist for a given identifier.

const description = getMeta(Service, 'fetch.desc', 'N/A');

Callback

If you need to create more advanced metadata, you can specify a callback as the first argument for the meta() decorator method. When using a callback you gain access to the target that is being decorated, as well as the decorator context. The callback MUST return an object that contains a key and a value property.

import { meta } from '@aedart/support/meta';

class Service {

    @meta((target, context) => {
        return {
            key: context.name,
            value: '...'
        }
    })
    delegateTo(gateway) {
        // ...not shown...
    }
}

Although the above example is a bit cumbersome to read, it shows a simple way to defined metadata for a method, which utilises the decorator context. If you wish, you can use this approach to create your own specialised meta decorators. Doing so can also improve the readability of your class. Consider the following example:

import { meta } from '@aedart/support/meta';

function delegateMeta() {
    return meta((target, context) => {
        return {
            key: context.name,
            value: '...'
        }
    });
}

class Service {

    @delegateMeta()
    delegateTo(gateway) {
        // ...not shown...
    }
}
Edit page
Last Updated:
Contributors: alin
Prev
Supported Elements
Next
Inheritance