aboutsummaryrefslogtreecommitdiffstats
path: root/packages/web/lib
diff options
context:
space:
mode:
authorMohamedBassem <me@mbassem.com>2024-03-05 13:11:06 +0000
committerMohamedBassem <me@mbassem.com>2024-03-05 13:11:06 +0000
commit8a46ecb7373d6c5e7300861169ea51a7917cd2b4 (patch)
tree4ad318c3b5fc8b7a74cba6d0e37b6ade24db829a /packages/web/lib
parent224aa38d5976523f213e2860b6addc7630d472ba (diff)
downloadkarakeep-8a46ecb7373d6c5e7300861169ea51a7917cd2b4.tar.zst
refactor: Extract trpc logic into its package
Diffstat (limited to 'packages/web/lib')
-rw-r--r--packages/web/lib/testUtils.ts70
-rw-r--r--packages/web/lib/trpc.tsx2
-rw-r--r--packages/web/lib/types/api/bookmarks.ts70
-rw-r--r--packages/web/lib/types/api/lists.ts18
-rw-r--r--packages/web/lib/types/api/tags.ts10
-rw-r--r--packages/web/lib/types/api/users.ts13
6 files changed, 1 insertions, 182 deletions
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<typeof getTestDB>;
-
-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<typeof getApiCaller>;
-
-export interface CustomTestContext {
- apiCallers: APICallerType[];
- unauthedAPICaller: APICallerType;
- db: TestDB;
-}
-
-export async function buildTestContext(
- seedDB: boolean,
-): Promise<CustomTestContext> {
- const db = getTestDB();
- let users: Awaited<ReturnType<typeof seedUsers>> = [];
- 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<AppRouter>();
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<typeof zBookmarkedLinkSchema>;
-
-export const zBookmarkedTextSchema = z.object({
- type: z.literal("text"),
- text: z.string().max(2000),
-});
-export type ZBookmarkedText = z.infer<typeof zBookmarkedTextSchema>;
-
-export const zBookmarkContentSchema = z.discriminatedUnion("type", [
- zBookmarkedLinkSchema,
- zBookmarkedTextSchema,
-]);
-export type ZBookmarkContent = z.infer<typeof zBookmarkContentSchema>;
-
-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<typeof zBookmarkSchema>;
-
-// POST /v1/bookmarks
-export const zNewBookmarkRequestSchema = zBookmarkContentSchema;
-export type ZNewBookmarkRequest = z.infer<typeof zNewBookmarkRequestSchema>;
-
-// 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<typeof zGetBookmarksRequestSchema>;
-
-export const zGetBookmarksResponseSchema = z.object({
- bookmarks: z.array(zBookmarkSchema),
-});
-export type ZGetBookmarksResponse = z.infer<typeof zGetBookmarksResponseSchema>;
-
-// 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<typeof zBookmarkListSchema>;
-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<typeof zAttachedByEnumSchema>;
-export const zBookmarkTagSchema = z.object({
- id: z.string(),
- name: z.string(),
- attachedBy: zAttachedByEnumSchema,
-});
-export type ZBookmarkTags = z.infer<typeof zBookmarkTagSchema>;
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"],
- });