diff options
| author | MohamedBassem <me@mbassem.com> | 2024-10-13 01:49:13 +0000 |
|---|---|---|
| committer | MohamedBassem <me@mbassem.com> | 2024-10-13 01:49:13 +0000 |
| commit | de9cf0a45227da9d33feabe9c51a71845dad6763 (patch) | |
| tree | 1b34bacc1cf767d63bf55d5b898afd013cbbeca5 /apps/web/app/api | |
| parent | 2ccc15ea8865966618cf804968a5ca14ae364345 (diff) | |
| download | karakeep-de9cf0a45227da9d33feabe9c51a71845dad6763.tar.zst | |
feature: Allow importing hoarder's own bookmark file. Fixes #527
Diffstat (limited to 'apps/web/app/api')
| -rw-r--r-- | apps/web/app/api/bookmarks/export/route.tsx | 55 |
1 files changed, 13 insertions, 42 deletions
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<typeof zExportSchema> = { + 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"`, }, - ); + }); } |
