1
mirror of https://github.com/jakejarvis/jarv.is.git synced 2025-04-27 14:56:21 -04:00
jarv.is/api/projects.js
Jake Jarvis b755b66d19
use preact for common components across site (#663)
* convert GitHub cards grid from lit-html to preact

* give hit counter the preact treatment

* extract loading spinner component to a shared location

* move *some* loading spinner styles to its JSX

* Update .percy.yml

* pick up images in JS w/ webpack

* pull star/fork icons straight from @primer/octicons

* a bit of cleanup

* check `typeof window !== "undefined"` before rendering

* bump misc. deps

* silence missing license warnings for preact-hooks and preact-compat

* add source-map-loader

* Update loading.js
2021-11-24 13:51:29 -05:00

104 lines
2.8 KiB
JavaScript

import * as Sentry from "@sentry/node";
import { graphql } from "@octokit/graphql";
Sentry.init({
dsn: process.env.SENTRY_DSN || "",
environment: process.env.NODE_ENV || process.env.VERCEL_ENV || "",
});
export default async (req, res) => {
try {
// permissive access control headers
res.setHeader("Access-Control-Allow-Methods", "GET");
res.setHeader("Access-Control-Allow-Origin", "*");
if (req.method !== "GET") {
return res.status(405).send(); // 405 Method Not Allowed
}
// allow custom limit, max. 24 results
let limit = 24;
if (req.query.limit && req.query.limit > 0 && req.query.limit < limit) {
limit = req.query.limit;
}
let result;
if (typeof req.query.top !== "undefined") {
// get most popular repos (/projects/?top)
result = await fetchRepos("STARGAZERS", limit);
} else {
// default to latest repos
result = await fetchRepos("PUSHED_AT", limit);
}
// let Vercel edge and browser cache results for 15 mins
res.setHeader("Cache-Control", "public, max-age=900, s-maxage=900, stale-while-revalidate");
return res.status(200).json(result);
} catch (error) {
console.error(error);
// log error to sentry, give it 2 seconds to finish sending
Sentry.captureException(error);
await Sentry.flush(2000);
const message = error instanceof Error ? error.message : "Unknown error.";
// 500 Internal Server Error
return res.status(500).json({ success: false, message });
}
};
const fetchRepos = async (sort, limit) => {
// https://docs.github.com/en/graphql/reference/objects#repository
const { user } = await graphql(
`
query ($username: String!, $sort: String, $limit: Int) {
user(login: $username) {
repositories(
first: $limit
isLocked: false
isFork: false
ownerAffiliations: OWNER
privacy: PUBLIC
orderBy: { field: $sort, direction: DESC }
) {
edges {
node {
name
url
description
pushedAt
stargazerCount
forkCount
primaryLanguage {
name
color
}
}
}
}
}
}
`,
{
username: "jakejarvis",
limit: parseInt(limit, 10),
sort,
headers: {
authorization: `token ${process.env.GH_PUBLIC_TOKEN}`,
},
}
);
return user.repositories.edges.map(({ node: repo }) => ({
name: repo.name,
url: repo.url,
description: repo.description,
updatedAt: new Date(repo.pushedAt),
stars: repo.stargazerCount,
forks: repo.forkCount,
language: repo.primaryLanguage,
}));
};