0
Fork 0
mirror of https://github.com/withastro/astro.git synced 2024-12-16 21:46:22 -05:00
astro/.changeset/poor-berries-occur.md
Florian Lefebvre 2668ef9841
feat(astro): experimental astro:env (#10974)
* feat(env): add schema, types and envField (#10805)

* feat(env): add validators (#10827)

* feat(env): add vite plugin (#10829)

* feat(env): client/public variables (#10848)

Co-authored-by: Bjorn Lu <bjornlu.dev@gmail.com>
Co-authored-by: Emanuele Stoppa <my.burning@gmail.com>

* feat(env): server/public variables (#10881)

Co-authored-by: Bjorn Lu <bjornlu.dev@gmail.com>
Co-authored-by: Emanuele Stoppa <my.burning@gmail.com>

* feat(env): server/secret variables (#10954)

Co-authored-by: Bjorn Lu <bjornlu.dev@gmail.com>
Co-authored-by: Emanuele Stoppa <my.burning@gmail.com>

* fix: import

* fix: test

* feat: work on jsdoc

* feat: more jsdoc

* chore: remove todo

* feat: fix test error and write changeset

* feat: update config reference

* feat: apply recommendations from review

* feat: rework getEnv/setGetEnv

* chore: move tests

* fix: rename

* fix: dev mode

* chore: improve error

* feat: add overrideProcessEnv helper

* fix: make eslint happy

* Update .changeset/poor-berries-occur.md

Co-authored-by: Paul Valladares <85648028+dreyfus92@users.noreply.github.com>

* Update .changeset/poor-berries-occur.md

Co-authored-by: Paul Valladares <85648028+dreyfus92@users.noreply.github.com>

* Update .changeset/poor-berries-occur.md

Co-authored-by: Paul Valladares <85648028+dreyfus92@users.noreply.github.com>

* Update .changeset/poor-berries-occur.md

Co-authored-by: Paul Valladares <85648028+dreyfus92@users.noreply.github.com>

* feat: fix build and address reviews

* fix: container api

* fix: tests

* Apply suggestions from code review

Co-authored-by: Sarah Rainsberger <sarah@rainsberger.ca>

* Update packages/astro/src/@types/astro.ts

Co-authored-by: Sarah Rainsberger <sarah@rainsberger.ca>

* Apply suggestions from code review

Co-authored-by: Sarah Rainsberger <sarah@rainsberger.ca>

* Update packages/astro/src/@types/astro.ts

* chore: update changeset

* feat: address reviews

* feat: address Ema's reviews

* Update .changeset/poor-berries-occur.md

Co-authored-by: Sarah Rainsberger <sarah@rainsberger.ca>

---------

Co-authored-by: Bjorn Lu <bjornlu.dev@gmail.com>
Co-authored-by: Emanuele Stoppa <my.burning@gmail.com>
Co-authored-by: Paul Valladares <85648028+dreyfus92@users.noreply.github.com>
Co-authored-by: Sarah Rainsberger <sarah@rainsberger.ca>
2024-06-05 11:40:17 +01:00

70 lines
No EOL
2.9 KiB
Markdown

---
"astro": minor
---
Adds experimental support for the `astro:env` API.
The `astro:env` API lets you configure a type-safe schema for your environment variables, and indicate whether they should be available on the server or the client. Import and use your defined variables from the appropriate `/client` or `/server` module:
```astro
---
import { PUBLIC_APP_ID } from "astro:env/client"
import { PUBLIC_API_URL, getSecret } from "astro:env/server"
const API_TOKEN = getSecret("API_TOKEN")
const data = await fetch(`${PUBLIC_API_URL}/users`, {
method: "POST",
headers: {
"Content-Type": "application/json",
"Authorization": `Bearer ${API_TOKEN}`
},
body: JSON.stringify({ appId: PUBLIC_APP_ID })
})
---
```
To define the data type and properties of your environment variables, declare a schema in your Astro config in `experimental.env.schema`. The `envField` helper allows you define your variable as a string, number, or boolean and pass properties in an object:
```js
// astro.config.mjs
import { defineConfig, envField } from "astro/config"
export default defineConfig({
experimental: {
env: {
schema: {
PUBLIC_API_URL: envField.string({ context: "client", access: "public", optional: true }),
PUBLIC_PORT: envField.number({ context: "server", access: "public", default: 4321 }),
API_SECRET: envField.string({ context: "server", access: "secret" }),
}
}
}
})
```
There are three kinds of environment variables, determined by the combination of `context` (`client` or `server`) and `access` (`private` or `public`) settings defined in your [`env.schema`](#experimentalenvschema):
- **Public client variables**: These variables end up in both your final client and server bundles, and can be accessed from both client and server through the `astro:env/client` module:
```js
import { PUBLIC_API_URL } from "astro:env/client"
```
- **Public server variables**: These variables end up in your final server bundle and can be accessed on the server through the `astro:env/server` module:
```js
import { PUBLIC_PORT } from "astro:env/server"
```
- **Secret server variables**: These variables are not part of your final bundle and can be accessed on the server through the `getSecret()` helper function available from the `astro:env/server` module:
```js
import { getSecret } from "astro:env/server"
const API_SECRET = getSecret("API_SECRET") // typed
const SECRET_NOT_IN_SCHEMA = getSecret("SECRET_NOT_IN_SCHEMA") // string | undefined
```
**Note:** Secret client variables are not supported because there is no safe way to send this data to the client. Therefore, it is not possible to configure both `context: "client"` and `access: "secret"` in your schema.
To learn more, check out [the documentation](https://docs.astro.build/en/reference/configuration-reference/#experimentalenv).