Skip to content

Commit

Permalink
implement env variables in graph-client
Browse files Browse the repository at this point in the history
  • Loading branch information
nil-amrutlal committed Aug 31, 2023
1 parent 0bc5eeb commit f9fae16
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 1 deletion.
2 changes: 2 additions & 0 deletions packages/apps/graph-client/.env.example
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
# Depth necessary on a block to call it "confirmed", omit to use default value: 6
CONFIRMED_CONFIRMATION_DEPTH=
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { env } from '../../../utils/env';
import { IBlock } from '../../../utils/hooks/use-parsed-blocks';
import { Box } from '../../box';
import { Text } from '../../text';
Expand Down Expand Up @@ -43,7 +44,8 @@ export const ChainBlock = (props: IChainBlockProps): JSX.Element => {
<TimerIcon />
<TimeTicker date={new Date(block.creationtime)} />

{block.confirmationDepth >= 6 ? (
{block.confirmationDepth >=
env('CONFIRMED_CONFIRMATION_DEPTH', 6) ? (
<CheckCircledIcon />
) : (
<InfoCircledIcon />
Expand Down
27 changes: 27 additions & 0 deletions packages/apps/graph-client/src/utils/env.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
export interface IEnvInterface {
CONFIRMED_CONFIRMATION_DEPTH?: number;
}

type RequiredEnv = Required<IEnvInterface>;

export const dotenv: IEnvInterface = {
CONFIRMED_CONFIRMATION_DEPTH: Number(
process.env.CONFIRMED_CONFIRMATION_DEPTH,
),
};

export const env = <T extends keyof RequiredEnv, TDefault>(
key: T,
defaultValue: TDefault,
): TDefault | NonNullable<RequiredEnv[T]> => {
console.log(dotenv[key]);
const falsyKey =
(typeof dotenv[key] === 'number' && isNaN(Number(dotenv[key]))) ||
dotenv[key] === null ||
dotenv[key] === undefined;
// TODO: uncomment this line when there's env vars that are strings
//dotenv[key] === '';
return falsyKey
? (defaultValue as TDefault)
: (dotenv[key] as NonNullable<RequiredEnv[T]>);
};

0 comments on commit f9fae16

Please sign in to comment.