From e2b7bb9132c9f98057eaae90ab1777f8b921e5ad Mon Sep 17 00:00:00 2001 From: datejer Date: Sun, 22 Nov 2020 13:17:33 +0100 Subject: [PATCH] Add github oauth app authorization for increased ratelimits --- api/[owner]/[repo].js | 44 ++++++++++++++++++++++++++++--------------- api/ratelimit.js | 18 ++++++++++++++++++ 2 files changed, 47 insertions(+), 15 deletions(-) create mode 100644 api/ratelimit.js diff --git a/api/[owner]/[repo].js b/api/[owner]/[repo].js index 056f4d2..a702a95 100644 --- a/api/[owner]/[repo].js +++ b/api/[owner]/[repo].js @@ -4,7 +4,13 @@ module.exports = (req, res) => { const { owner, repo } = req.query; axios - .get(`https://api.github.com/repos/${owner}/${repo}/deployments`) + .get(`https://api.github.com/repos/${owner}/${repo}/deployments`, { + headers: { + Authorization: `Basic ${Buffer.from( + `${process.env.ID}:${process.env.SECRET}` + ).toString("base64")}`, + }, + }) .then((response) => { if (response.data.length <= 0) return res.status(404).json({ @@ -32,20 +38,28 @@ module.exports = (req, res) => { const latest = vercelDeployments[0]; - axios.get(latest.statuses_url).then((response) => { - if (response.data[0].state === "success") - return res.status(200).json({ - url: "https://img.shields.io/badge/vercel-passing-success", - }); - else if (response.data[0].state === "failure") - return res.status(200).json({ - url: "https://img.shields.io/badge/vercel-failed-critical", - }); - else if (response.data[0].state === "pending") - return res.status(200).json({ - url: "https://img.shields.io/badge/vercel-pending-yellow", - }); - }); + axios + .get(latest.statuses_url, { + headers: { + Authorization: `Basic ${Buffer.from( + `${process.env.ID}:${process.env.SECRET}` + ).toString("base64")}`, + }, + }) + .then((response) => { + if (response.data[0].state === "success") + return res.status(200).json({ + url: "https://img.shields.io/badge/vercel-passing-success", + }); + else if (response.data[0].state === "failure") + return res.status(200).json({ + url: "https://img.shields.io/badge/vercel-failed-critical", + }); + else if (response.data[0].state === "pending") + return res.status(200).json({ + url: "https://img.shields.io/badge/vercel-pending-yellow", + }); + }); }) .catch((error) => { return res.status(error.response.status).json({ diff --git a/api/ratelimit.js b/api/ratelimit.js new file mode 100644 index 0000000..8c17cda --- /dev/null +++ b/api/ratelimit.js @@ -0,0 +1,18 @@ +const axios = require("axios"); + +module.exports = (req, res) => { + axios + .get(`https://api.github.com/rate_limit`, { + headers: { + Authorization: `Basic ${Buffer.from( + `${process.env.ID}:${process.env.SECRET}` + ).toString("base64")}`, + }, + }) + .then((response) => { + return res.status(200).json(response.data); + }) + .catch((error) => { + return res.status(400).json(error.response.data); + }); +};