saeed Ahmadluei
saeed Ahmadluei

Reputation: 1

How to Inject Dependency to Nest.js Pipe

I have pipe like this

@Injectable({ scope: Scope.DEFAULT })
export class EntValidatePipe implements PipeTransform<any, Promise<any>> {
  
  constructor(private OpType:EnumOpType,private EntSrv:ServiceBase) {}
  
  async transform(InDto: any, metadata: ArgumentMetadata): Promise<any> {
    return InDto;
  }
}

In controller I have to manually create instance of pipe and cold not use Nest.js IOC. However I can nnot not pass service instance to pipe and get the error : "tpscript Object is possibly 'undefined'.ts(2532)" this is the controller class

@Controller('karfarma')
export class KarfarmaController {
  constructor(private readonly karfarmaService: KarfarmaService,
              private catsService: CatsService
  ) {}

  @Post()
  create(@Body(new EntValidatePipe(EnumOpType.INSERT,this.karfarmaService)) createKarfarmaDto: CreateKarfarmaDto) {
    return this.karfarmaService.create(createKarfarmaDto);

  }
}

KarfarmaService is a class implemented abstract class ServiceBase .

why injecting dependency to pipe is not simply possible ??

Upvotes: 0

Views: 14

Answers (0)

Related Questions