環境變數 API 參考
新增於:
astro@5.0.0
astro:env
API 讓你為已設定的環境變數設定型別安全的資料標準。這讓你可以標記它們應該在伺服器或客戶端上,並定義它們的資料型別和其他屬性。要取得範例與使用說明,請參閱 astro:env
指南 (EN)。
從 astro:env
引入
Section titled “從 astro:env 引入”import { getSecret, } from 'astro:env/server';
getSecret()
Section titled “getSecret()”
新增於:
astro@5.0.0
getSecret()
工具函式讓你從環境變數的鍵值取得它原始的值。
例如你可以將布林值當做字串取回:
import { FEATURE_FLAG, // boolean getSecret} from 'astro:env/server'
getSecret('FEATURE_FLAG') // string | undefined
這在取得沒定義在資料標準的秘密時也有用,例如相依於從資料庫或 API 來的動態資料的秘密。
如果你需要用程式取回環境變數,我們推薦使用 getSecret()
而不是 process.env
(或相同的東西)。因為它的實作是由配接器提供的,如果換了配接器,你不需要更新所有有呼叫到它的地方。它在開發與建置環境的預設值是 process.env
。