currency-converter-global
v1.0.0
Published
A simple JavaScript library for currency conversion using real-time exchange rates. This library is compatible with both **Node.js** and **browser** environments, making it suitable for use in **Angular**, **React**, and **Vue** applications.
Downloads
4
Readme
Currency Converter
A simple JavaScript library for currency conversion using real-time exchange rates. This library is compatible with both Node.js and browser environments, making it suitable for use in Angular, React, and Vue applications.
Features
- Fetches real-time exchange rates from Open Exchange Rates API.
- Converts between any two supported currencies.
- Easy integration with frontend frameworks like React, Angular, and Vue.
- Works both server-side (Node.js) and client-side (browser environments).
Installation
You can install the package using npm:
npm install currency-converter
Usage
Node.js
const CurrencyConverter = require('currency-converter');
const converter = new CurrencyConverter('YOUR_API_KEY'); // Replace with your Open Exchange Rates API key
(async () => {
try {
const result = await converter.convert('USD', 'EUR', 100);
console.log(`100 USD is equal to ${result.toFixed(2)} EUR`);
} catch (error) {
console.error(error.message);
}
})();
Browser (React, Angular, Vue)
React Example
import React, { useState, useEffect } from 'react';
import CurrencyConverter from 'currency-converter';
const converter = new CurrencyConverter('YOUR_API_KEY');
const CurrencyConverterComponent = () => {
const [convertedAmount, setConvertedAmount] = useState(0);
useEffect(() => {
async function convertCurrency() {
try {
const result = await converter.convert('USD', 'EUR', 100);
setConvertedAmount(result);
} catch (error) {
console.error(error.message);
}
}
convertCurrency();
}, []);
return <div>100 USD is equal to {convertedAmount.toFixed(2)} EUR</div>;
};
export default CurrencyConverterComponent;
Vue Example
<template>
<div>
100 USD is equal to {{ convertedAmount }} EUR
</div>
</template>
<script>
import CurrencyConverter from 'currency-converter';
export default {
data() {
return {
convertedAmount: 0
};
},
mounted() {
const converter = new CurrencyConverter('YOUR_API_KEY');
converter.convert('USD', 'EUR', 100)
.then(result => {
this.convertedAmount = result.toFixed(2);
})
.catch(error => console.error(error.message));
}
};
</script>
Angular Example
import { Component, OnInit } from '@angular/core';
import CurrencyConverter from 'currency-converter';
@Component({
selector: 'app-currency-converter',
template: `<p>100 USD is equal to {{ convertedAmount }} EUR</p>`,
})
export class CurrencyConverterComponent implements OnInit {
convertedAmount: number = 0;
converter = new CurrencyConverter('YOUR_API_KEY');
ngOnInit(): void {
this.converter.convert('USD', 'EUR', 100)
.then(result => {
this.convertedAmount = result.toFixed(2);
})
.catch(error => console.error(error.message));
}
}
API
CurrencyConverter(apiKey)
Creates a new instance of the CurrencyConverter
class. Requires an API key from the Open Exchange Rates service.
Parameters:
apiKey
(string): Your Open Exchange Rates API key.
convert(fromCurrency, toCurrency, amount)
Converts an amount from one currency to another.
Parameters:
fromCurrency
(string): The three-letter currency code of the base currency (e.g.,USD
).toCurrency
(string): The three-letter currency code of the target currency (e.g.,EUR
).amount
(number): The amount to be converted.
Returns:
- A
Promise
that resolves to the converted amount.
Example:
const converter = new CurrencyConverter('YOUR_API_KEY');
converter.convert('USD', 'EUR', 100)
.then(result => console.log(result))
.catch(error => console.error(error));
Setup
Before using the library, ensure you have a valid API key from the Open Exchange Rates website. Sign up for an account, and get your API key for real-time exchange rates.
Build
If you're modifying the source code, you can bundle it for client-side usage by running the following command:
npm run build
This will create the bundled file currency-converter.js
in the dist
directory.
License
This project is licensed under the MIT License.
Contributing
Feel free to submit pull requests or open issues for improvements or bug fixes!