Skip to content

fdorantesm/nestjs-polly

Repository files navigation






NestJS Polly Module

Installation

yarn add nestjs-polly
npm i nestjs-polly


Configuration


Entry module:

@Module({
  imports: [
    PollyModule.register({
      region: polly.region,
      credentials: {
        accessKeyId: polly.accessKeyId,
        secretAccessKey: polly.secretAccessKey,
      }
    })
  ],
  controllers: [AppController],
  providers: [AppService],
})
export class AppModule {}

Entry module:

@Module({
  imports: [
    PollyModule.registerAsync({
    imports: [ConfigModule.forFeature(pollyConfig)],
    inject: [ConfigService],
    async useFactory(config: ConfigService) {
        const pollyConfig = config.get<PollyConfig>('polly');
        return {
          region: polly.region,
          credentials: {
            accessKeyId: polly.accessKeyId,
            secretAccessKey: polly.secretAccessKey,
          },
        };
      },
    }),
  ],
  controllers: [AppController],
  providers: [AppService],
})
export class AppModule {}


Usage


Inject polly client into a service:

@Injectable()
export class AppService {
  constructor(@InjectPolly() private readonly polly: PollyClient) {}

  getHello() {
    return this.polly.send(
      new SynthesizeSpeechCommand({
        TextType: 'text',
        Text: `This is a test of nest and polly module.`,
        OutputFormat: 'mp3',
        VoiceId: 'Ivy',
        LanguageCode: 'en-US',
        Engine: 'neural',
      }),
    );
  }
}

In the controller...

@Controller()
export class AppController {
  constructor(private readonly appService: AppService) {}

  @Get()
  async getHello(@Res() res: Response) {
    const audio = await this.appService.getHelloPolly();
    const file = await audio.AudioStream.transformToByteArray();
    res.header('Content-Type', audio.ContentType);
    res.send(Buffer.from(file.buffer));
  }
}

Result:

Audio





About

An AWS Polly module for NestJS

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published