feat: centralize grpc client registration
All checks were successful
Publish / Publish Job (push) Successful in 44s
All checks were successful
Publish / Publish Job (push) Successful in 44s
This commit is contained in:
46
lib/grpc/grpc.module.ts
Normal file
46
lib/grpc/grpc.module.ts
Normal file
@@ -0,0 +1,46 @@
|
||||
import { type DynamicModule, Module } from '@nestjs/common'
|
||||
import { ConfigService } from '@nestjs/config'
|
||||
|
||||
import { GRPC_CLIENT_PREFIX } from './constants/grpc.constants'
|
||||
import { GrpcClientFactory } from './factory/grpc-client.factory'
|
||||
import { GRPC_CLIENTS } from './registry/grpc.registry'
|
||||
|
||||
@Module({})
|
||||
export class GrpcModule {
|
||||
public static register(
|
||||
clients: Array<keyof typeof GRPC_CLIENTS>
|
||||
): DynamicModule {
|
||||
return {
|
||||
module: GrpcModule,
|
||||
providers: [
|
||||
GrpcClientFactory,
|
||||
...clients.map(token => {
|
||||
const cfg = GRPC_CLIENTS[token]
|
||||
return {
|
||||
provide: `${GRPC_CLIENT_PREFIX}_${token}`,
|
||||
useFactory: (
|
||||
factory: GrpcClientFactory,
|
||||
config: ConfigService
|
||||
) => {
|
||||
const url = config.getOrThrow(cfg.env)
|
||||
const client = factory.createClient({
|
||||
package: cfg.package,
|
||||
protoPath: cfg.protoPath,
|
||||
url
|
||||
})
|
||||
|
||||
factory.register(token, client)
|
||||
|
||||
return client
|
||||
},
|
||||
inject: [GrpcClientFactory, ConfigService]
|
||||
}
|
||||
})
|
||||
],
|
||||
exports: [
|
||||
GrpcClientFactory,
|
||||
...clients.map(token => `${GRPC_CLIENT_PREFIX}_${token}`)
|
||||
]
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user