Skip to content

Commit

Permalink
docs: mention reflect-metadata behaviour
Browse files Browse the repository at this point in the history
  • Loading branch information
antongolub committed Jan 23, 2024
1 parent 7ec8fa6 commit c036b0c
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 3 deletions.
9 changes: 9 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
6 changes: 3 additions & 3 deletions src/main/ts/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,12 +56,12 @@ export function getClassChain(ctor: any): any[] {
// Proxy is slow, so we use this wrapper
export const Refl: Pick<typeof Reflect, 'getMetadataKeys' | 'defineMetadata' | 'getMetadata'> = ({
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)
}
})

0 comments on commit c036b0c

Please sign in to comment.