@bleed-believer/meta
TypeScript icon, indicating that this package has built-in type declarations

0.11.1 • Public • Published

@bleed-believer/meta

Adds metadata easifuly to your objects.

Disclaimer

Since ESM hs been heavely adopted by the whole node.js community (including transpilers, unit testing, and many other libraries), the CJS support has been removed. If you still needs the CJS compatibility, please use this version or earlier.

Installation

Use npm to get the last version:

npm i --save @bleed-believer/meta

Concepts

To understand how this library works, first we need to define some concepts:

Target:

Any object that you want to write inside of, data about itself. It's common, for example, have classes that you need to specify how these classes will be instantiated and used for a particular third party tool or library. So in thoses cases you may need to write data dinamically into the prototype por example.

Metadata:

The data do you want to attach to the target. This data normally describes how to use the target object to another one that requires for it. We use interfaces to describe the structure of a kind of metadata.

How to use

  • First create an interface with the structure of your metadata:

    export interface EndpointMeta {
        path:   string;
        routes: {
            method: string;
            key:    string;
        }[];
    }
  • Create a new instance of MetaManager:

    export const ENDPOINT = new MetaManager<EndpointMeta>();
  • Now you can attach metadata into your objects:

    import { ENDPOINT } from './endpoint.js';
    
    export class UserController {
        get(): Promise<void> {
            // bla bla bla bla bla bla
        }
        
        set(): Promise<void> {
            // bla bla bla bla bla bla
        }
    }
    
    ENDPOINT.set(UserController, {
        path: 'user',
        routes: [
            { method: 'GET',    key: 'get' },
            { method: 'POST',   key: 'set' },
        ]
    });
  • ...or get its metadata:

    import { UserController } from './user.controller.js';
    import { ENDPOINT } from './endpoint.js';
    
    const meta = ENDPOINT.get(UserController);
    console.log(meta);
    // Prints:
    // {
    //     path: 'user',
    //     routes: [
    //         { method: 'GET',    key: 'get' },
    //         { method: 'POST',   key: 'set' },
    //     ]
    // }

Readme

Keywords

Package Sidebar

Install

npm i @bleed-believer/meta

Weekly Downloads

1

Version

0.11.1

License

MIT

Unpacked Size

9.97 kB

Total Files

17

Last publish

Collaborators

  • sleep-written