Skip to content

Commit

Permalink
First commit
Browse files Browse the repository at this point in the history
  • Loading branch information
Oli Girling committed Dec 18, 2020
0 parents commit cbd233f
Show file tree
Hide file tree
Showing 12 changed files with 569 additions and 0 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
.idea
node_modules
7 changes: 7 additions & 0 deletions .prettierrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"singleQuote": true,
"semi": false,
"trailingComma": "none",
"tabWidth": 4,
"printWidth": 100
}
170 changes: 170 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,170 @@
# CurrencyApi NodeJs wrapper

<a href="https://currencyapi.net" title="CurrencyApi">CurrencyApi.net</a> provides live currency rates via a REST API. A live currency feed for over 152 currencies, including physical (USD, GBP, EUR + more) and cryptos (Bitcoin, Litecoin, Ethereum + more). A JSON and XML currency api updated every 60 seconds.

Features:

- Live exchange rates (updated every 60 seconds).
- 152 currencies world currencies.
- Popular cryptocurrencies included; Bitcoin, Litecoin etc.
- Convert currencies on the fly with the convert endpoint.
- Historical currency rates back to year 2000.
- Easy to follow <a href="https://currencyapi.net/documentation" title="currency-api-documentation">documentation</a>

Signup for a free or paid account <a href="https://currencyapi.net/#pricing-sec" title="currency-api-pricing">here</a>.

## This package

NodeJs wrapper for <a href="https://currencyapi.net" title="CurrencyApi">CurrencyApi.net</a> endpoints.

#### Prerequisites

- Minimum NodeJs v8 (npm v5 and above)
- Free or Paid account with CurrencyApi.net

## Installation

#### Using npm:

```bash
npm install currencyapi-node
```
then include the package with:

```javascript
const CurrencyApi = require('./currencyapi-node')
```

## Usage

### Instantiating

```javascript
const currency = new CurrencyApi('API_KEY');
```

### Live rates:

```javascript
const result = await currency.rates().get()
```
or
```javascript
currency.rates().get()
.then(console.log)
```

Example with all available methods (methods can be chained):
```javascript
const result = await currency
.rates()
.base('USD')
.output('JSON')
.get()
```
**Available methods for rates endpoint**

| Methods | Description |
| --- | --- |
| `base()` | The base currency you wish you receive the currency conversions for. This will output all currency conversions for that currency. **Default: USD**. |
| `output()` | Response output in either JSON or XML. **Default: JSON**. |

<br>

### List of available currencies:

```javascript
const result = await currency.currencies().get()
```

Example with all available methods:
```javascript
const result = await currency
.currencies()
.output('XML')
.get()
```

**Available methods for currencies endpoint**

| Methods | Description |
| --- | --- |
| `output()` | Response output in either JSON or XML. **Default: JSON**. |

<br>

### Convert:

```javascript
const result = await currency
.convert()
.from('BTC')
.to('USD')
.amount(100)
.get()
```

**Available methods for convert endpoint**

| Methods | Description |
| --- | --- |
| `amount()` | The value of the currency you want to convert from. This should be a number and can contain a decimal place. **Required**. |
| `from()` | The currency you want to convert. This will be a three letter ISO 4217 currency code from one of the currencies we have rates for. **Required**. |
| `to()` | The currency you want to convert the amount 'to'. Again this will be a three letter currency code from the ones we offer. **Required**. |
| `output()` | Response output in either JSON or XML. **Default: JSON**. |

<br>

### Historical:

```javascript
const result = await currency.history().date('2019-01-01').get()
```

Example with all available methods:

```javascript
const result = await currency
.history()
.date('2019-01-01')
.base('GBP')
.output('JSON')
.get()
```

**Available methods for historical endpoint**

| Methods | Description |
| --- | --- |
| `date()` | The historical date you wish to receive the currency conversions for. This should be formatted as YYYY-MM-DD. **Required**. |
| `base()` | The base currency you wish you receive the currency conversions for. This will output all currency conversions for that currency. **Default: USD**. |
| `output()` | Response output in either JSON or XML. **Default: JSON**. |

<br>

### Timeframe:

```javascript
const result = await currency.timeframe().startDate('2019-01-01').endDate('2019-01-05').get()
```

Example with all available methods:

```javascript
const result = await currency
.timeframe()
.startDate('2019-01-01')
.endDate('2019-01-05')
.base('GBP')
.output('JSON')
.get()
```

**Available methods for timeframe endpoint**

| Methods | Description |
| --- | --- |
| `startDate()` | The historical date you wish to receive the currency conversions from. This should be formatted as YYYY-MM-DD. **Required**. |
| `endDate()` | The historical date you wish to receive the currency conversions until. This should be formatted as YYYY-MM-DD. **Required**. |
| `base()` | The base currency you wish you receive the currency conversions for. This will output all currency conversions for that currency. **Default: USD**. |
| `output()` | Response output in either JSON or XML. **Default: JSON**. |
13 changes: 13 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

18 changes: 18 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
{
"name": "currencyapi-node",
"description": "Npm package for the CurrencyApi.net JSON & XML live currency feed",
"keywords": ["currency","feed","api","live-rates", "json currency", "crypto"],
"homepage": "https://currencyapi.net",
"authors": [
{
"name": "Oli Girling",
"email": "support@currencyapi.net",
"homepage": "https://currencyapi.net",
"role": "Developer"
}
],
"version": "1.0.0",
"dependencies": {
"node-fetch": "^2.6.0"
}
}
69 changes: 69 additions & 0 deletions src/CurrencyApi.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
const Rates = require('./classes/Rates')
const Convert = require('./classes/Convert')
const History = require('./classes/History')
const Timeframe = require('./classes/Timeframe')
const Currencies = require('./classes/Currencies')

/**
* @class CurrencyApi
* @link https://currencyapi.net/documentation
*/
class CurrencyApi
{
/**
* CurrencyApi constructor
*
* @param {string} key
*/
constructor(key) {
this.key = key;
}

/**
* Use the rates endpoint
*
* @returns {Rates}
*/
rates() {
return new Rates(this.key)
}

/**
* Use the convert endpoint
*
* @returns {Convert}
*/
convert() {
return new Convert(this.key)
}

/**
* Use the history endpoint
*
* @returns {History}
*/
history() {
return new History(this.key)
}

/**
* Use the timeframe endpoint
*
* @returns {Timeframe}
*/
timeframe() {
return new Timeframe(this.key)
}

/**
* Use the currencies endpoint
*
* @returns {Currencies}
*/
currencies() {
return new Currencies(this.key)
}

}

module.exports = CurrencyApi
51 changes: 51 additions & 0 deletions src/classes/Convert.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
const Endpoint = require('./Endpoint')

/**
* @class Convert
*/
class Convert extends Endpoint {

/**
* Convert constructor
*
* @param {string} key
*/
constructor(key) {
super(key, 'convert')
}

/**
* Set the amount
*
* @param {number} amount eg. 10.99
* @returns {Convert}
*/
amount(amount) {
this.addParam('amount', amount)
return this
}

/**
* Set the currency you want to convert to
*
* @param {string} currency eg. 'USD'
* @returns {Convert}
*/
to(currency) {
this.addParam('to', currency.toUpperCase())
return this
}

/**
* Set the currency you want to convert from
*
* @param {string} currency eg. 'GBP'
* @returns {Convert}
*/
from(currency) {
this.addParam('from', currency.toUpperCase())
return this
}
}

module.exports = Convert
18 changes: 18 additions & 0 deletions src/classes/Currencies.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
const Endpoint = require('./Endpoint')

/**
* @class Rates
*/
class Currencies extends Endpoint {

/**
* Rates constructor
*
* @param {string} key
*/
constructor(key) {
super(key, 'currencies')
}
}

module.exports = Currencies

0 comments on commit cbd233f

Please sign in to comment.