From 80bb8a108f29331cdb2f2695f6801beee104dc89 Mon Sep 17 00:00:00 2001 From: MohamedBassem Date: Thu, 8 Feb 2024 15:14:23 +0000 Subject: [refactor] Move the different packages to the package subdir --- web/lib/api.ts | 81 -------------------------------------------- web/lib/auth.ts | 25 -------------- web/lib/config.ts | 22 ------------ web/lib/services/links.ts | 78 ------------------------------------------ web/lib/types/api/links.ts | 33 ------------------ web/lib/types/api/tags.ts | 6 ---- web/lib/types/next-auth.d.ts | 12 ------- web/lib/utils.ts | 6 ---- 8 files changed, 263 deletions(-) delete mode 100644 web/lib/api.ts delete mode 100644 web/lib/auth.ts delete mode 100644 web/lib/config.ts delete mode 100644 web/lib/services/links.ts delete mode 100644 web/lib/types/api/links.ts delete mode 100644 web/lib/types/api/tags.ts delete mode 100644 web/lib/types/next-auth.d.ts delete mode 100644 web/lib/utils.ts (limited to 'web/lib') diff --git a/web/lib/api.ts b/web/lib/api.ts deleted file mode 100644 index 56686cde..00000000 --- a/web/lib/api.ts +++ /dev/null @@ -1,81 +0,0 @@ -"use client"; - -import { ZodTypeAny, z } from "zod"; -import { - ZNewBookmarkedLinkRequest, - zGetLinksResponseSchema, -} from "./types/api/links"; - -import serverConfig from "./config"; - -const BASE_URL = `${serverConfig.api_url}/api/v1`; - -export type FetchError = { - status?: number; - message?: string; -}; - -type InputSchema = T extends ZodTypeAny ? T : undefined; - -async function doRequest( - path: string, - respSchema?: InputSchema, - opts?: RequestInit, -): Promise< - | (InputSchema extends ZodTypeAny - ? [z.infer>, undefined] - : [undefined, undefined]) - | [undefined, FetchError] -> { - try { - const res = await fetch(`${BASE_URL}${path}`, opts); - if (!res.ok) { - return [ - undefined, - { status: res.status, message: await res.text() }, - ] as const; - } - if (!respSchema) { - return [undefined, undefined] as const; - } - - let parsed = respSchema.safeParse(await res.json()); - if (!parsed.success) { - return [ - undefined, - { message: `Failed to parse response: ${parsed.error.toString()}` }, - ] as const; - } - - return [parsed.data, undefined] as const; - } catch (error: any) { - return [ - undefined, - { message: `Failed to execute fetch request: ${error}` }, - ] as const; - } -} - -export default class APIClient { - static async getLinks() { - return await doRequest(`/links`, zGetLinksResponseSchema, { - next: { tags: ["links"] }, - }); - } - - static async bookmarkLink(url: string) { - const body: ZNewBookmarkedLinkRequest = { - url, - }; - return await doRequest(`/links`, undefined, { - method: "POST", - body: JSON.stringify(body), - }); - } - - static async unbookmarkLink(linkId: string) { - return await doRequest(`/links/${linkId}`, undefined, { - method: "DELETE", - }); - } -} diff --git a/web/lib/auth.ts b/web/lib/auth.ts deleted file mode 100644 index cd6404de..00000000 --- a/web/lib/auth.ts +++ /dev/null @@ -1,25 +0,0 @@ -import NextAuth, { NextAuthOptions } from "next-auth"; -import { PrismaAdapter } from "@next-auth/prisma-adapter"; -import AuthentikProvider from "next-auth/providers/authentik"; -import serverConfig from "@/lib/config"; -import prisma from "@remember/db"; - -let providers = []; - -if (serverConfig.auth.authentik) { - providers.push(AuthentikProvider(serverConfig.auth.authentik)); -} - -export const authOptions: NextAuthOptions = { - // Configure one or more authentication providers - adapter: PrismaAdapter(prisma), - providers: providers, - callbacks: { - session({ session, user }) { - session.user = { ...user }; - return session; - }, - }, -}; - -export const authHandler = NextAuth(authOptions); diff --git a/web/lib/config.ts b/web/lib/config.ts deleted file mode 100644 index ec042b54..00000000 --- a/web/lib/config.ts +++ /dev/null @@ -1,22 +0,0 @@ -function buildAuthentikConfig() { - let { AUTHENTIK_ID, AUTHENTIK_SECRET, AUTHENTIK_ISSUER } = process.env; - - if (!AUTHENTIK_ID || !AUTHENTIK_SECRET || !AUTHENTIK_ISSUER) { - return undefined; - } - - return { - clientId: AUTHENTIK_ID, - clientSecret: AUTHENTIK_SECRET, - issuer: AUTHENTIK_ISSUER, - }; -} - -const serverConfig = { - api_url: process.env.API_URL || "http://localhost:3000", - auth: { - authentik: buildAuthentikConfig(), - }, -}; - -export default serverConfig; diff --git a/web/lib/services/links.ts b/web/lib/services/links.ts deleted file mode 100644 index d273b118..00000000 --- a/web/lib/services/links.ts +++ /dev/null @@ -1,78 +0,0 @@ -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>, -): 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({ - where: { - id: linkId, - userId, - }, - }); -} - -export async function bookmarkLink(url: string, userId: string) { - const link = await prisma.bookmarkedLink.create({ - data: { - url, - userId, - }, - select: defaultLinkFields, - }); - - // Enqueue crawling request - await LinkCrawlerQueue.add("crawl", { - linkId: link.id, - url: link.url, - }); - - return toZodSchema(link); -} - -export async function getLinks(userId: string) { - 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 deleted file mode 100644 index f84445f6..00000000 --- a/web/lib/types/api/links.ts +++ /dev/null @@ -1,33 +0,0 @@ -import { z } from "zod"; -import { zBookmarkTagSchema } from "@/lib/types/api/tags"; - -export const zBookmarkedLinkSchema = z.object({ - id: z.string(), - url: z.string().url(), - createdAt: z.coerce.date(), - - details: z - .object({ - title: z.string().nullish(), - description: z.string().nullish(), - imageUrl: z.string().url().nullish(), - favicon: z.string().url().nullish(), - }) - .nullish(), - tags: z.array(zBookmarkTagSchema), -}); -export type ZBookmarkedLink = z.infer; - -// POST /v1/links -export const zNewBookmarkedLinkRequestSchema = zBookmarkedLinkSchema.pick({ - url: true, -}); -export type ZNewBookmarkedLinkRequest = z.infer< - typeof zNewBookmarkedLinkRequestSchema ->; - -// GET /v1/links -export const zGetLinksResponseSchema = z.object({ - links: z.array(zBookmarkedLinkSchema), -}); -export type ZGetLinksResponse = z.infer; diff --git a/web/lib/types/api/tags.ts b/web/lib/types/api/tags.ts deleted file mode 100644 index f2d2bc18..00000000 --- a/web/lib/types/api/tags.ts +++ /dev/null @@ -1,6 +0,0 @@ -import { z } from "zod"; - -export const zBookmarkTagSchema = z.object({ - id: z.string(), - name: z.string(), -}); diff --git a/web/lib/types/next-auth.d.ts b/web/lib/types/next-auth.d.ts deleted file mode 100644 index cd47dfce..00000000 --- a/web/lib/types/next-auth.d.ts +++ /dev/null @@ -1,12 +0,0 @@ -import { DefaultSession } from "next-auth"; - -declare module "next-auth" { - /** - * Returned by `useSession`, `getSession` and received as a prop on the `SessionProvider` React Context - */ - export interface Session { - user: { - id: string; - } & DefaultSession["user"]; - } -} diff --git a/web/lib/utils.ts b/web/lib/utils.ts deleted file mode 100644 index 365058ce..00000000 --- a/web/lib/utils.ts +++ /dev/null @@ -1,6 +0,0 @@ -import { type ClassValue, clsx } from "clsx"; -import { twMerge } from "tailwind-merge"; - -export function cn(...inputs: ClassValue[]) { - return twMerge(clsx(inputs)); -} -- cgit v1.2.3-70-g09d2