npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@cardbrother/nestjs-tencent-cloud-sdk

v1.0.29

Published

nestjs tencent cloud sdk

Downloads

32

Readme

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,
});

📝 CHANGELOG

License MIT