Get a List of Reflected Metadata of Decorated Class Fields

I use reflect-metadatawith typescript. I wrote my own property decorator and named it Field. How to get a list of fields / properties that are decorated Field, of any type. For example: I want to get a field ProductID, ProductNamewith their metadata from the class Product/, shown below /.

import 'reflect-metadata';

export const FIELD_METADATA_KEY = 'Field';

export interface FieldDecorator {
  field?: string;
  title?: string;
  type?: string;
}

export function Field(field: FieldDecorator) {
  return Reflect.metadata(FIELD_METADATA_KEY, field);
}


export class Product {
  @Field({
    title: 'Product Id'
  })
  ProductID: string;

  @Field({
    title: 'Product Name',
    type: 'text'
  })
  ProductName: string;

  UnitPrice: number; //not decorated
}
+4
source share

Source: https://habr.com/ru/post/1679617/


All Articles