Skip to content

Commit e2cb7ee

Browse files
feature(microservices): add clients module (factory abstraction)
1 parent a5d3ce5 commit e2cb7ee

6 files changed

Lines changed: 35 additions & 0 deletions

File tree

packages/microservices/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,5 +11,6 @@ export * from './decorators';
1111
export * from './enums';
1212
export * from './exceptions';
1313
export * from './interfaces';
14+
export * from './module';
1415
export * from './nest-microservice';
1516
export * from './server';
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
import { getClientToken } from './clients.utils';
2+
3+
export const InjectClient = (name: string) => getClientToken(name);
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
import { DynamicModule, Module } from '@nestjs/common';
2+
import { ClientProxyFactory } from 'client';
3+
import { getClientToken } from './clients.utils';
4+
import { ClientsModuleOptions } from './interfaces/clients-module.interface';
5+
6+
@Module({})
7+
export class ClientsModule {
8+
static register(options: ClientsModuleOptions): DynamicModule {
9+
const clients = (options || []).map(item => ({
10+
provide: getClientToken(item.name),
11+
useValue: ClientProxyFactory.create(item.options),
12+
}));
13+
return {
14+
module: ClientsModule,
15+
providers: clients,
16+
exports: clients,
17+
};
18+
}
19+
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export const getClientToken = (name: string) => `${name.toUpperCase()}_CLIENT`;
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
export * from './clients.decorators';
2+
export * from './clients.module';
3+
export * from './clients.utils';
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
import { ClientOptions } from '../../interfaces';
2+
3+
export interface ClientProviderOptions {
4+
name: string;
5+
options: ClientOptions;
6+
}
7+
8+
export interface ClientsModuleOptions extends Array<ClientProviderOptions> {}

0 commit comments

Comments
 (0)