UNPKG

@julr/vite-plugin-validate-env

Version:

✅ Vite plugin for validating your environment variables

61 lines (57 loc) 2.2 kB
import { UserConfig, ConfigEnv, Plugin } from 'vite'; import { z } from 'zod'; import { StandardSchemaV1 } from '@standard-schema/spec'; import { ValidateFn } from '@poppinss/validator-lite/types'; export { schema as Schema } from '@poppinss/validator-lite'; /** * Schema defined by the user */ type RecordViteKeys<T> = Record<`${string}_${string}`, T>; /** * Options that can be passed to the plugin * The schema can be defined at the top level. */ type PluginOptions = Schema | FullPluginOptions; type FullPluginOptions = ({ validator: 'builtin'; schema: PoppinsSchema; } | { validator: 'standard'; schema: StandardSchema; }) & { debug?: boolean; configFile?: string; }; type PoppinsSchema = RecordViteKeys<ValidateFn<any>>; type StandardSchema = RecordViteKeys<StandardSchemaV1>; type Schema = PoppinsSchema | StandardSchema; type ConfigOptions = Pick<UserConfig, 'envDir' | 'envPrefix' | 'root'> & Pick<ConfigEnv, 'mode'>; /** * Infer the schema type from the plugin options */ type EnvSchema<UserOptions extends PluginOptions> = UserOptions extends { schema: infer T; } ? T : UserOptions; /** * Get the primitive value that is returned by the schema validator function */ type EnvValue<Fn> = Fn extends (...args: any) => any ? ReturnType<Fn> : Fn extends z.ZodType ? z.infer<Fn> : Fn extends StandardSchemaV1 ? StandardSchemaV1.InferOutput<Fn> : never; /** * Augment the import.meta.env object with the values returned by the schema validator */ type ImportMetaEnvAugmented<UserOptions extends PluginOptions> = { [K in keyof EnvSchema<UserOptions>]: EnvValue<EnvSchema<UserOptions>[K]>; }; /** * Validate environment variables against a schema */ declare const ValidateEnv: (options?: PluginOptions) => Plugin; /** * Validate environment variables and load them inside `process.env` * Can be useful when you want to validate outside of Vite's build process. */ declare const loadAndValidateEnv: (config: ConfigOptions, options?: PluginOptions) => Promise<{ [k: string]: any; }>; declare const defineConfig: <T extends PluginOptions>(config: T) => T; export { type ImportMetaEnvAugmented, ValidateEnv, defineConfig, loadAndValidateEnv };