diff options
| author | MohamedBassem <me@mbassem.com> | 2024-03-30 16:26:16 +0000 |
|---|---|---|
| committer | MohamedBassem <me@mbassem.com> | 2024-03-30 16:26:16 +0000 |
| commit | 46b78eaac30be26fe40520e97786563344af8403 (patch) | |
| tree | c4c0e1ae1d3d21a6f1fbf5f44f68e99243bbb5d3 /packages/trpc | |
| parent | 853ed13450b3a0d92cba144cc0dfd0696e7c810c (diff) | |
| download | karakeep-46b78eaac30be26fe40520e97786563344af8403.tar.zst | |
format: Add missing lint and format, and format the entire repo
Diffstat (limited to 'packages/trpc')
| -rw-r--r-- | packages/trpc/auth.ts | 3 | ||||
| -rw-r--r-- | packages/trpc/index.ts | 19 | ||||
| -rw-r--r-- | packages/trpc/package.json | 2 | ||||
| -rw-r--r-- | packages/trpc/routers/_app.ts | 1 | ||||
| -rw-r--r-- | packages/trpc/routers/admin.ts | 6 | ||||
| -rw-r--r-- | packages/trpc/routers/apiKeys.ts | 10 | ||||
| -rw-r--r-- | packages/trpc/routers/bookmarks.test.ts | 6 | ||||
| -rw-r--r-- | packages/trpc/routers/bookmarks.ts | 44 | ||||
| -rw-r--r-- | packages/trpc/routers/lists.ts | 3 | ||||
| -rw-r--r-- | packages/trpc/routers/users.test.ts | 10 | ||||
| -rw-r--r-- | packages/trpc/testUtils.ts | 7 | ||||
| -rw-r--r-- | packages/trpc/tsconfig.json | 3 | ||||
| -rw-r--r-- | packages/trpc/types/bookmarks.ts | 3 | ||||
| -rw-r--r-- | packages/trpc/vitest.config.ts | 2 |
14 files changed, 65 insertions, 54 deletions
diff --git a/packages/trpc/auth.ts b/packages/trpc/auth.ts index c766dd9a..846c07b6 100644 --- a/packages/trpc/auth.ts +++ b/packages/trpc/auth.ts @@ -1,7 +1,8 @@ import { randomBytes } from "crypto"; -import { apiKeys } from "@hoarder/db/schema"; import * as bcrypt from "bcryptjs"; + import { db } from "@hoarder/db"; +import { apiKeys } from "@hoarder/db/schema"; // API Keys diff --git a/packages/trpc/index.ts b/packages/trpc/index.ts index 51c713c0..4055fa5d 100644 --- a/packages/trpc/index.ts +++ b/packages/trpc/index.ts @@ -1,20 +1,21 @@ -import { db } from "@hoarder/db"; -import serverConfig from "@hoarder/shared/config"; -import { TRPCError, initTRPC } from "@trpc/server"; +import { initTRPC, TRPCError } from "@trpc/server"; import superjson from "superjson"; import { ZodError } from "zod"; -type User = { +import type { db } from "@hoarder/db"; +import serverConfig from "@hoarder/shared/config"; + +interface User { id: string; name?: string | null | undefined; email?: string | null | undefined; role: "admin" | "user" | null; -}; +} -export type Context = { +export interface Context { user: User | null; db: typeof db; -}; +} // Avoid exporting the entire t-object // since it's not very descriptive. @@ -29,7 +30,7 @@ const t = initTRPC.context<Context>().create({ data: { ...shape.data, zodError: - error.code === 'BAD_REQUEST' && error.cause instanceof ZodError + error.code === "BAD_REQUEST" && error.cause instanceof ZodError ? error.cause.flatten() : null, }, @@ -53,7 +54,7 @@ export const publicProcedure = procedure; export const authedProcedure = procedure.use(function isAuthed(opts) { const user = opts.ctx.user; - if (!user || !user.id) { + if (!user?.id) { throw new TRPCError({ code: "UNAUTHORIZED" }); } diff --git a/packages/trpc/package.json b/packages/trpc/package.json index 411397dc..ec858ca5 100644 --- a/packages/trpc/package.json +++ b/packages/trpc/package.json @@ -6,6 +6,8 @@ "type": "module", "scripts": { "typecheck": "tsc --noEmit", + "format": "prettier . --ignore-path ../../.prettierignore", + "lint": "eslint .", "test": "vitest" }, "dependencies": { diff --git a/packages/trpc/routers/_app.ts b/packages/trpc/routers/_app.ts index 780fd76d..577b523e 100644 --- a/packages/trpc/routers/_app.ts +++ b/packages/trpc/routers/_app.ts @@ -5,6 +5,7 @@ import { bookmarksAppRouter } from "./bookmarks"; import { listsAppRouter } from "./lists"; import { tagsAppRouter } from "./tags"; import { usersAppRouter } from "./users"; + export const appRouter = router({ bookmarks: bookmarksAppRouter, apiKeys: apiKeysAppRouter, diff --git a/packages/trpc/routers/admin.ts b/packages/trpc/routers/admin.ts index 8a7b592d..4a7c6a80 100644 --- a/packages/trpc/routers/admin.ts +++ b/packages/trpc/routers/admin.ts @@ -1,6 +1,6 @@ -import { adminProcedure, router } from "../index"; -import { z } from "zod"; import { count } from "drizzle-orm"; +import { z } from "zod"; + import { bookmarks, users } from "@hoarder/db/schema"; import { LinkCrawlerQueue, @@ -8,6 +8,8 @@ import { SearchIndexingQueue, } from "@hoarder/shared/queues"; +import { adminProcedure, router } from "../index"; + export const adminAppRouter = router({ stats: adminProcedure .output( diff --git a/packages/trpc/routers/apiKeys.ts b/packages/trpc/routers/apiKeys.ts index 3093b433..deeb108f 100644 --- a/packages/trpc/routers/apiKeys.ts +++ b/packages/trpc/routers/apiKeys.ts @@ -1,9 +1,11 @@ -import { generateApiKey, validatePassword } from "../auth"; -import { authedProcedure, publicProcedure, router } from "../index"; +import { TRPCError } from "@trpc/server"; +import { and, eq } from "drizzle-orm"; import { z } from "zod"; + import { apiKeys } from "@hoarder/db/schema"; -import { eq, and } from "drizzle-orm"; -import { TRPCError } from "@trpc/server"; + +import { generateApiKey, validatePassword } from "../auth"; +import { authedProcedure, publicProcedure, router } from "../index"; const zApiKeySchema = z.object({ id: z.string(), diff --git a/packages/trpc/routers/bookmarks.test.ts b/packages/trpc/routers/bookmarks.test.ts index 724a9998..58f4739d 100644 --- a/packages/trpc/routers/bookmarks.test.ts +++ b/packages/trpc/routers/bookmarks.test.ts @@ -1,5 +1,7 @@ -import { CustomTestContext, defaultBeforeEach } from "../testUtils"; -import { expect, describe, test, beforeEach, assert } from "vitest"; +import { assert, beforeEach, describe, expect, test } from "vitest"; + +import type { CustomTestContext } from "../testUtils"; +import { defaultBeforeEach } from "../testUtils"; beforeEach<CustomTestContext>(defaultBeforeEach(true)); diff --git a/packages/trpc/routers/bookmarks.ts b/packages/trpc/routers/bookmarks.ts index 4fb29c4c..9611829f 100644 --- a/packages/trpc/routers/bookmarks.ts +++ b/packages/trpc/routers/bookmarks.ts @@ -21,19 +21,19 @@ import { } from "@hoarder/shared/queues"; import { getSearchIdxClient } from "@hoarder/shared/search"; -import { authedProcedure, Context, router } from "../index"; +import type { Context } from "../index"; +import type { ZBookmark, ZBookmarkContent } from "../types/bookmarks"; +import type { ZBookmarkTags } from "../types/tags"; +import { authedProcedure, router } from "../index"; import { DEFAULT_NUM_BOOKMARKS_PER_PAGE, zBareBookmarkSchema, - ZBookmark, - ZBookmarkContent, zBookmarkSchema, zGetBookmarksRequestSchema, zGetBookmarksResponseSchema, zNewBookmarkRequestSchema, zUpdateBookmarksRequestSchema, } from "../types/bookmarks"; -import { ZBookmarkTags } from "../types/tags"; export const ensureBookmarkOwnership = experimental_trpcMiddleware<{ ctx: Context; @@ -423,29 +423,29 @@ export const bookmarksAppRouter = router({ input.ids ? inArray(bookmarks.id, input.ids) : undefined, input.tagId !== undefined ? exists( - ctx.db - .select() - .from(tagsOnBookmarks) - .where( - and( - eq(tagsOnBookmarks.bookmarkId, bookmarks.id), - eq(tagsOnBookmarks.tagId, input.tagId), + ctx.db + .select() + .from(tagsOnBookmarks) + .where( + and( + eq(tagsOnBookmarks.bookmarkId, bookmarks.id), + eq(tagsOnBookmarks.tagId, input.tagId), + ), ), - ), - ) + ) : undefined, input.listId !== undefined ? exists( - ctx.db - .select() - .from(bookmarksInLists) - .where( - and( - eq(bookmarksInLists.bookmarkId, bookmarks.id), - eq(bookmarksInLists.listId, input.listId), + ctx.db + .select() + .from(bookmarksInLists) + .where( + and( + eq(bookmarksInLists.bookmarkId, bookmarks.id), + eq(bookmarksInLists.listId, input.listId), + ), ), - ), - ) + ) : undefined, input.cursor ? lte(bookmarks.createdAt, input.cursor) : undefined, ), diff --git a/packages/trpc/routers/lists.ts b/packages/trpc/routers/lists.ts index db5bb38e..fb6d8637 100644 --- a/packages/trpc/routers/lists.ts +++ b/packages/trpc/routers/lists.ts @@ -5,7 +5,8 @@ import { z } from "zod"; import { SqliteError } from "@hoarder/db"; import { bookmarkLists, bookmarksInLists } from "@hoarder/db/schema"; -import { authedProcedure, Context, router } from "../index"; +import type { Context } from "../index"; +import { authedProcedure, router } from "../index"; import { zBookmarkListSchema } from "../types/lists"; import { ensureBookmarkOwnership } from "./bookmarks"; diff --git a/packages/trpc/routers/users.test.ts b/packages/trpc/routers/users.test.ts index 87814407..ea342d33 100644 --- a/packages/trpc/routers/users.test.ts +++ b/packages/trpc/routers/users.test.ts @@ -1,9 +1,7 @@ -import { - CustomTestContext, - defaultBeforeEach, - getApiCaller, -} from "../testUtils"; -import { expect, describe, test, beforeEach, assert } from "vitest"; +import { assert, beforeEach, describe, expect, test } from "vitest"; + +import type { CustomTestContext } from "../testUtils"; +import { defaultBeforeEach, getApiCaller } from "../testUtils"; beforeEach<CustomTestContext>(defaultBeforeEach(false)); diff --git a/packages/trpc/testUtils.ts b/packages/trpc/testUtils.ts index d5f24def..67fbddcc 100644 --- a/packages/trpc/testUtils.ts +++ b/packages/trpc/testUtils.ts @@ -1,7 +1,8 @@ -import { users } from "@hoarder/db/schema"; import { getInMemoryDB } from "@hoarder/db/drizzle"; -import { appRouter } from "./routers/_app"; +import { users } from "@hoarder/db/schema"; + import { createCallerFactory } from "./index"; +import { appRouter } from "./routers/_app"; export function getTestDB() { return getInMemoryDB(true); @@ -63,7 +64,7 @@ export async function buildTestContext( }; } -export function defaultBeforeEach(seedDB: boolean = true) { +export function defaultBeforeEach(seedDB = true) { return async (context: object) => { Object.assign(context, await buildTestContext(seedDB)); }; diff --git a/packages/trpc/tsconfig.json b/packages/trpc/tsconfig.json index 80329662..dbd0afdc 100644 --- a/packages/trpc/tsconfig.json +++ b/packages/trpc/tsconfig.json @@ -5,6 +5,5 @@ "exclude": ["node_modules"], "compilerOptions": { "tsBuildInfoFile": "node_modules/.cache/tsbuildinfo.json" - }, + } } - diff --git a/packages/trpc/types/bookmarks.ts b/packages/trpc/types/bookmarks.ts index 1b78d6da..b6c8691f 100644 --- a/packages/trpc/types/bookmarks.ts +++ b/packages/trpc/types/bookmarks.ts @@ -1,4 +1,5 @@ import { z } from "zod"; + import { zBookmarkTagSchema } from "./tags"; export const zBookmarkedLinkSchema = z.object({ @@ -30,7 +31,7 @@ export const zBookmarkContentSchema = z.discriminatedUnion("type", [ zBookmarkedLinkSchema, zBookmarkedTextSchema, zBookmarkedAssetSchema, - z.object({type: z.literal("unknown")}), + z.object({ type: z.literal("unknown") }), ]); export type ZBookmarkContent = z.infer<typeof zBookmarkContentSchema>; diff --git a/packages/trpc/vitest.config.ts b/packages/trpc/vitest.config.ts index c3d02f71..41fd70c4 100644 --- a/packages/trpc/vitest.config.ts +++ b/packages/trpc/vitest.config.ts @@ -1,7 +1,7 @@ /// <reference types="vitest" /> -import { defineConfig } from "vitest/config"; import tsconfigPaths from "vite-tsconfig-paths"; +import { defineConfig } from "vitest/config"; // https://vitejs.dev/config/ export default defineConfig({ |
