tududi/frontend/utils/notesService.ts
antanst 6cf79c322d Update frontend to use UID-based routing
- Update all entities to include UID field
- Convert components to use UID for navigation and links
- Update services to support UID-based API calls
- Add slug utilities for UID extraction
- Update store to handle UID fields
- Fix routing throughout all components
2025-08-06 15:54:45 +03:00

63 lines
1.8 KiB
TypeScript

import { Note } from '../entities/Note';
import {
handleAuthResponse,
getDefaultHeaders,
getPostHeaders,
} from './authUtils';
export const fetchNotes = async (): Promise<Note[]> => {
const response = await fetch('/api/notes', {
credentials: 'include',
headers: getDefaultHeaders(),
});
await handleAuthResponse(response, 'Failed to fetch notes.');
return await response.json();
};
export const createNote = async (noteData: Note): Promise<Note> => {
const response = await fetch('/api/note', {
method: 'POST',
credentials: 'include',
headers: getPostHeaders(),
body: JSON.stringify(noteData),
});
await handleAuthResponse(response, 'Failed to create note.');
return await response.json();
};
export const updateNote = async (
noteId: number,
noteData: Note
): Promise<Note> => {
const response = await fetch(`/api/note/${noteId}`, {
method: 'PATCH',
credentials: 'include',
headers: getPostHeaders(),
body: JSON.stringify(noteData),
});
await handleAuthResponse(response, 'Failed to update note.');
return await response.json();
};
export const deleteNote = async (noteId: number): Promise<void> => {
const response = await fetch(`/api/note/${noteId}`, {
method: 'DELETE',
credentials: 'include',
headers: getDefaultHeaders(),
});
await handleAuthResponse(response, 'Failed to delete note.');
};
export const fetchNoteBySlug = async (uidSlug: string): Promise<Note> => {
const response = await fetch(`/api/note/${uidSlug}`, {
credentials: 'include',
headers: getDefaultHeaders(),
});
await handleAuthResponse(response, 'Failed to fetch note.');
return await response.json();
};