@@ -2,23 +2,24 @@ import { arbitrumSepolia, arbitrum } from "wagmi/chains";
22
33import { DEFAULT_CHAIN } from "../consts/chains" ;
44
5- function assertEnvVar ( key : string ) : string {
6- const value = process . env [ key ] ;
7- if ( ! value ) {
8- throw new Error ( `${ key } not set` ) ;
9- }
5+ const throwIfNotSet = ( key : string , value : string | undefined ) => {
6+ if ( ! value ) throw new Error ( `${ key } not set.` ) ;
107 return value ;
11- }
12-
13- export function getGraphqlUrl ( isDisputeTemplate = false , chainId : number = DEFAULT_CHAIN ) {
14- const coreSubgraphEnvVar = "NEXT_PUBLIC_CORE_SUBGRAPH" ;
15- const drtArbSepoliaSubgraphEnvVar = "NEXT_PUBLIC_DRT_ARBSEPOLIA_SUBGRAPH" ;
16- const drtArbMainnetSubgraphEnvVar = "NEXT_PUBLIC_DRT_ARBMAINNET_SUBGRAPH" ;
17-
18- const chainIdToDrtSubgraph : { [ key : number ] : string } = {
19- [ arbitrumSepolia . id ] : drtArbSepoliaSubgraphEnvVar ,
20- [ arbitrum . id ] : drtArbMainnetSubgraphEnvVar ,
21- } ;
8+ } ;
229
23- return assertEnvVar ( isDisputeTemplate ? chainIdToDrtSubgraph [ chainId ] : coreSubgraphEnvVar ) ;
24- }
10+ // Warning: we cannot access process.env[key] with key a variable
11+ // because of the way Next.js handles env variables, unless hacking next.config
12+ // More info: https://stackoverflow.com/questions/64152943/cant-access-process-env-values-using-dynamic-keys
13+ export const getGraphqlUrl = ( isDisputeTemplate = false , chainId : number = DEFAULT_CHAIN ) => {
14+ if ( ! isDisputeTemplate ) {
15+ return throwIfNotSet ( "NEXT_PUBLIC_CORE_SUBGRAPH" , process . env . NEXT_PUBLIC_CORE_SUBGRAPH ) ;
16+ }
17+ switch ( chainId ) {
18+ case arbitrumSepolia . id :
19+ return throwIfNotSet ( "NEXT_PUBLIC_DRT_ARBSEPOLIA_SUBGRAPH" , process . env . NEXT_PUBLIC_DRT_ARBSEPOLIA_SUBGRAPH ) ;
20+ case arbitrum . id :
21+ return throwIfNotSet ( "NEXT_PUBLIC_DRT_ARBMAINNET_SUBGRAPH" , process . env . NEXT_PUBLIC_DRT_ARBMAINNET_SUBGRAPH ) ;
22+ default :
23+ throw new Error ( `Unsupported chainId: ${ chainId } ` ) ;
24+ }
25+ } ;
0 commit comments