Last active
May 17, 2025 15:59
-
-
Save maxktz/1e61c3dd6b1633671b6226eab3e6e9fe to your computer and use it in GitHub Desktop.
env vars declaration I use for cloudflare workers (complexity = 1 zod validation and only on cold starts)
This file contains hidden or 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
import { env as baseEnv } from 'cloudflare:workers'; // pnpm i -D @cloudflare/workers-types | |
import { z } from 'zod'; // pnpm i zod | |
export const envSchema = z.object({ | |
// secrets | |
BOT_TOKEN: z.string(), | |
TURSO_AUTH_TOKEN: z.string(), | |
WEBHOOK_SECRET: z.string().optional().describe('Random generated string to secure the webhook'), | |
// variables | |
TURSO_DATABASE_URL: z.string().url(), | |
WEBHOOK_PATH: z.string().startsWith('/').default('/'), | |
ENVIRONMENT: z.enum(['development', 'production']).default('development'), | |
}); | |
export type Env = z.infer<typeof envSchema>; | |
const parse = envSchema.safeParse(baseEnv); | |
if (!parse.success) { | |
console.error('Invalid environment variables', parse.error.format()); | |
throw new Error('Invalid environment variables'); | |
} | |
export const env = parse.data; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment