From 8a46ecb7373d6c5e7300861169ea51a7917cd2b4 Mon Sep 17 00:00:00 2001 From: MohamedBassem Date: Tue, 5 Mar 2024 13:11:06 +0000 Subject: refactor: Extract trpc logic into its package --- packages/web/lib/testUtils.ts | 70 --------------------------------- packages/web/lib/trpc.tsx | 2 +- packages/web/lib/types/api/bookmarks.ts | 70 --------------------------------- packages/web/lib/types/api/lists.ts | 18 --------- packages/web/lib/types/api/tags.ts | 10 ----- packages/web/lib/types/api/users.ts | 13 ------ 6 files changed, 1 insertion(+), 182 deletions(-) delete mode 100644 packages/web/lib/testUtils.ts delete mode 100644 packages/web/lib/types/api/bookmarks.ts delete mode 100644 packages/web/lib/types/api/lists.ts delete mode 100644 packages/web/lib/types/api/tags.ts delete mode 100644 packages/web/lib/types/api/users.ts (limited to 'packages/web/lib') diff --git a/packages/web/lib/testUtils.ts b/packages/web/lib/testUtils.ts deleted file mode 100644 index bad78463..00000000 --- a/packages/web/lib/testUtils.ts +++ /dev/null @@ -1,70 +0,0 @@ -import { users } from "@hoarder/db/schema"; -import { getInMemoryDB } from "@hoarder/db/drizzle"; -import { appRouter } from "@/server/api/routers/_app"; -import { createCallerFactory } from "@/server/api/trpc"; - -export function getTestDB() { - return getInMemoryDB(true); -} - -export type TestDB = ReturnType; - -export async function seedUsers(db: TestDB) { - return await db - .insert(users) - .values([ - { - name: "Test User 1", - email: "test1@test.com", - }, - { - name: "Test User 2", - email: "test2@test.com", - }, - ]) - .returning(); -} - -export function getApiCaller(db: TestDB, userId?: string) { - const createCaller = createCallerFactory(appRouter); - return createCaller({ - user: userId - ? { - id: userId, - role: "user", - } - : null, - db, - }); -} - -export type APICallerType = ReturnType; - -export interface CustomTestContext { - apiCallers: APICallerType[]; - unauthedAPICaller: APICallerType; - db: TestDB; -} - -export async function buildTestContext( - seedDB: boolean, -): Promise { - const db = getTestDB(); - let users: Awaited> = []; - if (seedDB) { - users = await seedUsers(db); - } - const callers = users.map((u) => getApiCaller(db, u.id)); - - return { - apiCallers: callers, - unauthedAPICaller: getApiCaller(db), - db, - }; -} - -export function defaultBeforeEach(seedDB: boolean = true) { - return async (context: object) => { - Object.assign(context, await buildTestContext(seedDB)); - }; -} diff --git a/packages/web/lib/trpc.tsx b/packages/web/lib/trpc.tsx index aa246047..79a2a9fe 100644 --- a/packages/web/lib/trpc.tsx +++ b/packages/web/lib/trpc.tsx @@ -1,5 +1,5 @@ "use client"; -import type { AppRouter } from "@/server/api/routers/_app"; +import type { AppRouter } from "@hoarder/trpc/routers/_app"; import { createTRPCReact } from "@trpc/react-query"; export const api = createTRPCReact(); diff --git a/packages/web/lib/types/api/bookmarks.ts b/packages/web/lib/types/api/bookmarks.ts deleted file mode 100644 index 5fabc7ca..00000000 --- a/packages/web/lib/types/api/bookmarks.ts +++ /dev/null @@ -1,70 +0,0 @@ -import { z } from "zod"; -import { zBookmarkTagSchema } from "@/lib/types/api/tags"; - -export const zBookmarkedLinkSchema = z.object({ - type: z.literal("link"), - url: z.string().url(), - title: z.string().nullish(), - description: z.string().nullish(), - imageUrl: z.string().url().nullish(), - favicon: z.string().url().nullish(), - htmlContent: z.string().nullish(), - crawledAt: z.date().nullish(), -}); -export type ZBookmarkedLink = z.infer; - -export const zBookmarkedTextSchema = z.object({ - type: z.literal("text"), - text: z.string().max(2000), -}); -export type ZBookmarkedText = z.infer; - -export const zBookmarkContentSchema = z.discriminatedUnion("type", [ - zBookmarkedLinkSchema, - zBookmarkedTextSchema, -]); -export type ZBookmarkContent = z.infer; - -export const zBareBookmarkSchema = z.object({ - id: z.string(), - createdAt: z.date(), - archived: z.boolean(), - favourited: z.boolean(), - taggingStatus: z.enum(["success", "failure", "pending"]).nullable(), -}); - -export const zBookmarkSchema = zBareBookmarkSchema.merge( - z.object({ - tags: z.array(zBookmarkTagSchema), - content: zBookmarkContentSchema, - }), -); -export type ZBookmark = z.infer; - -// POST /v1/bookmarks -export const zNewBookmarkRequestSchema = zBookmarkContentSchema; -export type ZNewBookmarkRequest = z.infer; - -// GET /v1/bookmarks - -export const zGetBookmarksRequestSchema = z.object({ - ids: z.array(z.string()).optional(), - archived: z.boolean().optional(), - favourited: z.boolean().optional(), -}); -export type ZGetBookmarksRequest = z.infer; - -export const zGetBookmarksResponseSchema = z.object({ - bookmarks: z.array(zBookmarkSchema), -}); -export type ZGetBookmarksResponse = z.infer; - -// PATCH /v1/bookmarks/[bookmarkId] -export const zUpdateBookmarksRequestSchema = z.object({ - bookmarkId: z.string(), - archived: z.boolean().optional(), - favourited: z.boolean().optional(), -}); -export type ZUpdateBookmarksRequest = z.infer< - typeof zUpdateBookmarksRequestSchema ->; diff --git a/packages/web/lib/types/api/lists.ts b/packages/web/lib/types/api/lists.ts deleted file mode 100644 index 4b0ccaca..00000000 --- a/packages/web/lib/types/api/lists.ts +++ /dev/null @@ -1,18 +0,0 @@ -import { z } from "zod"; - -export const zBookmarkListSchema = z.object({ - id: z.string(), - name: z.string(), - icon: z.string(), -}); - -export const zBookmarkListWithBookmarksSchema = zBookmarkListSchema.merge( - z.object({ - bookmarks: z.array(z.string()), - }), -); - -export type ZBookmarkList = z.infer; -export type ZBookmarkListWithBookmarks = z.infer< - typeof zBookmarkListWithBookmarksSchema ->; diff --git a/packages/web/lib/types/api/tags.ts b/packages/web/lib/types/api/tags.ts deleted file mode 100644 index 7a99dad4..00000000 --- a/packages/web/lib/types/api/tags.ts +++ /dev/null @@ -1,10 +0,0 @@ -import { z } from "zod"; - -export const zAttachedByEnumSchema = z.enum(["ai", "human"]); -export type ZAttachedByEnum = z.infer; -export const zBookmarkTagSchema = z.object({ - id: z.string(), - name: z.string(), - attachedBy: zAttachedByEnumSchema, -}); -export type ZBookmarkTags = z.infer; diff --git a/packages/web/lib/types/api/users.ts b/packages/web/lib/types/api/users.ts deleted file mode 100644 index c2fe182a..00000000 --- a/packages/web/lib/types/api/users.ts +++ /dev/null @@ -1,13 +0,0 @@ -import { z } from "zod"; - -export const zSignUpSchema = z - .object({ - name: z.string().min(1, { message: "Name can't be empty" }), - email: z.string().email(), - password: z.string().min(8), - confirmPassword: z.string(), - }) - .refine((data) => data.password === data.confirmPassword, { - message: "Passwords don't match", - path: ["confirmPassword"], - }); -- cgit v1.2.3-70-g09d2