Skip to content

Commit f50135c

Browse files
committed
refactor: make env var assertion more intuitive
The problem right now is that the constants that are loaded from the environment have an additional name if specified. This additional name is very confusing if you're presented with the error message Environment variable Alchemy API key is not set. This makes you wonder if perhaps you have mistyped the variable name. It's almost always better to just print the variable name as it is expected in your .env file. One notable exception is perhaps our Web3Up proof and secret. That's why this patch changes the approach of assertExists() to only deal with environment variables (it's not used in any other way currently) and have the caller specify the variable name and an optional hint to help the person stumbling over a missing env var error with additional context. The name of assertExists() was renamed to getRequiredEnvVar() to better reflect what the function is doing. The filename was also renamed to further support this.
1 parent 9e80918 commit f50135c

File tree

4 files changed

+25
-58
lines changed

4 files changed

+25
-58
lines changed

src/index.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import "./instrument.js";
22
import express, { type Express } from "express";
33
import "reflect-metadata";
44
import cors from "cors";
5-
import { assertExists } from "./utils/assertExists.js";
5+
import { getRequiredEnvVar } from "./utils/envVars.js";
66
import { yoga } from "./client/graphql.js";
77
import swaggerUi from "swagger-ui-express";
88
import swaggerJson from "./__generated__/swagger.json" assert { type: "json" };
@@ -21,7 +21,7 @@ BigInt.prototype.fromJSON = function () {
2121
return BigInt(this.toString());
2222
};
2323

24-
const PORT = assertExists(process.env.PORT, "PORT");
24+
const PORT = getRequiredEnvVar("PORT");
2525

2626
const app: Express = express();
2727

src/utils/assertExists.ts

Lines changed: 0 additions & 7 deletions
This file was deleted.

src/utils/constants.ts

Lines changed: 13 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -1,54 +1,18 @@
1-
import { assertExists } from "./assertExists.js";
1+
import { getRequiredEnvVar } from "./envVars.js";
22

3-
export const supabaseCachingUrl = assertExists(
4-
process.env.SUPABASE_CACHING_DB_URL,
5-
"SUPABASE_CACHING_DB_URL",
6-
);
7-
8-
export const supabaseCachingApiKey = assertExists(
9-
process.env.SUPABASE_CACHING_ANON_API_KEY,
3+
export const supabaseCachingUrl = getRequiredEnvVar("SUPABASE_CACHING_DB_URL");
4+
export const supabaseCachingApiKey = getRequiredEnvVar(
105
"SUPABASE_CACHING_ANON_API_KEY",
116
);
12-
13-
export const supabaseDataUrl = assertExists(
14-
process.env.SUPABASE_DATA_DB_URL,
15-
"SUPABASE_DATA_DB_URL",
16-
);
17-
18-
export const supabaseDataServiceApiKey = assertExists(
19-
process.env.SUPABASE_DATA_SERVICE_API_KEY,
7+
export const supabaseDataUrl = getRequiredEnvVar("SUPABASE_DATA_DB_URL");
8+
export const supabaseDataServiceApiKey = getRequiredEnvVar(
209
"SUPABASE_DATA_SERVICE_API_KEY",
2110
);
22-
23-
export const web3upKey = assertExists(process.env.KEY, "WEB3UP_KEY");
24-
export const web3upProof = assertExists(process.env.PROOF, "WEB3UP_PROOF");
25-
26-
export const indexerEnvironment = assertExists(
27-
process.env.INDEXER_ENVIRONMENT,
28-
"INDEXER_ENVIRONMENT",
29-
);
30-
31-
export const alchemyApiKey = assertExists(
32-
process.env.ALCHEMY_API_KEY,
33-
"Alchemy API key",
34-
);
35-
36-
export const infuraApiKey = assertExists(
37-
process.env.INFURA_API_KEY,
38-
"Infura API key",
39-
);
40-
41-
export const drpcApiPkey = assertExists(
42-
process.env.DRPC_API_KEY,
43-
"dRPC API KEY",
44-
);
45-
46-
export const cachingDatabaseUrl = assertExists(
47-
process.env.CACHING_DATABASE_URL,
48-
"CACHING_DATABASE_URL",
49-
);
50-
51-
export const dataDatabaseUrl = assertExists(
52-
process.env.DATA_DATABASE_URL,
53-
"DATA_DATABASE_URL",
54-
);
11+
export const web3upKey = getRequiredEnvVar("KEY", "WEB3UP Key");
12+
export const web3upProof = getRequiredEnvVar("PROOF", "WEB3UP Proof");
13+
export const indexerEnvironment = getRequiredEnvVar("INDEXER_ENVIRONMENT");
14+
export const alchemyApiKey = getRequiredEnvVar("ALCHEMY_API_KEY");
15+
export const infuraApiKey = getRequiredEnvVar("INFURA_API_KEY");
16+
export const drpcApiPkey = getRequiredEnvVar("DRPC_API_KEY");
17+
export const cachingDatabaseUrl = getRequiredEnvVar("CACHING_DATABASE_URL");
18+
export const dataDatabaseUrl = getRequiredEnvVar("DATA_DATABASE_URL");

src/utils/envVars.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
export const getRequiredEnvVar = (variableName: string, hint?: string) => {
2+
const variable = process.env[variableName];
3+
if (!variable) {
4+
throw new Error(
5+
`Environment variable ${variableName}${hint ? ` (${hint})` : ""} is not set.`,
6+
);
7+
}
8+
9+
return variable;
10+
};

0 commit comments

Comments
 (0)