diff --git a/src/lib/sync.ts b/src/lib/sync.ts new file mode 100644 index 0000000..7b79e1f --- /dev/null +++ b/src/lib/sync.ts @@ -0,0 +1,72 @@ +import type { Note, Folder } from '../db' +import { authHeaders } from './auth' + +const API = '/api' + +export type SyncPayload = { notes: Note[]; folders: Folder[] } + +export async function pullAll(): Promise { + const res = await fetch(`${API}/notes/sync`, { headers: authHeaders() }) + if (!res.ok) throw new Error('同步失败') + return res.json() +} + +export async function pushAll(payload: SyncPayload): Promise { + const res = await fetch(`${API}/notes/sync`, { + method: 'POST', + headers: { 'Content-Type': 'application/json', ...authHeaders() }, + body: JSON.stringify(payload), + }) + if (!res.ok) throw new Error('推送失败') +} + +export async function pushNote(note: Note): Promise { + fetch(`${API}/notes/${note.id}`, { + method: 'PUT', + headers: { 'Content-Type': 'application/json', ...authHeaders() }, + body: JSON.stringify(note), + }).catch(() => {}) +} + +export async function pushFolder(folder: Folder): Promise { + fetch(`${API}/folders/${folder.id}`, { + method: 'PUT', + headers: { 'Content-Type': 'application/json', ...authHeaders() }, + body: JSON.stringify(folder), + }).catch(() => {}) +} + +export async function pushDeleteNote(id: string): Promise { + fetch(`${API}/notes/${id}`, { + method: 'DELETE', + headers: authHeaders(), + }).catch(() => {}) +} + +export async function pushDeleteFolder(id: string): Promise { + fetch(`${API}/folders/${id}`, { + method: 'DELETE', + headers: authHeaders(), + }).catch(() => {}) +} + +export function mergeNotes(local: Note[], remote: Note[]): Note[] { + const map = new Map() + for (const n of local) map.set(n.id, n) + for (const n of remote) { + const existing = map.get(n.id) + if (!existing || n.updatedAt > existing.updatedAt) { + map.set(n.id, { ...n, tags: Array.isArray(n.tags) ? n.tags : JSON.parse(n.tags as any) }) + } + } + return Array.from(map.values()) +} + +export function mergeFolders(local: Folder[], remote: Folder[]): Folder[] { + const map = new Map() + for (const f of local) map.set(f.id, f) + for (const f of remote) { + if (!map.has(f.id)) map.set(f.id, f) + } + return Array.from(map.values()) +}