mirror of
https://github.com/jakejarvis/jarv.is.git
synced 2025-07-03 17:46:39 -04:00
playing with typescript (probably all wrong)
This commit is contained in:
@ -1,12 +1,14 @@
|
||||
"use strict";
|
||||
|
||||
const faunadb = require("faunadb"),
|
||||
q = faunadb.query;
|
||||
const numeral = require("numeral");
|
||||
const pluralize = require("pluralize");
|
||||
require("dotenv").config();
|
||||
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";
|
||||
|
||||
module.exports = async (req, res) => {
|
||||
dotenv.config();
|
||||
|
||||
module.exports = async (req: VercelRequest, res: VercelResponse) => {
|
||||
const { slug } = req.query;
|
||||
|
||||
try {
|
||||
@ -21,12 +23,13 @@ module.exports = async (req, res) => {
|
||||
throw new Error("Parameter `slug` is required.");
|
||||
}
|
||||
|
||||
const client = new faunadb.Client({
|
||||
const client = new 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));
|
||||
// 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
|
||||
res.setHeader("Cache-Control", "private, no-cache, no-store, must-revalidate");
|
@ -1,14 +1,18 @@
|
||||
"use strict";
|
||||
|
||||
const { GraphQLClient, gql } = require("graphql-request");
|
||||
const { escape } = require("html-escaper");
|
||||
const numeral = require("numeral");
|
||||
const { DateTime } = require("luxon");
|
||||
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();
|
||||
|
||||
const username = "jakejarvis";
|
||||
const endpoint = "https://api.github.com/graphql";
|
||||
|
||||
async function fetchRepos(sort, limit) {
|
||||
async function fetchRepos(sort: string, limit: number) {
|
||||
// https://docs.github.com/en/graphql/guides/forming-calls-with-graphql
|
||||
const client = new GraphQLClient(endpoint, {
|
||||
headers: {
|
||||
@ -64,7 +68,7 @@ async function fetchRepos(sort, limit) {
|
||||
return currentRepos;
|
||||
}
|
||||
|
||||
module.exports = async (req, res) => {
|
||||
module.exports = async (req: VercelRequest, res: VercelResponse) => {
|
||||
try {
|
||||
// some rudimentary error handling
|
||||
if (req.method !== "GET") {
|
86
api/stats.js
86
api/stats.js
@ -1,86 +0,0 @@
|
||||
"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);
|
||||
|
||||
// let Vercel edge cache results for 15 mins
|
||||
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);
|
||||
|
||||
return res.status(400).json({ message: error.message });
|
||||
}
|
||||
};
|
102
api/stats.ts
Normal file
102
api/stats.ts
Normal file
@ -0,0 +1,102 @@
|
||||
"use strict";
|
||||
|
||||
import { VercelRequest, VercelResponse } from "@vercel/node";
|
||||
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) => {
|
||||
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 Client({
|
||||
secret: process.env.FAUNADB_SERVER_SECRET,
|
||||
});
|
||||
|
||||
// get database and RSS results asynchronously
|
||||
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
||||
const [feed, result] = await Promise.all<{ [key: string]: any }, any>([
|
||||
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)))
|
||||
)
|
||||
),
|
||||
]);
|
||||
|
||||
// TODO: make typescript interface/type
|
||||
const stats = {
|
||||
total: {
|
||||
hits: 0,
|
||||
pretty_hits: "",
|
||||
pretty_unit: "",
|
||||
},
|
||||
pages: result.data,
|
||||
};
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
// 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 }) => {
|
||||
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);
|
||||
|
||||
// let Vercel edge cache results for 15 mins
|
||||
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);
|
||||
|
||||
return res.status(400).json({ message: error.message });
|
||||
}
|
||||
};
|
Reference in New Issue
Block a user