forked from velodrome-finance/v1
-
Notifications
You must be signed in to change notification settings - Fork 2
/
checkEnv.ts
36 lines (30 loc) · 959 Bytes
/
checkEnv.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
import { z, type ZodFormattedError } from "zod";
const schema = z.object({
ALCHEMY_GOERLI_ARBITRUM_API_KEY: z.string().min(1),
ARB_SCAN_API_KEY: z.string().min(1),
});
type DestructedEnv = {
[k in keyof z.infer<typeof schema>]: z.infer<typeof schema>[k] | undefined;
};
const destructedEnv: DestructedEnv = {
ALCHEMY_GOERLI_ARBITRUM_API_KEY: process.env.ALCHEMY_GOERLI_ARBITRUM_API_KEY,
ARB_SCAN_API_KEY: process.env.ARB_SCAN_API_KEY,
};
const _env = schema.safeParse(destructedEnv);
const formatErrors = (
errors: ZodFormattedError<Map<string, string>, string>
) => {
return Object.entries(errors)
.map(([name, value]) => {
if (value && "_errors" in value)
return `${name}: ${value._errors.join(", ")}\n`;
})
.filter(Boolean);
};
if (!_env.success) {
console.error(
"❌ Invalid environment variables:\n",
...formatErrors(_env.error.format())
);
throw new Error("Invalid environment variables");
}