-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #10 from codibre/decorator-helpers
feat: creating decorator helpers
- Loading branch information
Showing
7 changed files
with
88 additions
and
28 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
import { getProperMetadataTarget } from './internal/get-proper-metadata-target'; | ||
import { getClassMetadata } from './meta-info'; | ||
import { ClassType, Key } from './meta-type'; | ||
|
||
export const DEFAULT = Symbol('default'); | ||
|
||
export function applyPropertyAndMethodsDecorators<T extends object = object>( | ||
cls: ClassType<T>, | ||
propertyAndMethodsDecorators: Partial< | ||
Record<Key<T> | typeof DEFAULT, (MethodDecorator | PropertyDecorator)[]> | ||
>, | ||
) { | ||
const def = propertyAndMethodsDecorators[DEFAULT]; | ||
const meta = getClassMetadata(cls); | ||
if (!meta) throw new Error('No class metadata found'); | ||
for (const prop of meta?.properties.values()) { | ||
const decorators = propertyAndMethodsDecorators[prop.name] ?? def; | ||
if (decorators) { | ||
Reflect.decorate( | ||
decorators, | ||
getProperMetadataTarget(prop, cls), | ||
prop.name, | ||
); | ||
} | ||
} | ||
for (const prop of meta?.methods.values()) { | ||
const decorators = propertyAndMethodsDecorators[prop.name] ?? def; | ||
if (decorators) { | ||
Reflect.decorate( | ||
decorators, | ||
getProperMetadataTarget(prop, cls), | ||
prop.name, | ||
); | ||
} | ||
} | ||
} | ||
|
||
export function applyClassDecorators<T extends object = object>( | ||
cls: ClassType<T>, | ||
decorators: ClassDecorator[], | ||
) { | ||
Reflect.decorate(decorators, cls); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,7 @@ | ||
export * from './decorator-helpers'; | ||
export * from './meta-info'; | ||
export * from './meta-type'; | ||
|
||
import './plugin/decorators'; | ||
import { blockAccess } from './internal'; | ||
|
||
blockAccess(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
import { PropertyMetadata, ClassType, MethodMetadata } from '../meta-type'; | ||
|
||
export function getProperMetadataTarget<T extends object = object>( | ||
prop: PropertyMetadata<T> | MethodMetadata<T>, | ||
cls: ClassType, | ||
): Object { | ||
return prop.modifiers.static ? cls : cls.prototype; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,2 @@ | ||
export * from './get-proper-metadata-target'; | ||
export * from './meta-storage'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters