feat: init

This commit is contained in:
2023-07-26 13:47:01 +02:00
commit 8e461cea26
28 changed files with 732 additions and 0 deletions

11
routes/api/index.ts Normal file
View File

@ -0,0 +1,11 @@
import { HandlerContext } from "$fresh/server.ts";
import { getDocuments } from "../../lib/documents.ts";
export const handler = async (
_req: Request,
_ctx: HandlerContext,
): Promise<Response> => {
const documents = await getDocuments();
const response = new Response(JSON.stringify(documents));
return response;
};

View File

@ -0,0 +1,23 @@
import { HandlerContext } from "$fresh/server.ts";
import { getDocument } from "../../../lib/documents.ts";
import { parseRecipe } from "../../../lib/recipes.ts";
export async function getRecipe(name: string) {
const document = await getDocument(`Recipes/${name}.md`);
const recipe = parseRecipe(document, name);
return recipe;
}
export const handler = async (
_req: Request,
_ctx: HandlerContext,
): Promise<Response> => {
const recipe = await getRecipe(_ctx.params.name);
const headers = new Headers();
headers.append("Content-Type", "application/json");
return new Response(JSON.stringify(recipe));
};

View File

@ -0,0 +1,26 @@
import { HandlerContext } from "$fresh/server.ts";
function copyHeader(headerName: string, to: Headers, from: Headers) {
const hdrVal = from.get(headerName);
if (hdrVal) {
to.set(headerName, hdrVal);
}
}
export const handler = async (
_req: Request,
_ctx: HandlerContext,
): Promise<Response> => {
const proxyRes = await fetch(
"http://192.168.178.56:3007/Recipes/images/" + _ctx.params.image,
);
console.log({ params: _ctx.params });
const headers = new Headers();
copyHeader("content-length", headers, proxyRes.headers);
copyHeader("content-type", headers, proxyRes.headers);
copyHeader("content-disposition", headers, proxyRes.headers);
return new Response(proxyRes.body, {
status: proxyRes.status,
headers,
});
};

View File

@ -0,0 +1,34 @@
import { HandlerContext } from "$fresh/server.ts";
import { getDocument, getDocuments } from "../../../lib/documents.ts";
import { parseRecipe } from "../../../lib/recipes.ts";
export async function getRecipes() {
const documents = await getDocuments();
return Promise.all(
documents.filter((d) => {
return d.name.startsWith("Recipes/") &&
d.contentType === "text/markdown" &&
!d.name.endsWith("index.md");
}).map(async (doc) => {
const document = await getDocument(doc.name);
const recipe = parseRecipe(document, doc.name);
return {
...recipe,
id: recipe.id.replace(/^Recipes\//, "").replace(/\.md$/, ""),
};
}),
);
}
export const handler = async (
_req: Request,
_ctx: HandlerContext,
): Promise<Response> => {
const headers = new Headers();
headers.append("Content-Type", "application/json");
const recipes = await getRecipes();
return new Response(JSON.stringify(recipes), { headers });
};