mirror of
https://github.com/jakejarvis/jarv.is.git
synced 2025-11-23 02:20:50 -05:00
add popular posts API endpoint
This commit is contained in:
@@ -17,7 +17,7 @@ exports.handler = async (event) => {
|
||||
return {
|
||||
statusCode: 400,
|
||||
body: JSON.stringify({
|
||||
message: "Page slug required.",
|
||||
message: "Parameter `slug` is required.",
|
||||
}),
|
||||
};
|
||||
}
|
||||
@@ -40,7 +40,6 @@ exports.handler = async (event) => {
|
||||
Pragma: "no-cache",
|
||||
"Access-Control-Allow-Methods": "GET",
|
||||
"Access-Control-Allow-Origin": "*",
|
||||
"x-fauna-ts": result.ts.toString().slice(0, -3),
|
||||
},
|
||||
body: JSON.stringify({
|
||||
slug: result.data.slug,
|
||||
|
||||
51
functions/popular.js
Normal file
51
functions/popular.js
Normal file
@@ -0,0 +1,51 @@
|
||||
"use strict";
|
||||
|
||||
const faunadb = require("faunadb"),
|
||||
q = faunadb.query;
|
||||
const numeral = require("numeral");
|
||||
const pluralize = require("pluralize");
|
||||
require("dotenv").config();
|
||||
|
||||
exports.handler = async () => {
|
||||
try {
|
||||
const client = new faunadb.Client({
|
||||
secret: process.env.FAUNADB_SERVER_SECRET,
|
||||
});
|
||||
|
||||
const result = await client.query(
|
||||
q.Map(
|
||||
q.Paginate(q.Documents(q.Collection("hits"))),
|
||||
q.Lambda((x) => q.Select("data", q.Get(x)))
|
||||
)
|
||||
);
|
||||
|
||||
const posts = result.data.sort((a, b) => {
|
||||
return a.hits > b.hits ? -1 : 1;
|
||||
});
|
||||
|
||||
posts.forEach((p) => {
|
||||
p.pretty_hits = numeral(p.hits).format("0,0");
|
||||
p.pretty_unit = pluralize("hit", p.hits);
|
||||
p.url = "https://jarv.is/" + p.slug + "/";
|
||||
});
|
||||
|
||||
return {
|
||||
statusCode: 200,
|
||||
headers: {
|
||||
"Content-Type": "application/json; charset=utf-8",
|
||||
"Access-Control-Allow-Methods": "GET",
|
||||
"Access-Control-Allow-Origin": "*",
|
||||
},
|
||||
body: JSON.stringify(posts),
|
||||
};
|
||||
} catch (error) {
|
||||
console.error(error);
|
||||
|
||||
return {
|
||||
statusCode: 400,
|
||||
body: JSON.stringify({
|
||||
message: error.message,
|
||||
}),
|
||||
};
|
||||
}
|
||||
};
|
||||
Reference in New Issue
Block a user