Skip to content

Commit

Permalink
feat(microservices): allow to set custom transport to decorators
Browse files Browse the repository at this point in the history
  • Loading branch information
xavierchow committed Mar 17, 2022
1 parent 0dc4f91 commit 4539568
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 15 deletions.
22 changes: 15 additions & 7 deletions packages/microservices/decorators/event-pattern.decorator.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@
import { isObject, isNumber, isNil } from '@nestjs/common/utils/shared.utils';
import {
isObject,
isNumber,
isNil,
isSymbol,
} from '@nestjs/common/utils/shared.utils';
import {
PATTERN_HANDLER_METADATA,
PATTERN_METADATA,
Expand All @@ -13,26 +18,29 @@ import { Transport } from '../enums';
*/
export const EventPattern: {
<T = string>(metadata?: T): MethodDecorator;
<T = string>(metadata?: T, transport?: Transport): MethodDecorator;
<T = string>(metadata?: T, transport?: Transport | symbol): MethodDecorator;
<T = string>(metadata?: T, extras?: Record<string, any>): MethodDecorator;
<T = string>(
metadata?: T,
transport?: Transport,
transport?: Transport | symbol,
extras?: Record<string, any>,
): MethodDecorator;
} = <T = string>(
metadata?: T,
transportOrExtras?: Transport | Record<string, any>,
transportOrExtras?: Transport | symbol | Record<string, any>,
maybeExtras?: Record<string, any>,
): MethodDecorator => {
let transport: Transport;
let transport: Transport | symbol;
let extras: Record<string, any>;
if (isNumber(transportOrExtras) && isNil(maybeExtras)) {
if (
(isNumber(transportOrExtras) || isSymbol(transportOrExtras)) &&
isNil(maybeExtras)
) {
transport = transportOrExtras;
} else if (isObject(transportOrExtras) && isNil(maybeExtras)) {
extras = transportOrExtras;
} else {
transport = transportOrExtras as Transport;
transport = transportOrExtras as Transport | symbol;
extras = maybeExtras;
}
return (
Expand Down
22 changes: 15 additions & 7 deletions packages/microservices/decorators/message-pattern.decorator.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@
import { isObject, isNumber, isNil } from '@nestjs/common/utils/shared.utils';
import {
isObject,
isNumber,
isNil,
isSymbol,
} from '@nestjs/common/utils/shared.utils';
/* eslint-disable @typescript-eslint/no-use-before-define */
import {
PATTERN_HANDLER_METADATA,
Expand All @@ -23,30 +28,33 @@ export const MessagePattern: {
<T = PatternMetadata | string>(metadata?: T): MethodDecorator;
<T = PatternMetadata | string>(
metadata?: T,
transport?: Transport,
transport?: Transport | symbol,
): MethodDecorator;
<T = PatternMetadata | string>(
metadata?: T,
extras?: Record<string, any>,
): MethodDecorator;
<T = PatternMetadata | string>(
metadata?: T,
transport?: Transport,
transport?: Transport | symbol,
extras?: Record<string, any>,
): MethodDecorator;
} = <T = PatternMetadata | string>(
metadata?: T,
transportOrExtras?: Transport | Record<string, any>,
transportOrExtras?: Transport | symbol | Record<string, any>,
maybeExtras?: Record<string, any>,
): MethodDecorator => {
let transport: Transport;
let transport: Transport | symbol;
let extras: Record<string, any>;
if (isNumber(transportOrExtras) && isNil(maybeExtras)) {
if (
(isNumber(transportOrExtras) || isSymbol(transportOrExtras)) &&
isNil(maybeExtras)
) {
transport = transportOrExtras;
} else if (isObject(transportOrExtras) && isNil(maybeExtras)) {
extras = transportOrExtras;
} else {
transport = transportOrExtras as Transport;
transport = transportOrExtras as Transport | symbol;
extras = maybeExtras;
}
return (
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { Transport } from '../enums';

export interface CustomTransportStrategy {
readonly transportId?: Transport;
readonly transportId?: Transport | symbol;
listen(callback: (...optionalParams: unknown[]) => any): any;
close(): any;
}
23 changes: 23 additions & 0 deletions packages/microservices/test/listeners-controller.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,11 @@ describe('ListenersController', () => {
metadataExplorer: ListenerMetadataExplorer,
server: any,
serverTCP: any,
serverCustom: any,
customTransport: Symbol,
addSpy: sinon.SinonSpy,
addSpyTCP: sinon.SinonSpy,
addSpyCustom: sinon.SinonSpy,
proxySpy: sinon.SinonSpy,
container: NestContainer,
injector: Injector,
Expand Down Expand Up @@ -61,6 +64,12 @@ describe('ListenersController', () => {
addHandler: addSpyTCP,
transportId: Transport.TCP,
};
addSpyCustom = sinon.spy();
customTransport = Symbol();
serverCustom = {
addHandler: addSpyCustom,
transportId: customTransport,
};
});

describe('registerPatternHandlers', () => {
Expand Down Expand Up @@ -118,6 +127,20 @@ describe('ListenersController', () => {
instance.registerPatternHandlers(new InstanceWrapper(), serverTCP, '');
expect(addSpyTCP.calledTwice).to.be.true;
});
it(`should call "addHandler" method of server with custom transportID for pattern handler with the same custom token`, () => {
const serverHandlers = [
{
pattern: { cmd: 'test' },
targetCallback: 'tt',
transport: customTransport,
},
{ pattern: 'test2', targetCallback: '2', transport: Transport.KAFKA },
];

explorer.expects('explore').returns(serverHandlers);
instance.registerPatternHandlers(new InstanceWrapper(), serverCustom, '');
expect(addSpyCustom.calledOnce).to.be.true;
});
it(`should call "addHandler" method of server with extras data`, () => {
const serverHandlers = [
{ pattern: 'test', targetCallback: 'tt', extras: { param: 'value' } },
Expand Down

0 comments on commit 4539568

Please sign in to comment.