nestjs-polly
TypeScript icon, indicating that this package has built-in type declarations

1.1.0 • Public • Published






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





Package Sidebar

Install

npm i nestjs-polly

Weekly Downloads

1

Version

1.1.0

License

MIT

Unpacked Size

17.2 kB

Total Files

22

Last publish

Collaborators

  • fdorantesm