عکس vahidvdn
Currency conversion module for nestjsTypeScript
موضوع‌ها
۰
فورک‌ها
۲
ستاره‌ها
۶۰
تاریخ ایجاد
۲۸ فروردین ۱۴۰۰
آخرین بروزرسانی
حدود ۱ سال قبل
لایسنس
MIT License

alt nestjs alt license alt npm-download alt npm-version

Description

Currency conversion module for nestjs framework.

Installation

npm install nestjs-cashify

Usage

Basic Usage

Import CashifyModule in the main module with configuration

import { CashifyModule } from 'nestjs-cashify';

const rates = {
  GBP: 0.92,
  EUR: 1.00,
  USD: 1.12
};

@Module({
  imports: [CashifyModule.forRoot({base: 'EUR', rates})],
  controllers: [AppController],
  providers: [AppService],
})
export class AppModule {}

forRootAsync with configuration and useFactory

@Module({
  imports: [
    CashifyModule.forRootAsync({
      imports: [ConfigModule],
      useFactory: async (configService: ConfigService) => ({
        base: configService.get<string>('BASE'), rates
      }),
      inject: [ConfigService]
    })
  ]
})
export class AppModule {}

forRootAsync with configuration and useClass

@Module({
  imports: [
    CashifyModule.forRootAsync({
      useClass: CashifyConfigService,
      import: [ConfigModule],
      extraProviders: [ConfigService],
    })
  ]
})
export class AppModule {}

And for CashifyConfigService we have the following:

import { CashifyOptionsFactory } from 'nestjs-cashify';

@Injectable()
export class CashifyConfigService implements CashifyOptionsFactory {
  constructor(private configService: ConfigService) {}

  createCashifyOptions() {
    const rates = {
      GBP: 0.92,
      EUR: 1.00,
      USD: 1.12
    };
    return {
      base: this.configService.get<string>('BASE'),
      rates
    };
  }
}

Convert

In order to convert currencies to each other in controllers or services, we have two approaches:

Method 1: service method

import { CashifyService } from 'nestjs-cashify';

constructor(private cashifyService: CashifyService) {
  const result = this.cashifyService.convert(10, {from: 'EUR', to: 'GBP'});
  console.log(result);
}

Method 2: injecting instance of cashify class

Note: Since this module is based on nodejs cashify library, in case there can be more methods added to the core library, you can inject the instance and use it as following:

import { Cashify } from 'cashify';
import { CASHIFY } from 'nestjs-cashify';

constructor(@Inject(CASHIFY) private cashify: Cashify) {
  const result2 = this.cashify.convert(10, {from: 'EUR', to: 'GBP'});
  console.log(result2);
}

Parsing

Cashify supports parsing, so you can pass a string to the amount argument and the from and/or to currency will be automatically detected:

// Basic parsing
this.cashifyService.convert('€10 EUR', {to: 'GBP'});

// Full parsing
this.cashifyService.convert('10 EUR to GBP');

Support

Any support is welcome. You can give the project a star, if you liked it ⭐

Contribute

Feel free to contribute.

License

MIT