@@ -9,7 +5,6 @@ const { data } = useGreeting("chef");
Eat Something New
-
{{ data?.greeting }}
Generate a random recipe.
Random Recipe Now
diff --git a/server/api/recipes/[id].get.ts b/server/api/recipes/[id].get.ts
deleted file mode 100644
index d035d26..0000000
--- a/server/api/recipes/[id].get.ts
+++ /dev/null
@@ -1,30 +0,0 @@
-import { parseRecipeData } from "~/utils/recipes";
-import type { Meal } from "~/types/recipe";
-import { idSchema } from "~/types/id";
-
-export default defineEventHandler(async (event) => {
- const { apiUrl } = useRuntimeConfig();
- const routeParam = getRouterParam(event, "id");
-
- const parsed = idSchema.safeParse(routeParam);
- if (!parsed.success) {
- throw createError({
- statusCode: 400,
- statusMessage: "Invalid recipe id",
- message: parsed.error.message,
- });
- }
-
- const data = await $fetch<{ meals: Meal[] }>(
- new URL(`lookup.php?i=${parsed.data}`, apiUrl).toString(),
- );
- if (!data?.meals) {
- throw createError({
- statusCode: 404,
- statusMessage: "Recipe not found",
- });
- }
-
- const recipes = parseRecipeData(data);
- return recipes[0];
-});
diff --git a/server/trpc/context.ts b/server/trpc/context.ts
index 44dfea8..762b3d2 100644
--- a/server/trpc/context.ts
+++ b/server/trpc/context.ts
@@ -8,7 +8,6 @@ export async function createContext(event: H3Event) {
const authorization = getRequestHeader(event, "authorization");
async function getUserFromHeader() {
if (authorization) {
- console.log("authorization:", authorization);
return { isAdmin: true };
}
return null;
diff --git a/server/trpc/routers/index.ts b/server/trpc/routers/index.ts
index 32a4473..3c187e7 100644
--- a/server/trpc/routers/index.ts
+++ b/server/trpc/routers/index.ts
@@ -1,8 +1,9 @@
import type { inferRouterOutputs } from "@trpc/server";
import { z } from "zod";
-import { publicProcedure, privateProcedure, router } from "../trpc";
+import { privateProcedure, router, mergeRouters } from "../trpc";
+import { recipeRouter } from "./recipes";
-export const appRouter = router({
+export const helloRouter = router({
// hello: publicProcedure
hello: privateProcedure
.input(
@@ -17,6 +18,7 @@ export const appRouter = router({
}),
});
+export const appRouter = mergeRouters(helloRouter, recipeRouter);
// export type definition of API
export type AppRouter = typeof appRouter;
export type RouterOutput = inferRouterOutputs;
diff --git a/server/trpc/routers/recipes.ts b/server/trpc/routers/recipes.ts
new file mode 100644
index 0000000..0418fab
--- /dev/null
+++ b/server/trpc/routers/recipes.ts
@@ -0,0 +1,32 @@
+import { z } from "zod";
+import { publicProcedure, router } from "../trpc";
+import type { Meal } from "~/types/recipe";
+import { parseRecipeData } from "~/utils/recipes";
+
+const { apiUrl } = useRuntimeConfig();
+
+export const recipeRouter = router({
+ recipeGet: publicProcedure
+ .input(
+ z.coerce
+ .number({
+ required_error: "recipe id is required",
+ invalid_type_error: "recipe id must be a number",
+ })
+ .positive("recipe id must be positive"),
+ )
+ .query(async ({ input }) => {
+ const data = await $fetch<{ meals: Meal[] }>(
+ new URL(`lookup.php?i=${input}`, apiUrl).href,
+ );
+ if (!data?.meals) {
+ throw createError({
+ statusCode: 404,
+ statusMessage: "Recipe not found",
+ });
+ }
+
+ const recipes = parseRecipeData(data);
+ return recipes[0];
+ }),
+});
diff --git a/server/trpc/trpc.ts b/server/trpc/trpc.ts
index c7cf93a..b2ff203 100644
--- a/server/trpc/trpc.ts
+++ b/server/trpc/trpc.ts
@@ -11,6 +11,7 @@ const t = initTRPC.context().create();
export const publicProcedure = t.procedure;
export const router = t.router;
export const middleware = t.middleware;
+export const mergeRouters = t.mergeRouters;
export const authMiddleware = middleware(({ next, ctx }) => {
if (!ctx.user?.isAdmin) {
@@ -22,4 +23,5 @@ export const authMiddleware = middleware(({ next, ctx }) => {
},
});
});
+
export const privateProcedure = t.procedure.use(authMiddleware);
diff --git a/types/id.ts b/types/id.ts
index 4cd795f..1185e3e 100644
--- a/types/id.ts
+++ b/types/id.ts
@@ -1,8 +1,6 @@
import { z } from "zod";
-export const idSchema = z
- .number({
- required_error: "recipe id is required",
- invalid_type_error: "recipe id must be a number",
- })
- .positive("recipe id must be positive");
+export const idSchema = z.string({
+ required_error: "recipe id is required",
+ invalid_type_error: "recipe id must be a number",
+});