-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.ts
72 lines (57 loc) · 2 KB
/
app.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
import express from "express";
import api from "./config/config";
import bodyParser from "body-parser";
const request = require("request");
const serverless = require ("serverless-http");
let app = express();
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: true }));
app.get('/convert', (req, res) => {
const fromCurrency = req.query.from
const toCurrency = (req.query.to)
let amount:any = req.query.amount
let q = fromCurrency + '_' + toCurrency;
console.log('q:', q)
let url = api.url + 'convert?q=' + q + '&apiKey=' + api.apiKey;
request(url, function (error, response, body) {
console.log('error:', error);
console.log('statusCode:', response && response.statusCode);
console.log('body:', body);
let jsonObj = JSON.parse(body).results;
let val = (jsonObj[Object.keys(jsonObj)[0]].val);
let id = (jsonObj[Object.keys(jsonObj)[0]].id);
let convertedAmount = parseFloat(amount) * val
const result = {
query: {
from: fromCurrency,
to: toCurrency,
amount: parseFloat(amount)
},
results: {
id: id,
rate: val,
convertedAmount: parseFloat(convertedAmount.toFixed(4))
}
};
res.send(result)
})
});
app.get('/currencies', (req, res) => {
let url = api.url + "currencies?" + '&apiKey=' + api.apiKey;
request(url, function (error, response, body) {
console.log('error:', error);
console.log('statusCode:', response && response.statusCode);
console.log('body:', body);
res.send(JSON.parse(body))
})
});
app.get('/countries', (req, res) => {
let url = api.url + "countries?" + '&apiKey=' + api.apiKey;
request(url, function (error, response, body) {
console.log('error:', error);
console.log('statusCode:', response && response.statusCode);
console.log('body:', body);
res.send(JSON.parse(body))
})
})
exports.handler = serverless(app)