1
mirror of https://github.com/jakejarvis/jarv.is.git synced 2025-07-03 18:26:38 -04:00

dear lord typescript gets messy fast

This commit is contained in:
2021-06-05 21:10:51 -04:00
parent 0a9e0d789e
commit 63cb4dde53
7 changed files with 117 additions and 72 deletions

View File

@ -4,11 +4,8 @@ import { VercelRequest, VercelResponse } from "@vercel/node";
import { Client, query as q } from "faunadb";
import numeral from "numeral";
import pluralize from "pluralize";
import dotenv from "dotenv";
dotenv.config();
module.exports = async (req: VercelRequest, res: VercelResponse) => {
export default async (req: VercelRequest, res: VercelResponse): Promise<VercelResponse> => {
const { slug } = req.query;
try {
@ -27,22 +24,32 @@ module.exports = async (req: VercelRequest, res: VercelResponse) => {
secret: process.env.FAUNADB_SERVER_SECRET,
});
type PageHits = {
slug: string;
hits: number;
pretty_hits?: string;
pretty_unit?: string;
};
// refer to snippet below for the `hit` function defined in the Fauna cloud
// eslint-disable-next-line @typescript-eslint/no-explicit-any
const result = await client.query<any>(q.Call(q.Function("hit"), slug));
// send client the new hit count
const hits: PageHits = {
...result.data,
pretty_hits: numeral(result.data.hits).format("0,0"),
pretty_unit: pluralize("hit", result.data.hits),
};
// disable caching on both ends
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),
});
// send client the *new* hit count
return res.json(hits);
} catch (error) {
console.error(error);

View File

@ -1,13 +1,11 @@
"use strict";
import { VercelRequest, VercelResponse } from "@vercel/node";
import { GraphQLClient, gql } from "graphql-request";
import { escape } from "html-escaper";
import numeral from "numeral";
import { DateTime } from "luxon";
import dotenv from "dotenv";
dotenv.config();
import numeral from "numeral";
import { GraphQLClient } from "graphql-request";
import gql from "graphql-tag";
const username = "jakejarvis";
const endpoint = "https://api.github.com/graphql";
@ -55,20 +53,34 @@ async function fetchRepos(sort: string, limit: number) {
}
`;
const response = await client.request(query, { sort, limit });
type Repository = {
name: string;
url: string;
description: string;
pushedAt: string;
pushedAt_relative?: string;
stargazerCount: number;
stargazerCount_pretty?: string;
forkCount: number;
forkCount_pretty?: string;
primaryLanguage?: unknown;
};
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" }),
}));
const response = await client.request(query, { sort, limit });
const currentRepos: Array<Repository> = response.user.repositories.edges.map(
({ node: repo }: { [key: string]: Repository }) => ({
...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: VercelRequest, res: VercelResponse) => {
export default async (req: VercelRequest, res: VercelResponse): Promise<VercelResponse> => {
try {
// some rudimentary error handling
if (req.method !== "GET") {
@ -89,6 +101,7 @@ module.exports = async (req: VercelRequest, res: VercelResponse) => {
res.setHeader("Cache-Control", "s-maxage=900, 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);

View File

@ -5,13 +5,10 @@ import { Client, query as q } from "faunadb";
import numeral from "numeral";
import pluralize from "pluralize";
import rssParser from "rss-parser";
import dotenv from "dotenv";
dotenv.config();
const baseUrl = "https://jarv.is/";
module.exports = async (req: VercelRequest, res: VercelResponse) => {
export default async (req: VercelRequest, res: VercelResponse): Promise<VercelResponse> => {
try {
// some rudimentary error handling
if (!process.env.FAUNADB_SERVER_SECRET) {
@ -40,45 +37,50 @@ module.exports = async (req: VercelRequest, res: VercelResponse) => {
),
]);
// TODO: make typescript interface/type
const stats = {
total: {
hits: 0,
pretty_hits: "",
pretty_unit: "",
},
pages: result.data,
type SiteStats = {
hits: number;
pretty_hits?: string;
pretty_unit?: string;
};
type PageStats = {
title: string;
url: string;
date: string;
slug?: string;
hits: number;
pretty_hits: string;
pretty_unit: string;
};
type OverallStats = {
total: SiteStats;
pages: Array<PageStats>;
};
stats.pages.map(
(p: {
title: string;
url: string;
date: string;
slug?: string;
hits: number;
pretty_hits: string;
pretty_unit: string;
}) => {
// match URLs from RSS feed with db to populate some metadata
const match = feed.items.find((x: { link: string }) => x.link === baseUrl + p.slug + "/");
if (match) {
p.title = match.title;
p.url = match.link;
p.date = match.isoDate;
delete p.slug;
}
const pages: Array<PageStats> = result.data;
const stats: OverallStats = {
total: { hits: 0 },
pages,
};
// 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;
pages.map((p: PageStats) => {
// match URLs from RSS feed with db to populate some metadata
const match = feed.items.find((x: { link: string }) => 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: { hits: number }, b: { hits: number }) => {
@ -93,6 +95,7 @@ module.exports = async (req: VercelRequest, res: VercelResponse) => {
res.setHeader("Cache-Control", "s-maxage=900, stale-while-revalidate");
res.setHeader("Access-Control-Allow-Methods", "GET");
res.setHeader("Access-Control-Allow-Origin", "*");
return res.json(stats);
} catch (error) {
console.error(error);