mirror of
https://github.com/kmc7468/arkvault.git
synced 2026-11-04 08:10:28 +09:00
서버 코드를 Router, Service, Repository 계층으로 분리하고 오류 처리 및 파일 삭제 로직 개선
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
ed732be999
commit
6a6aa1765b
72 files changed
+1928
-1509
No files matched your search
+41
-7
@@ -1,14 +1,34 @@
|
||||
import type { RequestEvent } from "@sveltejs/kit";
|
||||
import { initTRPC, TRPCError } from "@trpc/server";
|
||||
import superjson from "superjson";
|
||||
import { authorizeMiddleware, authorizeClientMiddleware } from "./middlewares/authorize";
|
||||
import { authorize, type ClientSession, type Session } from "#lib/server/auth.js";
|
||||
import { AppError, type ErrorCode } from "#lib/server/errors.js";
|
||||
|
||||
export const createContext = (event: RequestEvent) => event;
|
||||
export type Context = Awaited<ReturnType<typeof createContext>>;
|
||||
|
||||
export const t = initTRPC.context<Context>().create({ transformer: superjson });
|
||||
export const router = t.router;
|
||||
export const publicProcedure = t.procedure;
|
||||
|
||||
const trpcErrorCodes = {
|
||||
BAD_REQUEST: "BAD_REQUEST",
|
||||
UNAUTHORIZED: "UNAUTHORIZED",
|
||||
FORBIDDEN: "FORBIDDEN",
|
||||
NOT_FOUND: "NOT_FOUND",
|
||||
CONFLICT: "CONFLICT",
|
||||
RANGE_NOT_SATISFIABLE: "BAD_REQUEST",
|
||||
INTERNAL_SERVER_ERROR: "INTERNAL_SERVER_ERROR",
|
||||
} as const satisfies Record<ErrorCode, TRPCError["code"]>;
|
||||
|
||||
// Converts `AppError`s thrown by services into tRPC errors
|
||||
export const publicProcedure = t.procedure.use(async ({ next }) => {
|
||||
const result = await next();
|
||||
if (!result.ok && result.error.cause instanceof AppError) {
|
||||
const { code, message } = result.error.cause;
|
||||
throw new TRPCError({ code: trpcErrorCodes[code], message, cause: result.error.cause });
|
||||
}
|
||||
return result;
|
||||
});
|
||||
|
||||
const authedProcedure = publicProcedure.use(async ({ ctx, next }) => {
|
||||
if (!ctx.locals.session) {
|
||||
@@ -17,10 +37,24 @@ const authedProcedure = publicProcedure.use(async ({ ctx, next }) => {
|
||||
return next();
|
||||
});
|
||||
|
||||
const sessionProcedure = (requiredPermission: "any" | "notClient") =>
|
||||
authedProcedure.use(async ({ ctx, next }) => {
|
||||
const session: Session = await authorize(ctx.locals, requiredPermission);
|
||||
return next({ ctx: { session } });
|
||||
});
|
||||
|
||||
const clientSessionProcedure = (
|
||||
requiredPermission: "anyClient" | "pendingClient" | "activeClient",
|
||||
) =>
|
||||
authedProcedure.use(async ({ ctx, next }) => {
|
||||
const session: ClientSession = await authorize(ctx.locals, requiredPermission);
|
||||
return next({ ctx: { session } });
|
||||
});
|
||||
|
||||
export const roleProcedure = {
|
||||
any: authedProcedure.use(authorizeMiddleware("any")),
|
||||
notClient: authedProcedure.use(authorizeMiddleware("notClient")),
|
||||
anyClient: authedProcedure.use(authorizeClientMiddleware("anyClient")),
|
||||
pendingClient: authedProcedure.use(authorizeClientMiddleware("pendingClient")),
|
||||
activeClient: authedProcedure.use(authorizeClientMiddleware("activeClient")),
|
||||
any: sessionProcedure("any"),
|
||||
notClient: sessionProcedure("notClient"),
|
||||
anyClient: clientSessionProcedure("anyClient"),
|
||||
pendingClient: clientSessionProcedure("pendingClient"),
|
||||
activeClient: clientSessionProcedure("activeClient"),
|
||||
};
|
||||
Reference in new issue
Block a user