nest-sens
TypeScript icon, indicating that this package has built-in type declarations

1.1.0 • Public • Published

Nest Logo

NCP SENS Client component for Nest.js

NPM Version Package License NPM Downloads Coverage

Base on @pickk/sens

1. Features

  • [x] send SMS
  • [x] send Alimtalk

2. Installation

$ npm i --save nest-inicis
# or
$ yarn add nest-sens

3. Usage

1. configure

sync

import { SensModule } from 'nest-sens';

@Module({
  imports: [
    SensModule.forRoot({
      accessKey: 'ACCESS_KEY',
      secretKey: 'SECRET_KEY',
      sms: {
        smsServiceId: 'SMS_SERVICE_ID',
        smsSecretKey: 'SMS_SECRET_KEY',
        callingNumber: '01012341234',
      },
      alimtalk: {
        alimtalkServiceId: 'ALIMTALK_SERVICE_ID',
        plusFriendId: 'PLUS_FRIEND_ID',
      }
    }),
  ],
})

async

import {
  SensModule,
  SensModuleAsyncOptions,
  SensModuleOptions,
} from 'nest-sens';

import { SensConfigModule, SensConfigService } from 'your-config-path';

@Module({
  imports: [
    SensModule.forRootAsync({
      imports: [SensConfigModule],
      useFactory: async (sensConfigService: SensConfigService) =>
        ({
          accessKey: sensConfigService.ncloudAccessKey,
          secretKey: sensConfigService.ncloudSecretKey,
          sms: {
            smsServiceId: sensConfigService.ncloudSmsServiceId,
            smsSecretKey: sensConfigService.ncloudSmsSecretKey,
            callingNumber: sensConfigService.ncloudSmsCallingNumber,
          },
          alimtalk: {
            alimtalkServiceId: sensConfigService.ncloudAlimtalkServiceId,
            plusFriendId: sensConfigService.plusFriendId,
          },
        } as SensModuleOptions),
      inject: [SensConfigService],
    } as SensModuleAsyncOptions),
  ],
})

2. inject & send

import { AlimtalkClient, SmsClient } from 'nest-sens';

@Injectable()
export class YourService {
  constructor(
    @Inject(AlimtalkClient) private readonly alimtalkClient: AlimtalkClient,
    @Inject(SmsClient) private readonly smsClient: SmsClient
  ) {}

  async sendAlimtalk(templateCode: string, to: string, content: string) {
    await this.alimtalkClient.send({
      templateCode,
      messages: [{ to, content }],
    });
  }

  async sendSms(to: string, content: string) {
    await this.smsClient.send({ to, content });
  }
}

Author

License

Nest-Sens is MIT licensed.

Package Sidebar

Install

npm i nest-sens

Weekly Downloads

1

Version

1.1.0

License

MIT

Unpacked Size

12.7 kB

Total Files

11

Last publish

Collaborators

  • greatsumini