-
-
Notifications
You must be signed in to change notification settings - Fork 7.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: allow for microservice options to come from the di container
Microservices are now able to be created by getting their options from within the DI container itself. This has been a long requested feature of developers and I finally had some time to work through how we could possibly let this happen.
- Loading branch information
Showing
3 changed files
with
130 additions
and
8 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,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(), | ||
}); | ||
|
||
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(); | ||
}); | ||
}); |
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