From de9cf0a45227da9d33feabe9c51a71845dad6763 Mon Sep 17 00:00:00 2001 From: MohamedBassem Date: Sun, 13 Oct 2024 01:49:13 +0000 Subject: feature: Allow importing hoarder's own bookmark file. Fixes #527 --- apps/web/app/api/bookmarks/export/route.tsx | 55 +++++++---------------------- 1 file changed, 13 insertions(+), 42 deletions(-) (limited to 'apps/web/app') diff --git a/apps/web/app/api/bookmarks/export/route.tsx b/apps/web/app/api/bookmarks/export/route.tsx index aefa76b5..7ae46c56 100644 --- a/apps/web/app/api/bookmarks/export/route.tsx +++ b/apps/web/app/api/bookmarks/export/route.tsx @@ -1,35 +1,8 @@ +import { toExportFormat, zExportSchema } from "@/lib/exportBookmarks"; import { api, createContextFromRequest } from "@/server/api/client"; +import { z } from "zod"; -import type { ZBookmark } from "@hoarder/shared/types/bookmarks"; -import { - BookmarkTypes, - MAX_NUM_BOOKMARKS_PER_PAGE, -} from "@hoarder/shared/types/bookmarks"; - -function toExportFormat(bookmark: ZBookmark) { - return { - createdAt: bookmark.createdAt.toISOString(), - title: - bookmark.title ?? - (bookmark.content.type === BookmarkTypes.LINK - ? bookmark.content.title - : null), - tags: bookmark.tags.map((t) => t.name), - type: bookmark.content.type, - content: { - type: bookmark.content.type, - url: - bookmark.content.type === BookmarkTypes.LINK - ? bookmark.content.url - : undefined, - text: - bookmark.content.type === BookmarkTypes.TEXT - ? bookmark.content.text - : undefined, - }, - note: bookmark.note, - }; -} +import { MAX_NUM_BOOKMARKS_PER_PAGE } from "@hoarder/shared/types/bookmarks"; export const dynamic = "force-dynamic"; export async function GET(request: Request) { @@ -53,17 +26,15 @@ export async function GET(request: Request) { results = [...results, ...resp.bookmarks.map(toExportFormat)]; } - return new Response( - JSON.stringify({ - // Exclude asset types for now - bookmarks: results.filter((b) => b.type !== BookmarkTypes.ASSET), - }), - { - status: 200, - headers: { - "Content-type": "application/json", - "Content-disposition": `attachment; filename="hoarder-export-${new Date().toISOString()}.json"`, - }, + const exportData: z.infer = { + bookmarks: results.filter((b) => b.content !== null), + }; + + return new Response(JSON.stringify(exportData), { + status: 200, + headers: { + "Content-type": "application/json", + "Content-disposition": `attachment; filename="hoarder-export-${new Date().toISOString()}.json"`, }, - ); + }); } -- cgit v1.2.3-70-g09d2