preload data for all

This commit is contained in:
2023-02-18 18:42:23 +02:00
parent 727bfdaae7
commit 4074e775b1
9 changed files with 98 additions and 63 deletions
+65 -1
View File
@@ -5,7 +5,7 @@ import type { Provider } from "@auth/core/providers";
import type { Profile } from "@auth/core/types";
import { redirect, type Handle } from "@sveltejs/kit";
import { sequence } from "@sveltejs/kit/hooks";
import { announcements } from "./stores";
import { announcements, pubnixUsers, blogPosts, blogTags, blogAuthors } from "./stores";
import axios from "axios";
import { Agent } from "https";
@@ -59,6 +59,19 @@ export const handle: Handle = sequence(
}
);
export const fetchGhost = async (action: string, additional?: string) => {
return await axios(
env.GHOST_URL +
"/ghost/api/content/" +
action +
"/?key=" +
env.GHOST_API_KEY +
"&include=authors,tags&limit=all&formats=html,plaintext" +
(additional ? additional : ""),
{ httpsAgent: agent, timeout: 10000 }
);
};
const updateMap = async () => {
try {
const res = await axios(
@@ -74,6 +87,57 @@ const updateMap = async () => {
} catch (err) {
announcements.set({ error: true, message: "Error: " + err });
}
try {
const res = await axios(
"https://publapi.p.projectsegfau.lt/users",
{ httpsAgent: agent, timeout: 10000 }
);
if (res.status === 200) {
pubnixUsers.set(res.data);
} else {
pubnixUsers.set({ error: true, message: "Error: " + res.status });
}
} catch (err) {
pubnixUsers.set({ error: true, message: "Error: " + err });
}
try {
const res = await fetchGhost("posts");
if (res.status === 200) {
blogPosts.set(res.data);
} else {
blogPosts.set({ error: true, message: "Error: " + res.status });
}
} catch (err) {
blogPosts.set({ error: true, message: "Error: " + err });
}
try {
const res = await fetchGhost("tags");
if (res.status === 200) {
blogTags.set(res.data);
} else {
blogTags.set({ error: true, message: "Error: " + res.status });
}
} catch (err) {
blogTags.set({ error: true, message: "Error: " + err });
}
try {
const res = await fetchGhost("authors");
if (res.status === 200) {
blogAuthors.set(res.data);
} else {
blogAuthors.set({ error: true, message: "Error: " + res.status });
}
} catch (err) {
blogAuthors.set({ error: true, message: "Error: " + err });
}
};
updateMap();