Jotzy/frontend/src/api/notes.tsx

62 lines
1.6 KiB
TypeScript
Raw Normal View History

2025-11-23 09:08:01 +00:00
import axios from "axios";
2025-11-24 19:48:46 +00:00
import { NoteRead } from "./folders";
import { deriveKey, encryptString, decryptString } from "./encryption";
2025-11-23 09:08:01 +00:00
const API_URL = import.meta.env.PROD ? "/api" : "http://localhost:8000/api";
export interface Note {
id: number;
title: string;
folder_id?: number;
content: string;
created_at: string;
updated_at: string;
}
export interface NoteCreate {
title: string;
content: string;
folder_id: number | null;
2025-11-23 15:14:48 +00:00
encrypted: boolean;
2025-11-23 09:08:01 +00:00
}
2025-11-23 15:14:48 +00:00
const createNote = async (note: NoteCreate) => {
2025-11-24 19:48:46 +00:00
var key = await deriveKey("Test");
var noteContent = await encryptString(note.content, key);
var noteTitle = await encryptString(note.title, key);
var encryptedNote = {
title: noteTitle,
content: noteContent,
folder_id: note.folder_id,
};
console.log(encryptedNote);
return axios.post(`${API_URL}/notes`, encryptedNote);
};
2025-11-23 15:14:48 +00:00
2025-11-24 19:48:46 +00:00
const fetchNotes = async () => {
const { data } = await axios.get(`${API_URL}/notes`);
console.log(data);
var key = await deriveKey("Test");
const decryptedNotes = await Promise.all(
data.map(async (note: Note) => ({
...note,
title: await decryptString(note.title, key),
content: await decryptString(note.content, key),
})),
);
2025-11-23 15:14:48 +00:00
2025-11-24 19:48:46 +00:00
return decryptedNotes;
2025-11-23 15:14:48 +00:00
};
2025-11-23 09:08:01 +00:00
export const notesApi = {
2025-11-24 19:48:46 +00:00
list: () => fetchNotes(),
2025-11-23 09:08:01 +00:00
get: (id: number) => axios.get(`${API_URL}/notes/${id}`),
2025-11-23 15:14:48 +00:00
create: (note: NoteCreate) => createNote(note),
update: (id: number, note: Partial<Note>) =>
2025-11-23 09:08:01 +00:00
axios.patch(`${API_URL}/notes/${id}`, note),
delete: (id: number) => axios.delete(`${API_URL}/notes/${id}`),
};