/nest-tencent-cloud-sdk

Tencent Cloud nestjs Toolkit

Primary LanguageTypeScript

@cardbrother/nestjs-tencent-cloud-sdk

CI Release & Publish package version NPM Downloads

GitHub commit activity (branch) GitHub commit activity (branch)

🚨tencentcloud-sdk-nodejs 🚨cos-nodejs-sdk-v5

GitHub Tag GitHub Tag

Language: 中文

tencentcloud-sdk-nodejs & cos-nodejs-sdk-v5 for NestJS developers

🔨 Installation

  • npm
npm install @cardbrother/nest-tencent-cloud-sdk
  • yarn
$ yarn add @cardbrother/nest-tencent-cloud-sdk
  • 🚀 pnpm Recommended
$ pnpm add @cardbrother/nest-tencent-cloud-sdk

🍚 Usage

In your AppModuleor FeatureModule module, import the TencentCloudModule and call the forRoot method to configure the Tencent Cloud SDK with your secretId and secretKey:

Basic usage:

import { Module } from '@nestjs/common';
import { TencentCloudModule } from '@cardbrother/nest-tencent-cloud-sdk';

@Module({
  imports: [
    TencentCloudModule.forRoot({
      apiId: 'TENCENT_API_ID',
      apiSecret: 'TENCENT_API_SECRET',
      region: 'TENCENT_REGION',
      cos: {
        Bucket: 'COS_BUCKET',
        Region: 'COS_REGION',
        SecretId: 'COS_SECRET_ID',
        SecretKey: 'COS_SECRET_KEY',
      },
      global: true, // if true, the TencentCloudService will be a global service, default is false
    }),
  ],
})
export class AppModule {}

Dynamic useage

If you want to use a dynamic configuration, you can use the forRootAsync method to configure the Tencent Cloud SDK with your secretId and secretKey:

import { TencentCloudModule } from '@cardbrother/nestjs-tencent-cloud-sdk';
import { Module } from '@nestjs/common';
import { ConfigModule, ConfigService } from '@nestjs/config';

@Module({
  imports: [
    ConfigModule.forRoot({
      isGlobal: true,
    }),
    TencentCloudModule.forRootAsync({
      useFactory: async (configService: ConfigService) => ({
        apiId: configService.get<string>('TENCENT_API_ID'),
        apiSecret: configService.get<string>('TENCENT_API_SECRET'),
        region: configService.get<string>('TENCENT_REGION', 'ap-shanghai'),
        cos: {
          Bucket: configService.get<string>('COS_BUCKET'),
          Region: configService.get<string>('COS_REGION'),
          SecretId: configService.get<string>('COS_SECRET_ID'),
          SecretKey: configService.get<string>('COS_SECRET_KEY'),
        },
        global: true, // if true, the TencentCloudService will be a global service, default is false
      }),
      imports: [ConfigModule],
      inject: [ConfigService],
      global: true,
    }),
  ],
})
export class AppModule {}

Use TencentCloudService

import { Injectable } from '@nestjs/common';
import { TencentCloudService } from '@cardbrother/nest-tencent-cloud-sdk';

@Injectable()
export class AppService {
  constructor(
    private readonly tencentCloudService: TencentCloudService,
    @Inject(TENCENT_CLOUD_MODULE_OPTIONS_TOKEN)
    private readonly options: TencentCloudModuleOptions, // If u want to get the options u can use this
  ) {}

  // imgData is a base64 string
  async useOCR(imgData: string) {
    // Here the encapsulated object is dynamically instantiated based on the enumeration
    const ocrClient = await this.tencentCloudService.useClient('OCR'); // or SMS,COS,etc
    return ocrClient.fastOcr({
      ImageBase64: imgData,
    });
  }
}

Usage Tencent COS

💡 Tips: If you pass Bucket, Region, or any parameter here it overrides the configuration at the time of imported module the reference, if you don't pass it then the import TencentCloudModule.forRoot configuration is used.

const cosClient = await this.tencentCloudService.useClient('COS');
cosClient.upload({
  Bucket: this.options.cos.Bucket, // It's not required. Default use the configuration at the Module Import time
  Region: this.options.cos.Region, // It's not required. Default use the configuration at the Module Import time
  Key: 'test.txt',
  Body: 'hello world',
  FilePath: 'test.txt',
});

Usage Tencent STS

const sts_client = await tencentCloudService.useClient(
  TencentCloudClientType.STS,
);
expect(sts_client).toBeDefined();
expect(sts_client).toBeInstanceOf(StsProvider);
const tempSignature = await sts_client.createTemporary({
  Name: 'Test',
  Policy: encodeURI(
    JSON.stringify({
      version: '2.0',
      statement: [
        {
          effect: 'allow',
          action: ['name/cos:PutObject'],
          resource: ['*'],
        },
      ],
    }),
  ),
  DurationSeconds: 1800,
});

License MIT