Let's say I have this interface:
export interface IParser {
parse(text: string):string
}
and a couple of classes implementing the interface:
@Injectable()
export class FirstParser implements IParser {
public parse(text: string): string {
return text + 'first';
}
}
@Injectable()
export class SecondParser implements IParser {
public parse(text: string): string {
return text + 'second';
}
}
Now, I have a service that I'm trying to inject to him list of parsers:
@Injectable()
export class ParserService {
constructor(private readonly parsers: IParser[]) {}
public parse(text: string): string {
let parsedText = text;
parsedText = this.parsers.reduce((acc: string, result) => {
return result.parse(acc);
}, parsedText);
return parsedText;
}
}
I'm trying to make the app module (in this case the parser-service.module.ts) to work by injecting array/list of the class including FirstParser, SecondParser.
@Module({
providers: [
ParserService,
{
provide: Array<IParser>,
useClass: ???,
},
],
})
export class ParserServiceModule {}
Any ideas? is it possible at all in NestJs?
Thanks.