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: allow for microservice options to come from the di container #12622

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
98 changes: 98 additions & 0 deletions integration/microservices/e2e/sum-rpc-async.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
import {
Controller,
INestMicroservice,
Injectable,
Module,
} from '@nestjs/common';
import {
AsyncOptions,
ClientTCP,
ClientsModule,
MessagePattern,
MicroserviceOptions,
Payload,
TcpClientOptions,
Transport,
} from '@nestjs/microservices';
import { expect } from 'chai';
import { NestFactory } from '@nestjs/core';

let port: number;

do {
port = Math.round(Math.random() * 10000);
} while (port < 1000);

@Injectable()
class RpcOptionsProvider {
getOptions(): TcpClientOptions {
return {
transport: Transport.TCP,
options: {
port,
host: '0.0.0.0',
},
};
}
}

@Controller()
class RpcController {
@MessagePattern({ cmd: 'sum' })
sumPayload(@Payload() payload: number[]) {
return payload.reduce((a, b) => a + b, 0);
}
}

@Module({
imports: [
ClientsModule.register([
{
name: 'RPC_CLIENT',
transport: Transport.TCP,
options: {
port,
host: '0.0.0.0',
},
},
]),
],
controllers: [RpcController],
providers: [RpcOptionsProvider],
})
class RpcModule {}

describe('RPC Async transport', () => {
let app: INestMicroservice;
let client: ClientTCP;

beforeEach(async () => {
app = await NestFactory.createMicroservice<
AsyncOptions<MicroserviceOptions>
>(RpcModule, {
logger: false,
inject: [RpcOptionsProvider],
useFactory: (optionsProvider: RpcOptionsProvider) =>
optionsProvider.getOptions(),
});
Comment on lines +70 to +77
Copy link
Member

@micalevisk micalevisk Oct 22, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wonder if we couldn't have something to make this feels like a traditional dynamic module import...

something like:

const app = await NestFactory.createMicroservice(
  RpcModule,
  MicroserviceOptionsFactory.createAsync<MicroserviceOptions>({
    inject: [RpcOptionsProvider],
    useFactory: (optionsProvider: RpcOptionsProvider) => ({
      logger: false,
      ...optionsProvider.getOptions(),
    })
  })
)

perharps that's too overkill (or impossible) to have? yeah...

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We could, just to give a unified API, but the underlying code and how it gets read in the server options resolver would pretty much be the same. This would just be a simple wrapper around the object we already return, and I don't think there would be much benefit


await app.listen();
client = app.get('RPC_CLIENT', { strict: false });
});

it(`/POST`, done => {
let retData = 0;
client.send({ cmd: 'sum' }, [1, 2, 3, 4, 5]).subscribe({
next: val => (retData += val),
error: done,
complete: () => {
expect(retData).to.eq(15);
done();
},
});
});

afterEach(async () => {
await app.close();
});
});
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Type } from '@nestjs/common';
import { FactoryProvider, InjectionToken, Type } from '@nestjs/common';
import { ConnectionOptions } from 'tls';
import { Transport } from '../enums/transport.enum';
import { ChannelOptions } from '../external/grpc-options.interface';
Expand Down Expand Up @@ -28,6 +28,16 @@ export type MicroserviceOptions =
| KafkaOptions
| CustomStrategy;

export type AsyncMicroserviceOptions = {
inject: InjectionToken[];
useFactory: (...args: any[]) => MicroserviceOptions;
};

export type AsyncOptions<T extends object> = {
inject: InjectionToken[];
useFactory: (...args: any[]) => T;
};

/**
* @publicApi
*/
Expand Down
28 changes: 21 additions & 7 deletions packages/microservices/nest-microservice.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,10 @@ import { GraphInspector } from '@nestjs/core/inspector/graph-inspector';
import { NestApplicationContext } from '@nestjs/core/nest-application-context';
import { Transport } from './enums/transport.enum';
import { CustomTransportStrategy } from './interfaces/custom-transport-strategy.interface';
import { MicroserviceOptions } from './interfaces/microservice-configuration.interface';
import {
AsyncMicroserviceOptions,
MicroserviceOptions,
} from './interfaces/microservice-configuration.interface';
import { MicroservicesModule } from './microservices-module';
import { Server } from './server/server';
import { ServerFactory } from './server/server-factory';
Expand All @@ -43,7 +46,8 @@ export class NestMicroservice

constructor(
container: NestContainer,
config: NestMicroserviceOptions & MicroserviceOptions = {},
config: NestMicroserviceOptions &
(MicroserviceOptions | AsyncMicroserviceOptions) = {},
private readonly graphInspector: GraphInspector,
private readonly applicationConfig: ApplicationConfig,
) {
Expand All @@ -60,12 +64,22 @@ export class NestMicroservice
this.selectContextModule();
}

public createServer(config: NestMicroserviceOptions & MicroserviceOptions) {
public createServer(
config: NestMicroserviceOptions &
(MicroserviceOptions | AsyncMicroserviceOptions),
) {
try {
this.microserviceConfig = {
transport: Transport.TCP,
...config,
} as any;
if ('useFactory' in config) {
const args = config.inject?.map(token =>
this.get(token, { strict: false }),
);
this.microserviceConfig = config.useFactory(...args);
} else {
this.microserviceConfig = {
transport: Transport.TCP,
...config,
} as any;
}
const { strategy } = config as any;
this.server = strategy
? strategy
Expand Down
Loading