refactor: remove some duplicated code
This commit is contained in:
@ -1,23 +1,10 @@
|
||||
import { HandlerContext } from "$fresh/server.ts";
|
||||
import { getDocument } from "@lib/documents.ts";
|
||||
import { parseRecipe } from "@lib/recipes.ts";
|
||||
import { Handlers } from "$fresh/server.ts";
|
||||
import { getRecipe } from "@lib/resource/recipes.ts";
|
||||
import { json } from "@lib/helpers.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));
|
||||
export const handler: Handlers = {
|
||||
async GET(_, ctx) {
|
||||
const recipe = await getRecipe(ctx.params.name);
|
||||
return json(recipe);
|
||||
},
|
||||
};
|
||||
|
@ -1,34 +1,10 @@
|
||||
import { HandlerContext } from "$fresh/server.ts";
|
||||
import { getDocument, getDocuments } from "@lib/documents.ts";
|
||||
import { parseRecipe } from "@lib/recipes.ts";
|
||||
import { Handlers } from "$fresh/server.ts";
|
||||
import { getAllRecipes } from "@lib/resource/recipes.ts";
|
||||
import { json } from "@lib/helpers.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 });
|
||||
export const handler: Handlers = {
|
||||
async GET() {
|
||||
const recipes = await getAllRecipes();
|
||||
return json(recipes);
|
||||
},
|
||||
};
|
||||
|
Reference in New Issue
Block a user