level-up/backend/trpc/base.ts
2025-04-12 21:51:01 +02:00

25 lines
No EOL
804 B
TypeScript

import { initTRPC } from "@trpc/server"
import type { Request, Response } from "express"
import superjson from "superjson"
import type { SessionId } from "../session"
export interface Context {
res?: Response
sessionId: SessionId
isCrew: boolean
}
export async function createContext(req: Request, res: Response | undefined): Promise<Context> {
const sessionId = req.headers["auio-session-id"]
if (sessionId === null || typeof sessionId !== "string" || sessionId.length !== 64) throw new Error(`Missing or invalid session ID: ${sessionId}`)
return {
res,
sessionId: sessionId as SessionId,
isCrew: req.headers["auio-crew-token"] === process.env.AUIO_CREW_TOKEN
}
}
export const t = initTRPC.context<Context>().create({
transformer: superjson
})