1.0.0-alpha.0 • Published 4 years ago

nest-aws v1.0.0-alpha.0

Weekly downloads
50
License
MIT
Repository
github
Last release
4 years ago

Description

Alpha Release: Use it with caution

AWS Module for Nest.js framework

Installation

NPM

npm i --save nest-aws

Yarn

yarn add nest-aws

Usage

Import AwsModule into root AwsModule

@Module({
  imports: [
    AwsModule.forRoot({
      region: 'ap-northeast-2'
      accessKeyId: 'your-aws-access-key',
      secretAccessKeyId: 'your-aws-secret-key',
    }),
  ],
  providers: [...],
})
export class AppModule {}

Define AWS Services that you wan't to use

import { S3, SES } from 'aws-sdk'

@Module({
  imports: [
    AwsModule.forFeature(S3, SES),
  ],
  providers: [...],
})
export class AppModule {}

Now, inject S3 and SES with InjectAwsService

@Injectable()
export class UserService {
  constructor(
    @InjectAwsService(S3)
    private readonly s3: S3,
    @InjectAwsService(SES)
    private readonly ses: SES,
  ) {}
}

All AWS Services will be initiated with settings that you have provided in forRoot

Options

AwsModule.forRoot({
  // AWS Region (Optional)
  region: '',


  // AWS Access Key Id (Optional)
  accessKeyId: '...',

  // AWS Secret Access Key Id (Optional)
  secretAccessKeyId: '...',
}),

Async Options

  1. Use factory
AwsModule.forRootAsync({
  useFactory: () => ({
    region: 'ap-northeast-2',
  }),
})

OR

AwsModule.forRootAsync({
  imports: [ConfigModule],
  useFactory: (configService: ConfigService) => ({
    accessKeyId: configService.get('accessKeyId'),
    secretAccessKeyId: configService.get('secretAccessKeyId'),
  }),
  inject: [ConfigService],
})
  1. Use Class
AwsModule.forRootAsync({
  useClass: AwsConfigService
})
class AwsConfigService implements AwsOptionsFactory {
  createAwsOptions(): Promise<AwsModuleOptions> | AwsModuleOptions {
    return {
      region: 'ap-northeast-2',
    }
  }
}
  1. Use existing
AwsModule.forRootAsync({
  imports: [ConfigModule],
  useExisting: ConfigService,
})

TODO

  • Write Tests
  • Support More Options
  • Seperate AwsModule with AwsCoreModule (Like @nestjs/typeorm)