You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

55 lines
1.3 KiB
TypeScript

import { initTRPC, TRPCError } from "@trpc/server";
import type { getDbClient } from "../../database/postgres";
import type { createOpenRouter } from "@openrouter/ai-sdk-provider";
import type { JWT } from "@auth/core/jwt";
/**
* Initialization of tRPC backend
* Should be done only once per backend!
*/
const t = initTRPC
.context<
object & {
dbClient: ReturnType<typeof getDbClient>;
openrouter: ReturnType<typeof createOpenRouter>;
jwt?: JWT | null;
}
>()
.create(/*{
sse: {
maxDurationMs: 5 * 60 * 1_000, // 5 minutes
ping: {
enabled: true,
intervalMs: 3_000,
},
client: {
reconnectAfterInactivityMs: 5_000,
},
},
}*/);
/**
* Export reusable router and procedure helpers
* that can be used throughout the router
*/
export const router = t.router;
export const publicProcedure = t.procedure;
export const authProcedure = publicProcedure.use(
async ({ ctx: { jwt }, next }) => {
if (!jwt) {
throw new TRPCError({
code: "UNAUTHORIZED",
message: JSON.stringify(jwt),
});
}
if (!jwt.id) {
throw new TRPCError({ code: "UNAUTHORIZED" });
}
return await next({
ctx: { jwt },
});
}
);
export const createCallerFactory = t.createCallerFactory;