Saltar al contenido principal

ENV

Agregar variables

  • Si son cadenas de texto, utilizar comillas ("txt")
wrangler.toml
name = "<projectName>"
compatibility_date = ""

[vars]
MY_VAR = "my-variable"
  • Se pueden crear variables en formato JSON
name = "my-worker-dev"

[env.staging.vars]
API_HOST = "staging.example.com"
API_ACCOUNT_ID = "staging_example_user"
SERVICE_X_DATA = { URL = "service-x-api.dev.example", MY_ID = 123 }

[env.production.vars]
API_HOST = "production.example.com"
API_ACCOUNT_ID = "production_example_user"
SERVICE_X_DATA = { URL = "service-x-api.prod.example", MY_ID = 456 }
  • Si se utiliza git, crear un archivo .template y verificar el .gitignore para no subir las credenciales al repositorio

Usar

src\index.ts
import { Hono } from "hono";

// NOMBRE: tipo
export type Env = {
  MY_VAR: string;
};

// Pasarlo al context
const app = new Hono<{ Bindings: Env }>();

// Utilizar
app.get("/", (c) => {
  return c.text(c.env.MY_VAR);
});

export default app;

.dev.vars

  • Tiene el mismo uso y configuración anterior
  • Por defecto ya está en el .gitignore
.dev.vars
PRIVATE_VAR=abc123