Skip to content

Commit

Permalink
fix: add try catch
Browse files Browse the repository at this point in the history
  • Loading branch information
Myphz committed Oct 2, 2023
1 parent 220ff14 commit 0c8192d
Showing 1 changed file with 32 additions and 13 deletions.
45 changes: 32 additions & 13 deletions src/routers/crypto.router.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,24 @@ import fetch from "node-fetch";
import express from "express";
import isFileValid from "../helpers/isFileValid.helper.js";
import { promises as fs } from "fs";
import { BINANCE_BASE_URL, COINMARKETCAP_BASE_URL, COINMARKETCAP_API_KEY, COINMARKETCAP_LIMIT, CRYTPO_INFO_FILE, GATEIO_BASE_URL } from "../config/config.js";
import {
BINANCE_BASE_URL,
COINMARKETCAP_BASE_URL,
COINMARKETCAP_API_KEY,
COINMARKETCAP_LIMIT,
CRYTPO_INFO_FILE,
GATEIO_BASE_URL,
} from "../config/config.js";
import { BINANCE_ERROR } from "../config/errors.js";

const router = express.Router();

// Endpoint to redirect binance API calls (as they don't work in the browser)
router.get("/binance/*", async (req, res, next) => {
// Construct the URL with the given endpoint and params
const data = await fetch(`${BINANCE_BASE_URL}${req.params[0]}?${new URLSearchParams(req.query)}`);
const data = await fetch(
`${BINANCE_BASE_URL}${req.params[0]}?${new URLSearchParams(req.query)}`
);
// Return error if the endpoint hasn't been found
if (!data.ok) {
const { message, status } = BINANCE_ERROR;
Expand All @@ -23,7 +32,12 @@ router.get("/binance/*", async (req, res, next) => {
router.get("/image/:coin", async (req, res, next) => {
const { coin } = req.params;
// Send request and fetch image data
const imageRequest = await fetch(`${GATEIO_BASE_URL}${coin.toLowerCase()}.png`);
let imageRequest;
try {
imageRequest = await fetch(`${GATEIO_BASE_URL}${coin.toLowerCase()}.png`);
} catch (err) {
return res.sendStatus(404);
}
if (!imageRequest.ok) return res.sendStatus(imageRequest.status);
const image = await imageRequest.arrayBuffer();

Expand All @@ -39,32 +53,37 @@ router.get("/image/:coin", async (req, res, next) => {
// For a maximum amount of 1 day (as set in the config file).
router.get("/info", async (req, res, next) => {
// Check if the info have already been cached
if (await isFileValid()) return res.json(JSON.parse(await fs.readFile(CRYTPO_INFO_FILE)));
if (await isFileValid())
return res.json(JSON.parse(await fs.readFile(CRYTPO_INFO_FILE)));
// Send request to coinmarketcap API
const response = await fetch(`${COINMARKETCAP_BASE_URL}listings/latest?limit=${COINMARKETCAP_LIMIT}`, {
headers: {
"X-CMC_PRO_API_KEY": COINMARKETCAP_API_KEY
const response = await fetch(
`${COINMARKETCAP_BASE_URL}listings/latest?limit=${COINMARKETCAP_LIMIT}`,
{
headers: {
"X-CMC_PRO_API_KEY": COINMARKETCAP_API_KEY,
},
}
});
);

const { data, status } = await response.json();
// Return error if an error has occurred
if (status.error_code) return next({ status: response.status, msg: status.error_message });
if (status.error_code)
return next({ status: response.status, msg: status.error_message });

const ret = {};
for (const crypto of data) {
const { name, symbol } = crypto;
if (symbol in ret) continue;
ret[symbol] = {
name,
mcap: crypto.quote["USD"]["market_cap"]
}
};
mcap: crypto.quote["USD"]["market_cap"],
};
}

// Cache file
fs.writeFile(CRYTPO_INFO_FILE, JSON.stringify(ret));

res.json(ret);
});

export default router;
export default router;

0 comments on commit 0c8192d

Please sign in to comment.