-
-
Notifications
You must be signed in to change notification settings - Fork 7.6k
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
jmcdo29
wants to merge
1
commit into
nestjs:master
Choose a base branch
from
jmcdo29:feat/async-microservce-config
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+130
−8
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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:
perharps that's too overkill (or impossible) to have? yeah...
There was a problem hiding this comment.
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