Add basic static json api

This commit is contained in:
Kevin Kandlbinder 2021-02-14 19:48:59 +01:00
parent cfada612bb
commit ab665a50f2
2 changed files with 105 additions and 1 deletions

View file

@ -1,5 +1,6 @@
/* eslint-disable no-undef */
const path = require(`path`);
const fs = require("fs")
exports.createPages = async ({ actions, graphql, reporter }) => {
const { createPage } = actions
@ -37,4 +38,106 @@ exports.createPages = async ({ actions, graphql, reporter }) => {
})
});
}
const config = require("./config.js");
exports.onPostBuild = async ({graphql, reporter}) => {
console.log("Building static api...");
const apiPrefix = "./public/api";
if (!fs.existsSync(apiPrefix)) fs.mkdirSync(apiPrefix)
fs.writeFileSync(`${apiPrefix}.json`, JSON.stringify({
success: true,
endpoints: {
projects: [
{
name: "Projects Overview",
description: "Returns overview of all available projects",
path: "/api/projects.json"
},
{
name: "Projects Overview for Language",
description: "Returns overview of all available projects in a specified language",
path: "/api/projects/:lang.json"
},
{
name: "Get specific Project",
description: "Returns specific project in specified language",
path: "/api/projects/:lang/:slug.json"
}
]
}
}));
const projectsPrefix = apiPrefix+"/projects";
if (!fs.existsSync(projectsPrefix)) fs.mkdirSync(projectsPrefix)
await graphql(`
query {
allProjectsJson {
nodes {
longDescription
urlname
shortDescription
name
links {
github
website
}
lang
image {
publicURL
}
featured
}
}
}
`).then(res => {
if (res.errors) {
reporter.panicOnBuild(`Error while running GraphQL query.`)
return
}
let projects = res.data.allProjectsJson.nodes;
fs.writeFileSync(`${projectsPrefix}.json`, JSON.stringify({
success: true,
projects: projects.map((project) => {return {slug: project.urlname, lang: project.lang, api: `/api/projects/${project.lang}/${project.urlname}.json`};})
}));
config.languages.forEach((lang) => {
if (!fs.existsSync(`${projectsPrefix}/${lang}`)) fs.mkdirSync(`${projectsPrefix}/${lang}`)
fs.writeFileSync(`${projectsPrefix}/${lang}.json`, JSON.stringify({
success: true,
projects: projects.filter((project) => {return project.lang == lang;}).map((project) => {return {slug: project.urlname, lang: project.lang, api: `/api/projects/${project.lang}/${project.urlname}.json`};})
}));
});
projects.forEach((project) => {
if(project.lang == "ignoreme") return;
fs.writeFileSync(`${projectsPrefix}/${project.lang}/${project.urlname}.json`, JSON.stringify({
success: true,
project: {
slug: project.urlname,
lang: project.lang,
name: project.name,
shortDescription: project.shortDescription,
longDescription: project.longDescription,
links: project.links !== null ? {
github: project.links.github,
website: project.links.website
} : null,
image: project.image.publicURL,
featured: project.featured,
frontend: `/${project.lang}/projects/${project.urlname}`
}
}));
});
})
}

View file

@ -19,7 +19,8 @@ async function checkLang({ request }) {
requestURL.pathname.startsWith("/icons") ||
requestURL.pathname.startsWith("/manifest.webmanifest") ||
requestURL.pathname.startsWith("/favicon") ||
requestURL.pathname.startsWith("/sw.js")) {
requestURL.pathname.startsWith("/sw.js") ||
requestURL.pathname.startsWith("/api")) {
return;
}