Skip to content

Commit f480a8b

Browse files
committed
v1.0.0
0 parents  commit f480a8b

14 files changed

Lines changed: 18671 additions & 0 deletions

.babelrc

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
{
2+
"presets": [
3+
"@babel/preset-env"
4+
],
5+
"plugins": [
6+
["@babel/plugin-proposal-class-properties"],
7+
["@babel/plugin-transform-async-to-generator"]
8+
]
9+
}

.github/workflows/npm-publish.yml

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
name: npm-publish
2+
3+
on:
4+
push:
5+
branches:
6+
- master
7+
8+
jobs:
9+
publish:
10+
runs-on: ubuntu-latest
11+
12+
env:
13+
EXCHANGE_RATES_API_KEY: ${{ secrets.API_KEY }}
14+
15+
steps:
16+
- uses: actions/checkout@v1
17+
18+
- uses: actions/setup-node@v1
19+
with:
20+
node-version: 10
21+
22+
- name: Install and test
23+
run: |
24+
rm package-lock.json
25+
npm install --no-optional
26+
npm run test
27+
28+
- uses: JS-DevTools/npm-publish@v1
29+
with:
30+
token: ${{ secrets.NPM_TOKEN }}
31+
access: "public"

.github/workflows/unittest.yml

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
name: Unit test
2+
3+
on: [push, pull_request]
4+
5+
jobs:
6+
unit_test:
7+
runs-on: ${{ matrix.operating-system }}
8+
strategy:
9+
matrix:
10+
operating-system: [ ubuntu-18.04, windows-2016 ]
11+
node-version: [10.x, 12.x, 14.x, 15.x]
12+
13+
env:
14+
EXCHANGE_RATES_API_KEY: ${{ secrets.API_KEY }}
15+
16+
steps:
17+
- uses: actions/checkout@v2
18+
19+
- name: Use Node.js ${{ matrix.node-version }}
20+
uses: actions/setup-node@v1
21+
with:
22+
node-version: ${{ matrix.node-version }}
23+
24+
- name: Run test
25+
run: |
26+
rm package-lock.json
27+
npm install --no-optional
28+
npm run test

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
node_modules
2+
.idea

LICENSE

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
MIT License
2+
3+
Copyright (c) 2021 Abstract
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in all
13+
copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+
SOFTWARE.

README.md

Lines changed: 177 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,177 @@
1+
# AbstractAPI javascript-exchange-rates library
2+
3+
Integrate the powerful [Exchange Rates API from Abstract](https://www.abstractapi.com/exchange-rate-api) in your Javascript or NodeJS project in a few lines of code.
4+
5+
The Exchange Rate API is an REST API that allows you to:
6+
7+
- look up the latest exchange rates for 80+ currencies via the *live* endpoint
8+
- get historical exchange rates using the *historical* endpoint
9+
- convert an arbitrary amount from one currency to another using the *convert* endpoint
10+
11+
It's very simple to use: you only need to submit your API key and a currency symbole (such as "USD"), and the API will respond with current exchange rate, historical data, or convertion rates.
12+
13+
# Documentation
14+
15+
## Installation
16+
17+
You can install **javascript-exchange-rates** via npm, from our CDN, or download the source into your project.
18+
19+
### ES6
20+
21+
Download and install the library from npm:
22+
23+
```
24+
npm install @abstractapi/javascript-exchange-rates --save
25+
```
26+
27+
In your project, import it and configure your `API_KEY`:
28+
29+
```js
30+
import {AbstractExchangeRates} from 'javascript-exchange-rates'
31+
32+
AbstractExchangeRates.configure('API_KEY')
33+
```
34+
35+
### Browser, from the CDN
36+
37+
You can have the browser download the library from its closest location through jsDeliver CDN:
38+
39+
```js
40+
<script src="https://cdn.jsdelivr.net/npm/@abstractapi/javascript-core@latest/dist/javascript-core.js"></script>
41+
<script src="https://cdn.jsdelivr.net/npm/@abstractapi/javascript-exchange-rates@latest/dist/javascript-exchange-rates.js"></script>
42+
<script>
43+
AbstractExchangeRates.configure('API_KEY');
44+
45+
// use the library
46+
</script>
47+
```
48+
49+
### Browser, from the built file
50+
51+
You can build the library yourself, or get the already built file from the `dist` directory and load it:
52+
53+
```js
54+
<script src="dist/javascript-exchange-rates.js"></script>
55+
<script>
56+
AbstractExchangeRates.configure('API_KEY');
57+
58+
// use the library
59+
</script>
60+
```
61+
62+
## API key
63+
64+
Get your API key for free and without hassle from the [Abstact website](https://app.abstractapi.com/users/signup?target=/api/exchange-rates/pricing/select).
65+
66+
## Quickstart
67+
68+
AbstractAPI **javascript-exchange-rates** library returns a [Promise](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise) so you can use one of the following approaches:
69+
70+
### Async/Await
71+
72+
```js
73+
async function getLiveRates(currency) {
74+
let response = await AbstractExchangeRates.live(currency);
75+
console.log(response);
76+
}
77+
78+
async function getHistoricalRates(currency, date) {
79+
let response = await AbstractExchangeRates.historical(currency, date);
80+
console.log(response);
81+
}
82+
83+
async function getConvertion(currency, target) {
84+
let response = await AbstractExchangeRates.convert(currency, target);
85+
console.log(response);
86+
}
87+
```
88+
89+
### Using .then()
90+
91+
```js
92+
function getLiveRates(currency) {
93+
AbstractExchangeRates.live(currency)
94+
.then(response => {
95+
console.log(response);
96+
})
97+
}
98+
99+
function getHistoricalRates(currency, date) {
100+
AbstractExchangeRates.historical(currency, date)
101+
.then(response => {
102+
console.log(response);
103+
})
104+
}
105+
106+
function getConvertion(currency, target) {
107+
AbstractExchangeRates.convert(currency, target)
108+
.then(response => {
109+
console.log(response);
110+
})
111+
}
112+
```
113+
114+
## API response
115+
116+
The API response contains the following fields:
117+
118+
### `live` response parameters
119+
| Parameter| Type| Details |
120+
| - | - | - |
121+
| base | String | The base currency used to get the exchange rates. |
122+
| last_updated | String | The Unix timestamp of when the returned data was last updated. |
123+
| exchange_rates | Object | A JSON Object containing each of the target currency as the key and its exchange rate versus the base currency as that key's value. |
124+
125+
### `historical` response parameters
126+
127+
| Parameter | Type | Details |
128+
| - | - | - |
129+
| base | String | The base currency used to get the exchange rates. |
130+
| date | String | The date the currencies were pulled from, per the successful request. |
131+
| exchange_rates | Object | A JSON Object containing each of the target currency as the key and its exchange rate versus the base currency as that key's value. |
132+
133+
### `convert` response parameters
134+
135+
| Parameter | Type | Details |
136+
| - | - | - |
137+
| base | String | The base currency used to get the exchange rates. |
138+
| target | String | The target currency that the base_amount was converted into. |
139+
| date | String | The date the currencies were pulled from, per the successful request. |
140+
| base_amount | Float | The amount of the base currency from the request. |
141+
| converted_amount | Float | The amount of the target currency that the base_amount has been converted into |
142+
| exchange_rate | Float | The exchange rate used to convert the base_amount from the base currency to the target currency |
143+
144+
## Detailed documentation
145+
146+
You will find additional information and request examples in the [Abstract help page](https://app.abstractapi.com/api/exchange-rates/documentation).
147+
148+
## Getting help
149+
150+
If you need help installing or using the library, please contact [Abstract's Support](https://app.abstractapi.com/api/exchange-rates/support).
151+
152+
For bug report and feature suggestion, please use [this repository issues page](https://github.com/abstractapi/javascript-exchange-rates/issues).
153+
154+
# Contribution
155+
156+
Contributions are always welcome, as they improve the quality of the libraries we provide to the community.
157+
158+
Please provide your changes covered by the appropriate unit tests, and post them in the [pull requests page](https://github.com/abstractapi/javascript-exchange-rates/pulls).
159+
160+
## NPM
161+
162+
### Installation
163+
164+
Run `npm install` in the command line to install the dependencies. To update those dependencies you need to run `npm update`.
165+
166+
### Building
167+
168+
To build the library and generate the minified file in the *dist* directory, you need to run `npm run build`.
169+
170+
To build the lib, you need to run `npm run build:lib`.
171+
172+
### Test
173+
174+
To run the test suite, you need the API key from the abstract website and you can run:
175+
176+
EXCHANGE_RATES_API_KEY=(your key here) npm run test
177+

dist/javascript-exchange-rates.js

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

index.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
module.exports = require('./lib/index');

lib/index.js

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
"use strict";
2+
3+
function _typeof(obj) { "@babel/helpers - typeof"; if (typeof Symbol === "function" && typeof Symbol.iterator === "symbol") { _typeof = function _typeof(obj) { return typeof obj; }; } else { _typeof = function _typeof(obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }; } return _typeof(obj); }
4+
5+
Object.defineProperty(exports, "__esModule", {
6+
value: true
7+
});
8+
exports.AbstractExchangeRates = void 0;
9+
10+
var core = _interopRequireWildcard(require("@abstractapi/javascript-core"));
11+
12+
function _getRequireWildcardCache(nodeInterop) { if (typeof WeakMap !== "function") return null; var cacheBabelInterop = new WeakMap(); var cacheNodeInterop = new WeakMap(); return (_getRequireWildcardCache = function _getRequireWildcardCache(nodeInterop) { return nodeInterop ? cacheNodeInterop : cacheBabelInterop; })(nodeInterop); }
13+
14+
function _interopRequireWildcard(obj, nodeInterop) { if (!nodeInterop && obj && obj.__esModule) { return obj; } if (obj === null || _typeof(obj) !== "object" && typeof obj !== "function") { return { "default": obj }; } var cache = _getRequireWildcardCache(nodeInterop); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (key !== "default" && Object.prototype.hasOwnProperty.call(obj, key)) { var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null; if (desc && (desc.get || desc.set)) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } newObj["default"] = obj; if (cache) { cache.set(obj, newObj); } return newObj; }
15+
16+
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
17+
18+
function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
19+
20+
var AbstractExchangeRates = function AbstractExchangeRates() {
21+
_classCallCheck(this, AbstractExchangeRates);
22+
};
23+
24+
exports.AbstractExchangeRates = AbstractExchangeRates;
25+
26+
_defineProperty(AbstractExchangeRates, "apiKey", void 0);
27+
28+
_defineProperty(AbstractExchangeRates, "configure", function (apiKey) {
29+
AbstractExchangeRates.apiKey = apiKey;
30+
});
31+
32+
_defineProperty(AbstractExchangeRates, "live", function (base, target) {
33+
if (!base) {
34+
throw new Error('Base is not provided.');
35+
}
36+
37+
var query = "base=".concat(base);
38+
if (target && target !== "") query += "&target=".concat(target);
39+
return core.makeApiCall('exchange-rates', AbstractExchangeRates.apiKey, query, 'live');
40+
});
41+
42+
_defineProperty(AbstractExchangeRates, "historical", function (base, date, target) {
43+
if (!base) {
44+
throw new Error('Base is not provided.');
45+
}
46+
47+
if (!date) {
48+
throw new Error('Date is not provided.');
49+
}
50+
51+
var query = "base=".concat(base, "&date=").concat(date);
52+
if (target && target !== "") query += "&target=".concat(target);
53+
return core.makeApiCall('exchange-rates', AbstractExchangeRates.apiKey, query, 'historical');
54+
});
55+
56+
_defineProperty(AbstractExchangeRates, "convert", function (base, target, date, base_amount) {
57+
if (!base) {
58+
throw new Error('Base is not provided.');
59+
}
60+
61+
if (!target) {
62+
throw new Error('Target is not provided.');
63+
}
64+
65+
var query = "base=".concat(base, "&target=").concat(target);
66+
if (date && date !== "") query += "&date=".concat(date);
67+
if (base_amount && base_amount !== "") query += "&base_amount=".concat(base_amount);
68+
return core.makeApiCall('exchange-rates', AbstractExchangeRates.apiKey, query, 'convert');
69+
});

0 commit comments

Comments
 (0)