25 lines
No EOL
801 B
TypeScript
25 lines
No EOL
801 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"]
|
|
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
|
|
}) |