Jotzy/frontend/src/api/notes.tsx

108 lines
2.9 KiB
TypeScript
Raw Normal View History

import { encryptString, decryptString } from "./encryption";
import { useAuthStore } from "../stores/authStore";
import { CamelCasedPropertiesDeep } from "type-fest";
import { components } from "@/types/api";
import client from "./client";
2025-11-23 09:08:01 +00:00
export type NoteRead = CamelCasedPropertiesDeep<
components["schemas"]["NoteRead"]
>;
export type NoteCreate = CamelCasedPropertiesDeep<
components["schemas"]["NoteCreate"]
>;
2025-11-23 09:08:01 +00:00
2025-11-23 15:14:48 +00:00
const createNote = async (note: NoteCreate) => {
const encryptionKey = useAuthStore.getState().encryptionKey;
if (!encryptionKey) throw new Error("Not authenticated");
var noteContent = await encryptString(note.content, encryptionKey);
var noteTitle = await encryptString(note.title, encryptionKey);
2025-11-24 19:48:46 +00:00
var encryptedNote = {
title: noteTitle,
content: noteContent,
folderId: note.folderId,
2025-11-24 19:48:46 +00:00
};
console.log(encryptedNote);
return client.POST(`/api/notes/`, { body: encryptedNote });
2025-11-24 19:48:46 +00:00
};
const fetchNotes = async () => {
const encryptionKey = useAuthStore.getState().encryptionKey;
if (!encryptionKey) throw new Error("Not authenticated");
const { data } = await client.GET(`/api/notes/`);
2025-11-24 19:48:46 +00:00
console.log(data);
const decryptedNotes = await Promise.all(
data.map(async (note: NoteRead) => ({
2025-11-24 19:48:46 +00:00
...note,
title: await decryptString(note.title, encryptionKey),
content: await decryptString(note.content, encryptionKey),
tags: await Promise.all(
note.tags.map(async (tag) => ({
...tag,
name: await decryptString(tag.name, encryptionKey),
})),
),
2025-11-24 19:48:46 +00:00
})),
);
return decryptedNotes;
2025-11-23 15:14:48 +00:00
};
const updateNote = async (id: number, note: Partial<NoteRead>) => {
const encryptionKey = useAuthStore.getState().encryptionKey;
if (!encryptionKey) throw new Error("Not authenticated");
var encryptedNote: Partial<NoteRead> = {};
if (note.content) {
encryptedNote.content = await encryptString(note.content, encryptionKey);
}
if (note.title) {
encryptedNote.title = await encryptString(note.title, encryptionKey);
}
if (note.folderId) {
encryptedNote.folderId = note.folderId;
}
// if (!note.folderId){
// throw new Error("Folder id missing from note.")
// }
const { data, error } = await client.PATCH(`/api/notes/{note_id}`, {
body: encryptedNote,
params: {
path: {
note_id: id,
},
},
});
if (data) {
console.log(data);
}
if (error) {
console.log(error);
}
};
2025-11-23 09:08:01 +00:00
export const notesApi = {
2025-11-24 19:48:46 +00:00
list: () => fetchNotes(),
get: (id: number) =>
client.GET(`/api/notes/{note_id}`, {
params: {
path: {
note_id: id,
},
},
}),
2025-11-23 15:14:48 +00:00
create: (note: NoteCreate) => createNote(note),
update: (id: number, note: Partial<NoteRead>) => updateNote(id, note),
delete: (id: number) =>
client.DELETE(`/api/notes/{note_id}`, {
params: {
path: {
note_id: id,
},
},
}),
2025-11-23 09:08:01 +00:00
};