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

fix(microservices): when postfixId is an empty string on kafka #11540

Closed
wants to merge 9 commits into from
10 changes: 9 additions & 1 deletion packages/microservices/client/client-kafka.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ export class ClientKafka extends ClientProxy {
const consumerOptions =
this.getOptionsProp(this.options, 'consumer') || ({} as ConsumerConfig);
const postfixId =
this.getOptionsProp(this.options, 'postfixId') ?? '-client';
this.getOptionsProp(this.options, 'postfixId', '-client', true);
this.producerOnlyMode =
this.getOptionsProp(this.options, 'producerOnlyMode') || false;

Expand Down Expand Up @@ -283,6 +283,14 @@ export class ClientKafka extends ClientProxy {
}
}

public getGroupId() {
return this.groupId
}

public getClientId() {
return this.clientId
}

protected getResponsePatternName(pattern: string): string {
return `${pattern}.reply`;
}
Expand Down
8 changes: 6 additions & 2 deletions packages/microservices/client/client-proxy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -129,8 +129,12 @@ export abstract class ClientProxy {
protected getOptionsProp<
T extends ClientOptions['options'],
K extends keyof T,
>(obj: T, prop: K, defaultValue: T[K] = undefined) {
return (obj && obj[prop]) || defaultValue;
>(obj: T, prop: K, defaultValue: T[K] = undefined, nullishCoalesce = false) {
if (nullishCoalesce) {
return (obj && obj[prop]) ?? defaultValue;
} else {
return (obj && obj[prop]) || defaultValue;
}
}

protected normalizePattern(pattern: MsPattern): string {
Expand Down
14 changes: 14 additions & 0 deletions packages/microservices/test/client/client-kafka.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -218,6 +218,20 @@ describe('ClientKafka', () => {

expect(logCreatorSpy.called).to.be.true;
});

it('should allow an empty postfixId', async () => {
const client = new ClientKafka({
postfixId: ''
});
expect(client.getClientId()).to.eq('nestjs-consumer');
expect(client.getGroupId()).to.eq('nestjs-group');
});

it('should postfix clientId and groupId with postfixId', async () => {
Auspicus marked this conversation as resolved.
Show resolved Hide resolved
const client = new ClientKafka({});
expect(client.getClientId()).to.eq('nestjs-consumer-client');
expect(client.getGroupId()).to.eq('nestjs-group-client');
});
});

describe('subscribeToResponseOf', () => {
Expand Down