aboutsummaryrefslogtreecommitdiffstats
path: root/web/lib
diff options
context:
space:
mode:
Diffstat (limited to 'web/lib')
-rw-r--r--web/lib/api.ts12
-rw-r--r--web/lib/services/links.ts65
-rw-r--r--web/lib/types/api/links.ts2
-rw-r--r--web/lib/types/api/tags.ts6
4 files changed, 55 insertions, 30 deletions
diff --git a/web/lib/api.ts b/web/lib/api.ts
index 2304826b..56686cde 100644
--- a/web/lib/api.ts
+++ b/web/lib/api.ts
@@ -15,18 +15,6 @@ export type FetchError = {
message?: string;
};
-async function doRequest<Schema extends ZodTypeAny>(
- _path: string,
- respSchema: Schema,
- _opts: RequestInit | undefined,
-): Promise<[z.infer<typeof respSchema>, undefined] | [undefined, FetchError]>;
-
-async function doRequest<_Schema>(
- _path: string,
- _respSchema: undefined,
- _opts: RequestInit | undefined,
-): Promise<[undefined, undefined] | [undefined, FetchError]>;
-
type InputSchema<T> = T extends ZodTypeAny ? T : undefined;
async function doRequest<T>(
diff --git a/web/lib/services/links.ts b/web/lib/services/links.ts
index dbcbe9c4..d273b118 100644
--- a/web/lib/services/links.ts
+++ b/web/lib/services/links.ts
@@ -1,5 +1,43 @@
import { LinkCrawlerQueue } from "@remember/shared/queues";
import prisma from "@remember/db";
+import { ZBookmarkedLink } from "@/lib/types/api/links";
+
+const defaultLinkFields = {
+ id: true,
+ url: true,
+ createdAt: true,
+ details: {
+ select: {
+ title: true,
+ description: true,
+ imageUrl: true,
+ favicon: true,
+ },
+ },
+ tags: {
+ include: {
+ tag: true,
+ },
+ },
+};
+
+async function dummyPrismaReturnType() {
+ return await prisma.bookmarkedLink.findFirstOrThrow({
+ select: defaultLinkFields,
+ });
+}
+
+function toZodSchema(
+ link: Awaited<ReturnType<typeof dummyPrismaReturnType>>,
+): ZBookmarkedLink {
+ return {
+ id: link.id,
+ url: link.url,
+ createdAt: link.createdAt,
+ details: link.details,
+ tags: link.tags.map((t) => t.tag),
+ };
+}
export async function unbookmarkLink(linkId: string, userId: string) {
await prisma.bookmarkedLink.delete({
@@ -16,6 +54,7 @@ export async function bookmarkLink(url: string, userId: string) {
url,
userId,
},
+ select: defaultLinkFields,
});
// Enqueue crawling request
@@ -24,26 +63,16 @@ export async function bookmarkLink(url: string, userId: string) {
url: link.url,
});
- return link;
+ return toZodSchema(link);
}
export async function getLinks(userId: string) {
- return await prisma.bookmarkedLink.findMany({
- where: {
- userId,
- },
- select: {
- id: true,
- url: true,
- createdAt: true,
- details: {
- select: {
- title: true,
- description: true,
- imageUrl: true,
- favicon: true,
- },
+ return (
+ await prisma.bookmarkedLink.findMany({
+ where: {
+ userId,
},
- },
- });
+ select: defaultLinkFields,
+ })
+ ).map(toZodSchema);
}
diff --git a/web/lib/types/api/links.ts b/web/lib/types/api/links.ts
index 644589b4..f84445f6 100644
--- a/web/lib/types/api/links.ts
+++ b/web/lib/types/api/links.ts
@@ -1,4 +1,5 @@
import { z } from "zod";
+import { zBookmarkTagSchema } from "@/lib/types/api/tags";
export const zBookmarkedLinkSchema = z.object({
id: z.string(),
@@ -13,6 +14,7 @@ export const zBookmarkedLinkSchema = z.object({
favicon: z.string().url().nullish(),
})
.nullish(),
+ tags: z.array(zBookmarkTagSchema),
});
export type ZBookmarkedLink = z.infer<typeof zBookmarkedLinkSchema>;
diff --git a/web/lib/types/api/tags.ts b/web/lib/types/api/tags.ts
new file mode 100644
index 00000000..f2d2bc18
--- /dev/null
+++ b/web/lib/types/api/tags.ts
@@ -0,0 +1,6 @@
+import { z } from "zod";
+
+export const zBookmarkTagSchema = z.object({
+ id: z.string(),
+ name: z.string(),
+});