From d39f0cc89de8b0b6bf8c07457d3a59bf0727a820 Mon Sep 17 00:00:00 2001 From: Shishant Biswas Date: Tue, 17 Feb 2026 04:35:23 +0530 Subject: [PATCH] refactor: using api routes for crud ops --- app/composables/useTodoActions.ts | 8 ++++---- server/api/user.get.ts | 7 ------- server/{api/todo.post.ts => routes/todos.post.ts} | 12 ++++++------ 3 files changed, 10 insertions(+), 17 deletions(-) delete mode 100644 server/api/user.get.ts rename server/{api/todo.post.ts => routes/todos.post.ts} (79%) diff --git a/app/composables/useTodoActions.ts b/app/composables/useTodoActions.ts index bd5a8d6..4db06e0 100644 --- a/app/composables/useTodoActions.ts +++ b/app/composables/useTodoActions.ts @@ -6,25 +6,25 @@ interface Todo { export const useTodoActions = () => { const fetchTodos = () => - $fetch<{ limit: number; todos: Array; total: number }>("/api/todo", { + $fetch<{ limit: number; todos: Array; total: number }>("/todos", { method: "POST", body: { action: "get" }, }); const createTodo = (title: string) => - $fetch("/api/todo", { + $fetch("/todos", { method: "POST", body: { action: "create", data: { title } }, }); const deleteTodo = (id: number) => - $fetch("/api/todo", { + $fetch("/todos", { method: "POST", body: { action: "delete", data: { id } }, }); const toggleTodo = (todo: any) => - $fetch("/api/todo", { + $fetch("/todos", { method: "POST", body: { action: "toggle", data: todo }, }); diff --git a/server/api/user.get.ts b/server/api/user.get.ts deleted file mode 100644 index d95ad15..0000000 --- a/server/api/user.get.ts +++ /dev/null @@ -1,7 +0,0 @@ -import { getApi } from "../utils/bknd"; - -export default defineEventHandler(async (event) => { - const api = await getApi({ verify: true, headers: event.headers }); - const user = api.getUser(); - return { user }; -}); diff --git a/server/api/todo.post.ts b/server/routes/todos.post.ts similarity index 79% rename from server/api/todo.post.ts rename to server/routes/todos.post.ts index 445e698..fa8e3dd 100644 --- a/server/api/todo.post.ts +++ b/server/routes/todos.post.ts @@ -1,10 +1,9 @@ -import { getApi } from "../utils/bknd"; - export default defineEventHandler(async (event) => { const body = await readBody(event); - const { action, data } = body; - const api = await getApi({}); + const { data, action } = body; + const api = await getApi({}); + switch (action) { case 'get': const limit = 5; @@ -21,6 +20,7 @@ export default defineEventHandler(async (event) => { return await api.data.updateOne("todos", data.id, { done: !data.done }); default: - throw createError({ statusCode: 400, statusMessage: "Invalid Action" }); + return { path: action }; } -}); \ No newline at end of file + +});