Interface de variáveis de ambiente
Beta
A interface de Variáveis de Ambiente pode ser usada dentro de edge functions para acessar variáveis de ambiente. Você pode usar essas variáveis para interagir com sistemas de back-end, como bancos de dados, APIs privadas ou qualquer serviço autenticado. Essas variáveis podem ser gerenciadas por meio da Azion CLI nas versões anteriores à azion 1.x.y.
Sintaxe
const apiToken = Azion.env.get('API_SERVICE_TOKEN');
Parâmetros
Parâmetro | Tipo | Descrição |
---|---|---|
key | string | A chave da variável que está sendo acessada |
Nota: se a chave informada estiver incorreta, o retorno será
undefined
.
Saiba mais sobre as variáveis de Ambiente.
Valor de retorno
Uma string
com o value armazenado pela key da variável fornecida ou undefined
se a chave não existir.
Exemplos
// Here the environment variables are retrieved and, later on, used // to make the connection to the DB through a fetch request. const dbUrl = Azion.env.get('DB_URL'); const dbKey = Azion.env.get('DB_KEY');
addEventListener('fetch', event => { event.respondWith(handleRequest(event.request)); });
async function handleRequest(request) { // In this example, a fetch request is sent to Supabase in order to retrieve a list of names from a pre-defined table. // The table must be configured on the supabase platform. You can find its base URL and key there as well. const apiUrl = `${supabaseUrl}/rest/v1/names?select=*`; const headers = new Headers({ 'Content-Type': 'application/json', 'apikey': supabaseKey, 'Authorization': `Bearer ${supabaseKey}` });
try { const response = await fetch(apiUrl, { headers });
if (!response.ok) { throw new Error(String(response.status)); }
const data = await response.json();
const responseBody = { data };
return new Response(JSON.stringify(responseBody), { headers: { 'Content-Type': 'application/json' } }); } catch (error) { console.error('Error connecting to Supabase: ', error);
return new Response(`Error connecting to Supabase: ${error}`, { status: 500 }); } }
Contribuidores