Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/shy-dingos-tease.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'astro': patch
---

Allow setting descriptions on env schema config
4 changes: 4 additions & 0 deletions packages/astro/src/env/schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ const StringSchema = z.object({
includes: z.string().optional(),
startsWith: z.string().optional(),
endsWith: z.string().optional(),
description: z.string().optional(),
});
export type StringSchema = z.infer<typeof StringSchema>;
const NumberSchema = z.object({
Expand All @@ -22,12 +23,14 @@ const NumberSchema = z.object({
lt: z.number().optional(),
max: z.number().optional(),
int: z.boolean().optional(),
description: z.string().optional(),
});
export type NumberSchema = z.infer<typeof NumberSchema>;
const BooleanSchema = z.object({
type: z.literal('boolean'),
optional: z.boolean().optional(),
default: z.boolean().optional(),
description: z.string().optional(),
});
const EnumSchema = z.object({
type: z.literal('enum'),
Expand All @@ -39,6 +42,7 @@ const EnumSchema = z.object({
),
optional: z.boolean().optional(),
default: z.string().optional(),
description: z.string().optional(),
});
export type EnumSchema = z.infer<typeof EnumSchema>;

Expand Down
9 changes: 8 additions & 1 deletion packages/astro/src/env/sync.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,11 @@ export function syncAstroEnv(settings: AstroSettings): void {
let server = '';

for (const [key, options] of Object.entries(settings.config.env.schema)) {
const str = ` export const ${key}: ${getEnvFieldType(options)}; \n`;
let str = '';
if (options.description) {
str += ` /** ${options.description} */\n`;
}
str += ` export const ${key}: ${getEnvFieldType(options)};\n`;
if (options.context === 'client') {
client += str;
} else {
Expand All @@ -21,6 +25,9 @@ export function syncAstroEnv(settings: AstroSettings): void {
${client}}`;
}
if (server !== '') {
if (content !== '') {
content += '\n';
}
content += `declare module 'astro:env/server' {
${server}}`;
}
Expand Down
4 changes: 2 additions & 2 deletions packages/astro/test/fixtures/astro-env/astro.config.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@ import { defineConfig, envField } from 'astro/config';
export default defineConfig({
env: {
schema: {
FOO: envField.string({ context: "client", access: "public", optional: true, default: "ABC" }),
FOO: envField.string({ context: "client", access: "public", optional: true, default: "ABC", description: "The FOO variable", }),
BAR: envField.string({ context: "client", access: "public", optional: true, default: "DEF" }),
BAZ: envField.string({ context: "server", access: "public", optional: true, default: "GHI" }),
BAZ: envField.string({ context: "server", access: "public", optional: true, default: "GHI", description: "The BAZ variable" }),
}
}
});
Loading