mirror of
https://github.com/shishantbiswas/bknd-examples.git
synced 2026-02-27 12:01:16 +00:00
refactor: using api routes for crud ops
This commit is contained in:
@@ -6,25 +6,25 @@ interface Todo {
|
|||||||
|
|
||||||
export const useTodoActions = () => {
|
export const useTodoActions = () => {
|
||||||
const fetchTodos = () =>
|
const fetchTodos = () =>
|
||||||
$fetch<{ limit: number; todos: Array<Todo>; total: number }>("/api/todo", {
|
$fetch<{ limit: number; todos: Array<Todo>; total: number }>("/todos", {
|
||||||
method: "POST",
|
method: "POST",
|
||||||
body: { action: "get" },
|
body: { action: "get" },
|
||||||
});
|
});
|
||||||
|
|
||||||
const createTodo = (title: string) =>
|
const createTodo = (title: string) =>
|
||||||
$fetch("/api/todo", {
|
$fetch("/todos", {
|
||||||
method: "POST",
|
method: "POST",
|
||||||
body: { action: "create", data: { title } },
|
body: { action: "create", data: { title } },
|
||||||
});
|
});
|
||||||
|
|
||||||
const deleteTodo = (id: number) =>
|
const deleteTodo = (id: number) =>
|
||||||
$fetch("/api/todo", {
|
$fetch("/todos", {
|
||||||
method: "POST",
|
method: "POST",
|
||||||
body: { action: "delete", data: { id } },
|
body: { action: "delete", data: { id } },
|
||||||
});
|
});
|
||||||
|
|
||||||
const toggleTodo = (todo: any) =>
|
const toggleTodo = (todo: any) =>
|
||||||
$fetch("/api/todo", {
|
$fetch("/todos", {
|
||||||
method: "POST",
|
method: "POST",
|
||||||
body: { action: "toggle", data: todo },
|
body: { action: "toggle", data: todo },
|
||||||
});
|
});
|
||||||
|
|||||||
@@ -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 };
|
|
||||||
});
|
|
||||||
@@ -1,8 +1,7 @@
|
|||||||
import { getApi } from "../utils/bknd";
|
|
||||||
|
|
||||||
export default defineEventHandler(async (event) => {
|
export default defineEventHandler(async (event) => {
|
||||||
const body = await readBody(event);
|
const body = await readBody(event);
|
||||||
const { action, data } = body;
|
const { data, action } = body;
|
||||||
|
|
||||||
const api = await getApi({});
|
const api = await getApi({});
|
||||||
|
|
||||||
switch (action) {
|
switch (action) {
|
||||||
@@ -21,6 +20,7 @@ export default defineEventHandler(async (event) => {
|
|||||||
return await api.data.updateOne("todos", data.id, { done: !data.done });
|
return await api.data.updateOne("todos", data.id, { done: !data.done });
|
||||||
|
|
||||||
default:
|
default:
|
||||||
throw createError({ statusCode: 400, statusMessage: "Invalid Action" });
|
return { path: action };
|
||||||
}
|
}
|
||||||
|
|
||||||
});
|
});
|
||||||
Reference in New Issue
Block a user