abi
abi13mo ago

Safety measures (statically, not runtime) for libraries that require environment variables?

Suppose I have a Deno library which expects certain environment variables to have been configured, otherwise a crash will happen. Does anyone know of a good way to somehow ensure at "compile-time" that whatever Deno application is using the library has configured these environment variables?
10 Replies
𝕒𝕣𝕤𝕙
𝕒𝕣𝕤𝕙13mo ago
but env vars will be read at runtime?
abi
abi13mo ago
@arshxcx yes, i know, it's a weird question, not sure why i'm even asking... i figured maybe i've missed something, maybe one could do some cool stuff using dotenv, maybe something involving weird type inference? i don't even know man.
𝕒𝕣𝕤𝕙
𝕒𝕣𝕤𝕙13mo ago
is your dev environment the same as your deployment environment?
abi
abi13mo ago
nah, not at all
𝕒𝕣𝕤𝕙
𝕒𝕣𝕤𝕙13mo ago
so you only want safety for the dev env
abi
abi13mo ago
well, i guess it's technically only possible for dev env... unless i'm missing something. right now, i use a zod schema which gets fed Deno.env.toObject(), so it will crash at startup. i'm just thinking maybe there's something more clever.
𝕒𝕣𝕤𝕙
𝕒𝕣𝕤𝕙13mo ago
well yeah its only possible for dev env, the alternative would be a type system that can predict the future if you want the put in the effort, you could have a script that periodically writes Deno.env to a .ts file and exports it as an object with as const you could dynamically import the file and you would have the exact types with exact values
abi
abi13mo ago
yeah, currently i'm experimenting with importing the environment configuration as a JSON import...
𝕒𝕣𝕤𝕙
𝕒𝕣𝕤𝕙13mo ago
yeah json works if you dont need to look at the values, json imports just give you keys
abi
abi13mo ago
ah, good point i'll try your idea, thanks for your input