-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
70 lines (60 loc) · 2.11 KB
/
index.js
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
const PORT = process.env.PORT || 3000;
const dotenv = require("dotenv").config();
const express = require("express");
const axios = require("axios");
const app = express();
const apiURL = process.env.APIURL;
const bucketURL = process.env.BUCKETURL;
const apikey = process.env.APIKEY;
const headers = {
apikey: apikey,
};
// Base endpoint
app.get("/", (req, res) => {
res.json("Cryptofonts to the moon!");
});
// Endpoint for address only
app.get("/:address", async (req, res) => {
lowercaseAddress = req.params.address.toLowerCase();
axios
.get(apiURL + "address=eq." + lowercaseAddress + "&select=name,logoURI,symbol,chainId,decimals,address,chain", { headers })
.then((response) => {
const modres = response.data.map((item) => {
return {
...item,
logoURI: bucketURL + item.logoURI,
};
});
if (modres.length === 0) {
return res.status(404).json({ error: "No data found for " + lowercaseAddress });
}
res.json(modres);
})
.catch((error) => {
console.log("API error: ", error.message);
res.status(500).json({ error: "An error occurred while fetching data from the API." });
});
});
// Endpoint for chainId and address (recommended)
app.get("/:chainId/:address", async (req, res) => {
lowercaseAddress = req.params.address.toLowerCase();
axios
.get(apiURL + "chainId=eq." + req.params.chainId + "&address=eq." + lowercaseAddress + "&select=name,logoURI,symbol,chainId,decimals,address,chain", { headers })
.then((response) => {
const modres = response.data.map((item) => {
return {
...item,
logoURI: bucketURL + item.logoURI,
};
});
if (modres.length === 0) {
return res.status(404).json({ error: "No data found " + lowercaseAddress + " on chain " + req.params.chainId });
}
res.json(modres);
})
.catch((error) => {
console.log("API error: ", error.message);
res.status(500).json({ error: "An error occurred while fetching data from the API." });
});
});
app.listen(PORT, () => console.log(`Server running on port ${PORT}`));