(opts: CreateNextContextOptions)
| 42 | }; |
| 43 | |
| 44 | export const createOpenApiContext = async (opts: CreateNextContextOptions) => { |
| 45 | const { req, res } = opts; |
| 46 | |
| 47 | const apiKey = req.headers.authorization?.split(" ")[1] as string | null; |
| 48 | |
| 49 | if (!apiKey) { |
| 50 | throw new TRPCError({ code: "UNAUTHORIZED" }); |
| 51 | } |
| 52 | const key = await prisma.apiKey.findFirst({ |
| 53 | where: { apiKey }, |
| 54 | include: { project: true }, |
| 55 | }); |
| 56 | if (!key) { |
| 57 | throw new TRPCError({ code: "UNAUTHORIZED" }); |
| 58 | } |
| 59 | |
| 60 | return createInnerTRPCContext({ |
| 61 | key, |
| 62 | headers: req.headers, |
| 63 | res, |
| 64 | }); |
| 65 | }; |
| 66 | |
| 67 | export type TRPCContext = Awaited<ReturnType<typeof createOpenApiContext>>; |
| 68 |
nothing calls this directly
no test coverage detected