noteCategoryService.ts 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. import { API_BASE_URL, NoteCategory } from '../types';
  2. export const noteCategoryService = {
  3. async getAll(token: string): Promise<NoteCategory[]> {
  4. const res = await fetch(`${API_BASE_URL}/v1/note-categories`, {
  5. headers: { 'Authorization': `Bearer ${token}` }
  6. });
  7. if (!res.ok) throw new Error('Failed to fetch categories');
  8. return res.json();
  9. },
  10. async create(token: string, name: string, parentId?: string): Promise<NoteCategory> {
  11. const response = await fetch(`${API_BASE_URL}/v1/note-categories`, {
  12. method: 'POST',
  13. headers: {
  14. 'Authorization': `Bearer ${token}`,
  15. 'Content-Type': 'application/json'
  16. },
  17. body: JSON.stringify({ name, parentId })
  18. })
  19. if (!response.ok) throw new Error('Failed to create category')
  20. return response.json()
  21. },
  22. async update(token: string, id: string, name?: string, parentId?: string): Promise<NoteCategory> {
  23. const response = await fetch(`${API_BASE_URL}/v1/note-categories/${id}`, {
  24. method: 'PUT',
  25. headers: {
  26. 'Authorization': `Bearer ${token}`,
  27. 'Content-Type': 'application/json'
  28. },
  29. body: JSON.stringify({ name, parentId })
  30. })
  31. if (!response.ok) throw new Error('Failed to update category')
  32. return response.json()
  33. },
  34. async delete(token: string, id: string): Promise<void> {
  35. const res = await fetch(`${API_BASE_URL}/v1/note-categories/${id}`, {
  36. method: 'DELETE',
  37. headers: { 'Authorization': `Bearer ${token}` }
  38. });
  39. if (!res.ok) throw new Error('Failed to delete category');
  40. }
  41. };