-
Notifications
You must be signed in to change notification settings - Fork 52
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1985 from ever-co/improve-recaptcha
Improve Env Variable Retrieval
- Loading branch information
Showing
25 changed files
with
241 additions
and
94 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
const NEXT_PUBLIC_ENVS: { value: Env } = { value: {} }; | ||
|
||
type Env = Record<string, string | undefined>; | ||
|
||
type OptionObject<T> = { | ||
default?: string; | ||
map?: (value: string | undefined) => T; | ||
}; | ||
type Options<T> = string | OptionObject<T>; | ||
|
||
type InferValue<T> = T extends { map: (value: any) => infer U } ? U : string | undefined; | ||
|
||
type ReturnedType<T> = { | ||
readonly value: T extends string ? string : InferValue<T>; | ||
}; | ||
|
||
/** | ||
* This function only loads environment variables starting with NEXT_PUBLIC_* | ||
* | ||
* Useful for getting the latest value of the variable at runtime rather than at build time | ||
* | ||
* @param name | ||
* @param options | ||
* @returns | ||
*/ | ||
export function getNextPublicEnv<O extends Options<unknown>>(name: string, options?: O): ReturnedType<O> { | ||
return { | ||
get value() { | ||
const defaultValue = typeof options === 'string' ? options : options?.default; | ||
|
||
let value = NEXT_PUBLIC_ENVS.value[name] || defaultValue; | ||
if (typeof options === 'object' && options.map) { | ||
value = options.map(value) as any; | ||
} | ||
|
||
return value as any; | ||
} | ||
}; | ||
} | ||
|
||
export function setNextPublicEnv(envs: Env) { | ||
if (envs) { | ||
NEXT_PUBLIC_ENVS.value = { | ||
...NEXT_PUBLIC_ENVS.value, | ||
...envs | ||
}; | ||
} | ||
} | ||
|
||
export function loadNextPublicEnvs() { | ||
return Object.keys(process.env) | ||
.filter((key) => key.startsWith('NEXT_PUBLIC')) | ||
.reduce( | ||
(acc, value) => { | ||
if (process.env[value]) { | ||
acc[value] = process.env[value] as string; | ||
} | ||
return acc; | ||
}, | ||
{} as Record<string, string> | ||
); | ||
} | ||
|
||
// Preload Some variables | ||
setNextPublicEnv(loadNextPublicEnvs()); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,9 @@ | ||
import { GAUZY_API_BASE_SERVER_URL } from '@app/constants'; | ||
import { get } from '../axios'; | ||
|
||
export async function getLanguageListAPI(is_system: boolean) { | ||
const endpoint = `/languages?is_system=${is_system}`; | ||
const data = await get(endpoint, true); | ||
|
||
return process.env.NEXT_PUBLIC_GAUZY_API_SERVER_URL ? data.data : data; | ||
return GAUZY_API_BASE_SERVER_URL.value ? data.data : data; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.