Skip to content
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

fix(grpc-loader): add new options by generic props loader #13941

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,11 @@ import {
import { Serializer } from './serializer.interface';
import { ConnectionOptions } from 'tls';

export type ClientOptions =
export type ClientOptions<ClientProviderCustomOptions = {}> =
| RedisOptions
| NatsOptions
| MqttOptions
| GrpcOptions
| GrpcOptions<ClientProviderCustomOptions>
| KafkaOptions
| TcpClientOptions
| RmqOptions;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@ import { CustomTransportStrategy } from './custom-transport-strategy.interface';
import { Deserializer } from './deserializer.interface';
import { Serializer } from './serializer.interface';

export type MicroserviceOptions =
| GrpcOptions
export type MicroserviceOptions<GrpcLoaderOption = {}> =
| GrpcOptions<GrpcLoaderOption>
| TcpOptions
| RedisOptions
| NatsOptions
Expand All @@ -40,10 +40,24 @@ export interface CustomStrategy {
options?: {};
}

interface DefaultLoaderOptions {
keepCase?: boolean;
alternateCommentMode?: boolean;
longs?: Function;
enums?: Function;
bytes?: Function;
defaults?: boolean;
arrays?: boolean;
objects?: boolean;
oneofs?: boolean;
json?: boolean;
includeDirs?: string[];
}

/**
* @publicApi
*/
export interface GrpcOptions {
export interface GrpcOptions<GrpcLoaderOptions = {}> {
transport?: Transport.GRPC;
options: {
url?: string;
Expand All @@ -67,19 +81,7 @@ export interface GrpcOptions {
packageDefinition?: any;
gracefulShutdown?: boolean;
onLoadPackageDefinition?: (pkg: any, server: any) => void;
loader?: {
keepCase?: boolean;
alternateCommentMode?: boolean;
longs?: Function;
enums?: Function;
bytes?: Function;
defaults?: boolean;
arrays?: boolean;
objects?: boolean;
oneofs?: boolean;
json?: boolean;
includeDirs?: string[];
};
loader?: GrpcLoaderOptions | DefaultLoaderOptions;
};
}

Expand Down
5 changes: 4 additions & 1 deletion packages/microservices/module/clients.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,17 @@ import {

@Module({})
export class ClientsModule {
static register(options: ClientsModuleOptions): DynamicModule {
static register<ClientProviderCustomOptions>(
options: ClientsModuleOptions<ClientProviderCustomOptions>,
): DynamicModule {
const clientsOptions = !Array.isArray(options) ? options.clients : options;
const clients = (clientsOptions || []).map(item => {
return {
provide: item.name,
useValue: this.assignOnAppShutdownHook(ClientProxyFactory.create(item)),
};
});

return {
module: ClientsModule,
global: !Array.isArray(options) && options.isGlobal,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,16 +1,19 @@
import { ClientOptions, CustomClientOptions } from '../../interfaces';
import { Type, Provider, ModuleMetadata } from '@nestjs/common/interfaces';

export type ClientProvider = ClientOptions | CustomClientOptions;
export type ClientProvider<ClientProviderCustomOptions = {}> =
| ClientOptions<ClientProviderCustomOptions>
| CustomClientOptions;

export type ClientProviderOptions = ClientProvider & {
name: string | symbol;
};
export type ClientProviderOptions<ClientProviderCustomOptions> =
ClientProvider<ClientProviderCustomOptions> & {
name: string | symbol;
};

export type ClientsModuleOptions =
| Array<ClientProviderOptions>
export type ClientsModuleOptions<ClientProviderCustomOptions = {}> =
| Array<ClientProviderOptions<ClientProviderCustomOptions>>
| {
clients: Array<ClientProviderOptions>;
clients: Array<ClientProviderOptions<ClientProviderCustomOptions>>;
isGlobal?: boolean;
};

Expand Down