Skip to content

Commit

Permalink
fix(faucet,store-indexer): fix invalid env message (#1546)
Browse files Browse the repository at this point in the history
  • Loading branch information
holic authored Sep 19, 2023
1 parent b554071 commit 301bcb7
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 30 deletions.
6 changes: 6 additions & 0 deletions .changeset/mean-islands-brake.md
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
21 changes: 3 additions & 18 deletions packages/faucet/bin/faucet-server.ts
Original file line number Diff line number Diff line change
@@ -1,28 +1,13 @@
#!/usr/bin/env node
import "dotenv/config";
import { z } from "zod";
import fastify from "fastify";
import { fastifyTRPCPlugin } from "@trpc/server/adapters/fastify";
import { http, parseEther, isHex, createClient } from "viem";
import { http, createClient } from "viem";
import { privateKeyToAccount } from "viem/accounts";
import { AppRouter, createAppRouter } from "../src/createAppRouter";
import { parseEnv } from "./parseEnv";

const env = 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)),
})
.parse(process.env, {
errorMap: (issue) => ({
message: `Missing or invalid environment variable: ${issue.path.join(".")}`,
}),
});
const env = parseEnv();

const client = createClient({
transport: http(env.RPC_HTTP_URL),
Expand Down
29 changes: 29 additions & 0 deletions packages/faucet/bin/parseEnv.ts
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;
}
}
31 changes: 19 additions & 12 deletions packages/store-indexer/bin/parseEnv.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { isDefined } from "@latticexyz/common/utils";
import { z, ZodIntersection, ZodTypeAny } from "zod";
import { z, ZodError, ZodIntersection, ZodTypeAny } from "zod";

const commonSchema = z.intersection(
z.object({
Expand All @@ -9,22 +8,30 @@ const commonSchema = z.intersection(
MAX_BLOCK_RANGE: z.coerce.bigint().positive().default(1000n),
POLLING_INTERVAL: z.coerce.number().positive().default(1000),
}),
z
.object({
z.union([
z.object({
RPC_HTTP_URL: z.string(),
RPC_WS_URL: z.string().optional(),
}),
z.object({
RPC_HTTP_URL: z.string().optional(),
RPC_WS_URL: z.string(),
})
.partial()
.refine((values) => Object.values(values).some(isDefined))
}),
])
);

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;
return envSchema.parse(process.env, {
errorMap: (issue) => ({
message: `Missing or invalid environment variable: ${issue.path.join(".")}`,
}),
});
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;
}
}

0 comments on commit 301bcb7

Please sign in to comment.