({ req }: CreateExpressContextOptions)
| 13 | import { parse as languageParse } from 'accept-language-parser'; |
| 14 | |
| 15 | export async function createContext({ req }: CreateExpressContextOptions) { |
| 16 | const authorization = req.headers['authorization'] ?? ''; |
| 17 | const token = authorization.replace('Bearer ', ''); |
| 18 | const timezone = req.headers['timezone'] |
| 19 | ? String(req.headers['timezone']) |
| 20 | : 'utc'; |
| 21 | const language = |
| 22 | get(languageParse(req.headers['accept-language']), [0, 'code']) ?? 'en'; |
| 23 | |
| 24 | let origin = ''; |
| 25 | if (req.headers['origin']) { |
| 26 | origin = String(req.headers['origin']); |
| 27 | } else if (req.headers['x-forwarded-proto'] && req.headers['host']) { |
| 28 | origin = `${req.headers['x-forwarded-proto']}://${req.headers['host']}`; |
| 29 | } else if (req.headers['host']) { |
| 30 | origin = `${req.protocol}://${req.headers['host']}`; |
| 31 | } |
| 32 | |
| 33 | return { token, timezone, language, req, origin }; |
| 34 | } |
| 35 | |
| 36 | type Context = Awaited<ReturnType<typeof createContext>>; |
| 37 | const t = initTRPC.context<Context>().meta<OpenApiMeta>().create(); |
no test coverage detected