-
Notifications
You must be signed in to change notification settings - Fork 203
/
index.ts
102 lines (83 loc) · 2.28 KB
/
index.ts
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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
export enum ConnectionState {
LOADING,
L1_CONNECTED,
L2_CONNECTED,
NETWORK_ERROR
}
export const sanitizeImageSrc = (url: string): string => {
if (url.startsWith('ipfs')) {
return `https://ipfs.io/ipfs/${url.substring(7)}`
}
return url
}
export function preloadImages(imageSources: string[]) {
imageSources.forEach(imageSrc => {
const image = new Image()
image.src = imageSrc
})
}
export const loadEnvironmentVariableWithFallback = ({
env,
fallback
}: {
env?: string
fallback?: string
}): string => {
const isValidEnv = (value?: string) => {
return typeof value === 'string' && value.trim().length !== 0
}
if (isValidEnv(env)) {
return String(env)
}
if (isValidEnv(fallback)) {
return String(fallback)
}
throw new Error(
`
Neither the provided value or its fallback are a valid environment variable.
Expected one of them to be a non-empty string but received env: '${env}', fallback: '${fallback}'.
`
)
}
export const sanitizeQueryParams = (data: any) => {
return JSON.parse(JSON.stringify(data))
}
export const getAPIBaseUrl = () => {
// if dev environment, eg. tests, then prepend actual running environment
// Resolves: next-js-error-only-absolute-urls-are-supported in test:ci
return process.env.NODE_ENV === 'test' ? 'http://localhost:3000' : ''
}
// add feature flags to the array
const featureFlags = [] as const
type FeatureFlag = (typeof featureFlags)[number]
export const isExperimentalFeatureEnabled = (flag: FeatureFlag) => {
const query = new URLSearchParams(window.location.search)
const flags = query.get('experiments')
if (!flags) {
return false
}
return flags.split(',').includes(flag)
}
export const isExperimentalModeEnabled = () => {
const query = new URLSearchParams(window.location.search)
const flags = query.get('experiments')
return flags !== null
}
export const sanitizeExperimentalFeaturesQueryParam = (
flags: string | null | undefined
) => {
if (!flags) {
return undefined
}
const flagsArray = flags.split(',')
if (flagsArray.length === 0) {
return undefined
}
const validFlagsArray = flagsArray.filter(f =>
featureFlags.includes(f as FeatureFlag)
)
if (validFlagsArray.length === 0) {
return undefined
}
return validFlagsArray.join(',')
}