mirror of
https://github.com/jakejarvis/jarv.is.git
synced 2025-07-03 17:46:39 -04:00
deploy to Vercel
This commit is contained in:
80
api/hits.js
Normal file
80
api/hits.js
Normal file
@ -0,0 +1,80 @@
|
||||
"use strict";
|
||||
|
||||
const faunadb = require("faunadb"),
|
||||
q = faunadb.query;
|
||||
const numeral = require("numeral");
|
||||
const pluralize = require("pluralize");
|
||||
require("dotenv").config();
|
||||
|
||||
module.exports = async (req, res) => {
|
||||
const { slug } = req.query;
|
||||
|
||||
try {
|
||||
// some rudimentary error handling
|
||||
if (!process.env.FAUNADB_SERVER_SECRET) {
|
||||
throw new Error("Database credentials aren't set.");
|
||||
}
|
||||
if (req.method !== "GET") {
|
||||
throw new Error(`Method ${req.method} not allowed.`);
|
||||
}
|
||||
if (!slug || slug === "/") {
|
||||
throw new Error("Parameter `slug` is required.");
|
||||
}
|
||||
|
||||
const client = new faunadb.Client({
|
||||
secret: process.env.FAUNADB_SERVER_SECRET,
|
||||
});
|
||||
|
||||
// refer to snippet below for the `hit` function defined in the Fauna cloud
|
||||
const result = await client.query(q.Call(q.Function("hit"), slug));
|
||||
|
||||
// send client the new hit count
|
||||
res.setHeader("Cache-Control", "private, no-cache, no-store, must-revalidate");
|
||||
res.setHeader("Expires", 0);
|
||||
res.setHeader("Pragma", "no-cache");
|
||||
res.setHeader("Access-Control-Allow-Methods", "GET");
|
||||
res.setHeader("Access-Control-Allow-Origin", "*");
|
||||
return res.json({
|
||||
slug: result.data.slug,
|
||||
hits: result.data.hits,
|
||||
pretty_hits: numeral(result.data.hits).format("0,0"),
|
||||
pretty_unit: pluralize("hit", result.data.hits),
|
||||
});
|
||||
} catch (error) {
|
||||
console.error(error);
|
||||
|
||||
return res.status(400).json({ message: error.message });
|
||||
}
|
||||
};
|
||||
|
||||
/**
|
||||
|
||||
This is the FaunaDB function named `hit` defined in the cloud:
|
||||
https://dashboard.fauna.com/functions/hit/@db/global/jarv.is
|
||||
https://docs.fauna.com/fauna/current/api/fql/user_defined_functions
|
||||
|
||||
{
|
||||
name: "hit",
|
||||
role: null,
|
||||
body: Query(
|
||||
Lambda(
|
||||
"slug",
|
||||
Let(
|
||||
{ match: Match(Index("hits_by_slug"), Var("slug")) },
|
||||
If(
|
||||
Exists(Var("match")),
|
||||
Let(
|
||||
{
|
||||
ref: Select("ref", Get(Var("match"))),
|
||||
hits: ToInteger(Select("hits", Select("data", Get(Var("match")))))
|
||||
},
|
||||
Update(Var("ref"), { data: { hits: Add(Var("hits"), 1) } })
|
||||
),
|
||||
Create(Collection("hits"), { data: { slug: Var("slug"), hits: 1 } })
|
||||
)
|
||||
)
|
||||
)
|
||||
)
|
||||
}
|
||||
|
||||
*/
|
94
api/projects.js
Normal file
94
api/projects.js
Normal file
@ -0,0 +1,94 @@
|
||||
"use strict";
|
||||
|
||||
const { GraphQLClient, gql } = require("graphql-request");
|
||||
const { escape } = require("html-escaper");
|
||||
const numeral = require("numeral");
|
||||
const { DateTime } = require("luxon");
|
||||
|
||||
const username = "jakejarvis";
|
||||
const endpoint = "https://api.github.com/graphql";
|
||||
|
||||
async function fetchRepos(sort, limit) {
|
||||
// https://docs.github.com/en/graphql/guides/forming-calls-with-graphql
|
||||
const client = new GraphQLClient(endpoint, {
|
||||
headers: {
|
||||
Authorization: `Bearer ${process.env.GH_PUBLIC_TOKEN}`,
|
||||
Accept: "application/vnd.github.v3+json",
|
||||
},
|
||||
});
|
||||
|
||||
// https://docs.github.com/en/graphql/reference/objects#repository
|
||||
const query = gql`
|
||||
query ($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
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
`;
|
||||
|
||||
const response = await client.request(query, { sort, limit });
|
||||
|
||||
const currentRepos = response.user.repositories.edges.map(({ node: repo }) => ({
|
||||
...repo,
|
||||
description: escape(repo.description),
|
||||
stargazerCount_pretty: numeral(repo.stargazerCount).format("0,0"),
|
||||
forkCount_pretty: numeral(repo.forkCount).format("0,0"),
|
||||
pushedAt_relative: DateTime.fromISO(repo.pushedAt).toRelative({ locale: "en" }),
|
||||
}));
|
||||
|
||||
return currentRepos;
|
||||
}
|
||||
|
||||
module.exports = async (req, res) => {
|
||||
try {
|
||||
// some rudimentary error handling
|
||||
if (req.method !== "GET") {
|
||||
throw new Error(`Method ${req.method} not allowed.`);
|
||||
}
|
||||
if (!process.env.GH_PUBLIC_TOKEN) {
|
||||
throw new Error("GitHub API credentials aren't set.");
|
||||
}
|
||||
|
||||
// default to latest repos
|
||||
let sortBy = "PUSHED_AT";
|
||||
// get most popular repos (/projects?top)
|
||||
if (typeof req.query.top !== "undefined") sortBy = "STARGAZERS";
|
||||
|
||||
const repos = await fetchRepos(sortBy, 16);
|
||||
|
||||
// let Vercel edge cache results for 10 mins
|
||||
res.setHeader("Cache-Control", "s-maxage=600, stale-while-revalidate");
|
||||
res.setHeader("Access-Control-Allow-Methods", "GET");
|
||||
res.setHeader("Access-Control-Allow-Origin", "*");
|
||||
return res.json(repos);
|
||||
} catch (error) {
|
||||
console.error(error);
|
||||
|
||||
return res.status(400).json({ message: error.message });
|
||||
}
|
||||
};
|
84
api/stats.js
Normal file
84
api/stats.js
Normal file
@ -0,0 +1,84 @@
|
||||
"use strict";
|
||||
|
||||
const faunadb = require("faunadb"),
|
||||
q = faunadb.query;
|
||||
const numeral = require("numeral");
|
||||
const pluralize = require("pluralize");
|
||||
const rssParser = require("rss-parser");
|
||||
require("dotenv").config();
|
||||
|
||||
const baseUrl = "https://jarv.is/";
|
||||
|
||||
module.exports = async (req, res) => {
|
||||
try {
|
||||
// some rudimentary error handling
|
||||
if (!process.env.FAUNADB_SERVER_SECRET) {
|
||||
throw new Error("Database credentials aren't set.");
|
||||
}
|
||||
if (req.method !== "GET") {
|
||||
throw new Error(`Method ${req.method} not allowed.`);
|
||||
}
|
||||
|
||||
const parser = new rssParser({
|
||||
timeout: 3000,
|
||||
});
|
||||
const client = new faunadb.Client({
|
||||
secret: process.env.FAUNADB_SERVER_SECRET,
|
||||
});
|
||||
|
||||
// get database and RSS results asynchronously
|
||||
const [feed, result] = await Promise.all([
|
||||
parser.parseURL(baseUrl + "feed.xml"),
|
||||
client.query(
|
||||
q.Map(
|
||||
q.Paginate(q.Documents(q.Collection("hits"))),
|
||||
q.Lambda((x) => q.Select("data", q.Get(x)))
|
||||
)
|
||||
),
|
||||
]);
|
||||
|
||||
let stats = {
|
||||
total: {
|
||||
hits: 0,
|
||||
},
|
||||
pages: result.data,
|
||||
};
|
||||
|
||||
stats.pages.map((p) => {
|
||||
// match URLs from RSS feed with db to populate some metadata
|
||||
let match = feed.items.find((x) => x.link === baseUrl + p.slug + "/");
|
||||
if (match) {
|
||||
p.title = match.title;
|
||||
p.url = match.link;
|
||||
p.date = match.isoDate;
|
||||
delete p.slug;
|
||||
}
|
||||
|
||||
// it's easier to add comma-separated numbers and proper pluralization here on the backend
|
||||
p.pretty_hits = numeral(p.hits).format("0,0");
|
||||
p.pretty_unit = pluralize("hit", p.hits);
|
||||
|
||||
// add these hits to running tally
|
||||
stats.total.hits += p.hits;
|
||||
|
||||
return p;
|
||||
});
|
||||
|
||||
// sort by hits (descending)
|
||||
stats.pages.sort((a, b) => {
|
||||
return a.hits > b.hits ? -1 : 1;
|
||||
});
|
||||
|
||||
// do same prettification as above to totals
|
||||
stats.total.pretty_hits = numeral(stats.total.hits).format("0,0");
|
||||
stats.total.pretty_unit = pluralize("hit", stats.total.hits);
|
||||
|
||||
res.setHeader("Access-Control-Allow-Methods", "GET");
|
||||
res.setHeader("Access-Control-Allow-Origin", "*");
|
||||
return res.json(stats);
|
||||
} catch (error) {
|
||||
console.error(error);
|
||||
|
||||
return res.status(400).json({ message: error.message });
|
||||
}
|
||||
};
|
Reference in New Issue
Block a user