mirror of
https://github.com/ershisan99/todolist_next.git
synced 2025-12-16 12:33:57 +00:00
90 lines
1.9 KiB
TypeScript
90 lines
1.9 KiB
TypeScript
import { handleError } from "@/helpers"
|
|
import type {
|
|
CreateTaskResponse,
|
|
CreateTodolistResponse,
|
|
DeleteTaskResponse,
|
|
DeleteTodolistResponse,
|
|
Task,
|
|
TasksResponse,
|
|
Todolist,
|
|
UpdateTaskResponse,
|
|
} from "@/services"
|
|
import { todolistApiInstance } from "@/services/todolist-api/todolist-api.instance"
|
|
|
|
export const TodolistAPI = {
|
|
async getTodolists() {
|
|
const res = await todolistApiInstance.get<Todolist[]>("/todolists")
|
|
|
|
return res.data
|
|
},
|
|
|
|
async createTodolist({ title }: { title: string }) {
|
|
const res = await todolistApiInstance.post<CreateTodolistResponse>(
|
|
"/todolists",
|
|
{
|
|
title,
|
|
},
|
|
)
|
|
|
|
return res.data
|
|
},
|
|
|
|
async deleteTodolist({ todolistId }: { todolistId: string }) {
|
|
const res = await todolistApiInstance.delete<DeleteTodolistResponse>(
|
|
`/todolists/${todolistId}`,
|
|
)
|
|
|
|
return res.data
|
|
},
|
|
|
|
async getTodolistTasks({ todolistId }: { todolistId: string }) {
|
|
const res = await todolistApiInstance.get<TasksResponse>(
|
|
`/todolists/${todolistId}/tasks`,
|
|
)
|
|
|
|
return res.data
|
|
},
|
|
|
|
async createTask({
|
|
todolistId,
|
|
title,
|
|
}: {
|
|
todolistId: string
|
|
title: string
|
|
}) {
|
|
const res = await todolistApiInstance.post<CreateTaskResponse>(
|
|
`/todolists/${todolistId}/tasks`,
|
|
{ title },
|
|
)
|
|
|
|
return res.data
|
|
},
|
|
|
|
async updateTask({
|
|
id,
|
|
todolistId,
|
|
...rest
|
|
}: Partial<Task> & Required<Pick<Task, "id">> & { todolistId: string }) {
|
|
const res = await todolistApiInstance.patch<UpdateTaskResponse>(
|
|
`/todolists/${todolistId}/tasks/${id}`,
|
|
rest,
|
|
)
|
|
|
|
return res.data
|
|
},
|
|
|
|
async deleteTask({
|
|
todolistId,
|
|
taskId,
|
|
}: {
|
|
todolistId: string
|
|
taskId: string
|
|
}) {
|
|
const res = await todolistApiInstance.delete<DeleteTaskResponse>(
|
|
`/todolists/${todolistId}/tasks/${taskId}`,
|
|
)
|
|
|
|
return res.data
|
|
},
|
|
}
|