turboenv
Version:
Minimal env var validation for monorepos using Zod
43 lines (40 loc) • 1.52 kB
TypeScript
import { ZodRawShape, z } from 'zod';
type InitEnvOptions<TServer extends ZodRawShape, TClient extends ZodRawShape> = {
server: TServer;
client: TClient;
runtimeEnv?: Record<string, unknown>;
/**
* Whether to throw on missing environment variables.
* If false, will return undefined for missing optional variables.
* @default true
*/
strict?: boolean;
/**
* Custom error handler for missing/invalid environment variables
*/
onError?: (error: EnvError) => void;
};
declare class EnvError extends Error {
readonly type: "missing" | "invalid" | "client_access";
readonly variable: string;
readonly cause?: unknown | undefined;
constructor(type: "missing" | "invalid" | "client_access", variable: string, message: string, cause?: unknown | undefined);
}
declare function initEnv<TServer extends ZodRawShape, TClient extends ZodRawShape>(opts: InitEnvOptions<TServer, TClient>): {
client: z.infer<z.ZodObject<TClient>>;
server: z.core.$InferObjectOutput<TServer, {}> | undefined;
};
/**
* Creates Hono Bindings type from turboenv server schema
* Use this to get autocompletion for c.env in Hono applications
*/
type CreateHonoBindings<TServer extends ZodRawShape> = {
Bindings: z.infer<z.ZodObject<TServer>>;
};
/**
* Utility to extract server env types for Hono integration
*/
type ExtractServerEnv<T> = T extends {
server: infer S;
} ? S : never;
export { type CreateHonoBindings, EnvError, type ExtractServerEnv, initEnv };