currency-converter-vl
v1.0.2
Published
Simplified Currency Conversion in Node js Without API Calls
Downloads
17
Maintainers
Readme
Typescript Support
I've updated tsconfig.json to { "compilerOptions": { "allowJs": true}} and imported class import * as CurrencyConverter from 'currency-converter-vl';
Getting started
Installation
This package can be installed using npm
npm install currency-converter-vl
or, yarn
yarn add currency-converter-vl
Usage
Import currency-converter-vl
.
const CCV = require('currency-converter-vl')
Instant Setup: Node.js Currency Converter with Empty Constructor
let currencyConverter = new CCV()
Or, with a json object
let currencyConverter = new CCV({from:"USD", to:"INR", amount:100})
!!! Note: if you get incorrect conversion described in this issue then make sure you pass isDecimalComma: true
to the constructor as following:
let currencyConverter = new CCV({from:"USD", to:"INR", amount:100, isDecimalComma:true})
Node.js Currency Converter: Promise-Based Convert Method with Rate Caching
currencyConverter.convert().then((response) => {
console.log(response) //or do something else
})
convert
can also take the amount as a parameter.
currencyConverter.convert(100).then((response) => {
console.log(response) //or do something else
})
To find the rates use the rates
method.
currencyConverter.rates().then((response) => {
console.log(response) //or do something else
})
The Node js Currency Converter Library not only simplifies currency conversions by eliminating the need for API subscriptions but also supports efficient rate caching for currency pairs. To implement rate caching, instantiate an object of the CurrencyConverter class only once in your project. This should be done in a dedicated CurrencyConverter file, where you can set up the rates caching configuration. Subsequently, you can import this single instance of CurrencyConverter across the rest of your project. By centralizing the instantiation and configuration, you ensure that the cached rates are consistently utilized throughout your application, improving performance and reducing redundant requests.
Note: Moreover, the library supports method chaining, allowing for more readable and concise code when performing currency conversions. When caching is enabled, the process remains efficient as the rates are not deleted after the ratesCacheDuration expires. Instead, the rate remains in the cache until a new request for the same currency pair is made, at which point the old rate is overwritten with the updated value. This approach ensures that the conversion rates are always up-to-date while minimizing unnecessary API calls. The following is an example of how to set up and use the CurrencyConverter in a dedicated file, showcasing the implementation of rate caching and chaining for seamless currency conversions.
const CCV = require('currency-converter-vl')
let currencyConverter = new CCV()
let ratesCacheOptions = {
isRatesCaching: true, // Set this boolean to true to implement rate caching
ratesCacheDuration: 3600 // Set this to a positive number to set the number of seconds you want the rates to be cached. Defaults to 3600 seconds (1 hour)
}
currencyConverter = currencyConverter.setupRatesCache(ratesCacheOptions)
module.exports = currencyConverter
Effortless Currency Conversion with Chaining Support in Node js
currencyConverter.from("CAD").to("HRK").amount(125).convert().then((response) => {
console.log(response) //or do something else
})
Disclaimer
This package uses web scraping to provide the api.
Issues
If any issues are found, they can be reported here.
License
This project is licensed under the MIT license.