-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclient-env.ts
More file actions
80 lines (65 loc) · 2.21 KB
/
client-env.ts
File metadata and controls
80 lines (65 loc) · 2.21 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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
export interface ClientEnvironment {
readonly locale: string
readonly featureFlags: string[]
readonly login?: string
readonly copilotApiOverrideUrl: string | null
}
let env: ClientEnvironment | undefined
let getServerEnv: (() => ClientEnvironment | undefined) | null = null
export function getEnv() {
const serverEnv = getServerEnv ? getServerEnv() : undefined
const targetEnv = serverEnv ?? env
if (!targetEnv) {
throw new Error(
'Client env was requested before it was loaded. This likely means you are attempting to use client env at the module level in SSR, which is not supported. Please move your client env usage into a function.',
)
}
return targetEnv
}
export function getLocale() {
return getEnv().locale ?? 'en-US'
}
export function isLoggedIn() {
return !!getEnv().login
}
export function getCurrentUserLogin() {
return getEnv().login
}
function loadEnv() {
if (typeof document !== 'undefined') {
const envTag = document.getElementById('client-env')
if (envTag) {
try {
env = JSON.parse(envTag.textContent || '')
} catch (error) {
// eslint-disable-next-line no-console
console.error('Error parsing client-env', error)
}
}
}
}
// Automatically load the env on initial page load
loadEnv()
// This is a special helper method for setting the env in the SSR environment only
export function setClientEnvForSsr(clientEnv: ClientEnvironment | undefined) {
env = clientEnv
}
export function setServerEnvGetter(getter: () => ClientEnvironment | undefined) {
getServerEnv = getter
}
// This env object is used as a default for tests only and is not included in production builds
export const clientEnvForTests: ClientEnvironment = {
locale: 'en',
featureFlags: ['test_flag'],
copilotApiOverrideUrl: 'http://copilot-api.test',
}
export function _resetForTests({loadNewEnv, forceUndefined}: {loadNewEnv: boolean; forceUndefined?: boolean}) {
// forget the current env
env = forceUndefined ? undefined : clientEnvForTests
// Note: we don't reset getServerEnv here because it's set once at module load time
// by client-env.server.ts and should persist across test resets
if (loadNewEnv) {
// load the latest env
loadEnv()
}
}