-
-
Notifications
You must be signed in to change notification settings - Fork 2.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(next): make astro:env stable (#11679)
Co-authored-by: Sarah Rainsberger <sarah@rainsberger.ca>
- Loading branch information
1 parent
4cd6c43
commit ea71b90
Showing
32 changed files
with
210 additions
and
282 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
--- | ||
'astro': major | ||
--- | ||
|
||
The `astro:env` feature introduced behind a flag in [v4.10.0](https://github.com/withastro/astro/blob/main/packages/astro/CHANGELOG.md#x4100) is no longer experimental and is available for general use. If you have been waiting for stabilization before using `astro:env`, you can now do so. | ||
|
||
This feature lets you configure a type-safe schema for your environment variables, and indicate whether they should be available on the server or the client. | ||
|
||
To configure a schema, add the `env` option to your Astro config and define your client and server variables. If you were previously using this feature, please remove the experimental flag from your Astro config and move your entire `env` configuration unchanged to a top-level option. | ||
|
||
```js | ||
import { defineConfig, envField } from 'astro/config' | ||
|
||
export default defineConfig({ | ||
env: { | ||
schema: { | ||
API_URL: envField.string({ context: "client", access: "public", optional: true }), | ||
PORT: envField.number({ context: "server", access: "public", default: 4321 }), | ||
API_SECRET: envField.string({ context: "server", access: "secret" }), | ||
} | ||
} | ||
}) | ||
``` | ||
|
||
You can import and use your defined variables from the appropriate `/client` or `/server` module: | ||
|
||
```astro | ||
--- | ||
import { API_URL } from "astro:env/client" | ||
import { API_SECRET_TOKEN } from "astro:env/server" | ||
const data = await fetch(`${API_URL}/users`, { | ||
method: "GET", | ||
headers: { | ||
"Content-Type": "application/json", | ||
"Authorization": `Bearer ${API_SECRET_TOKEN}` | ||
}, | ||
}) | ||
--- | ||
<script> | ||
import { API_URL } from "astro:env/client" | ||
fetch(`${API_URL}/ping`) | ||
</script> | ||
``` | ||
|
||
Please see our [guide to using environment variables](https://docs.astro.build/en/guides/environment-variables/#astroenv) for more about this feature. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
--- | ||
'@astrojs/vercel': major | ||
'@astrojs/node': major | ||
--- | ||
|
||
Adds stable support for `astro:env` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,32 +1,34 @@ | ||
import fsMod from 'node:fs'; | ||
import type { AstroSettings } from '../types/astro.js'; | ||
import { TYPES_TEMPLATE_URL } from './constants.js'; | ||
import { ENV_TYPES_FILE } from './constants.js'; | ||
import { getEnvFieldType } from './validators.js'; | ||
|
||
export function syncAstroEnv(settings: AstroSettings, fs = fsMod): void { | ||
if (!settings.config.experimental.env) { | ||
return; | ||
} | ||
|
||
const schema = settings.config.experimental.env.schema ?? {}; | ||
|
||
export function syncAstroEnv(settings: AstroSettings): void { | ||
let client = ''; | ||
let server = ''; | ||
|
||
for (const [key, options] of Object.entries(schema)) { | ||
const str = `export const ${key}: ${getEnvFieldType(options)}; \n`; | ||
for (const [key, options] of Object.entries(settings.config.env.schema)) { | ||
const str = ` export const ${key}: ${getEnvFieldType(options)}; \n`; | ||
if (options.context === 'client') { | ||
client += str; | ||
} else { | ||
server += str; | ||
} | ||
} | ||
|
||
const template = fs.readFileSync(TYPES_TEMPLATE_URL, 'utf-8'); | ||
const content = template.replace('// @@CLIENT@@', client).replace('// @@SERVER@@', server); | ||
let content = ''; | ||
if (client !== '') { | ||
content = `declare module 'astro:env/client' { | ||
${client}}`; | ||
} | ||
if (server !== '') { | ||
content += `declare module 'astro:env/server' { | ||
${server}}`; | ||
} | ||
|
||
settings.injectedTypes.push({ | ||
filename: 'astro/env.d.ts', | ||
content, | ||
}); | ||
if (content !== '') { | ||
settings.injectedTypes.push({ | ||
filename: ENV_TYPES_FILE, | ||
content, | ||
}); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.