-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
merge request from discovery-meta-tags
feat: discover meta tags, auto binding
- Loading branch information
Showing
16 changed files
with
136 additions
and
58 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 @@ | ||
export * from './meta-teg.discovery'; |
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,57 @@ | ||
import { Inject, Injectable } from '@nestjs/common'; | ||
import { ModulesContainer, Reflector } from '@nestjs/core'; | ||
import { MetadataScanner } from '@nestjs/core'; | ||
import { InstanceWrapper } from '@nestjs/core/injector/instance-wrapper'; | ||
import { MetaTegsMap } from '../interfaces'; | ||
|
||
@Injectable() | ||
export class MetaTegsScannerService { | ||
constructor( | ||
private readonly metadataScanner: MetadataScanner, | ||
private readonly reflector: Reflector, | ||
private readonly modulesContainer: ModulesContainer, | ||
@Inject('TARGET_MODULE') private readonly targetModuleName: string, | ||
) {} | ||
|
||
public scan(metaTeg: string) { | ||
const rmqMessagesMap = new Map(); | ||
const modules = [...this.modulesContainer.values()]; | ||
|
||
const currentModule = modules.find( | ||
(module) => module.metatype?.name === this.targetModuleName, | ||
); | ||
if (!currentModule) return rmqMessagesMap; | ||
const providers = [...currentModule.providers.values()]; | ||
const controllers = [...currentModule.controllers.values()]; | ||
[...providers, ...controllers].forEach((provider: InstanceWrapper) => { | ||
const { instance } = provider; | ||
const prototype = Object.getPrototypeOf(instance); | ||
this.metadataScanner | ||
.getAllMethodNames(prototype) | ||
.forEach((name: string) => | ||
this.lookupMethods( | ||
metaTeg, | ||
rmqMessagesMap, | ||
instance, | ||
prototype, | ||
name, | ||
), | ||
); | ||
}); | ||
|
||
return rmqMessagesMap; | ||
} | ||
|
||
private lookupMethods( | ||
metaTeg: string, | ||
rmqMessagesMap: MetaTegsMap, | ||
instance: object, | ||
prototype: object, | ||
methodName: string, | ||
) { | ||
const method = prototype[methodName]; | ||
const event = this.reflector.get<string>(metaTeg, method); | ||
const boundHandler = instance[methodName].bind(instance); | ||
if (event) rmqMessagesMap.set(event, boundHandler); | ||
} | ||
} |
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,2 +1,4 @@ | ||
export const RMQ_CONNECT_OPTIONS = 'RMQ_CONNECT_OPTIONS'; | ||
export const RMQ_BROKER_OPTIONS = 'RMQ_BROKER_OPTIONS'; | ||
export const RMQ_MESSAGE_META_TEG = 'RMQ_MESSAGE_META_TEG'; | ||
export const RMQ_ROUTES_TRANSFORM = 'RMQ_ROUTES_TRANSFORM'; |
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,7 @@ | ||
import { RMQ_MESSAGE_META_TEG } from 'lib/constants'; | ||
|
||
export function RMQMessage(event: string) { | ||
return function (target: any, propertyKey: string | symbol, descriptor: any) { | ||
Reflect.defineMetadata(RMQ_MESSAGE_META_TEG, event, descriptor.value); | ||
}; | ||
} |
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,6 @@ | ||
import { applyDecorators, SetMetadata } from '@nestjs/common'; | ||
import { RMQ_ROUTES_TRANSFORM } from 'lib/constants'; | ||
|
||
export const RMQTransform = (): MethodDecorator => { | ||
return applyDecorators(SetMetadata(RMQ_ROUTES_TRANSFORM, true)); | ||
}; |
This file was deleted.
Oops, something went wrong.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,3 @@ | ||
export * from './connection'; | ||
export * from './exchange'; | ||
export * from './queue'; | ||
export * from './rmq-options.interface'; | ||
export * from './metategs'; | ||
export * from './rmqService'; |
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 @@ | ||
export type MetaTegsMap = Map<string | symbol, (...args: any[]) => any>; |
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
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 @@ | ||
export interface ImqService {} |
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
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