Removes the `Env` parameter from several functions to simplify their signatures and rely on the global `env` for configuration. This change reduces the number of arguments passed around, making the code cleaner and easier to maintain.
23 lines
548 B
TypeScript
23 lines
548 B
TypeScript
import { env as cloudflareEnv } from "cloudflare:workers";
|
|
import type { Bindings } from "hono/types";
|
|
|
|
type EnvVariable = keyof Cloudflare.Env;
|
|
const defaultValues: Record<EnvVariable, any> = {
|
|
ENABLE_ANIFY: true,
|
|
};
|
|
|
|
export function readEnvVariable<T>(
|
|
envVariable: EnvVariable,
|
|
env: Bindings | undefined = cloudflareEnv,
|
|
): T {
|
|
try {
|
|
return JSON.parse(env?.[envVariable] ?? null) ?? defaultValues[envVariable];
|
|
} catch (error) {
|
|
if (error instanceof SyntaxError) {
|
|
return env![envVariable];
|
|
}
|
|
|
|
throw error;
|
|
}
|
|
}
|