From c036b0c3e6c563299e4c800526b297be489dd960 Mon Sep 17 00:00:00 2001 From: Anton Golub Date: Wed, 24 Jan 2024 00:02:03 +0300 Subject: [PATCH] docs: mention reflect-metadata behaviour --- README.md | 9 +++++++++ src/main/ts/utils.ts | 6 +++--- 2 files changed, 12 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index 44b3395d..dd15da50 100644 --- a/README.md +++ b/README.md @@ -163,6 +163,15 @@ class Foo { } ``` +#### reflect-metadata +If the global `Reflect` provides `getMetadataKeys`, `getMetadata` and `defineMetadata` methods, they will be used to attach corresponding data to the decorated targets. +To enable, just add `reflect-metadata` to your dependencies and load it. +```ts +import 'reflect-metadata' +import {constructDecorator} from '@qiwi/decorator-utils' +// ... +``` + ### Refs * [JS decorators by Axel Rauschmayer](https://2ality.com/2022/10/javascript-decorators.html) * ["aspect" syntax for JS](https://github.com/tc39/proposal-decorators) diff --git a/src/main/ts/utils.ts b/src/main/ts/utils.ts index 9b01f99f..39d03852 100644 --- a/src/main/ts/utils.ts +++ b/src/main/ts/utils.ts @@ -56,12 +56,12 @@ export function getClassChain(ctor: any): any[] { // Proxy is slow, so we use this wrapper export const Refl: Pick = ({ getMetadataKeys(target: any) { - return Reflect.getMetadataKeys?.(target) || [] + return Reflect?.getMetadataKeys?.(target) || [] }, defineMetadata(metadataKey: any, metadataValue: any, target: any) { - return Reflect.defineMetadata?.(metadataKey, metadataValue, target) + return Reflect?.defineMetadata?.(metadataKey, metadataValue, target) }, getMetadata(metadataKey: any, target: any) { - return Reflect.getMetadata?.(metadataKey, target) + return Reflect?.getMetadata?.(metadataKey, target) } })