Skip to content

Commit

Permalink
fix(nestjs-grpc-reflection): lint and type errors
Browse files Browse the repository at this point in the history
  • Loading branch information
Amiditin committed Jan 24, 2025
1 parent bed9dbf commit 7b335b1
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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 | undefined>((fileDescriptorProto, service) => {
if (fileDescriptorProto) {
return fileDescriptorProto
}

// @ts-expect-error
// @ts-expect-error correct return type
return Object.values(service).reduce<FileDescriptorProto | undefined>((
descriptor,
method
Expand All @@ -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
})
}

Expand Down
28 changes: 15 additions & 13 deletions packages/nestjs-grpc-reflection/src/grpc/grpc.reflector.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,5 +16,6 @@ export interface GrpcReflectionModuleAsyncOptions extends Pick<ModuleMetadata, '
useFactory?: (
...args: Array<any>
) => GrpcReflectionModuleOptions | Promise<GrpcReflectionModuleOptions>
// eslint-disable-next-line @typescript-eslint/no-explicit-any
inject?: Array<any>
}
Original file line number Diff line number Diff line change
Expand Up @@ -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!,
},
]
Expand All @@ -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!],
}
}
Expand Down

0 comments on commit 7b335b1

Please sign in to comment.