Skip to content

Commit

Permalink
Migrate to TypeScript
Browse files Browse the repository at this point in the history
  • Loading branch information
datejer committed Jan 30, 2022
1 parent e4defac commit 0638261
Show file tree
Hide file tree
Showing 8 changed files with 375 additions and 124 deletions.
92 changes: 0 additions & 92 deletions api/[owner]/[repo].js

This file was deleted.

104 changes: 104 additions & 0 deletions api/[owner]/[repo].ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
import axios from "axios";
import path from "path";
import fs from "fs";

import type { VercelRequest, VercelResponse } from "@vercel/node";

export default (req: VercelRequest, res: VercelResponse) => {
const { owner, repo } = req.query;

if (!owner || typeof owner !== "string") {
res.statusCode = 400;
return res.json({ message: "Please input the repository owner!" });
}

if (!repo || typeof repo !== "string") {
res.statusCode = 400;
return res.json({ message: "Please input the repository name!" });
}

let style;
if (!req.query.style) style = "flat";
else if (
req.query.style === "flat" ||
req.query.style === "flat-square" ||
req.query.style === "for-the-badge" ||
req.query.style === "plastic"
)
style = req.query.style;

axios
.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) {
res.setHeader("Content-Type", "image/svg+xml");
return fs
.createReadStream(
path.join(__dirname, `../../assets/${style}/none.svg`)
)
.pipe(res);
}

const vercelDeployments = response.data.filter(
(deployment) =>
deployment.creator.login === "vercel[bot]" &&
deployment.creator.html_url === "https://github.com/apps/vercel" &&
deployment.creator.type === "Bot"
);

if (vercelDeployments.length <= 0) {
res.setHeader("Content-Type", "image/svg+xml");
return fs
.createReadStream(
path.join(__dirname, `../../assets/${style}/none.svg`)
)
.pipe(res);
}

const latest = vercelDeployments[0];

axios
.get(latest.statuses_url, {
headers: {
Authorization: `Basic ${Buffer.from(
`${process.env.ID}:${process.env.SECRET}`
).toString("base64")}`,
},
})
.then((response) => {
res.setHeader("Content-Type", "image/svg+xml");
if (response.data[0].state === "success")
return fs
.createReadStream(
path.join(__dirname, `../../assets/${style}/passing.svg`)
)
.pipe(res);
else if (response.data[0].state === "failure")
return fs
.createReadStream(
path.join(__dirname, `../../assets/${style}/failed.svg`)
)
.pipe(res);
else if (response.data[0].state === "pending")
return fs
.createReadStream(
path.join(__dirname, `../../assets/${style}/pending.svg`)
)
.pipe(res);
});
})
.catch((error) => {
res.setHeader("Content-Type", "image/svg+xml");
return fs
.createReadStream(
path.join(__dirname, `../../assets/${style}/error.svg`)
)
.pipe(res);
});
};
6 changes: 0 additions & 6 deletions api/index.js

This file was deleted.

8 changes: 8 additions & 0 deletions api/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import { VercelRequest, VercelResponse } from "@vercel/node";

export default (req: VercelRequest, res: VercelResponse) => {
res.json({
message:
"Use /[owner]/[repo] to get a Vercel deployment badge for your github repository!",
});
};
18 changes: 0 additions & 18 deletions api/ratelimit.js

This file was deleted.

20 changes: 20 additions & 0 deletions api/ratelimit.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import axios from "axios";

import { VercelRequest, VercelResponse } from "@vercel/node";

export default (req: VercelRequest, res: VercelResponse) => {
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);
});
};
Loading

0 comments on commit 0638261

Please sign in to comment.