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-env
npm astro add astro-env
yarn astro add astro-env
Or install it manually:
- Install the required dependencies
pnpm add astro-env
npm install astro-env
yarn 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";
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:
playground
contains code for testing the packagepackage
contains the actual package
Install dependencies using pnpm:
pnpm i --frozen-lockfile
Start the playground:
pnpm playground:dev
You 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.