mirror of
https://github.com/Unkn0wnCat/KevinK.dev.js.git
synced 2025-06-15 00:52:07 +02:00
Add basic static json api
This commit is contained in:
parent
cfada612bb
commit
ab665a50f2
2 changed files with 105 additions and 1 deletions
103
gatsby-node.js
103
gatsby-node.js
|
@ -1,5 +1,6 @@
|
||||||
/* eslint-disable no-undef */
|
/* eslint-disable no-undef */
|
||||||
const path = require(`path`);
|
const path = require(`path`);
|
||||||
|
const fs = require("fs")
|
||||||
|
|
||||||
exports.createPages = async ({ actions, graphql, reporter }) => {
|
exports.createPages = async ({ actions, graphql, reporter }) => {
|
||||||
const { createPage } = actions
|
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}`
|
||||||
|
}
|
||||||
|
}));
|
||||||
|
});
|
||||||
|
})
|
||||||
}
|
}
|
|
@ -19,7 +19,8 @@ async function checkLang({ request }) {
|
||||||
requestURL.pathname.startsWith("/icons") ||
|
requestURL.pathname.startsWith("/icons") ||
|
||||||
requestURL.pathname.startsWith("/manifest.webmanifest") ||
|
requestURL.pathname.startsWith("/manifest.webmanifest") ||
|
||||||
requestURL.pathname.startsWith("/favicon") ||
|
requestURL.pathname.startsWith("/favicon") ||
|
||||||
requestURL.pathname.startsWith("/sw.js")) {
|
requestURL.pathname.startsWith("/sw.js") ||
|
||||||
|
requestURL.pathname.startsWith("/api")) {
|
||||||
|
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue