Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(microservices): allow to set custom transport to decorators #9347

Merged
merged 1 commit into from
Mar 21, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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