2025-12-08 22:08:30 +00:00
|
|
|
import { encryptString, decryptString } from "./encryption";
|
|
|
|
|
import { useAuthStore } from "../stores/authStore";
|
2025-12-22 15:23:40 +00:00
|
|
|
import { CamelCasedPropertiesDeep } from "type-fest";
|
|
|
|
|
import { components } from "@/types/api";
|
|
|
|
|
import client from "./client";
|
2025-11-23 09:08:01 +00:00
|
|
|
|
2025-12-22 15:23:40 +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) => {
|
2025-12-08 22:08:30 +00:00
|
|
|
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,
|
2025-12-22 15:23:40 +00:00
|
|
|
folderId: note.folderId,
|
2025-11-24 19:48:46 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
console.log(encryptedNote);
|
2025-12-22 15:23:40 +00:00
|
|
|
return client.POST(`/api/notes/`, { body: encryptedNote });
|
2025-11-24 19:48:46 +00:00
|
|
|
};
|
|
|
|
|
const fetchNotes = async () => {
|
2025-12-08 22:08:30 +00:00
|
|
|
const encryptionKey = useAuthStore.getState().encryptionKey;
|
|
|
|
|
if (!encryptionKey) throw new Error("Not authenticated");
|
|
|
|
|
|
2025-12-22 15:23:40 +00:00
|
|
|
const { data } = await client.GET(`/api/notes/`);
|
2025-11-24 19:48:46 +00:00
|
|
|
|
|
|
|
|
console.log(data);
|
|
|
|
|
const decryptedNotes = await Promise.all(
|
2025-12-22 15:23:40 +00:00
|
|
|
data.map(async (note: NoteRead) => ({
|
2025-11-24 19:48:46 +00:00
|
|
|
...note,
|
2025-12-08 22:08:30 +00:00
|
|
|
title: await decryptString(note.title, encryptionKey),
|
|
|
|
|
content: await decryptString(note.content, encryptionKey),
|
2025-12-18 18:12:23 +00:00
|
|
|
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
|
|
|
};
|
|
|
|
|
|
2025-12-22 15:23:40 +00:00
|
|
|
const updateNote = async (id: number, note: Partial<NoteRead>) => {
|
2025-12-08 22:08:30 +00:00
|
|
|
const encryptionKey = useAuthStore.getState().encryptionKey;
|
|
|
|
|
if (!encryptionKey) throw new Error("Not authenticated");
|
|
|
|
|
|
2025-12-22 15:23:40 +00:00
|
|
|
var encryptedNote: Partial<NoteRead> = {};
|
2025-11-25 22:09:02 +00:00
|
|
|
if (note.content) {
|
2025-12-08 22:08:30 +00:00
|
|
|
encryptedNote.content = await encryptString(note.content, encryptionKey);
|
2025-11-25 22:09:02 +00:00
|
|
|
}
|
|
|
|
|
if (note.title) {
|
2025-12-08 22:08:30 +00:00
|
|
|
encryptedNote.title = await encryptString(note.title, encryptionKey);
|
2025-11-25 22:09:02 +00:00
|
|
|
}
|
2025-12-22 15:23:40 +00:00
|
|
|
if (note.folderId) {
|
|
|
|
|
encryptedNote.folderId = note.folderId;
|
2025-11-25 22:09:02 +00:00
|
|
|
}
|
2025-12-22 15:23:40 +00:00
|
|
|
// 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,
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
});
|
2025-11-25 22:09:02 +00:00
|
|
|
|
2025-12-22 15:23:40 +00:00
|
|
|
if (data) {
|
|
|
|
|
console.log(data);
|
|
|
|
|
}
|
|
|
|
|
if (error) {
|
|
|
|
|
console.log(error);
|
|
|
|
|
}
|
2025-11-25 22:09:02 +00:00
|
|
|
};
|
|
|
|
|
|
2025-11-23 09:08:01 +00:00
|
|
|
export const notesApi = {
|
2025-11-24 19:48:46 +00:00
|
|
|
list: () => fetchNotes(),
|
2025-12-22 15:23:40 +00:00
|
|
|
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),
|
2025-12-22 15:23:40 +00:00
|
|
|
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
|
|
|
};
|