({ req, res }: CreateFastifyContextOptions)
| 32 | }); |
| 33 | |
| 34 | export async function createContext({ req, res }: CreateFastifyContextOptions) { |
| 35 | const cookies = (req as any).cookies as Record<string, string | undefined>; |
| 36 | const setCookie: ISetCookie = (key, value, options) => { |
| 37 | // @ts-ignore |
| 38 | res.setCookie(key, value, { |
| 39 | maxAge: options.maxAge, |
| 40 | signed: options.signed, |
| 41 | ...COOKIE_OPTIONS, |
| 42 | }); |
| 43 | }; |
| 44 | |
| 45 | if (process.env.NODE_ENV !== 'production') { |
| 46 | await new Promise((res) => |
| 47 | setTimeout(() => res(1), Math.min(Math.random() * 500, 200)), |
| 48 | ); |
| 49 | } |
| 50 | |
| 51 | return { |
| 52 | req, |
| 53 | res, |
| 54 | session: (req as any).session as SessionValidationResult, |
| 55 | // we do not get types for `setCookie` from fastify |
| 56 | // so define it here and be safe in routers |
| 57 | setCookie, |
| 58 | cookies, |
| 59 | }; |
| 60 | } |
| 61 | export type Context = Awaited<ReturnType<typeof createContext>>; |
| 62 | |
| 63 | const t = initTRPC.context<Context>().create({ |
no outgoing calls
no test coverage detected