From 7b335b15c966cf797d7ab22a77b69bc0b6b0b9b8 Mon Sep 17 00:00:00 2001 From: Dmitry Date: Fri, 24 Jan 2025 14:35:16 +0300 Subject: [PATCH] fix(nestjs-grpc-reflection): lint and type errors --- .../src/grpc/grpc-services.registry.ts | 11 ++++---- .../src/grpc/grpc.reflector.ts | 28 ++++++++++--------- ...rpc-reflection-module-options.interface.ts | 1 + .../src/module/grpc-reflection.module.ts | 3 ++ 4 files changed, 25 insertions(+), 18 deletions(-) diff --git a/packages/nestjs-grpc-reflection/src/grpc/grpc-services.registry.ts b/packages/nestjs-grpc-reflection/src/grpc/grpc-services.registry.ts index 0a9705e7..a722e0e6 100644 --- a/packages/nestjs-grpc-reflection/src/grpc/grpc-services.registry.ts +++ b/packages/nestjs-grpc-reflection/src/grpc/grpc-services.registry.ts @@ -10,7 +10,7 @@ export class GrpcServicesRegistry { getServiceNameFromServiceDefinition(serviceDefinition: ServiceDefinition): string { const methodDefinition = Object.values(serviceDefinition).shift() - return methodDefinition!.path.split('/')[1] + return methodDefinition?.path.split('/')[1] || '' } addService(service: ServiceDefinition): void { @@ -28,13 +28,12 @@ export class GrpcServicesRegistry { getFileDescriptorProtoByFileContainingSymbol( fileContainingSymbol: string ): FileDescriptorProto | undefined { - // @ts-expect-error + // @ts-expect-error correct return type return this.services.reduce((fileDescriptorProto, service) => { if (fileDescriptorProto) { return fileDescriptorProto } - - // @ts-expect-error + // @ts-expect-error correct return type return Object.values(service).reduce(( descriptor, method @@ -47,7 +46,9 @@ export class GrpcServicesRegistry { return method.requestType.fileDescriptorProtos.find((fdp) => { const fileDescriptor = FileDescriptorProto.deserializeBinary(fdp) - return fileContainingSymbol.includes(fileDescriptor.getPackage()!) + const filePackage = fileDescriptor.getPackage() + + return filePackage ? fileContainingSymbol.includes(filePackage) : false }) } diff --git a/packages/nestjs-grpc-reflection/src/grpc/grpc.reflector.ts b/packages/nestjs-grpc-reflection/src/grpc/grpc.reflector.ts index 42bf40db..e29ae87d 100644 --- a/packages/nestjs-grpc-reflection/src/grpc/grpc.reflector.ts +++ b/packages/nestjs-grpc-reflection/src/grpc/grpc.reflector.ts @@ -28,28 +28,30 @@ export class GrpcReflector implements OnModuleInit { : [this.options.protoPath] for (const protoPath of protoPaths) { - // @ts-expect-error - const packageDefinition = loadSync(protoPath, this.options.loader) - const grpcContext = loadPackageDefinition(packageDefinition) + if (protoPath) { + // eslint-disable-next-line n/no-sync + const packageDefinition = loadSync(protoPath, this.options.loader) + const grpcContext = loadPackageDefinition(packageDefinition) - const packageNames = Array.isArray(this.options.package) - ? this.options.package - : [this.options.package] + const packageNames = Array.isArray(this.options.package) + ? this.options.package + : [this.options.package] - for (const packageName of packageNames) { - const grpcPkg = this.lookupPackage(grpcContext, packageName) + for (const packageName of packageNames) { + const grpcPkg = this.lookupPackage(grpcContext, packageName) - if (grpcPkg) { - for (const definition of grpcServer.getServiceNames(grpcPkg)) { - // eslint-disable-next-line @typescript-eslint/no-unsafe-argument - this.registry.addService(definition.service.service) + if (grpcPkg) { + for (const definition of grpcServer.getServiceNames(grpcPkg)) { + // eslint-disable-next-line @typescript-eslint/no-unsafe-argument + this.registry.addService(definition.service.service) + } } } } } } - // eslint-disable-next-line @typescript-eslint/explicit-module-boundary-types + // eslint-disable-next-line @typescript-eslint/explicit-module-boundary-types, @typescript-eslint/no-explicit-any public lookupPackage(root: any, packageName: string): any { let pkg = root diff --git a/packages/nestjs-grpc-reflection/src/module/grpc-reflection-module-options.interface.ts b/packages/nestjs-grpc-reflection/src/module/grpc-reflection-module-options.interface.ts index b6b10c97..8a333a2d 100644 --- a/packages/nestjs-grpc-reflection/src/module/grpc-reflection-module-options.interface.ts +++ b/packages/nestjs-grpc-reflection/src/module/grpc-reflection-module-options.interface.ts @@ -16,5 +16,6 @@ export interface GrpcReflectionModuleAsyncOptions extends Pick ) => GrpcReflectionModuleOptions | Promise + // eslint-disable-next-line @typescript-eslint/no-explicit-any inject?: Array } diff --git a/packages/nestjs-grpc-reflection/src/module/grpc-reflection.module.ts b/packages/nestjs-grpc-reflection/src/module/grpc-reflection.module.ts index 26d6f13f..1ab0c763 100644 --- a/packages/nestjs-grpc-reflection/src/module/grpc-reflection.module.ts +++ b/packages/nestjs-grpc-reflection/src/module/grpc-reflection.module.ts @@ -49,7 +49,9 @@ export class GrpcReflectionModule { return [ this.createAsyncOptionsProvider(options), { + // eslint-disable-next-line @typescript-eslint/no-non-null-assertion provide: options.useClass!, + // eslint-disable-next-line @typescript-eslint/no-non-null-assertion useClass: options.useClass!, }, ] @@ -68,6 +70,7 @@ export class GrpcReflectionModule { provide: GRPC_REFLECTION_MODULE_OPTIONS, useFactory: async (optionsFactory: GrpcReflectionOptionsFactory) => optionsFactory.createGrpcReflectionOptions(), + // eslint-disable-next-line @typescript-eslint/no-non-null-assertion inject: [options.useExisting! || options.useClass!], } }