diff options
Diffstat (limited to 'apps/web/app/api/v1')
5 files changed, 116 insertions, 4 deletions
diff --git a/apps/web/app/api/v1/bookmarks/[bookmarkId]/assets/[assetId]/route.ts b/apps/web/app/api/v1/bookmarks/[bookmarkId]/assets/[assetId]/route.ts new file mode 100644 index 00000000..3fc50801 --- /dev/null +++ b/apps/web/app/api/v1/bookmarks/[bookmarkId]/assets/[assetId]/route.ts @@ -0,0 +1,37 @@ +import { NextRequest } from "next/server"; +import { buildHandler } from "@/app/api/v1/utils/handler"; +import { z } from "zod"; + +export const dynamic = "force-dynamic"; + +export const PUT = ( + req: NextRequest, + params: { params: { bookmarkId: string; assetId: string } }, +) => + buildHandler({ + req, + bodySchema: z.object({ assetId: z.string() }), + handler: async ({ api, body }) => { + await api.bookmarks.replaceAsset({ + bookmarkId: params.params.bookmarkId, + oldAssetId: params.params.assetId, + newAssetId: body!.assetId, + }); + return { status: 204 }; + }, + }); + +export const DELETE = ( + req: NextRequest, + params: { params: { bookmarkId: string; assetId: string } }, +) => + buildHandler({ + req, + handler: async ({ api }) => { + await api.bookmarks.detachAsset({ + bookmarkId: params.params.bookmarkId, + assetId: params.params.assetId, + }); + return { status: 204 }; + }, + }); diff --git a/apps/web/app/api/v1/bookmarks/[bookmarkId]/assets/route.ts b/apps/web/app/api/v1/bookmarks/[bookmarkId]/assets/route.ts new file mode 100644 index 00000000..e5284a39 --- /dev/null +++ b/apps/web/app/api/v1/bookmarks/[bookmarkId]/assets/route.ts @@ -0,0 +1,36 @@ +import { NextRequest } from "next/server"; +import { buildHandler } from "@/app/api/v1/utils/handler"; + +import { zAssetSchema } from "@hoarder/shared/types/bookmarks"; + +export const dynamic = "force-dynamic"; + +export const GET = ( + req: NextRequest, + params: { params: { bookmarkId: string } }, +) => + buildHandler({ + req, + handler: async ({ api }) => { + const resp = await api.bookmarks.getBookmark({ + bookmarkId: params.params.bookmarkId, + }); + return { status: 200, resp: { assets: resp.assets } }; + }, + }); + +export const POST = ( + req: NextRequest, + params: { params: { bookmarkId: string } }, +) => + buildHandler({ + req, + bodySchema: zAssetSchema, + handler: async ({ api, body }) => { + const asset = await api.bookmarks.attachAsset({ + bookmarkId: params.params.bookmarkId, + asset: body!, + }); + return { status: 201, resp: asset }; + }, + }); diff --git a/apps/web/app/api/v1/bookmarks/search/route.ts b/apps/web/app/api/v1/bookmarks/search/route.ts new file mode 100644 index 00000000..f0c5417a --- /dev/null +++ b/apps/web/app/api/v1/bookmarks/search/route.ts @@ -0,0 +1,39 @@ +import { NextRequest } from "next/server"; +import { z } from "zod"; + +import { buildHandler } from "../../utils/handler"; + +export const dynamic = "force-dynamic"; + +export const GET = (req: NextRequest) => + buildHandler({ + req, + searchParamsSchema: z.object({ + q: z.string(), + limit: z.coerce.number().optional(), + cursor: z + .string() + // Search cursor V1 is just a number + .pipe(z.coerce.number()) + .transform((val) => { + return { ver: 1 as const, offset: val }; + }) + .optional(), + }), + handler: async ({ api, searchParams }) => { + const bookmarks = await api.bookmarks.searchBookmarks({ + text: searchParams.q, + cursor: searchParams.cursor, + limit: searchParams.limit, + }); + return { + status: 200, + resp: { + bookmarks: bookmarks.bookmarks, + nextCursor: bookmarks.nextCursor + ? `${bookmarks.nextCursor.offset}` + : null, + }, + }; + }, + }); diff --git a/apps/web/app/api/v1/highlights/route.ts b/apps/web/app/api/v1/highlights/route.ts index ebb96bae..a324d498 100644 --- a/apps/web/app/api/v1/highlights/route.ts +++ b/apps/web/app/api/v1/highlights/route.ts @@ -25,6 +25,6 @@ export const POST = (req: NextRequest) => bodySchema: zNewHighlightSchema, handler: async ({ body, api }) => { const resp = await api.highlights.create(body!); - return { status: 200, resp }; + return { status: 201, resp }; }, }); diff --git a/apps/web/app/api/v1/lists/[listId]/route.ts b/apps/web/app/api/v1/lists/[listId]/route.ts index 69c99fda..3fd0a32d 100644 --- a/apps/web/app/api/v1/lists/[listId]/route.ts +++ b/apps/web/app/api/v1/lists/[listId]/route.ts @@ -1,7 +1,7 @@ import { NextRequest } from "next/server"; import { buildHandler } from "@/app/api/v1/utils/handler"; -import { zNewBookmarkListSchema } from "@hoarder/shared/types/lists"; +import { zEditBookmarkListSchema } from "@hoarder/shared/types/lists"; export const dynamic = "force-dynamic"; @@ -28,11 +28,11 @@ export const PATCH = ( ) => buildHandler({ req, - bodySchema: zNewBookmarkListSchema.partial(), + bodySchema: zEditBookmarkListSchema.omit({ listId: true }), handler: async ({ api, body }) => { const list = await api.lists.edit({ - listId: params.listId, ...body!, + listId: params.listId, }); return { status: 200, resp: list }; }, |
