@esfx/metadata Package
Provides an API for defining metadata about an object.
Installation
npm i @esfx/metadata
Usage
import { Metadata, getObjectMetadata, getPropertyMetadata, getParameterMetadata } from "@esfx/metadata";
const Service = name => Metadata("Service", name);
const ReturnType = type => Metadata("ReturnType", type);
const Type = type => Metadata("Type", type);
@Service("MyService")
class MyClass {
@ReturnType("string")
method(@Type("number") x: number) {
return "hi";
}
}
const c = new MyClass();
getObjectMetadata(MyClass, "Service"); // "MyService"
getPropertyMetadata(c, "method", "ReturnType"); // "string"
getParameterMetadata(c, "method", 0, "Type"); // "number"
Functions
defineObjectMetadata(target, metadataKey, metadataValue)
Define metadata on an object.
Declaration
export declare function defineObjectMetadata(target: object, metadataKey: MetadataKey, metadataValue: unknown): void;
Parameters
- metadataValue
- unknown
The metadata value.
Returns
defineParameterMetadata(target, propertyKey, parameterIndex, metadataKey, metadataValue)
Define metadata on a parameter of a method of an object.
Declaration
export declare function defineParameterMetadata(target: object, propertyKey: PropertyKey, parameterIndex: number, metadataKey: MetadataKey, metadataValue: unknown): void;
Parameters
- propertyKey
- PropertyKey
The name of the method.
- metadataValue
- unknown
The metadata value.
Returns
definePropertyMetadata(target, propertyKey, metadataKey, metadataValue)
Define metadata for a property of an object. The property does not have to exist.
Declaration
export declare function definePropertyMetadata(target: object, propertyKey: PropertyKey, metadataKey: MetadataKey, metadataValue: unknown): void;
Parameters
- propertyKey
- PropertyKey
The name of the property.
- metadataValue
- unknown
The metadata value.
Returns
deleteObjectMetadata(target, metadataKey)
Deletes an own metadata key from an object.
Declaration
export declare function deleteObjectMetadata(target: object, metadataKey: MetadataKey): boolean;
Parameters
Returns
deleteParameterMetadata(target, propertyKey, parameterIndex, metadataKey)
Deletes an own metadata key from a parameter of a method of an object.
Declaration
export declare function deleteParameterMetadata(target: object, propertyKey: PropertyKey, parameterIndex: number, metadataKey: MetadataKey): boolean;
Parameters
- propertyKey
- PropertyKey
The name of the method.
Returns
deletePropertyMetadata(target, propertyKey, metadataKey)
Deletes an own metadata key from a property of an object.
Declaration
export declare function deletePropertyMetadata(target: object, propertyKey: PropertyKey, metadataKey: MetadataKey): boolean;
Parameters
- propertyKey
- PropertyKey
The name of the property.
Returns
getMetadataParameterLength(target, propertyKey)
Gets the maximum known length of a method of an object or its prototypes from its parameters that have metadata metadata.
Declaration
export declare function getMetadataParameterLength(target: object, propertyKey: PropertyKey): number;
Parameters
- propertyKey
- PropertyKey
The name of the method.
Returns
getMetadataProperties(target)
Gets the property names for an object or its prototypes for which there is metadata defined.
Declaration
export declare function getMetadataProperties(target: object): (string | symbol)[];
Parameters
Returns
getObjectMetadata(target, metadataKey)
Gets the metadata value for a metadata key on an object or its prototypes.
Declaration
export declare function getObjectMetadata(target: object, metadataKey: MetadataKey): unknown;
Parameters
Returns
getObjectMetadataKeys(target)
Gets the metadata keys defined on an object or its prototypes.
Declaration
export declare function getObjectMetadataKeys(target: object): MetadataKey[];
Parameters
Returns
getOwnMetadataParameterLength(target, propertyKey)
Gets the maximum known length of a method of an object from its parameters that have metadata metadata.
Declaration
export declare function getOwnMetadataParameterLength(target: object, propertyKey: PropertyKey): number;
Parameters
- propertyKey
- PropertyKey
The name of the method.
Returns
getOwnMetadataProperties(target)
Gets the own property names for an object for which there is metadata defined.
Declaration
export declare function getOwnMetadataProperties(target: object): (string | symbol)[];
Parameters
Returns
getOwnObjectMetadata(target, metadataKey)
Gets the metadata value for an own metadata key on an object.
Declaration
export declare function getOwnObjectMetadata(target: object, metadataKey: MetadataKey): unknown;
Parameters
Returns
getOwnObjectMetadataKeys(target)
Gets the own metadata keys defined on an object.
Declaration
export declare function getOwnObjectMetadataKeys(target: object): MetadataKey[];
Parameters
Returns
getOwnParameterMetadata(target, propertyKey, parameterIndex, metadataKey)
Gets the metadata value for an own metadata key on a parameter of a method of an object.
Declaration
export declare function getOwnParameterMetadata(target: object, propertyKey: PropertyKey, parameterIndex: number, metadataKey: MetadataKey): unknown;
Parameters
- propertyKey
- PropertyKey
The name of the method.
Returns
getOwnParameterMetadataKeys(target, propertyKey, parameterIndex)
Gets the own metadata keys defined on a parameter of a method of an object.
Declaration
export declare function getOwnParameterMetadataKeys(target: object, propertyKey: PropertyKey, parameterIndex: number): MetadataKey[];
Parameters
- propertyKey
- PropertyKey
The name of the method.
Returns
getOwnPropertyMetadata(target, propertyKey, metadataKey)
Gets the metadata value for an own metadata key on a property of an object.
Declaration
export declare function getOwnPropertyMetadata(target: object, propertyKey: PropertyKey, metadataKey: MetadataKey): unknown;
Parameters
- propertyKey
- PropertyKey
The name of the property.
Returns
getOwnPropertyMetadataKeys(target, propertyKey)
Gets the own metadata keys defined on a property of an object.
Declaration
export declare function getOwnPropertyMetadataKeys(target: object, propertyKey: PropertyKey): MetadataKey[];
Parameters
- propertyKey
- PropertyKey
The name of the property.
Returns
getParameterMetadata(target, propertyKey, parameterIndex, metadataKey)
Gets the metadata value for a metadata key on a parameter of a method of an object or its prototypes.
Declaration
export declare function getParameterMetadata(target: object, propertyKey: PropertyKey, parameterIndex: number, metadataKey: MetadataKey): unknown;
Parameters
- propertyKey
- PropertyKey
The name of the method.
Returns
getParameterMetadataKeys(target, propertyKey, parameterIndex)
Gets the metadata keys defined on a parameter of a method of an object or its prototypes.
Declaration
export declare function getParameterMetadataKeys(target: object, propertyKey: PropertyKey, parameterIndex: number): MetadataKey[];
Parameters
- propertyKey
- PropertyKey
The name of the method.
Returns
getPropertyMetadata(target, propertyKey, metadataKey)
Gets the metadata value for a metadata key on a property of an object or its prototypes.
Declaration
export declare function getPropertyMetadata(target: object, propertyKey: PropertyKey, metadataKey: MetadataKey): unknown;
Parameters
- propertyKey
- PropertyKey
The name of the property.
Returns
getPropertyMetadataKeys(target, propertyKey)
Gets the metadata keys defined on a property of an object or its prototypes.
Declaration
export declare function getPropertyMetadataKeys(target: object, propertyKey: PropertyKey): MetadataKey[];
Parameters
- propertyKey
- PropertyKey
The name of the property.
Returns
hasObjectMetadata(target, metadataKey)
Tests whether an object or its prototypes have metadata for the provided key.
Declaration
export declare function hasObjectMetadata(target: object, metadataKey: MetadataKey): boolean;
Parameters
Returns
hasOwnObjectMetadata(target, metadataKey)
Tests whether an object has own metadata for the provided key.
Declaration
export declare function hasOwnObjectMetadata(target: object, metadataKey: MetadataKey): boolean;
Parameters
Returns
hasOwnParameterMetadata(target, propertyKey, parameterIndex, metadataKey)
Tests whether a parameter of a method of an object has own metadata for the provided key.
Declaration
export declare function hasOwnParameterMetadata(target: object, propertyKey: PropertyKey, parameterIndex: number, metadataKey: MetadataKey): boolean;
Parameters
- propertyKey
- PropertyKey
The name of the method.
Returns
hasOwnPropertyMetadata(target, propertyKey, metadataKey)
Tests whether a property of an object has own metadata for the provided key.
Declaration
export declare function hasOwnPropertyMetadata(target: object, propertyKey: PropertyKey, metadataKey: MetadataKey): boolean;
Parameters
- propertyKey
- PropertyKey
The name of the property.
Returns
hasParameterMetadata(target, propertyKey, parameterIndex, metadataKey)
Tests whether a parameter of a method of an object or its prototypes have metadata for the provided key.
Declaration
export declare function hasParameterMetadata(target: object, propertyKey: PropertyKey, parameterIndex: number, metadataKey: MetadataKey): boolean;
Parameters
- propertyKey
- PropertyKey
The name of the method.
Returns
hasPropertyMetadata(target, propertyKey, metadataKey)
Tests whether a property of an object or its prototypes have metadata for the provided key.
Declaration
export declare function hasPropertyMetadata(target: object, propertyKey: PropertyKey, metadataKey: MetadataKey): boolean;
Parameters
- propertyKey
- PropertyKey
The name of the property.
Returns
Variables
metadata
Attaches metadata to a class, member, or parameter.
Declaration
metadata: ((metadataKey: MetadataKey, metadataValue?: unknown) => {
(target: Function): void;
(target: object, key: PropertyKey, descriptor?: PropertyDescriptor | undefined): void;
(target: object, key: PropertyKey, parameterIndex: number): void;
}) & {
defineObjectMetadata: typeof defineObjectMetadata;
deleteObjectMetadata: typeof deleteObjectMetadata;
hasOwnObjectMetadata: typeof hasOwnObjectMetadata;
hasObjectMetadata: typeof hasObjectMetadata;
getOwnObjectMetadata: typeof getOwnObjectMetadata;
getObjectMetadata: typeof getObjectMetadata;
getOwnObjectMetadataKeys: typeof getOwnObjectMetadataKeys;
getObjectMetadataKeys: typeof getObjectMetadataKeys;
definePropertyMetadata: typeof definePropertyMetadata;
deletePropertyMetadata: typeof deletePropertyMetadata;
hasOwnPropertyMetadata: typeof hasOwnPropertyMetadata;
hasPropertyMetadata: typeof hasPropertyMetadata;
getOwnPropertyMetadata: typeof getOwnPropertyMetadata;
getPropertyMetadata: typeof getPropertyMetadata;
getOwnPropertyMetadataKeys: typeof getOwnPropertyMetadataKeys;
getPropertyMetadataKeys: typeof getPropertyMetadataKeys;
getOwnMetadataProperties: typeof getOwnMetadataProperties;
getMetadataProperties: typeof getMetadataProperties;
defineParameterMetadata: typeof defineParameterMetadata;
deleteParameterMetadata: typeof deleteParameterMetadata;
hasOwnParameterMetadata: typeof hasOwnParameterMetadata;
hasParameterMetadata: typeof hasParameterMetadata;
getOwnParameterMetadata: typeof getOwnParameterMetadata;
getParameterMetadata: typeof getParameterMetadata;
getOwnParameterMetadataKeys: typeof getOwnParameterMetadataKeys;
getParameterMetadataKeys: typeof getParameterMetadataKeys;
getOwnMetadataParameterLength: typeof getOwnMetadataParameterLength;
getMetadataParameterLength: typeof getMetadataParameterLength;
}
Type Aliases
MetadataKey
Declaration
export declare type MetadataKey = string | symbol | number | boolean | bigint | object;