Skip to content

Commit 10bd87c

Browse files
committed
feat(microservice): hybrid microservice can inherit config
1 parent b2d37b8 commit 10bd87c

6 files changed

Lines changed: 79 additions & 5 deletions

File tree

packages/common/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ export {
3535
NestInterceptor,
3636
NestMiddleware,
3737
NestModule,
38+
NestHybridOptions,
3839
OnApplicationBootstrap,
3940
OnApplicationShutdown,
4041
OnModuleDestroy,

packages/common/interfaces/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,3 +28,4 @@ export * from './nest-microservice.interface';
2828
export * from './scope-options.interface';
2929
export * from './type.interface';
3030
export * from './websockets/web-socket-adapter.interface';
31+
export * from './microservices/nest-hybrid-options.interface';
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
export interface NestHybridOptions {
2+
inheritAppConfig?: boolean;
3+
}

packages/common/interfaces/nest-application.interface.ts

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,12 @@ import { CorsOptions } from './external/cors-options.interface';
22
import { CanActivate } from './features/can-activate.interface';
33
import { NestInterceptor } from './features/nest-interceptor.interface';
44
import { HttpServer } from './http/http-server.interface';
5-
import { ExceptionFilter, INestMicroservice, PipeTransform } from './index';
5+
import {
6+
ExceptionFilter,
7+
INestMicroservice,
8+
NestHybridOptions,
9+
PipeTransform,
10+
} from './index';
611
import { INestApplicationContext } from './nest-application-context.interface';
712
import { WebSocketAdapter } from './websockets/web-socket-adapter.interface';
813

@@ -80,9 +85,13 @@ export interface INestApplication extends INestApplicationContext {
8085
* to a hybrid instance.
8186
*
8287
* @param {T} options Microservice options object
88+
* @param {NestHybridOptions} hybridOptions Hybrid options object
8389
* @returns {INestMicroservice}
8490
*/
85-
connectMicroservice<T extends object = any>(options: T): INestMicroservice;
91+
connectMicroservice<T extends object = any>(
92+
options: T,
93+
hybridOptions?: NestHybridOptions,
94+
): INestMicroservice;
8695

8796
/**
8897
* Returns array of the microservices connected to the NestApplication.

packages/core/nest-application.ts

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import {
77
NestInterceptor,
88
PipeTransform,
99
WebSocketAdapter,
10+
NestHybridOptions,
1011
} from '@nestjs/common';
1112
import { CorsOptions } from '@nestjs/common/interfaces/external/cors-options.interface';
1213
import { NestApplicationOptions } from '@nestjs/common/interfaces/nest-application-options.interface';
@@ -170,14 +171,19 @@ export class NestApplication extends NestApplicationContext
170171
this.routesResolver.registerExceptionHandler();
171172
}
172173

173-
public connectMicroservice<T extends object>(options: T): INestMicroservice {
174+
public connectMicroservice<T extends object>(
175+
options: T,
176+
hybridOptions: NestHybridOptions = {},
177+
): INestMicroservice {
174178
const { NestMicroservice } = loadPackage(
175179
'@nestjs/microservices',
176180
'NestFactory',
177181
() => require('@nestjs/microservices'),
178182
);
179-
180-
const applicationConfig = new ApplicationConfig();
183+
const { inheritAppConfig } = hybridOptions;
184+
const applicationConfig = inheritAppConfig
185+
? this.config
186+
: new ApplicationConfig();
181187
const instance = new NestMicroservice(
182188
this.container,
183189
options,
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
import { expect } from 'chai';
2+
import { NestApplication } from '../nest-application';
3+
import { ApplicationConfig } from '../application-config';
4+
import { NestContainer } from '../injector';
5+
import { NoopHttpAdapter } from './utils/noop-adapter.spec';
6+
7+
describe('NestApplication', () => {
8+
describe('Hybrid Application', () => {
9+
class Interceptor {
10+
public intercept(context, next) {
11+
return next();
12+
}
13+
}
14+
it('default should use new ApplicationConfig', () => {
15+
const applicationConfig = new ApplicationConfig();
16+
const container = new NestContainer(applicationConfig);
17+
const instance = new NestApplication(
18+
container,
19+
new NoopHttpAdapter({}),
20+
applicationConfig,
21+
{},
22+
);
23+
instance.useGlobalInterceptors(new Interceptor());
24+
const microservice = instance.connectMicroservice({});
25+
expect((instance as any).config.getGlobalInterceptors().length).to.equal(
26+
1,
27+
);
28+
expect(
29+
(microservice as any).applicationConfig.getGlobalInterceptors().length,
30+
).to.equal(0);
31+
});
32+
it('should inherit existing ApplicationConfig', () => {
33+
const applicationConfig = new ApplicationConfig();
34+
const container = new NestContainer(applicationConfig);
35+
const instance = new NestApplication(
36+
container,
37+
new NoopHttpAdapter({}),
38+
applicationConfig,
39+
{},
40+
);
41+
instance.useGlobalInterceptors(new Interceptor());
42+
const microservice = instance.connectMicroservice(
43+
{},
44+
{ inheritAppConfig: true },
45+
);
46+
expect((instance as any).config.getGlobalInterceptors().length).to.equal(
47+
1,
48+
);
49+
expect(
50+
(microservice as any).applicationConfig.getGlobalInterceptors().length,
51+
).to.equal(1);
52+
});
53+
});
54+
});

0 commit comments

Comments
 (0)