Code Monkey home page Code Monkey logo

azure-sdk-nest's Introduction

Azure SDK NestJS

Azure SDK Javascript dynamic module for NestJS

TypeScript Npm package version

Quick Start

Let's build a Azure Storage Blob client and inject it into the nest app.

npm install @imaness/azure-sdk-nest @azure/storage-blob
  1. Register the module with a Blob Storage container client, in app.module.ts
import { Module } from '@nestjs/common';
import { AppController } from './app.controller';
import { AppService } from './app.service';
import { AzureSdkModule } from '@imaness/azure-sdk-nest';
import { BlobServiceClient  } from '@azure/storage-blob';
import { DefaultAzureCredential } from '@azure/identity';

@Module({
  imports: [
    // register Blob Service client
    AzureSdkModule.register({
      client: new ContainerClient(
        'https://myaccount.blob.core.windows.net/mycontainer', 
        new DefaultAzureCredential()
      ),
    }),
  ],
  controllers: [AppController],
  providers: [AppService],
})
export class AppModule {}
  1. use the Blob Storage Container client in app.controller.ts
import { ContainerClient } from '@azure/storage-blob';
import { Controller, Get } from '@nestjs/common';
import { AppService } from './app.service';
import { InjectAzure } from '@imaness/azure-sdk-nest';

@Controller()
export class AppController {
  constructor(
    private readonly appService: AppService,
    // inject the client
    @InjectAzure(ContainerClient) private readonly containerClient: ContainerClient 
  ) {}

  @Get()
  async helloAzure(): Promise<any> {
    const content = "Hello world!";
    const blobName = "newblob" + new Date().getTime();
    const blockBlobClient = await this.containerClient.getBlockBlobClient('asdsad');
    const uploadBlobResponse = await blockBlobClient.upload(content, content.length);
    console.log(`Upload block blob ${blobName} successfully`, uploadBlobResponse.requestId);
    return uploadBlobResponse;
  }
}
  1. done!

Register a Client

Register a client in any module, you can use any client you want. As long as it's a Azure SDK client

AzureSdkModule.register({
  client: new new ContainerClient(
    'https://myaccount.blob.core.windows.net/mycontainer', 
    new DefaultAzureCredential()
  ),
});

Async Register

AzureSdkModule.registerAsync({
  clientType: ContainerClient,
  useFactory: async () => {
    const containerClient = new ContainerClient(
      'https://myaccount.blob.core.windows.net/mycontainer',
      new DefaultAzureCredential()
    );

    return containerClient;
  },
});

Use @InjectAzure(Client)

Make sure the Client is the type you registered in module.

import { ContainerClient } from '@azure/storage-blob';
import { Controller, Get } from '@nestjs/common';
import { AppService } from './app.service';
import { InjectAzure } from '@imaness/azure-sdk-nest';

@Controller()
export class AppController {
  constructor(
    private readonly appService: AppService,
    @InjectAzure(ContainerClient) private readonly containerClient: ContainerClient 
  ) {}

  @Get()
  async helloAzure(): Promise<any> {
    const content = "Hello world!";
    const blobName = "newblob" + new Date().getTime();
    const blockBlobClient = await this.containerClient.getBlockBlobClient('asdsad');
    const uploadBlobResponse = await blockBlobClient.upload(content, content.length);
    console.log(`Upload block blob ${blobName} successfully`, uploadBlobResponse.requestId);
    return uploadBlobResponse;
  }
}

Multiple Injection/Instances

Please use key attribute as the identifier for each Client

Example:

  1. register the Container Client with a unique key
AzureSdkModule.register({
  // register the Blob container client with key MYACCOUNT-MYCONTAINER-1`
  key: 'MYACCOUNT-MYCONTAINER-1',
  client: new new ContainerClient(
    'https://myaccount.blob.core.windows.net/mycontainer-1', 
    new DefaultAzureCredential()
  ),
}),
AzureSdkModule.register({
  // register the Blob container client with key MYACCOUNT-MYCONTAINER-2`
  key: 'MYACCOUNT-MYCONTAINER-2',
  client: new new ContainerClient(
    'https://myaccount.blob.core.windows.net/mycontainer-2', 
    new DefaultAzureCredential()
  ),
}),
  1. refer the Blob Storage Container client use @InjectAzure(Client, key)
@InjectAzure(ContainerClient, 'MYACCOUNT-MYCONTAINER-1') private readonly containerClient1: ContainerClient,
@InjectAzure(ContainerClient, 'MYACCOUNT-MYCONTAINER-2') private readonly containerClient2: ContainerClient,

Global Module

Set the option isGlobal: true to enable it

AzureSdkModule.register({
  isGlobal: true,
  client: new ContainerClient(
    'https://myaccount.blob.core.windows.net/mycontainer', 
    new DefaultAzureCredential()
  ),
});

Credit

Contributor: @imaness

Inspired by: (https://github.com/deligenius/aws-sdk-v3-nest)

azure-sdk-nest's People

Contributors

imaness avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.