A upyun sdk for nest.js.
pnpm i @nailyjs.nest.modules/upyun
import { Module } from '@nestjs/common';
import { UpyunModule } from '@nailyjs.nest.modules/upyun';
@Module({
imports: [
UpyunModule.forRoot({
operator: 'operator',
password: 'password',
bucket: 'bucket',
domain: 'domain',
}),
],
})
export class AppModule {}
UpyunModule
provide forRoot
and forRootAsync
methods to create upyun client.
import { Module } from '@nestjs/common';
import { UpyunModule } from '@nailyjs.nest.modules/upyun';
@Module({
imports: [
UpyunModule.forRootAsync({
useFactory: () => ({
operator: 'operator',
password: 'password',
bucket: 'bucket',
}),
}),
],
})
export class AppModule {}
Then you can use UpyunService
in your service or controller.
import { Injectable } from '@nestjs/common';
import { UpyunService } from '@nailyjs.nest.modules/upyun';
@Injectable()
export class AppService {
constructor(private readonly upyunService: UpyunService) {}
async upload() {
return this.upyunService.listDir('/');
}
}
import { Module } from '@nestjs/common';
import { UpyunModule } from '@nailyjs.nest.modules/upyun';
// In real project, recommend to create a separate file to store constants.
export const UPYUN_CLIENT_1 = 'upyun1';
export const UPYUN_CLIENT_2 = 'upyun2';
@Module({
imports: [
UpyunModule.forRoot([
{
provide: UPYUN_CLIENT_1,
operator: 'operator1',
password: 'password1',
bucket: 'bucket1',
},
{
name: UPYUN_CLIENT_2,
operator: 'operator2',
password: 'password2',
bucket: 'bucket2',
},
])
],
})
export class AppModule {}
Also you can use
forRootAsync
to create multiple upyun instances.
Then you must use @Inject
decorator (import from @nestjs/common
) to inject the service.
import { Injectable, Inject } from '@nestjs/common';
import { UpyunService } from '@nailyjs.nest.modules/upyun';
import { UPYUN_CLIENT_1, UPYUN_CLIENT_2 } from './app.module';
@Injectable()
export class AppService {
constructor(
@Inject(UPYUN_CLIENT_1)
private readonly upyunService1: UpyunService,
@Inject(UPYUN_CLIENT_2)
private readonly upyunService2: UpyunService,
) {}
async upload() {
return this.upyunService1.listDir('/');
}
}
Upyun sdk is written in javascript, but you can use @types/upyun
to get types.
pnpm i -D @types/upyun
The author of
@types/upyun
is also me (QAQ)
👤 Naily Zero [email protected]
MIT