-
Notifications
You must be signed in to change notification settings - Fork 70
Expand file tree
/
Copy pathcontext.ts
More file actions
41 lines (35 loc) · 1.2 KB
/
context.ts
File metadata and controls
41 lines (35 loc) · 1.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
import type { Context } from "hono"
import { createContext, RouterContextProvider } from "react-router"
import { i18next } from "remix-hono/i18next"
import { getClientEnv, getServerEnv } from "~/env.server"
export const globalAppContext = createContext<LoadContext>()
export const getAppContext = async (c: Context) => {
// get the locale from the context
const locale = i18next.getLocale(c)
// get t function for the default namespace
const t = await i18next.getFixedT(c)
// get the server environment
const env = getServerEnv()
return {
lang: locale,
t,
isProductionDeployment: env.APP_ENV === "production",
env,
clientEnv: getClientEnv(),
// We do not add this to AppLoadContext type because it's not needed in the loaders, but it's used above to handle requests
body: c.body,
}
}
export const getLoadContext = async (c: Context) => {
const ctx = new RouterContextProvider()
const globalCtx = await getAppContext(c)
ctx.set(globalAppContext, globalCtx)
return ctx
}
interface LoadContext extends Awaited<ReturnType<typeof getAppContext>> {}
/**
* Declare our loaders and actions context type
*/
declare module "react-router" {
interface AppLoadContext extends Omit<LoadContext, "body"> {}
}