-
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.
The metadata was being registered, but not easily accessible. Some functions were introducuted to make it possible. Also, a jest friendly export had been added to make it easier to integrate with it
- Loading branch information
1 parent
d483571
commit eff2f49
Showing
11 changed files
with
213 additions
and
34 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
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,2 @@ | ||
module.exports = require('./dist'); | ||
module.exports.default = module.exports; |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
import { before } from './emitter'; | ||
|
||
export const name = 'nestjs-auto-reflect-metadata-emitter'; | ||
export const version = 1; | ||
export const factory = before; |
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,3 @@ | ||
export * from './emitter'; | ||
export * from './factory'; | ||
export * from './meta-info'; |
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,96 @@ | ||
import 'reflect-metadata'; | ||
// eslint-disable-next-line @typescript-eslint/no-explicit-any | ||
export type ClassType = abstract new (...args: any[]) => unknown; | ||
export type Key = string | symbol; | ||
|
||
export interface ConstructorMetadata { | ||
cls: ClassType; | ||
args: unknown[]; | ||
} | ||
export interface MethodMetadata { | ||
name: Key; | ||
args: unknown[]; | ||
returnType: unknown; | ||
propertyDescriptor: PropertyDescriptor; | ||
} | ||
export interface PropertyMetadata { | ||
name: Key; | ||
type: unknown; | ||
} | ||
|
||
interface ClassMetadata { | ||
ctor: ConstructorMetadata; | ||
properties: Map<Key, PropertyMetadata>; | ||
methods: Map<Key, MethodMetadata>; | ||
} | ||
const metadata = new Map<object, ClassMetadata>(); | ||
|
||
function getMeta(prototype: object) { | ||
let ref = metadata.get(prototype); | ||
if (!ref) { | ||
ref = { | ||
ctor: undefined as unknown as ConstructorMetadata, | ||
properties: new Map(), | ||
methods: new Map(), | ||
}; | ||
metadata.set(prototype, ref); | ||
} | ||
return ref; | ||
} | ||
|
||
export function registerClassMetadata(cls: ClassType) { | ||
const { prototype } = cls; | ||
const ref = getMeta(prototype); | ||
ref.ctor = { | ||
cls, | ||
args: Reflect.getMetadata('design:paramtypes', cls), | ||
}; | ||
} | ||
|
||
export function registerPropertyMetadata(prototype: object, key: Key) { | ||
const ref = getMeta(prototype); | ||
const type = Reflect.getMetadata('design:type', prototype, key); | ||
ref.properties.set(key, { | ||
name: key, | ||
type, | ||
}); | ||
} | ||
|
||
export function registerMethodMetadata( | ||
prototype: object, | ||
key: Key, | ||
propertyDescriptor: PropertyDescriptor, | ||
) { | ||
const ref = getMeta(prototype); | ||
const returnType = Reflect.getMetadata('design:returntype', prototype, key); | ||
ref.methods.set(key, { | ||
name: key, | ||
args: Reflect.getMetadata('design:paramtypes', prototype, key), | ||
returnType, | ||
propertyDescriptor, | ||
}); | ||
} | ||
|
||
/** | ||
* Return metadata of the class informed, or undefined if there is none | ||
* @param cls The Class to get metadata from | ||
*/ | ||
export function getClassMetadata(cls: ClassType) { | ||
return metadata.get(cls.prototype); | ||
} | ||
|
||
/** | ||
* Returns an iterable that allows you to iterate over all the metadata | ||
* collected. You must filter it as you need | ||
*/ | ||
export function iterateMetadata() { | ||
return metadata.values(); | ||
} | ||
|
||
/** | ||
* Clear all the metadata collected. We recommend you to call this function | ||
* at the and of the metadata consumption | ||
*/ | ||
export function clearAllMetadata() { | ||
metadata.clear(); | ||
} |
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 was deleted.
Oops, something went wrong.
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