Skip to content

Commit

Permalink
feat: 🎸 Use only one request to get reward data
Browse files Browse the repository at this point in the history
  • Loading branch information
fpbrault committed Dec 21, 2021
1 parent 624e2b2 commit d34190a
Showing 1 changed file with 9 additions and 31 deletions.
40 changes: 9 additions & 31 deletions pages/api/rewards.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,36 +4,13 @@ export default async function handler(req: NextApiRequest, res: NextApiResponse)
res.setHeader("Cache-Control", "s-maxage=300");

if (req.method === "GET") {
const resultsPage1 = await fetch("https://reward-api.aqua.network/api/rewards/?page=" + 1, {
method: "GET",
headers: { "Content-Type": "application/json" }
})
.then((response) => {
if (response.status >= 400 && response.status < 600) {
throw new Error(response.statusText);
}
return response.json();
})
.catch((error) => {
console.error(error);
});
const resultsPage2 = await fetch("https://reward-api.aqua.network/api/rewards/?page=" + 2, {
method: "GET",
headers: { "Content-Type": "application/json" }
})
.then((response) => {
if (response.status >= 400 && response.status < 600) {
throw new Error(response.statusText);
}
return response.json();
})
.catch((error) => {
console.error(error);
});
const resultsPage3 = await fetch("https://reward-api.aqua.network/api/rewards/?page=" + 3, {
method: "GET",
headers: { "Content-Type": "application/json" }
})
const resultsPage1 = await fetch(
"https://reward-api.aqua.network/api/rewards/?page=1&page_size=100&ordering=-daily_total_reward",
{
method: "GET",
headers: { "Content-Type": "application/json" }
}
)
.then((response) => {
if (response.status >= 400 && response.status < 600) {
throw new Error(response.statusText);
Expand All @@ -43,9 +20,10 @@ export default async function handler(req: NextApiRequest, res: NextApiResponse)
.catch((error) => {
console.error(error);
});

res.status(200);

res.json(resultsPage1.results.concat(resultsPage2.results, resultsPage3.results));
res.json(resultsPage1.results);
} else {
throw new Error(`The HTTP ${req.method} method is not supported at this route.`);
}
Expand Down

0 comments on commit d34190a

Please sign in to comment.