feat: init
This commit is contained in:
23
routes/api/recipes/[name].ts
Normal file
23
routes/api/recipes/[name].ts
Normal 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));
|
||||
};
|
26
routes/api/recipes/images/[image].ts
Normal file
26
routes/api/recipes/images/[image].ts
Normal 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,
|
||||
});
|
||||
};
|
34
routes/api/recipes/index.ts
Normal file
34
routes/api/recipes/index.ts
Normal 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 });
|
||||
};
|
Reference in New Issue
Block a user