-
Notifications
You must be signed in to change notification settings - Fork 187
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(faucet,store-indexer): fix invalid env message (#1546)
- Loading branch information
Showing
4 changed files
with
57 additions
and
30 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
--- | ||
"@latticexyz/faucet": patch | ||
"@latticexyz/store-indexer": patch | ||
--- | ||
|
||
Improves error message when parsing env variables |
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,29 @@ | ||
import { isHex, parseEther } from "viem"; | ||
import { z, ZodError, ZodIntersection, ZodTypeAny } from "zod"; | ||
|
||
const commonSchema = z.object({ | ||
HOST: z.string().default("0.0.0.0"), | ||
PORT: z.coerce.number().positive().default(3002), | ||
RPC_HTTP_URL: z.string(), | ||
FAUCET_PRIVATE_KEY: z.string().refine(isHex), | ||
DRIP_AMOUNT_ETHER: z | ||
.string() | ||
.default("1") | ||
.transform((ether) => parseEther(ether)), | ||
}); | ||
|
||
export function parseEnv<TSchema extends ZodTypeAny | undefined = undefined>( | ||
schema?: TSchema | ||
): z.infer<TSchema extends ZodTypeAny ? ZodIntersection<typeof commonSchema, TSchema> : typeof commonSchema> { | ||
const envSchema = schema !== undefined ? z.intersection(commonSchema, schema) : commonSchema; | ||
try { | ||
return envSchema.parse(process.env); | ||
} catch (error) { | ||
if (error instanceof ZodError) { | ||
const { _errors, ...invalidEnvVars } = error.format(); | ||
console.error(`\nMissing or invalid environment variables:\n\n ${Object.keys(invalidEnvVars).join("\n ")}\n`); | ||
process.exit(1); | ||
} | ||
throw error; | ||
} | ||
} |
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