-
Notifications
You must be signed in to change notification settings - Fork 51
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 #1988 from ever-co/develop
Release
- Loading branch information
Showing
34 changed files
with
454 additions
and
217 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
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 |
---|---|---|
@@ -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()); |
Oops, something went wrong.