astro-env v0.0.6
astro-env
This is an Astro integration that allows you to validate and type your environement variables automatically using zod.
Usage
Installation
Install the integration automatically using the Astro CLI:
pnpm astro add astro-envnpm astro add astro-envyarn astro add astro-envOr install it manually:
- Install the required dependencies
pnpm add astro-envnpm install astro-envyarn add astro-env- Add the integration to your astro config
+import astroEnv from "astro-env";
export default defineConfig({
integrations: [
+ astroEnv({ ... }),
],
});Configuration
Here is the TypeScript type:
export type Options = {
schema: AnyZodObject;
generateTypes?: boolean;
generateEnvTemplate?: boolean;
}schema
Zod schema used to validate your environment variables. You can import zod from astro/zod:
import astroEnv from "astro-env";
import { defineConfig } from "astro/config";
import { z } from "astro/zod";
// https://astro.build/config
export default defineConfig({
integrations: [
astroEnv({
schema: z.object({
ABC: z.string(),
}),
}),
],
});When using generateTypes, make sure that the schema doesn't contain any transform and that all values are strings (they can be z.string().url() for example).
Interested in supporting more data types? Open an issue!
generateTypes
If set to true, generates .astro/astro-env.d.ts with types based on the schema and updates src/env.d.ts. Defaults to true.
generateEnvTemplate
If set to true, generates a .env.template with keys based on the schema. Defaults to false
Contributing
This package is structured as a monorepo:
playgroundcontains code for testing the packagepackagecontains the actual package
Install dependencies using pnpm:
pnpm i --frozen-lockfileStart the playground:
pnpm playground:devYou can now edit files in package. Please note that making changes to those files may require restarting the playground dev server.
Licensing
MIT Licensed. Made with ❤️ by Florian Lefebvre.